From e8dda54c2420baeb3629796ee145abaed0993643 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Tue, 9 Jan 2024 14:05:02 -0300 Subject: [PATCH 01/18] emoji custom --- apps/meteor/app/emoji-custom/client/index.ts | 2 -- .../client/notifications/deleteEmojiCustom.ts | 6 ------ .../client/notifications/updateEmojiCustom.ts | 6 ------ .../EmojiPickerProvider.tsx | 11 +++++++---- .../providers/EmojiPickerProvider/index.ts | 1 + .../EmojiPickerProvider/useUpdateCustomEmoji.ts | 17 +++++++++++++++++ 6 files changed, 25 insertions(+), 18 deletions(-) delete mode 100644 apps/meteor/app/emoji-custom/client/notifications/deleteEmojiCustom.ts delete mode 100644 apps/meteor/app/emoji-custom/client/notifications/updateEmojiCustom.ts rename apps/meteor/client/providers/{ => EmojiPickerProvider}/EmojiPickerProvider.tsx (92%) create mode 100644 apps/meteor/client/providers/EmojiPickerProvider/index.ts create mode 100644 apps/meteor/client/providers/EmojiPickerProvider/useUpdateCustomEmoji.ts diff --git a/apps/meteor/app/emoji-custom/client/index.ts b/apps/meteor/app/emoji-custom/client/index.ts index d8a1b75e275db..780a12a3898f5 100644 --- a/apps/meteor/app/emoji-custom/client/index.ts +++ b/apps/meteor/app/emoji-custom/client/index.ts @@ -1,3 +1 @@ import './lib/emojiCustom'; -import './notifications/deleteEmojiCustom'; -import './notifications/updateEmojiCustom'; diff --git a/apps/meteor/app/emoji-custom/client/notifications/deleteEmojiCustom.ts b/apps/meteor/app/emoji-custom/client/notifications/deleteEmojiCustom.ts deleted file mode 100644 index 0ee00976956ea..0000000000000 --- a/apps/meteor/app/emoji-custom/client/notifications/deleteEmojiCustom.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Meteor } from 'meteor/meteor'; - -import { Notifications } from '../../../notifications/client'; -import { deleteEmojiCustom } from '../lib/emojiCustom'; - -Meteor.startup(() => Notifications.onLogged('deleteEmojiCustom', (data) => deleteEmojiCustom(data.emojiData))); diff --git a/apps/meteor/app/emoji-custom/client/notifications/updateEmojiCustom.ts b/apps/meteor/app/emoji-custom/client/notifications/updateEmojiCustom.ts deleted file mode 100644 index 326edef0fa6c1..0000000000000 --- a/apps/meteor/app/emoji-custom/client/notifications/updateEmojiCustom.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Meteor } from 'meteor/meteor'; - -import { Notifications } from '../../../notifications/client'; -import { updateEmojiCustom } from '../lib/emojiCustom'; - -Meteor.startup(() => Notifications.onLogged('updateEmojiCustom', (data) => updateEmojiCustom(data.emojiData))); diff --git a/apps/meteor/client/providers/EmojiPickerProvider.tsx b/apps/meteor/client/providers/EmojiPickerProvider/EmojiPickerProvider.tsx similarity index 92% rename from apps/meteor/client/providers/EmojiPickerProvider.tsx rename to apps/meteor/client/providers/EmojiPickerProvider/EmojiPickerProvider.tsx index f7d14b317d54a..35cb32077a849 100644 --- a/apps/meteor/client/providers/EmojiPickerProvider.tsx +++ b/apps/meteor/client/providers/EmojiPickerProvider/EmojiPickerProvider.tsx @@ -2,10 +2,11 @@ import { useDebouncedState, useLocalStorage } from '@rocket.chat/fuselage-hooks' import type { ReactNode, ReactElement, ContextType } from 'react'; import React, { useState, useCallback, useMemo, useEffect } from 'react'; -import type { EmojiByCategory } from '../../app/emoji/client'; -import { emoji, getFrequentEmoji, updateRecent, createEmojiList, createPickerEmojis, CUSTOM_CATEGORY } from '../../app/emoji/client'; -import { EmojiPickerContext } from '../contexts/EmojiPickerContext'; -import EmojiPicker from '../views/composer/EmojiPicker/EmojiPicker'; +import type { EmojiByCategory } from '../../../app/emoji/client'; +import { emoji, getFrequentEmoji, updateRecent, createEmojiList, createPickerEmojis, CUSTOM_CATEGORY } from '../../../app/emoji/client'; +import { EmojiPickerContext } from '../../contexts/EmojiPickerContext'; +import EmojiPicker from '../../views/composer/EmojiPicker/EmojiPicker'; +import { useUpdateCustomEmoji } from './useUpdateCustomEmoji'; const DEFAULT_ITEMS_LIMIT = 90; @@ -23,6 +24,8 @@ const EmojiPickerProvider = ({ children }: { children: ReactNode }): ReactElemen getFrequentEmoji(frequentEmojis.map(([emoji]) => emoji)), ); + useUpdateCustomEmoji(); + const addFrequentEmojis = useCallback( (emoji: string) => { const empty: [string, number][] = frequentEmojis.some(([emojiName]) => emojiName === emoji) ? [] : [[emoji, 0]]; diff --git a/apps/meteor/client/providers/EmojiPickerProvider/index.ts b/apps/meteor/client/providers/EmojiPickerProvider/index.ts new file mode 100644 index 0000000000000..4437d6b749d43 --- /dev/null +++ b/apps/meteor/client/providers/EmojiPickerProvider/index.ts @@ -0,0 +1 @@ +export { default } from './EmojiPickerProvider'; diff --git a/apps/meteor/client/providers/EmojiPickerProvider/useUpdateCustomEmoji.ts b/apps/meteor/client/providers/EmojiPickerProvider/useUpdateCustomEmoji.ts new file mode 100644 index 0000000000000..a0a0946006dbb --- /dev/null +++ b/apps/meteor/client/providers/EmojiPickerProvider/useUpdateCustomEmoji.ts @@ -0,0 +1,17 @@ +import { useStream } from '@rocket.chat/ui-contexts'; +import { useEffect } from 'react'; + +import { updateEmojiCustom, deleteEmojiCustom } from '../../../app/emoji-custom/client/lib/emojiCustom'; + +export const useUpdateCustomEmoji = () => { + const notify = useStream('notify-logged'); + useEffect(() => { + const unsubUpdate = notify('updateEmojiCustom', (data) => updateEmojiCustom(data.emojiData)); + const unsubDelete = notify('deleteEmojiCustom', (data) => deleteEmojiCustom(data.emojiData)); + + return () => { + unsubUpdate(); + unsubDelete(); + }; + }, [notify]); +}; From 5ef043a685fb7bba026a06b4fa4f41efed781244 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Tue, 9 Jan 2024 14:19:33 -0300 Subject: [PATCH 02/18] e2e.room --- apps/meteor/app/e2e/client/rocketchat.e2e.room.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/meteor/app/e2e/client/rocketchat.e2e.room.js b/apps/meteor/app/e2e/client/rocketchat.e2e.room.js index f64b243e0d882..bd0863d691a9a 100644 --- a/apps/meteor/app/e2e/client/rocketchat.e2e.room.js +++ b/apps/meteor/app/e2e/client/rocketchat.e2e.room.js @@ -7,7 +7,6 @@ import { RoomManager } from '../../../client/lib/RoomManager'; import { roomCoordinator } from '../../../client/lib/rooms/roomCoordinator'; import { RoomSettingsEnum } from '../../../definition/IRoomTypeConfig'; import { ChatRoom, Subscriptions, Messages } from '../../models/client'; -import { Notifications } from '../../notifications/client'; import { sdk } from '../../utils/client/lib/SDKClient'; import { E2ERoomState } from './E2ERoomState'; import { @@ -240,7 +239,7 @@ export class E2ERoom extends Emitter { this.setState(E2ERoomState.WAITING_KEYS); this.log('Requesting room key'); - Notifications.notifyUsersOfRoom(this.roomId, 'e2ekeyRequest', this.roomId, room.e2eKeyId); + sdk.publish('notify-room-users', [`${this.roomId}/e2ekeyRequest`, this.roomId, room.e2eKeyId]); } catch (error) { // this.error = error; this.setState(E2ERoomState.ERROR); From 106a103563c1faf9b491b15edcd7fd40dcb1ab63 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Tue, 9 Jan 2024 14:28:00 -0300 Subject: [PATCH 03/18] custom user status --- apps/meteor/app/user-status/client/index.ts | 2 -- .../notifications/deleteCustomUserStatus.js | 6 ------ .../notifications/updateCustomUserStatus.js | 6 ------ .../providers/UserProvider/UserProvider.tsx | 3 +++ .../hooks/useUpdateCustomUserStatus.ts | 17 +++++++++++++++++ 5 files changed, 20 insertions(+), 14 deletions(-) delete mode 100644 apps/meteor/app/user-status/client/notifications/deleteCustomUserStatus.js delete mode 100644 apps/meteor/app/user-status/client/notifications/updateCustomUserStatus.js create mode 100644 apps/meteor/client/providers/UserProvider/hooks/useUpdateCustomUserStatus.ts diff --git a/apps/meteor/app/user-status/client/index.ts b/apps/meteor/app/user-status/client/index.ts index 122887520cc84..62fb2a7e9fd03 100644 --- a/apps/meteor/app/user-status/client/index.ts +++ b/apps/meteor/app/user-status/client/index.ts @@ -1,5 +1,3 @@ -import './notifications/deleteCustomUserStatus'; -import './notifications/updateCustomUserStatus'; import './lib/customUserStatus'; export { userStatus } from './lib/userStatus'; diff --git a/apps/meteor/app/user-status/client/notifications/deleteCustomUserStatus.js b/apps/meteor/app/user-status/client/notifications/deleteCustomUserStatus.js deleted file mode 100644 index 24d503d57d724..0000000000000 --- a/apps/meteor/app/user-status/client/notifications/deleteCustomUserStatus.js +++ /dev/null @@ -1,6 +0,0 @@ -import { Meteor } from 'meteor/meteor'; - -import { Notifications } from '../../../notifications/client'; -import { deleteCustomUserStatus } from '../lib/customUserStatus'; - -Meteor.startup(() => Notifications.onLogged('deleteCustomUserStatus', (data) => deleteCustomUserStatus(data.userStatusData))); diff --git a/apps/meteor/app/user-status/client/notifications/updateCustomUserStatus.js b/apps/meteor/app/user-status/client/notifications/updateCustomUserStatus.js deleted file mode 100644 index f5949b0389486..0000000000000 --- a/apps/meteor/app/user-status/client/notifications/updateCustomUserStatus.js +++ /dev/null @@ -1,6 +0,0 @@ -import { Meteor } from 'meteor/meteor'; - -import { Notifications } from '../../../notifications/client'; -import { updateCustomUserStatus } from '../lib/customUserStatus'; - -Meteor.startup(() => Notifications.onLogged('updateCustomUserStatus', (data) => updateCustomUserStatus(data.userStatusData))); diff --git a/apps/meteor/client/providers/UserProvider/UserProvider.tsx b/apps/meteor/client/providers/UserProvider/UserProvider.tsx index 09f631ffa6a64..e49954656c3c7 100644 --- a/apps/meteor/client/providers/UserProvider/UserProvider.tsx +++ b/apps/meteor/client/providers/UserProvider/UserProvider.tsx @@ -15,6 +15,7 @@ import { createReactiveSubscriptionFactory } from '../../lib/createReactiveSubsc import { useCreateFontStyleElement } from '../../views/account/accessibility/hooks/useCreateFontStyleElement'; import { useEmailVerificationWarning } from './hooks/useEmailVerificationWarning'; import { useLDAPAndCrowdCollisionWarning } from './hooks/useLDAPAndCrowdCollisionWarning'; +import { useUpdateCustomUserStatus } from './hooks/useUpdateCustomUserStatus'; const getUserId = (): string | null => Meteor.userId(); @@ -78,6 +79,8 @@ const UserProvider = ({ children }: UserProviderProps): ReactElement => { useLDAPAndCrowdCollisionWarning(); useEmailVerificationWarning(user ?? undefined); + useUpdateCustomUserStatus(); + const contextValue = useMemo( (): ContextType => ({ userId, diff --git a/apps/meteor/client/providers/UserProvider/hooks/useUpdateCustomUserStatus.ts b/apps/meteor/client/providers/UserProvider/hooks/useUpdateCustomUserStatus.ts new file mode 100644 index 0000000000000..0ba973134cb27 --- /dev/null +++ b/apps/meteor/client/providers/UserProvider/hooks/useUpdateCustomUserStatus.ts @@ -0,0 +1,17 @@ +import { useStream } from '@rocket.chat/ui-contexts'; +import { useEffect } from 'react'; + +import { updateCustomUserStatus, deleteCustomUserStatus } from '../../../../app/user-status/client/lib/customUserStatus'; + +export const useUpdateCustomUserStatus = () => { + const notify = useStream('notify-logged'); + useEffect(() => { + const unsubUpdate = notify('updateCustomUserStatus', (data) => updateCustomUserStatus(data.userStatusData)); + const unsubDelete = notify('deleteCustomUserStatus', (data) => deleteCustomUserStatus(data.userStatusData)); + + return () => { + unsubUpdate(); + unsubDelete(); + }; + }, [notify]); +}; From fa5b03e558711fe1871429d8390e7fcc0d942b69 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Tue, 9 Jan 2024 14:39:40 -0300 Subject: [PATCH 04/18] user data --- apps/meteor/client/lib/userData.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/meteor/client/lib/userData.ts b/apps/meteor/client/lib/userData.ts index 5ca61d131f691..ee90f493a30ca 100644 --- a/apps/meteor/client/lib/userData.ts +++ b/apps/meteor/client/lib/userData.ts @@ -2,7 +2,6 @@ import type { ILivechatAgent, IUser, Serialized } from '@rocket.chat/core-typing import { ReactiveVar } from 'meteor/reactive-var'; import { Users } from '../../app/models/client'; -import { Notifications } from '../../app/notifications/client'; import { sdk } from '../../app/utils/client/lib/SDKClient'; export const isSyncReady = new ReactiveVar(false); @@ -60,7 +59,7 @@ export const synchronizeUserData = async (uid: IUser['_id']): Promise { + const result = sdk.stream('notify-user', [`${uid}/userData`], (data) => { switch (data.type) { case 'inserted': // eslint-disable-next-line @typescript-eslint/no-unused-vars From ab4d7a616bb8fcd6bff074cce0889536f26eea81 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Tue, 9 Jan 2024 14:52:42 -0300 Subject: [PATCH 05/18] User Deleted --- .../providers/UserProvider/UserProvider.tsx | 2 ++ .../UserProvider/hooks/useDeleteUser.ts | 32 +++++++++++++++++++ apps/meteor/client/startup/UserDeleted.ts | 28 ---------------- apps/meteor/client/startup/index.ts | 1 - 4 files changed, 34 insertions(+), 29 deletions(-) create mode 100644 apps/meteor/client/providers/UserProvider/hooks/useDeleteUser.ts delete mode 100644 apps/meteor/client/startup/UserDeleted.ts diff --git a/apps/meteor/client/providers/UserProvider/UserProvider.tsx b/apps/meteor/client/providers/UserProvider/UserProvider.tsx index e49954656c3c7..75e753a09f36b 100644 --- a/apps/meteor/client/providers/UserProvider/UserProvider.tsx +++ b/apps/meteor/client/providers/UserProvider/UserProvider.tsx @@ -13,6 +13,7 @@ import { afterLogoutCleanUpCallback } from '../../../lib/callbacks/afterLogoutCl import { useReactiveValue } from '../../hooks/useReactiveValue'; import { createReactiveSubscriptionFactory } from '../../lib/createReactiveSubscriptionFactory'; import { useCreateFontStyleElement } from '../../views/account/accessibility/hooks/useCreateFontStyleElement'; +import { useDeleteUser } from './hooks/useDeleteUser'; import { useEmailVerificationWarning } from './hooks/useEmailVerificationWarning'; import { useLDAPAndCrowdCollisionWarning } from './hooks/useLDAPAndCrowdCollisionWarning'; import { useUpdateCustomUserStatus } from './hooks/useUpdateCustomUserStatus'; @@ -80,6 +81,7 @@ const UserProvider = ({ children }: UserProviderProps): ReactElement => { useEmailVerificationWarning(user ?? undefined); useUpdateCustomUserStatus(); + useDeleteUser(); const contextValue = useMemo( (): ContextType => ({ diff --git a/apps/meteor/client/providers/UserProvider/hooks/useDeleteUser.ts b/apps/meteor/client/providers/UserProvider/hooks/useDeleteUser.ts new file mode 100644 index 0000000000000..e86fe9951a264 --- /dev/null +++ b/apps/meteor/client/providers/UserProvider/hooks/useDeleteUser.ts @@ -0,0 +1,32 @@ +import { useStream } from '@rocket.chat/ui-contexts'; +import { useEffect } from 'react'; + +import { ChatMessage } from '../../../../app/models/client'; + +export const useDeleteUser = () => { + const notify = useStream('notify-logged'); + + useEffect(() => { + return notify('Users:Deleted', ({ userId, messageErasureType, replaceByUser }) => { + if (messageErasureType === 'Unlink' && replaceByUser) { + return ChatMessage.update( + { + 'u._id': userId, + }, + { + $set: { + 'alias': replaceByUser.alias, + 'u._id': replaceByUser._id, + 'u.username': replaceByUser.username, + 'u.name': undefined, + }, + }, + { multi: true }, + ); + } + ChatMessage.remove({ + 'u._id': userId, + }); + }); + }, [notify]); +}; diff --git a/apps/meteor/client/startup/UserDeleted.ts b/apps/meteor/client/startup/UserDeleted.ts deleted file mode 100644 index bbaeb6bc02296..0000000000000 --- a/apps/meteor/client/startup/UserDeleted.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { Meteor } from 'meteor/meteor'; - -import { ChatMessage } from '../../app/models/client'; -import { Notifications } from '../../app/notifications/client'; - -Meteor.startup(() => { - Notifications.onLogged('Users:Deleted', ({ userId, messageErasureType, replaceByUser }) => { - if (messageErasureType === 'Unlink' && replaceByUser) { - return ChatMessage.update( - { - 'u._id': userId, - }, - { - $set: { - 'alias': replaceByUser.alias, - 'u._id': replaceByUser._id, - 'u.username': replaceByUser.username, - 'u.name': undefined, - }, - }, - { multi: true }, - ); - } - ChatMessage.remove({ - 'u._id': userId, - }); - }); -}); diff --git a/apps/meteor/client/startup/index.ts b/apps/meteor/client/startup/index.ts index 61eaa0da16ed6..972018f47f010 100644 --- a/apps/meteor/client/startup/index.ts +++ b/apps/meteor/client/startup/index.ts @@ -27,6 +27,5 @@ import './slashCommands'; import './startup'; import './streamMessage'; import './unread'; -import './UserDeleted'; import './userRoles'; import './userStatusManuallySet'; From 84cbf6fd777a190df508da992ea2ebeb16a748fa Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Tue, 9 Jan 2024 15:23:28 -0300 Subject: [PATCH 06/18] e2ekeyRequest listener --- apps/meteor/client/startup/e2e.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/meteor/client/startup/e2e.ts b/apps/meteor/client/startup/e2e.ts index ddfbada67c2ae..f9cf156f8d8b3 100644 --- a/apps/meteor/client/startup/e2e.ts +++ b/apps/meteor/client/startup/e2e.ts @@ -4,8 +4,8 @@ import { Tracker } from 'meteor/tracker'; import { e2e } from '../../app/e2e/client/rocketchat.e2e'; import { Subscriptions, ChatRoom } from '../../app/models/client'; -import { Notifications } from '../../app/notifications/client'; import { settings } from '../../app/settings/client'; +import { sdk } from '../../app/utils/client/lib/SDKClient'; import { onClientBeforeSendMessage } from '../lib/onClientBeforeSendMessage'; import { onClientMessageReceived } from '../lib/onClientMessageReceived'; import { isLayoutEmbedded } from '../lib/utils/isLayoutEmbedded'; @@ -38,23 +38,25 @@ Meteor.startup(() => { let observable: Meteor.LiveQueryHandle | null = null; let offClientMessageReceived: undefined | (() => void); let offClientBeforeSendMessage: undefined | (() => void); + let unsubNotifyUser: undefined | (() => void); Tracker.autorun(() => { if (!e2e.isReady()) { offClientMessageReceived?.(); - Notifications.unUser('e2ekeyRequest'); + unsubNotifyUser?.(); + unsubNotifyUser = undefined; observable?.stop(); offClientBeforeSendMessage?.(); return; } - Notifications.onUser('e2ekeyRequest', async (roomId, keyId): Promise => { + unsubNotifyUser = sdk.stream('notify-user', [`${Meteor.userId()}/e2ekeyRequest`], async (roomId, keyId): Promise => { const e2eRoom = await e2e.getInstanceByRoomId(roomId); if (!e2eRoom) { return; } e2eRoom.provideKeyToUser(keyId); - }); + }).stop; observable = Subscriptions.find().observe({ changed: async (sub: ISubscription) => { From 97c4c48951ca63ae7c2a6168aa20afa351498d25 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Tue, 9 Jan 2024 15:29:45 -0300 Subject: [PATCH 07/18] user roles --- apps/meteor/client/startup/userRoles.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/meteor/client/startup/userRoles.ts b/apps/meteor/client/startup/userRoles.ts index a311148a65639..77ba6978d4850 100644 --- a/apps/meteor/client/startup/userRoles.ts +++ b/apps/meteor/client/startup/userRoles.ts @@ -2,7 +2,6 @@ import { Meteor } from 'meteor/meteor'; import { Tracker } from 'meteor/tracker'; import { UserRoles, ChatMessage } from '../../app/models/client'; -import { Notifications } from '../../app/notifications/client'; import { sdk } from '../../app/utils/client/lib/SDKClient'; import { dispatchToastMessage } from '../lib/toast'; @@ -20,7 +19,7 @@ Meteor.startup(() => { dispatchToastMessage({ type: 'error', message: error }); }); - Notifications.onLogged('roles-change', (role) => { + sdk.stream('notify-logged', ['roles-change'], (role) => { if (role.type === 'added') { if (!role.scope) { if (!role.u) { From 413e131b6ec429adf0a1d32222d221e7363719b6 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Tue, 9 Jan 2024 16:34:56 -0300 Subject: [PATCH 08/18] incoming messages --- apps/meteor/client/startup/incomingMessages.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/meteor/client/startup/incomingMessages.ts b/apps/meteor/client/startup/incomingMessages.ts index 88e83b9cf5e85..a0fc68221ae13 100644 --- a/apps/meteor/client/startup/incomingMessages.ts +++ b/apps/meteor/client/startup/incomingMessages.ts @@ -2,8 +2,8 @@ import type { IMessage } from '@rocket.chat/core-typings'; import { Meteor } from 'meteor/meteor'; import { ChatMessage } from '../../app/models/client'; -import { Notifications } from '../../app/notifications/client'; import { CachedCollectionManager } from '../../app/ui-cached-collection/client'; +import { sdk } from '../../app/utils/client/lib/SDKClient'; Meteor.startup(() => { Tracker.autorun(() => { @@ -11,7 +11,9 @@ Meteor.startup(() => { return; } - Notifications.onUser('message', (msg: IMessage) => { + // Only event I found triggers this is from ephemeral messages + // Other types of messages come from another stream + sdk.stream('notify-user', [`${Meteor.userId()}/message`], (msg: IMessage) => { msg.u = msg.u || { username: 'rocket.cat' }; msg.private = true; @@ -20,7 +22,9 @@ Meteor.startup(() => { }); CachedCollectionManager.onLogin(() => { - Notifications.onUser('subscriptions-changed', (_action, sub) => { + sdk.stream('notify-user', [`${Meteor.userId()}/subscriptions-changed`], (_action, sub) => { + console.log('subscriptions-changed', _action, sub); + ChatMessage.update( { rid: sub.rid, From fbef1cc8aa032243412d62d4793e64678468984c Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Tue, 9 Jan 2024 16:37:03 -0300 Subject: [PATCH 09/18] force logout --- apps/meteor/client/startup/forceLogout.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/meteor/client/startup/forceLogout.ts b/apps/meteor/client/startup/forceLogout.ts index 9226229ae4182..f882354062cda 100644 --- a/apps/meteor/client/startup/forceLogout.ts +++ b/apps/meteor/client/startup/forceLogout.ts @@ -2,7 +2,7 @@ import { Meteor } from 'meteor/meteor'; import { Session } from 'meteor/session'; import { Tracker } from 'meteor/tracker'; -import { Notifications } from '../../app/notifications/client'; +import { sdk } from '../../app/utils/client/lib/SDKClient'; Meteor.startup(() => { Tracker.autorun(() => { @@ -12,7 +12,7 @@ Meteor.startup(() => { return; } Session.set('force_logout', false); - Notifications.onUser('force_logout', () => { + sdk.stream('notify-user', [`${userId}/force_logout`], () => { Session.set('force_logout', true); }); }); From b15a243e5500574088d5ead39631bcc35be5009d Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Wed, 10 Jan 2024 11:54:48 -0300 Subject: [PATCH 10/18] LegacyRoomManager --- .../ui-utils/client/lib/LegacyRoomManager.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts b/apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts index 04446ebd3bded..91b848ffefde4 100644 --- a/apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts +++ b/apps/meteor/app/ui-utils/client/lib/LegacyRoomManager.ts @@ -9,7 +9,6 @@ import { fireGlobalEvent } from '../../../../client/lib/utils/fireGlobalEvent'; import { getConfig } from '../../../../client/lib/utils/getConfig'; import { callbacks } from '../../../../lib/callbacks'; import { CachedChatRoom, ChatMessage, ChatSubscription, CachedChatSubscription } from '../../../models/client'; -import { Notifications } from '../../../notifications/client'; import { sdk } from '../../../utils/client/lib/SDKClient'; import { upsertMessage, RoomHistoryManager } from './RoomHistoryManager'; import { mainReady } from './mainReady'; @@ -37,8 +36,8 @@ function close(typeName: string) { if (openedRooms[typeName]) { if (openedRooms[typeName].rid) { sdk.stop('room-messages', openedRooms[typeName].rid); - Notifications.unRoom(openedRooms[typeName].rid, 'deleteMessage'); - Notifications.unRoom(openedRooms[typeName].rid, 'deleteMessageBulk'); + sdk.stop('notify-room', `${openedRooms[typeName].rid}/deleteMessage`); + sdk.stop('notify-room', `${openedRooms[typeName].rid}/deleteMessageBulk`); } openedRooms[typeName].ready = false; @@ -135,20 +134,21 @@ const computation = Tracker.autorun(() => { }); // when we receive a messages imported event we just clear the room history and fetch it again - Notifications.onRoom(record.rid, 'messagesImported', async () => { + sdk.stream('notify-room', [`${record.rid}/messagesImported`], async () => { await RoomHistoryManager.clear(record.rid); await RoomHistoryManager.getMore(record.rid); }); - Notifications.onRoom(record.rid, 'deleteMessage', (msg) => { + sdk.stream('notify-room', [`${record.rid}/deleteMessage`], (msg) => { ChatMessage.remove({ _id: msg._id }); // remove thread refenrece from deleted message ChatMessage.update({ tmid: msg._id }, { $unset: { tmid: 1 } }, { multi: true }); }); - Notifications.onRoom( - record.rid, - 'deleteMessageBulk', + + sdk.stream( + 'notify-room', + [`${record.rid}/deleteMessageBulk`], ({ rid, ts, excludePinned, ignoreDiscussion, users, ids, showDeletedStatus }) => { const query: Mongo.Selector = { rid }; @@ -177,7 +177,8 @@ const computation = Tracker.autorun(() => { return ChatMessage.remove(query); }, ); - Notifications.onRoom(record.rid, 'messagesRead', ({ tmid, until }) => { + + sdk.stream('notify-room', [`${record.rid}/messagesRead`], ({ tmid, until }) => { if (tmid) { return ChatMessage.update( { From 3d959c35c21b06a8448a557ed45724923af8db9c Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Wed, 10 Jan 2024 12:10:50 -0300 Subject: [PATCH 11/18] UserAction --- apps/meteor/app/ui/client/lib/UserAction.ts | 28 ++++++++++----------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/apps/meteor/app/ui/client/lib/UserAction.ts b/apps/meteor/app/ui/client/lib/UserAction.ts index ab58d641c1490..00ff3113e5860 100644 --- a/apps/meteor/app/ui/client/lib/UserAction.ts +++ b/apps/meteor/app/ui/client/lib/UserAction.ts @@ -3,8 +3,8 @@ import { debounce } from 'lodash'; import { Meteor } from 'meteor/meteor'; import { ReactiveDict } from 'meteor/reactive-dict'; -import { Notifications } from '../../../notifications/client'; import { settings } from '../../../settings/client'; +import { sdk } from '../../../utils/client/lib/SDKClient'; const TIMEOUT = 15000; const RENEW = TIMEOUT / 3; @@ -38,7 +38,7 @@ const shownName = function (user: IUser | null | undefined): string | undefined const emitActivities = debounce(async (rid: string, extras: IExtras): Promise => { const activities = roomActivities.get(extras?.tmid || rid) || new Set(); - Notifications.notifyRoom(rid, USER_ACTIVITY, shownName(Meteor.user() as unknown as IUser), [...activities], extras); + sdk.publish('notify-room', [`${rid}/${USER_ACTIVITY}`, shownName(Meteor.user() as unknown as IUser), [...activities], extras]); }, 500); function handleStreamAction(rid: string, username: string, activityTypes: string[], extras?: IExtras): void { @@ -65,10 +65,11 @@ function handleStreamAction(rid: string, username: string, activityTypes: string performingUsers.set(rid, roomActivities); } export const UserAction = new (class { - addStream(rid: string): void { + addStream(rid: string): () => void { if (rooms.get(rid)) { - return; + throw new Error('UserAction - addStream should only be called once per room'); } + const handler = function (username: string, activityType: string[], extras?: object): void { const user = Meteor.users.findOne(Meteor.userId() || undefined, { fields: { name: 1, username: 1 }, @@ -79,7 +80,15 @@ export const UserAction = new (class { handleStreamAction(rid, username, activityType, extras); }; rooms.set(rid, handler); - Notifications.onRoom(rid, USER_ACTIVITY, handler); + + const { stop } = sdk.stream('notify-room', [`${rid}/${USER_ACTIVITY}`], handler); + return () => { + if (!rooms.get(rid)) { + return; + } + stop(); + rooms.delete(rid); + }; } performContinuously(rid: string, activityType: string, extras: IExtras = {}): void { @@ -156,15 +165,6 @@ export const UserAction = new (class { void emitActivities(rid, extras); } - cancel(rid: string): void { - if (!rooms.get(rid)) { - return; - } - - Notifications.unRoom(rid, USER_ACTIVITY); - rooms.delete(rid); - } - get(roomId: string): IRoomActivity | undefined { return performingUsers.get(roomId); } From 9ecdd2d996061c017fa72b4d1425ffbecbb4f758 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Wed, 10 Jan 2024 15:34:00 -0300 Subject: [PATCH 12/18] User Action --- apps/meteor/client/views/room/providers/RoomProvider.tsx | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/apps/meteor/client/views/room/providers/RoomProvider.tsx b/apps/meteor/client/views/room/providers/RoomProvider.tsx index 5c539828a9141..2bdcd89209694 100644 --- a/apps/meteor/client/views/room/providers/RoomProvider.tsx +++ b/apps/meteor/client/views/room/providers/RoomProvider.tsx @@ -96,14 +96,7 @@ const RoomProvider = ({ rid, children }: RoomProviderProps): ReactElement => { return; } - UserAction.addStream(rid); - return (): void => { - try { - UserAction.cancel(rid); - } catch (error) { - // Do nothing - } - }; + return UserAction.addStream(rid); }, [rid, subscribed]); if (!pseudoRoom) { From 030f7c102c8ae1b6437249581bee1439f9c1cf81 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Wed, 10 Jan 2024 15:45:15 -0300 Subject: [PATCH 13/18] usersNamesChanged --- .../client/startup/notifications/index.ts | 1 - .../startup/notifications/usersNameChanged.ts | 69 ------------------- .../views/room/providers/RoomProvider.tsx | 3 + .../providers/hooks/useUsersNameChanged.ts | 69 +++++++++++++++++++ 4 files changed, 72 insertions(+), 70 deletions(-) delete mode 100644 apps/meteor/client/startup/notifications/usersNameChanged.ts create mode 100644 apps/meteor/client/views/room/providers/hooks/useUsersNameChanged.ts diff --git a/apps/meteor/client/startup/notifications/index.ts b/apps/meteor/client/startup/notifications/index.ts index e94aaacdd7837..e21f3a247251c 100644 --- a/apps/meteor/client/startup/notifications/index.ts +++ b/apps/meteor/client/startup/notifications/index.ts @@ -1,4 +1,3 @@ import './konchatNotifications'; import './notification'; import './updateAvatar'; -import './usersNameChanged'; diff --git a/apps/meteor/client/startup/notifications/usersNameChanged.ts b/apps/meteor/client/startup/notifications/usersNameChanged.ts deleted file mode 100644 index a1dacf9a1945c..0000000000000 --- a/apps/meteor/client/startup/notifications/usersNameChanged.ts +++ /dev/null @@ -1,69 +0,0 @@ -import type { IUser } from '@rocket.chat/core-typings'; -import { Meteor } from 'meteor/meteor'; - -import { Messages, Subscriptions } from '../../../app/models/client'; -import { Notifications } from '../../../app/notifications/client'; - -type UsersNameChangedEvent = Partial; - -Meteor.startup(() => { - Notifications.onLogged('Users:NameChanged', ({ _id, name, username }: UsersNameChangedEvent) => { - Messages.update( - { - 'u._id': _id, - }, - { - $set: { - 'u.username': username, - 'u.name': name, - }, - }, - { - multi: true, - }, - ); - - Messages.update( - { - 'editedBy._id': _id, - }, - { - $set: { - 'editedBy.username': username, - }, - }, - { - multi: true, - }, - ); - - Messages.update( - { - mentions: { - $elemMatch: { _id }, - }, - }, - { - $set: { - 'mentions.$.username': username, - 'mentions.$.name': name, - }, - }, - { - multi: true, - }, - ); - - Subscriptions.update( - { - name: username, - t: 'd', - }, - { - $set: { - fname: name, - }, - }, - ); - }); -}); diff --git a/apps/meteor/client/views/room/providers/RoomProvider.tsx b/apps/meteor/client/views/room/providers/RoomProvider.tsx index 2bdcd89209694..88884d01439d3 100644 --- a/apps/meteor/client/views/room/providers/RoomProvider.tsx +++ b/apps/meteor/client/views/room/providers/RoomProvider.tsx @@ -19,6 +19,7 @@ import ComposerPopupProvider from './ComposerPopupProvider'; import RoomToolboxProvider from './RoomToolboxProvider'; import { useRedirectOnSettingsChanged } from './hooks/useRedirectOnSettingsChanged'; import { useRoomQuery } from './hooks/useRoomQuery'; +import { useUsersNameChanged } from './hooks/useUsersNameChanged'; type RoomProviderProps = { children: ReactNode; @@ -42,6 +43,8 @@ const RoomProvider = ({ rid, children }: RoomProviderProps): ReactElement => { useRedirectOnSettingsChanged(subscriptionQuery.data); + useUsersNameChanged(); + const pseudoRoom = useMemo(() => { if (!room) { return null; diff --git a/apps/meteor/client/views/room/providers/hooks/useUsersNameChanged.ts b/apps/meteor/client/views/room/providers/hooks/useUsersNameChanged.ts new file mode 100644 index 0000000000000..80cdc2dd9f042 --- /dev/null +++ b/apps/meteor/client/views/room/providers/hooks/useUsersNameChanged.ts @@ -0,0 +1,69 @@ +import { useStream } from '@rocket.chat/ui-contexts'; +import { useEffect } from 'react'; + +import { Messages, Subscriptions } from '../../../../../app/models/client'; + +export const useUsersNameChanged = () => { + const notify = useStream('notify-logged'); + useEffect(() => { + return notify('Users:NameChanged', ({ _id, name, username }) => { + Messages.update( + { + 'u._id': _id, + }, + { + $set: { + 'u.username': username, + 'u.name': name, + }, + }, + { + multi: true, + }, + ); + + Messages.update( + { + 'editedBy._id': _id, + }, + { + $set: { + 'editedBy.username': username, + }, + }, + { + multi: true, + }, + ); + + Messages.update( + { + mentions: { + $elemMatch: { _id }, + }, + }, + { + $set: { + 'mentions.$.username': username, + 'mentions.$.name': name, + }, + }, + { + multi: true, + }, + ); + + Subscriptions.update( + { + name: username, + t: 'd', + }, + { + $set: { + fname: name, + }, + }, + ); + }); + }, [notify]); +}; From b92c8f9aa2b35fe24f09e0f0e8af6fa3fc1d7a8c Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Wed, 10 Jan 2024 15:45:38 -0300 Subject: [PATCH 14/18] remove incomingMessagesLog --- apps/meteor/client/startup/incomingMessages.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/meteor/client/startup/incomingMessages.ts b/apps/meteor/client/startup/incomingMessages.ts index a0fc68221ae13..e9659cc247244 100644 --- a/apps/meteor/client/startup/incomingMessages.ts +++ b/apps/meteor/client/startup/incomingMessages.ts @@ -23,8 +23,6 @@ Meteor.startup(() => { CachedCollectionManager.onLogin(() => { sdk.stream('notify-user', [`${Meteor.userId()}/subscriptions-changed`], (_action, sub) => { - console.log('subscriptions-changed', _action, sub); - ChatMessage.update( { rid: sub.rid, From ec6f01718642271e94bfd7ca423dda0ec7ddd66a Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Wed, 10 Jan 2024 15:55:07 -0300 Subject: [PATCH 15/18] update Users:NameChanged type --- ee/packages/ddp-client/src/types/streams.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ee/packages/ddp-client/src/types/streams.ts b/ee/packages/ddp-client/src/types/streams.ts index da9b913fc6dd1..6971e328bdb78 100644 --- a/ee/packages/ddp-client/src/types/streams.ts +++ b/ee/packages/ddp-client/src/types/streams.ts @@ -231,7 +231,7 @@ export interface StreamerEvents { }, ]; }, - { key: 'Users:NameChanged'; args: [Pick] }, + { key: 'Users:NameChanged'; args: [Pick] }, { key: 'private-settings-changed'; args: ['inserted' | 'updated' | 'removed' | 'changed', ISetting] }, { key: 'deleteCustomUserStatus'; args: [{ userStatusData: unknown }] }, { key: 'user-status'; args: [[IUser['_id'], IUser['username'], 0 | 1 | 2 | 3, IUser['statusText'], IUser['name'], IUser['roles']]] }, From e7c56c2e897c6b6cd755d3419ee253b472f425b8 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Wed, 10 Jan 2024 15:56:16 -0300 Subject: [PATCH 16/18] updateAvatar --- .../providers/UserProvider/UserProvider.tsx | 2 ++ .../UserProvider/hooks/useUpdateAvatar.ts | 15 +++++++++++++++ apps/meteor/client/startup/notifications/index.ts | 1 - .../client/startup/notifications/updateAvatar.ts | 12 ------------ 4 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 apps/meteor/client/providers/UserProvider/hooks/useUpdateAvatar.ts delete mode 100644 apps/meteor/client/startup/notifications/updateAvatar.ts diff --git a/apps/meteor/client/providers/UserProvider/UserProvider.tsx b/apps/meteor/client/providers/UserProvider/UserProvider.tsx index f1d9e7f783e0f..8552de8b61300 100644 --- a/apps/meteor/client/providers/UserProvider/UserProvider.tsx +++ b/apps/meteor/client/providers/UserProvider/UserProvider.tsx @@ -16,6 +16,7 @@ import { useCreateFontStyleElement } from '../../views/account/accessibility/hoo import { useDeleteUser } from './hooks/useDeleteUser'; import { useEmailVerificationWarning } from './hooks/useEmailVerificationWarning'; import { useLDAPAndCrowdCollisionWarning } from './hooks/useLDAPAndCrowdCollisionWarning'; +import { useUpdateAvatar } from './hooks/useUpdateAvatar'; import { useUpdateCustomUserStatus } from './hooks/useUpdateCustomUserStatus'; const getUserId = (): string | null => Meteor.userId(); @@ -82,6 +83,7 @@ const UserProvider = ({ children }: UserProviderProps): ReactElement => { useUpdateCustomUserStatus(); useDeleteUser(); + useUpdateAvatar(); const contextValue = useMemo( (): ContextType => ({ diff --git a/apps/meteor/client/providers/UserProvider/hooks/useUpdateAvatar.ts b/apps/meteor/client/providers/UserProvider/hooks/useUpdateAvatar.ts new file mode 100644 index 0000000000000..292880e23da8c --- /dev/null +++ b/apps/meteor/client/providers/UserProvider/hooks/useUpdateAvatar.ts @@ -0,0 +1,15 @@ +import { useStream } from '@rocket.chat/ui-contexts'; +import { Meteor } from 'meteor/meteor'; +import { useEffect } from 'react'; + +export const useUpdateAvatar = () => { + const notify = useStream('notify-logged'); + useEffect(() => { + return notify('updateAvatar', (data) => { + if ('username' in data) { + const { username, etag } = data; + username && Meteor.users.update({ username }, { $set: { avatarETag: etag } }); + } + }); + }, [notify]); +}; diff --git a/apps/meteor/client/startup/notifications/index.ts b/apps/meteor/client/startup/notifications/index.ts index e21f3a247251c..866dae4d52f3a 100644 --- a/apps/meteor/client/startup/notifications/index.ts +++ b/apps/meteor/client/startup/notifications/index.ts @@ -1,3 +1,2 @@ import './konchatNotifications'; import './notification'; -import './updateAvatar'; diff --git a/apps/meteor/client/startup/notifications/updateAvatar.ts b/apps/meteor/client/startup/notifications/updateAvatar.ts deleted file mode 100644 index b26e184aca205..0000000000000 --- a/apps/meteor/client/startup/notifications/updateAvatar.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Meteor } from 'meteor/meteor'; - -import { Notifications } from '../../../app/notifications/client'; - -Meteor.startup(() => { - Notifications.onLogged('updateAvatar', (data) => { - if ('username' in data) { - const { username, etag } = data; - username && Meteor.users.update({ username }, { $set: { avatarETag: etag } }); - } - }); -}); From b047f8fdbd18e7cbbcf0abb49da45d96ebba31b6 Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Wed, 10 Jan 2024 16:16:26 -0300 Subject: [PATCH 17/18] konchatNotifications --- .../startup/notifications/konchatNotifications.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/apps/meteor/client/startup/notifications/konchatNotifications.ts b/apps/meteor/client/startup/notifications/konchatNotifications.ts index 723901b7f70b7..cd16d4264479d 100644 --- a/apps/meteor/client/startup/notifications/konchatNotifications.ts +++ b/apps/meteor/client/startup/notifications/konchatNotifications.ts @@ -4,10 +4,10 @@ import { Tracker } from 'meteor/tracker'; import { lazy } from 'react'; import { CachedChatSubscription } from '../../../app/models/client'; -import { Notifications } from '../../../app/notifications/client'; import { settings } from '../../../app/settings/client'; import { KonchatNotification } from '../../../app/ui/client/lib/KonchatNotification'; import { getUserPreference } from '../../../app/utils/client'; +import { sdk } from '../../../app/utils/client/lib/SDKClient'; import { RoomManager } from '../../lib/RoomManager'; import { imperativeModal } from '../../lib/imperativeModal'; import { fireGlobalEvent } from '../../lib/utils/fireGlobalEvent'; @@ -71,17 +71,18 @@ Meteor.startup(() => { }; Tracker.autorun(() => { if (!Meteor.userId() || !settings.get('Outlook_Calendar_Enabled')) { - return Notifications.unUser('calendar'); + sdk.stop('notify-user', `${Meteor.userId()}/calendar`); } - Notifications.onUser('calendar', notifyUserCalendar); + sdk.stream('notify-user', [`${Meteor.userId()}/calendar`], notifyUserCalendar); }); Tracker.autorun(() => { if (!Meteor.userId()) { return; } - Notifications.onUser('notification', (notification) => { + + sdk.stream('notify-user', [`${Meteor.userId()}/notification`], (notification) => { const openedRoomId = ['channel', 'group', 'direct'].includes(router.getRouteName()!) ? RoomManager.opened : undefined; // This logic is duplicated in /client/startup/unread.coffee. @@ -111,7 +112,7 @@ Meteor.startup(() => { void notifyNewRoom(sub); }); - Notifications.onUser('subscriptions-changed', (action, sub) => { + sdk.stream('notify-user', [`${Meteor.userId()}/subscriptions-changed`], (action, sub) => { if (action === 'removed') { return; } From c4f40841f2cd7b0f35dba314bdee6c137ac0b26c Mon Sep 17 00:00:00 2001 From: gabriellsh Date: Wed, 10 Jan 2024 16:25:51 -0300 Subject: [PATCH 18/18] PrivateSettingsCachedCollection --- .../client/lib/settings/PrivateSettingsCachedCollection.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/meteor/client/lib/settings/PrivateSettingsCachedCollection.ts b/apps/meteor/client/lib/settings/PrivateSettingsCachedCollection.ts index 560d604534ed1..da3f7aa43973e 100644 --- a/apps/meteor/client/lib/settings/PrivateSettingsCachedCollection.ts +++ b/apps/meteor/client/lib/settings/PrivateSettingsCachedCollection.ts @@ -1,7 +1,7 @@ import type { ISetting } from '@rocket.chat/core-typings'; -import { Notifications } from '../../../app/notifications/client'; import { CachedCollection } from '../../../app/ui-cached-collection/client'; +import { sdk } from '../../../app/utils/client/lib/SDKClient'; class PrivateSettingsCachedCollection extends CachedCollection { constructor() { @@ -12,7 +12,7 @@ class PrivateSettingsCachedCollection extends CachedCollection { } async setupListener(): Promise { - Notifications.onLogged(this.eventName as 'private-settings-changed', async (t: string, { _id, ...record }: { _id: string }) => { + sdk.stream('notify-logged', [this.eventName as 'private-settings-changed'], async (t: string, { _id, ...record }: { _id: string }) => { this.log('record received', t, { _id, ...record }); this.collection.upsert({ _id }, record); this.sync();