-
Notifications
You must be signed in to change notification settings - Fork 10.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Add the possibility for removing the watcher for the message collection #30381
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
957fda2
chore: add a flag to disable db watchers on the message collection
MarcosSpessatto 5e61b10
Merge branch 'develop' into chore/remove-message-db-watcher-1
MarcosSpessatto be538b1
Merge branch 'develop' into chore/remove-message-db-watcher-1
MarcosSpessatto b6327ff
Merge branch 'develop' into chore/remove-message-db-watcher-1
MarcosSpessatto ef450c8
Merge branch 'develop' into chore/remove-message-db-watcher-1
MarcosSpessatto 1126a8a
Merge branch 'develop' into chore/remove-message-db-watcher-1
sampaiodiego de50690
Merge branch 'develop' into chore/remove-message-db-watcher-1
sampaiodiego 23b67e3
Merge branch 'develop' into chore/remove-message-db-watcher-1
MarcosSpessatto ce94c2a
Merge branch 'develop' into chore/remove-message-db-watcher-1
MarcosSpessatto 13e4a99
chore: remove unnecessary fn
MarcosSpessatto 4504415
chore: publish events directly using broadcast
MarcosSpessatto 4a92a42
chore: useless param
MarcosSpessatto 62965d3
Merge branch 'develop' into chore/remove-message-db-watcher-1
MarcosSpessatto 62a7f29
chore: undo param change
MarcosSpessatto 040c1af
chore: delete user working without relying on msg db watcher (#30435)
MarcosSpessatto a7a264e
Merge branch 'develop' into chore/remove-message-db-watcher-1
MarcosSpessatto b790dd8
Merge branch 'develop' into chore/remove-message-db-watcher-1
sampaiodiego File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ggazzo @sampaiodiego I decided to create another event to handle the "manual" process of sending events instead of using the existing
watch.messages
.