-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmessage.private.js
More file actions
76 lines (63 loc) · 2.22 KB
/
message.private.js
File metadata and controls
76 lines (63 loc) · 2.22 KB
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
import eventManager from 'src/utils/eventManager.js';
import * as settings from 'src/utils/settings/index.js';
import { global } from 'src/utils/global.js';
import { toast } from 'src/utils/2.toasts.js';
import { debug } from 'src/utils/debug.js';
import each from 'src/utils/each.js';
import { buttonCSS as css } from 'src/utils/1.variables.js';
import { isMod, name as username } from 'src/utils/user';
import streaming from './0.streamer.js';
// Toast for private messages while streaming mode is on
// TODO: translation
const busyMessage = ':me:is in do not disturb mode'; // TODO: configurable?
const allow = 'Allow';
const hide = 'Hide';
const silent = 'Hide (silent)';
const setting = settings.register({
name: 'Private Messages',
key: 'underscript.streamer.pms',
options: [allow, hide, silent],
default: hide,
category: 'Streamer Mode',
});
const toasts = {};
eventManager.on('preChat:getPrivateMessage', function streamerMode(data) {
if (!streaming() || data.open) return; // if not streaming, if window is already open
const val = setting.value();
if (val === allow) return; // if private messages are allowed
debug(data);
const message = JSON.parse(data.chatMessage);
const user = message.user;
if (isMod(user)) return; // Moderators are always allowed
this.canceled = true; // Cancel the event from going through
const userId = user.id;
const privateChats = global('privateChats');
const history = privateChats[userId] || [];
history.push(message);
if (userId === global('selfId')) return; // ignore automated reply
global('sendPrivateMessage')(busyMessage, `${userId}`); // send a message that you're busy
if (val === silent || toasts[userId]) return; // Don't announce anymore
toasts[userId] = toast({
// TODO: translation
text: `Message from ${username(user)}`,
buttons: [{
css,
// TODO: translation
text: 'Open',
className: 'dismiss',
onclick: () => {
open(user);
},
}],
className: 'dismissable',
});
});
eventManager.on(':unload', closeAll);
function open(user) {
const { id } = user;
global('openPrivateRoom')(id, username(user).replace('\'', ''));
delete toasts[id];
}
function closeAll() {
each(toasts, (t) => t.close());
}