From b2ecfa83ffc48993232bdef72fac38bf01765f9a Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Thu, 4 Feb 2021 14:01:15 -0300 Subject: [PATCH] =?UTF-8?q?Chore:=20Improve=20performance=20of=20messages?= =?UTF-8?q?=E2=80=99=20watcher=20(#20519)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/modules/watchers/watchers.module.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/server/modules/watchers/watchers.module.ts b/server/modules/watchers/watchers.module.ts index ca202fc85ee..a6f856a4413 100644 --- a/server/modules/watchers/watchers.module.ts +++ b/server/modules/watchers/watchers.module.ts @@ -1,3 +1,5 @@ +import mem from 'mem'; + import { SubscriptionsRaw } from '../../../app/models/server/raw/Subscriptions'; import { UsersRaw } from '../../../app/models/server/raw/Users'; import { SettingsRaw } from '../../../app/models/server/raw/Settings'; @@ -13,7 +15,7 @@ import { IBaseRaw } from '../../../app/models/server/raw/BaseRaw'; import { LivechatInquiryRaw } from '../../../app/models/server/raw/LivechatInquiry'; import { IBaseData } from '../../../definition/IBaseData'; import { IPermission } from '../../../definition/IPermission'; -import { ISetting } from '../../../definition/ISetting'; +import { ISetting, SettingValue } from '../../../definition/ISetting'; import { IInquiry } from '../../../definition/IInquiry'; import { UsersSessionsRaw } from '../../../app/models/server/raw/UsersSessions'; import { IUserSession } from '../../../definition/IUserSession'; @@ -83,6 +85,13 @@ export function initWatchers(models: IModelsParam, broadcast: BroadcastCallback, EmailInbox, } = models; + const getSettingCached = mem(async (setting: string): Promise => Settings.getValueById(setting), { maxAge: 10000 }); + + const getUserNameCached = mem(async (userId: string): Promise => { + const user = await Users.findOneById(userId, { projection: { name: 1 } }); + return user?.name; + }, { maxAge: 10000 }); + watch(Messages, async ({ clientAction, id, data }) => { switch (clientAction) { case 'inserted': @@ -93,18 +102,16 @@ export function initWatchers(models: IModelsParam, broadcast: BroadcastCallback, } if (message._hidden !== true && message.imported == null) { - const UseRealName = await Settings.getValueById('UI_Use_Real_Name') === true; + const UseRealName = await getSettingCached('UI_Use_Real_Name') === true; if (UseRealName) { if (message.u?._id) { - const user = await Users.findOneById(message.u._id); - message.u.name = user?.name; + message.u.name = await getUserNameCached(message.u._id); } if (message.mentions?.length) { for await (const mention of message.mentions) { - const user = await Users.findOneById(mention._id); - mention.name = user?.name; + mention.name = await getUserNameCached(mention._id); } } }