-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathdesktop_notifications.ts
56 lines (51 loc) · 2.13 KB
/
desktop_notifications.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
import {Channel} from '@mattermost/types/channels';
import {Post} from '@mattermost/types/posts';
import {getCurrentUserId} from 'mattermost-redux/selectors/entities/users';
import {CALL_START_POST_TYPE} from 'src/constants';
import {
channelIDForCurrentCall,
ringingEnabled,
threadIDForCurrentCall,
} from 'src/selectors';
import {DesktopNotificationArgs, Store} from 'src/types/mattermost-webapp';
import {RealNewPostMessageProps} from 'src/types/types';
import {isDmGmChannel} from 'src/utils';
export function desktopNotificationHandler(
store: Store,
post: Post,
msgProps: RealNewPostMessageProps,
channel: Channel,
args: DesktopNotificationArgs,
): { error?: string, args?: DesktopNotificationArgs } {
if (args.notify) {
// Calls will notify if:
// 1. it's a custom_calls post (call has started)
// 2. in a DM or GM channel
// 3. calls ringing is enabled on the server
// 4. calls is enabled and is v0.18.0+ (it is if this is running)
// 5. MM server is >= v8.1.0 (if not, this handler will not be called)
// @ts-ignore our imported webapp types are old
if (post.type === CALL_START_POST_TYPE &&
isDmGmChannel(channel) &&
ringingEnabled(store.getState())) {
// e2eNotificationsRejected is added when running the e2e tests
if (window.e2eDesktopNotificationsRejected) {
window.e2eDesktopNotificationsRejected.push(args);
}
return {args: {...args, notify: false}};
}
// Do not notify for a call's thread if the user is currently in that call...
if (channelIDForCurrentCall(store.getState()) === post.channel_id &&
threadIDForCurrentCall(store.getState()) === post.root_id) {
let mentions = [];
if (msgProps.mentions) {
mentions = JSON.parse(msgProps.mentions);
}
// ...and wasn't directly mentioned.
if (!mentions.includes(getCurrentUserId(store.getState()))) {
return {args: {...args, notify: false}};
}
}
}
return {args};
}