-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add the possibility for removing the watcher for the message c…
…ollection (#30381)
- Loading branch information
1 parent
f340139
commit c95cad2
Showing
14 changed files
with
146 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { IMessage, SettingValue, IUser } from '@rocket.chat/core-typings'; | ||
import { Messages, Settings, Users } from '@rocket.chat/models'; | ||
import mem from 'mem'; | ||
|
||
const getSettingCached = mem(async (setting: string): Promise<SettingValue> => Settings.getValueById(setting), { maxAge: 10000 }); | ||
|
||
const getUserNameCached = mem( | ||
async (userId: string): Promise<string | undefined> => { | ||
const user = await Users.findOne<Pick<IUser, 'name'>>(userId, { projection: { name: 1 } }); | ||
return user?.name; | ||
}, | ||
{ maxAge: 10000 }, | ||
); | ||
|
||
export const broadcastMessageSentEvent = async ({ | ||
id, | ||
data, | ||
broadcastCallback, | ||
}: { | ||
id: IMessage['_id']; | ||
broadcastCallback: (message: IMessage) => Promise<void>; | ||
data?: IMessage; | ||
}): Promise<void> => { | ||
const message = data ?? (await Messages.findOneById(id)); | ||
if (!message) { | ||
return; | ||
} | ||
|
||
if (message._hidden !== true && message.imported == null) { | ||
const UseRealName = (await getSettingCached('UI_Use_Real_Name')) === true; | ||
|
||
if (UseRealName) { | ||
if (message.u?._id) { | ||
const name = await getUserNameCached(message.u._id); | ||
if (name) { | ||
message.u.name = name; | ||
} | ||
} | ||
|
||
if (message.mentions?.length) { | ||
for await (const mention of message.mentions) { | ||
const name = await getUserNameCached(mention._id); | ||
if (name) { | ||
mention.name = name; | ||
} | ||
} | ||
} | ||
} | ||
|
||
void broadcastCallback(message); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { IMessage } from '@rocket.chat/core-typings'; | ||
|
||
import type { IServiceClass } from '../types/ServiceClass'; | ||
|
||
export const dbWatchersDisabled = ['yes', 'true'].includes(String(process.env.DISABLE_DB_WATCHERS).toLowerCase()); | ||
|
||
export const listenToMessageSentEvent = (service: IServiceClass, action: (message: IMessage) => Promise<void>): void => { | ||
if (dbWatchersDisabled) { | ||
return service.onEvent('message.sent', (message: IMessage) => action(message)); | ||
} | ||
return service.onEvent('watch.messages', ({ message }) => action(message)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters