-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversation.ts
34 lines (28 loc) · 1.03 KB
/
conversation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import Robot from "./robot";
import Response from "./response";
const MAX_MESSAGES_PER_CONVERSATION = 100;
export class ConversationBrain {
robot: Robot;
conversations: {[userId: string]: Response[]} = {};
constructor(robot: Robot) {
this.robot = robot;
}
public addToConversation(response: Response): void {
if (!response.message || !response.message.user || !response.message.user.id) {
this.robot.logger.warn(`[conversation] attempted to add response with no user`, response);
return;
}
const userId = response.message.user.id;
if (!this.conversations[userId]) {
this.conversations[userId] = [];
}
this.conversations[userId].push(response);
if (this.conversations[userId].length > MAX_MESSAGES_PER_CONVERSATION) {
this.conversations[userId].shift();
}
}
// Async because this may move to the DB for scaling in the future. And it doesn't hurt.
public async getConversationForUser(userId: string): Promise<Response[]> {
return this.conversations[userId] || [];
}
}