Skip to content

Commit

Permalink
Cache mattermost usernames.
Browse files Browse the repository at this point in the history
  • Loading branch information
haegelsperger authored and yGuy committed Jul 20, 2023
1 parent 0062b7b commit c6203eb
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/botservice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async function onClientMessage(msg: WebSocketMessage<JSONMessageData>, 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
})
}
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -171,6 +171,29 @@ async function getOlderPosts(refPost: Post, options: {lookBackTime?: number, pos
return posts
}

const usernameCache: Record<string, {username: string, expireTime: number}> = {}
/**
* 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<string> {
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<void> {
Log.options({json: true, colors: true})
Expand Down

0 comments on commit c6203eb

Please sign in to comment.