This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
notification_actions.jsx
134 lines (113 loc) · 5.02 KB
/
notification_actions.jsx
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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import ChannelStore from 'stores/channel_store.jsx';
import NotificationStore from 'stores/notification_store.jsx';
import UserStore from 'stores/user_store.jsx';
import Constants, {NotificationLevels, UserStatuses} from 'utils/constants.jsx';
import {isSystemMessage} from 'utils/post_utils.jsx';
import {isMacApp, isMobileApp, isWindowsApp} from 'utils/user_agent.jsx';
import * as Utils from 'utils/utils.jsx';
export function sendDesktopNotification(post, msgProps) {
if ((UserStore.getCurrentId() === post.user_id && post.props.from_webhook !== 'true')) {
return;
}
if (isSystemMessage(post)) {
return;
}
let mentions = [];
if (msgProps.mentions) {
mentions = JSON.parse(msgProps.mentions);
}
const teamId = msgProps.team_id;
let channel = ChannelStore.get(post.channel_id);
const user = UserStore.getCurrentUser();
const userStatus = UserStore.getStatus(user.id);
const member = ChannelStore.getMyMember(post.channel_id);
if (userStatus === UserStatuses.DND) {
return;
}
let notifyLevel = member && member.notify_props ? member.notify_props.desktop : NotificationLevels.DEFAULT;
if (notifyLevel === NotificationLevels.DEFAULT) {
notifyLevel = user && user.notify_props ? user.notify_props.desktop : NotificationLevels.ALL;
}
if (notifyLevel === NotificationLevels.NONE) {
return;
} else if (notifyLevel === NotificationLevels.MENTION && mentions.indexOf(user.id) === -1 && msgProps.channel_type !== Constants.DM_CHANNEL) {
return;
}
let username = Utils.localizeMessage('channel_loader.someone', 'Someone');
if (post.props.override_username && global.window.mm_config.EnablePostUsernameOverride === 'true') {
username = post.props.override_username;
} else if (msgProps.sender_name) {
username = msgProps.sender_name;
} else if (UserStore.hasProfile(post.user_id)) {
username = UserStore.getProfile(post.user_id).username;
}
let title = Utils.localizeMessage('channel_loader.posted', 'Posted');
if (!channel) {
title = msgProps.channel_display_name;
channel = {
name: msgProps.channel_name,
type: msgProps.channel_type
};
} else if (channel.type === Constants.DM_CHANNEL) {
title = Utils.localizeMessage('notification.dm', 'Direct Message');
} else {
title = channel.display_name;
}
if (title === '') {
if (msgProps.channel_type === Constants.DM_CHANNEL) {
title = Utils.localizeMessage('notification.dm', 'Direct Message');
} else {
title = msgProps.channel_display_name;
}
}
let notifyText = post.message;
const msgPropsPost = JSON.parse(msgProps.post);
const attachments = msgPropsPost && msgPropsPost.props && msgPropsPost.props.attachments ? msgPropsPost.props.attachments : [];
let image = false;
attachments.forEach((attachment) => {
if (notifyText.length === 0) {
notifyText = attachment.fallback ||
attachment.pretext ||
attachment.text;
}
image |= attachment.image_url.length > 0;
});
notifyText = notifyText.replace(/\n+/g, ' ');
if (notifyText.length > 50) {
notifyText = notifyText.substring(0, 49) + '...';
}
let body = '';
if (notifyText.length === 0) {
if (msgProps.image) {
body = username + Utils.localizeMessage('channel_loader.uploadedImage', ' uploaded an image');
} else if (msgProps.otherFile) {
body = username + Utils.localizeMessage('channel_loader.uploadedFile', ' uploaded a file');
} else if (image) {
body = username + Utils.localizeMessage('channel_loader.postedImage', ' posted an image');
} else {
body = username + Utils.localizeMessage('channel_loader.something', ' did something new');
}
} else {
body = username + Utils.localizeMessage('channel_loader.wrote', ' wrote: ') + notifyText;
}
let duration = Constants.DEFAULT_NOTIFICATION_DURATION;
if (user.notify_props && user.notify_props.desktop_duration) {
duration = parseInt(user.notify_props.desktop_duration, 10) * 1000;
}
//Play a sound if explicitly set in settings
const sound = !user.notify_props || user.notify_props.desktop_sound === 'true';
// Notify if you're not looking in the right channel or when
// the window itself is not active
const activeChannel = ChannelStore.getCurrent();
const channelId = channel ? channel.id : null;
const notify = (activeChannel && activeChannel.id !== channelId) || !NotificationStore.getFocus();
if (notify) {
Utils.notifyMe(title, body, channel, teamId, duration, !sound);
//Don't add extra sounds on native desktop clients
if (sound && !isWindowsApp() && !isMacApp() && !isMobileApp()) {
Utils.ding();
}
}
}