-
Notifications
You must be signed in to change notification settings - Fork 1
/
web-chat.component.ts
369 lines (313 loc) · 11.8 KB
/
web-chat.component.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import { Component, ElementRef, OnInit, ViewChild, Pipe, PipeTransform } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { Subject, Subscription, debounceTime, filter, take, takeUntil, timer } from 'rxjs';
import { MessagePayload } from 'src/app/models/messagePayload';
import { ParameterData } from 'src/app/models/parameterData';
import { ConnectionStatus, QuixService } from 'src/app/services/quix.service';
import { Animals } from '../../constants/animals';
import { TitleCasePipe } from '@angular/common';
import { MatDialog } from '@angular/material/dialog';
import { ShareChatroomDialogComponent } from '../dialogs/share-chatroom-dialog/share-chatroom-dialog.component';
import { RoomService } from 'src/app/services/room.service';
import { Colors } from 'src/app/constants/colors';
export const POSITIVE_THRESHOLD = 0.5;
export const NEGATIVE_THRESHOLD = -0.5;
export class UserTyping {
timeout?: Subscription;
sentiment?: number;
}
/**
* Custom Pipe to filter the list of messages, so that
* it only shows the messages in the chat where a sentiment
* is present.
*/
@Pipe({
name: 'sentimentFilter'
})
export class SentimentFilterPipe implements PipeTransform {
transform(messages: MessagePayload[], isTwitch: boolean): any[] {
if (!messages || !isTwitch) {
return messages;
}
// Filter the messages to only show the ones where sentiment is present
return messages.filter(message => message.sentiment !== undefined);
}
}
@Component({
selector: 'app-web-chat',
templateUrl: './web-chat.component.html',
styleUrls: ['./web-chat.component.scss'],
providers: [TitleCasePipe]
})
export class WebChatComponent implements OnInit {
username: string;
profilePic: string;
profilePicColor: string;
isTwitch: boolean = true;
scrollTimeoutId: any;
@ViewChild('chatWrapper') chatWrapperEle: ElementRef<HTMLElement>;
@ViewChild('messageInput') messageInputEle: ElementRef;
connectionState = ConnectionStatus;
readerConnectionStatus: ConnectionStatus = ConnectionStatus.Offline;
writerConnectionStatus: ConnectionStatus = ConnectionStatus.Offline;
usernameFC = new FormControl('', Validators.required);
messageFC = new FormControl("");
chatForm = new FormGroup({
message: this.messageFC,
});
messages: MessagePayload[] = [];
happyTypers = new Set<string>();
unhappyTypers = new Set<string>();
usersTyping: Map<string, UserTyping> = new Map();
averageSentiment: number = 0;
typingTimeout: number = 4000;
typingDebounce: number = 300;
messageSent: string | undefined;
draftGuid: string | undefined;
private unsubscribe$ = new Subject<void>();
constructor(public quixService: QuixService, public roomService: RoomService, private titleCasePipe: TitleCasePipe, private matDialog: MatDialog) { }
ngOnInit(): void {
this.profilePic = this.generateProfilePic();
this.profilePicColor = this.generateRandomColor();
this.messageFC.valueChanges.pipe(debounceTime(300), takeUntil(this.unsubscribe$)).subscribe((value) => {
// Prevents it triggering when they send message and debounce is triggered
if (this.messageSent === value) {
this.messageSent = undefined;
return;
}
// Generate a new GUID if there isn't one already or if they clear the input
if (!this.draftGuid || value === '') this.draftGuid = this.generateGUID();
this.sendMessage(true);
});
// Listen for connection status changes
this.quixService.readerConnStatusChanged$.pipe(takeUntil(this.unsubscribe$)).subscribe((status) => {
this.readerConnectionStatus = status;
});
this.quixService.writerConnStatusChanged$.pipe(takeUntil(this.unsubscribe$)).subscribe((status) => {
this.writerConnectionStatus = status
});
// Listen for reader messages
this.quixService.paramDataReceived$.pipe(
takeUntil(this.unsubscribe$),
filter((f) => f.streamId === this.roomService.selectedRoom) // Ensure there is no message leaks
).subscribe((payload) => {
this.messageReceived(payload);
});
this.roomService.roomChanged$.subscribe(({ roomId, isTwitch }) => {
this.messages = [];
this.isTwitch = isTwitch;
if (!isTwitch) {
this.roomService.getChatMessageHistory(roomId).pipe(take(1)).subscribe(lastMessages => {
let sortedMessages = lastMessages.sort((a, b) => a.timestamp - b.timestamp);
this.messages = sortedMessages;
this.scrollToChatBottom();
});
}
});
}
submit(): void {
if (!this.username) {
this.username = this.usernameFC.value!;
this.scrollToChatBottom();
setTimeout(() => this.messageInputEle.nativeElement.focus(), 0);
return;
}
this.sendMessage(false);
this.messageSent = this.messageFC.value!;
this.messageFC.reset("", { emitEvent: false });
}
private sendMessage(isDraft: boolean): void {
const message: string = this.messageFC.value || "";
if (!isDraft) this.draftGuid = undefined;
const payload = {
timestamps: [new Date().getTime() * 1000000],
tagValues: {
room: [this.roomService.selectedRoom],
role: ['Customer'],
name: [this.username],
profilePic: [this.profilePic],
profilePicColor: [this.profilePicColor],
draft_id: [this.draftGuid]
},
stringValues: {
"chat-message": [message],
}
};
this.roomService.sendMessage(payload, isDraft);
}
private messageReceived(payload: ParameterData): void {
let topicId = payload.topicId;
let [timestamp] = payload.timestamps;
let [name] = payload.tagValues["name"];
let profilePic = payload.tagValues["profilePic"]?.at(0);
let profilePicColor = payload.tagValues["profilePicColor"]?.at(0);
let sentiment = payload.numericValues["sentiment"]?.at(0) || undefined;
let averageSentiment = payload.numericValues["average_sentiment"]?.at(0) || 0;
let value = payload.stringValues["chat-message"]?.at(0);
let message = this.messages.find((f) => f.timestamp === timestamp && f.name === name);
let user = this.usersTyping.get(name);
if (topicId === this.quixService.draftsTopic) {
const timer$ = timer(3000);
// If they were already tying
if (user) this.usersTyping.get(name)?.timeout?.unsubscribe();
// When it finishes, removes the user from typing list
const subscription = timer$.subscribe(() => {
this.usersTyping.delete(name);
})
// Add the subscription to the object
this.usersTyping.set(name, {
...user,
timeout: subscription
});
}
if (topicId === this.quixService.draftsSentimentTopic) {
if (!user || !sentiment) return;
user = { ...user, sentiment };
if (sentiment > 0) this.happyTypers.add(name);
if (sentiment < 0) this.unhappyTypers.add(name);
this.usersTyping.set(name, user);
}
if (topicId === this.quixService.messagesSanitizedTopic || topicId === this.quixService.twitchMessagesTopic) {
// If the user is in the typing map then remove them
if (user) {
user?.timeout?.unsubscribe();
this.usersTyping.delete(name);
}
// Push the new message
this.messages.push({timestamp, name, profilePic, profilePicColor, sentiment, value });
}
if (topicId === this.quixService.sentimentTopic) {
if (!message) return;
// Update existing message with the sentiment
message.sentiment = sentiment;
this.averageSentiment = averageSentiment;
// Trigger change detection for the pipe
this.messages = [...this.messages];
}
// Scroll to the button of the chart
this.scrollToChatBottom(true);
}
/**
* Based on the users typing in chat, it generates statistics in the
* sense of faces (Smiley, Neutral and Frowney) on the sentiment of
* the users draft messages.
*
* @returns The statistics from the sentiment.
*/
public getSentimentStats(): { smileys: string[], neutrals: string[], frowneys: string[] } {
let smileys: string[] = [];
let neutrals: string[] = [];
let frowneys: string[] = [];
this.usersTyping.forEach(({sentiment}, name) => {
if (sentiment! > POSITIVE_THRESHOLD) smileys.push(name);
if (sentiment! < POSITIVE_THRESHOLD && sentiment! > NEGATIVE_THRESHOLD) neutrals.push(name);
if (sentiment! < NEGATIVE_THRESHOLD) frowneys.push(name);
});
return { smileys, neutrals, frowneys };
}
/**
* Based on the users typing in the chat, it generates the average sentiment
* of all their draft messages. This can then be displayed on the typing message.
*
* @returns An average of all the userTyping sentiments.
*/
public getDraftSentimentAverage(): number {
// Get all the sentiments for user's currently typing
const sentimentsArray = [...this.usersTyping.values()].map(userTyping => userTyping.sentiment || 0);
// Calculate the sum of all numbers
const sum = sentimentsArray.reduce((acc, current) => acc + current, 0);
// Calculate the mean (average)
const mean = sum / sentimentsArray.length;
return mean;
}
/**
* Based on how many users are typing, it generates the appropriate
* isTyping message to be displayed in the template.
*
* @returns The Html message.
*/
public getTypingMessage(): string | undefined {
const users = Array.from(this.usersTyping.entries())
.map(([key, value]) => ({
name: this.titleCasePipe.transform(key),
sentiment: value.sentiment!,
}));
if (!users.length) return undefined;
if (users.length === 1) {
const [user] = users;
return `<b>${user.name}</b> is typing...`;
}
if (users.length < 3) {
const usersJoined = users.map((m) => m.name).join(' and ');
return `<b>${usersJoined}</b> are typing...`;
}
if (users.length >= 3) {
return `<b>${users.length}</b> users are typing...`;
}
return undefined
}
/**
* Util method for generating a v4 GUID.
*
* @returns The generated GUID.
*/
private generateGUID(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
/**
* Util method for generating a random profile pic from
* a list of images.
*
* @returns The random profile pic.
*/
private generateProfilePic(): string {
const randomNumber = Math.floor(Math.random() * Animals?.length);
return `https://ssl.gstatic.com/docs/common/profile/${Animals[randomNumber]}_lg.png`;
}
/**
* Util method for generating a random color from
* a list of colors.
*
* @returns The random color.
*/
private generateRandomColor(): string {
const randomNumber = Math.floor(Math.random() * Colors?.length);
return Colors[randomNumber];
}
/**
* Only needed for mobile users.
* Scrolls the info section into view.
*/
scrollToInfo(): void {
const chatEle = document.getElementById('info-section');
chatEle?.scrollIntoView({
behavior: 'smooth'
})
}
/**
* Util method to scroll the user to the bottom of the chat
*/
scrollToChatBottom(isCheckBottom?: boolean): void {
const threshold = 30;
const el = this.chatWrapperEle.nativeElement;
const isScrollToBottom = Math.round(el.scrollTop + el.clientHeight) >= el.scrollHeight - threshold;
if (isCheckBottom && !isScrollToBottom) return;
// Clear the existing timeout if it exists
if (this.scrollTimeoutId) clearTimeout(this.scrollTimeoutId);
this.scrollTimeoutId = setTimeout(() => el.scrollTop = el.scrollHeight, 0);
}
openShareChatroomDialog(): void {
this.matDialog.open(ShareChatroomDialogComponent, {
maxWidth: '480px',
autoFocus: false
});
}
ngOnDestroy(): void {
this.unsubscribe$.next();
this.unsubscribe$.complete();
}
}