/
desktop-notifications.js.es6
170 lines (143 loc) · 4.56 KB
/
desktop-notifications.js.es6
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
import DiscourseURL from 'discourse/lib/url';
import KeyValueStore from 'discourse/lib/key-value-store';
import { formatUsername } from 'discourse/lib/utilities';
let primaryTab = false;
let liveEnabled = false;
let havePermission = null;
let mbClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
let lastAction = -1;
const focusTrackerKey = "focus-tracker";
const idleThresholdTime = 1000 * 10; // 10 seconds
const context = "discourse_desktop_notifications_";
const keyValueStore = new KeyValueStore(context);
// Called from an initializer
function init(messageBus, appEvents) {
liveEnabled = false;
mbClientId = messageBus.clientId;
if (!Discourse.User.current()) {
return;
}
try {
keyValueStore.getItem(focusTrackerKey);
} catch (e) {
Em.Logger.info('Discourse desktop notifications are disabled - localStorage denied.');
return;
}
if (!("Notification" in window)) {
Em.Logger.info('Discourse desktop notifications are disabled - not supported by browser');
return;
}
try {
if (Notification.permission === "granted") {
havePermission = true;
} else if (Notification.permission === "denied") {
havePermission = false;
return;
}
} catch (e) {
Em.Logger.warn('Unexpected error, Notification is defined on window but not a responding correctly ' + e);
}
liveEnabled = true;
try {
// Preliminary checks passed, continue with setup
setupNotifications(appEvents);
} catch (e) {
Em.Logger.error(e);
}
}
// This function is only called if permission was granted
function setupNotifications(appEvents) {
window.addEventListener("storage", function(e) {
// note: This event only fires when other tabs setItem()
const key = e.key;
if (key !== `${context}${focusTrackerKey}`) {
return true;
}
primaryTab = false;
});
window.addEventListener("focus", function() {
if (!primaryTab) {
primaryTab = true;
keyValueStore.setItem(focusTrackerKey, mbClientId);
}
});
if (document && (typeof document.hidden !== "undefined") && document["hidden"]) {
primaryTab = false;
} else {
primaryTab = true;
keyValueStore.setItem(focusTrackerKey, mbClientId);
}
if (document) {
document.addEventListener("scroll", resetIdle);
}
appEvents.on('page:changed', resetIdle);
}
function resetIdle() {
lastAction = Date.now();
}
function isIdle() {
return lastAction + idleThresholdTime < Date.now();
}
// Call-in point from message bus
function onNotification(data) {
if (!liveEnabled) { return; }
if (!primaryTab) { return; }
if (!isIdle()) { return; }
if (keyValueStore.getItem('notifications-disabled')) { return; }
const notificationTitle = I18n.t(i18nKey(data.notification_type), {
site_title: Discourse.SiteSettings.title,
topic: data.topic_title,
username: formatUsername(data.username)
});
const notificationBody = data.excerpt;
const notificationIcon = Discourse.SiteSettings.logo_small_url || Discourse.SiteSettings.logo_url;
const notificationTag = "discourse-notification-" + Discourse.SiteSettings.title + "-" + data.topic_id;
requestPermission().then(function() {
// This shows the notification!
const notification = new Notification(notificationTitle, {
body: notificationBody,
icon: notificationIcon,
tag: notificationTag
});
function clickEventHandler() {
DiscourseURL.routeTo(data.post_url);
// Cannot delay this until the page renders
// due to trigger-based permissions
window.focus();
}
notification.addEventListener('click', clickEventHandler);
setTimeout(function() {
notification.close();
notification.removeEventListener('click', clickEventHandler);
}, 10 * 1000);
});
}
// Utility function
// Wraps Notification.requestPermission in a Promise
function requestPermission() {
if (havePermission === true) {
return Ember.RSVP.resolve();
} else if (havePermission === false) {
return Ember.RSVP.reject();
} else {
return new Ember.RSVP.Promise(function(resolve, reject) {
Notification.requestPermission(function(status) {
if (status === "granted") {
resolve();
} else {
reject();
}
});
});
}
}
function i18nKey(notification_type) {
return "notifications.popup." + Discourse.Site.current().get("notificationLookup")[notification_type];
}
function alertChannel(user) {
return `/notification-alert/${user.get('id')}`;
}
function unsubscribe(bus, user) {
bus.unsubscribe(alertChannel(user));
}
export { context, init, onNotification, unsubscribe, alertChannel };