Skip to content
Snippets Groups Projects
Unverified Commit a87289db authored by Guilherme Gazzo's avatar Guilherme Gazzo Committed by GitHub
Browse files

chore(omnichannel): cache room and users between bridges (#32839)

parent e827c58a
No related branches found
No related tags found
No related merge requests found
export const cachedFunction = <F extends (...args: any[]) => any>(fn: F) => {
const cache = new Map<string, unknown>();
return ((...args) => {
const cacheKey = JSON.stringify(args);
if (cache.has(cacheKey)) {
return cache.get(cacheKey) as ReturnType<F>;
}
const result = fn(...args);
cache.set(cacheKey, result);
return result;
}) as F;
};
import { Messages, Rooms, Users } from '@rocket.chat/models';
import { Random } from '@rocket.chat/random';
import { cachedFunction } from './cachedFunction';
import { transformMappedData } from './transformMappedData';
export class AppMessagesConverter {
mem = new WeakMap();
constructor(orch) {
this.orch = orch;
}
......@@ -19,6 +22,15 @@ export class AppMessagesConverter {
return undefined;
}
const cache =
this.mem.get(msgObj) ??
new Map([
['room', cachedFunction(this.orch.getConverters().get('rooms').convertById.bind(this.orch.getConverters().get('rooms')))],
['user', cachedFunction(this.orch.getConverters().get('users').convertById.bind(this.orch.getConverters().get('users')))],
]);
this.mem.set(msgObj, cache);
const map = {
id: '_id',
threadId: 'tmid',
......@@ -37,7 +49,7 @@ export class AppMessagesConverter {
token: 'token',
blocks: 'blocks',
room: async (message) => {
const result = await this.orch.getConverters().get('rooms').convertById(message.rid);
const result = await cache.get('room')(message.rid);
delete message.rid;
return result;
},
......@@ -49,7 +61,7 @@ export class AppMessagesConverter {
return undefined;
}
return this.orch.getConverters().get('users').convertById(editedBy._id);
return cache.get('user')(editedBy._id);
},
attachments: async (message) => {
const result = await this._convertAttachmentsToApp(message.attachments);
......@@ -61,7 +73,7 @@ export class AppMessagesConverter {
return undefined;
}
let user = await this.orch.getConverters().get('users').convertById(message.u._id);
let user = await cache.get('user')(message.u._id);
// When the sender of the message is a Guest (livechat) and not a user
if (!user) {
......
......@@ -5,6 +5,7 @@ import type { IUser } from '@rocket.chat/core-typings';
import { isEditedMessage, type IMessage } from '@rocket.chat/core-typings';
import { Messages } from '@rocket.chat/models';
import { cachedFunction } from './cachedFunction';
import { transformMappedData } from './transformMappedData';
// eslint-disable-next-line @typescript-eslint/naming-convention
......@@ -18,24 +19,6 @@ interface Orchestrator {
};
}
const cachedFunction = <F extends (...args: any[]) => any>(fn: F) => {
const cache = new Map<string, unknown>();
return ((...args) => {
const cacheKey = JSON.stringify(args);
if (cache.has(cacheKey)) {
return cache.get(cacheKey) as ReturnType<F>;
}
const result = fn(...args);
cache.set(cacheKey, result);
return result;
}) as F;
};
export class AppThreadsConverter implements IAppThreadsConverter {
constructor(
private readonly orch: {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment