-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMMM-PushBulletNotifications.js
372 lines (327 loc) · 14.3 KB
/
MMM-PushBulletNotifications.js
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
370
371
372
/* Magic Mirror
* Module: MMM-PushBulletNotifications
*
* By Bas Knol
* MIT Licensed.
*/
Module.register("MMM-PushBulletNotifications", {
defaults: {
accessToken: "", //PushBullet API Access Token
endToEndPassword: null,
numberOfNotifications: 3,
filterTargetDeviceName: "", //Only show pushes send to all devices or the filterd target device
showPushesSentToAllDevices: true, //Show pushes to all devices
onlyAllowCommandsFromSourceDevices: [],
fetchLimitPushBullet: 50,
showPushes: true,
showPushesOnLoad: true,
showDismissedPushes: true,
showMirroredNotifications: true,
onlyShowLastNotificationFromApplication: false,
showIndividualNotifications: false,
showSMS: true,
showMessage: true,
showIcons: true,
showDateTime: true,
localesDateTime: 'nl-NL',
playSoundOnNotificationReceived: true,
soundFile: 'modules/MMM-PushBulletNotifications/sounds/new-message.mp3', //Relative path to MagicMirror root
maxMsgCharacters: 50,
maxHeaderCharacters: 32,
showModuleIfNoNotifications: true,
noNotificationsMessage: "No new notifications",
debugMode: false,
},
requiresVersion: "2.3.1", // Minimum required version of MagicMirror
//Keep track of devices, pushes, ephemerals and notifications (=mix of pushes and ephemerals)
devices: [],
pushes: [],
ephemerals: [],
notifications: [],
start: function () {
console.log("PushBulletNotifications module started!");
this.loaded = false;
this.debugMode = this.config.debugMode;
this.sendSocketNotification("START", this.config);
},
getDom: function () {
var wrapper = document.createElement("table");
wrapper.className = "small";
var self = this;
if (this.notifications.length > 0) {
// Only display how many notifications are specified by the config
self.notifications.slice(0, this.config.numberOfNotifications).forEach(function (o) {
var header;
switch (o.type.toLowerCase()) {
//Normal push
case "note":
header = o.sender_name;
break;
//Mirrored notification
case "mirror":
header = o.application_name + " - " + o.title;
break;
//SMS
case "sms_changed":
header = "SMS: " + o.notifications[0].title;
//Add body to object
o.body = o.notifications[0].body;
//Time received SMS
o.created = o.notifications[0].timestamp;
break;
}
// Determine if the header texts need truncating
if (header.length > self.config.maxHeaderCharacters) {
header = header.substring(0, self.config.maxHeaderCharacters) + "...";
}
var notificationWrapper = document.createElement("tr");
notificationWrapper.className = "normal";
//Set icon
var icon = null;
if (self.config.showIcons) {
icon = document.createElement("span");
icon.className = "icon";
//Normal push (decide what icon to use based on device) or SMS
if (o.type === "note" || o.type === "sms_changed") {
//Get device that has sent notification
var device = self.getDevice(o.source_device_iden);
var iconPath = "/modules/MMM-PushBulletNotifications/icons/";
//Sometimes device is null because push does not contain a 'source_device_iden'
if (device == null) {
iconPath += 'message.png';
}
else {
//Set icon based on device
switch (device.icon) {
case "phone":
iconPath += 'phone.png';
break;
case "desktop":
if (device.type == "windows") {
iconPath += 'windows.png';
}
else {
iconPath += 'desktop.png';
}
break;
case "system":
iconPath += 'system.png';
break;
default:
iconPath += 'message.png';
break;
}
}
icon.innerHTML = "<img src=\"" + iconPath + "\" width=\"24\" />";
}
else {
//Show icon that was passed in notification (ephemeral) as base64
icon.innerHTML = "<img src=\"data:image/png;base64, " + o.icon + "\" width=\"24\" />";
}
}
//Name of sender
var nameWrapper = document.createElement("td");
nameWrapper.className = "bright";
nameWrapper.innerHTML = (icon != null) ? icon.outerHTML + header : header;
notificationWrapper.appendChild(nameWrapper);
wrapper.appendChild(notificationWrapper);
//Show date time with message
if (self.config.showDateTime) {
var dateTimeWrapper = document.createElement("tr");
var dateTimeContentWrapper = document.createElement("td");
dateTimeContentWrapper.className = "normal xsmall";
var date = new Date(o.created * 1000);
var dateTimeOptions = { hour12: false };
//Only show time for pushes sent 'today'
if (self.isSameDay(date, new Date())) {
dateTimeContentWrapper.innerHTML = date.toLocaleTimeString(self.config.localesDateTime, dateTimeOptions);
}
else {
dateTimeContentWrapper.innerHTML = date.toLocaleString(self.config.localesDateTime, dateTimeOptions);
}
dateTimeWrapper.appendChild(dateTimeContentWrapper);
wrapper.appendChild(dateTimeWrapper);
}
//Shoge message
if (self.config.showMessage) {
var bodyWrapper = document.createElement("tr");
var bodyContentWrapper = document.createElement("td");
// Determine if the message texts need truncating
var message = o.body;
if (o.body.length > self.config.maxMsgCharacters) {
message = o.body.substring(0, self.config.maxMsgCharacters) + "...";
}
bodyContentWrapper.className = "normal xsmall message";
bodyContentWrapper.innerHTML = message;
bodyWrapper.appendChild(bodyContentWrapper);
wrapper.appendChild(bodyWrapper);
}
});
//Show module
self.show();
}
else if (!this.config.showModuleIfNoNotifications) {
//Hide module if we have no notifications to show
self.hide();
}
else {
wrapper.innerHTML = this.translate(this.config.noNotificationsMessage);
wrapper.className = "normal xsmall dimmed";
}
return wrapper;
},
getScripts: function () {
return [];
},
getStyles: function () {
return [
"MMM-PushBulletNotifications.css",
];
},
setNotifications: function () {
//Destructuring assignment - ES6
this.notifications = [...new Set([...this.pushes, ...this.ephemerals])]; //Merge two array's and remove duplicates
this.notifications.sort(function (a, b) { return b.created - a.created }); //Sort date desc
},
addNotification: function (notification) {
var self = this;
for (var i = 0; i < self.ephemerals.length; i++) {
var ephemeral = self.ephemerals[i];
//Clean up ephemarals if we have a duplicate
if ((ephemeral.package_name === notification.package_name && ephemeral.title === notification.title && ephemeral.body === notification.body) //Clean-up duplicates
|| (ephemeral.package_name === notification.package_name && ephemeral.title === notification.title && !self.config.showIndividualNotifications) //Individual notifications
|| (ephemeral.package_name === notification.package_name && self.config.onlyShowLastNotificationFromApplication)) { //Last notification from application
self.ephemerals.splice(i, 1);
i--;
}
}
this.ephemerals.push(notification);
},
removeNotification: function (dismissal) {
var self = this;
for (var i = 0; i < self.ephemerals.length; i++) {
var ephemeral = self.ephemerals[i];
if ((ephemeral.package_name === dismissal.package_name && ephemeral.notification_id === dismissal.notification_id && ephemeral.notification_tag === dismissal.notification_tag)
|| (dismissal.package_name === "sms" && ephemeral.type === "sms_changed")) {
self.ephemerals.splice(i, 1);
i--;
}
}
},
socketNotificationReceived: function (notification, payload) {
this.debug(notification);
//Received pushes
if (notification === "PUSHES") {
if (payload) {
this.pushes = payload;
this.setNotifications();
this.updateDom();
}
}
else if (notification === "FILE") {
//Notifiy other modules there is a PushBullet file upload
this.sendNotification("PUSHBULLET_FILE_UPLOAD", payload);
}
//Received Ephemeral (SMS or Mirrored Notifications)
else if (notification === "SMS" || notification === "MIRROR") {
if (payload) {
//Add created date, not available in ephemeral
var now = new Date();
payload.created = now.getTime() / 1000; //seconds
this.addNotification(payload);
this.setNotifications();
this.updateDom();
}
}
else if (notification === "DISMISSAL") {
if (payload) {
this.removeNotification(payload);
this.setNotifications();
this.updateDom();
}
}
//Received devices
else if(notification === "DEVICES") {
if(payload) {
this.loaded = true;
this.devices = payload;
}
}
//Commands
else if (notification === "COMMAND") {
var push = payload;
if (push.body.startsWith("mm:")) {
var command = push.body.substring(3);
if (command.startsWith("say:")) {
var message = command.substring(4);
this.sendNotification('MMM-TTS', message);
}
else if (command.startsWith("hide module:")) {
var module = command.substring(command.indexOf(":") + 1);
this.setModuleVisibility(module, false);
}
else if (command.startsWith("show module:")) {
var module = command.substring(command.indexOf(":") + 1);
this.setModuleVisibility(module, true);
}
else {
switch (command.toLowerCase().trim()) {
case "hide all modules":
var options = { lockString: this.identifier };
var modules = MM.getModules();
modules.enumerate(function (module) {
module.hide(1000, null, options);
});
break;
case "show all modules":
var options = { lockString: this.identifier };
var modules = MM.getModules();
modules.enumerate(function (module) {
module.show(1000, null, options);
});
break;
}
}
}
}
},
setModuleVisibility: function (moduleName, visible) {
var options = { lockString: this.identifier };
var modules = MM.getModules();
modules.enumerate(function (module) {
if (module.name === moduleName) {
if (visible) {
module.show(1000, null, options);
}
else {
module.hide(1000, null, options);
}
}
});
},
//Compare two date on same day
isSameDay: function (d1, d2) {
return d1.getFullYear() === d2.getFullYear()
&& d1.getDate() === d2.getDate()
&& d1.getMonth() === d2.getMonth();
},
//Get pushbullet device base on device iden
getDevice: function (deviceIden) {
var device = null;
if (this.devices != null && this.devices.length > 0 && deviceIden !== "") {
for (var i = 0; i < this.devices.length; i++) {
var d = this.devices[i];
if (d != null && d.iden === deviceIden) {
device = d;
break;
}
}
}
return device;
},
debug: function (message) {
if (this.debugMode) {
console.log(message);
}
}
});