From c6203ebb4265901f38b0baf1ed203c241789d278 Mon Sep 17 00:00:00 2001 From: haegelsperger Date: Thu, 20 Jul 2023 14:06:15 +0200 Subject: [PATCH] Cache mattermost usernames. --- src/botservice.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/botservice.ts b/src/botservice.ts index 7c7a152..5822170 100644 --- a/src/botservice.ts +++ b/src/botservice.ts @@ -66,7 +66,7 @@ async function onClientMessage(msg: WebSocketMessage, meId: str } else { chatmessages.push({ role: ChatCompletionRequestMessageRoleEnum.User, - name: (await mmClient.getUser(threadPost.user_id)).username, + name: await userIdToName(threadPost.user_id), content: threadPost.message }) } @@ -124,7 +124,7 @@ function isMessageIgnored(msgData: MessageData, meId: string, previousPosts: Pos if(previousPosts[i].props.bot_status === 'stopped') { return true } if(previousPosts[i].user_id === meId || previousPosts[i].message.includes(name)) { - // we are in a thread were we are actively participating or we were mentioned in the thread => respond + // we are in a thread were we are actively participating, or we were mentioned in the thread => respond return false } } @@ -171,6 +171,29 @@ async function getOlderPosts(refPost: Post, options: {lookBackTime?: number, pos return posts } +const usernameCache: Record = {} +/** + * Looks up the mattermost username for the given userId. Every username which is looked up will be cached for 5 minutes. + * @param userId + */ +async function userIdToName(userId: string): Promise { + let username: string + + // check if userId is in cache and not outdated + if(usernameCache[userId] && Date.now() < usernameCache[userId].expireTime ) { + username = usernameCache[userId].username + } else { + // username not in cache our outdated + username = (await mmClient.getUser(userId)).username + usernameCache[userId] = { + username: username, + expireTime: Date.now() + 1000 * 60 * 5 + } + } + + return username +} + /* Entry point */ async function main(): Promise { Log.options({json: true, colors: true})