forked from prsousa/UnreadTopics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications.js
More file actions
113 lines (93 loc) · 3.09 KB
/
Copy pathnotifications.js
File metadata and controls
113 lines (93 loc) · 3.09 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
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
"use strict"
var NotificationsManager = function () {
this.prefs = {
enabled: true,
audioVolume: 0.5,
muteHourPeriod: 2
};
this.db = {
currentIP: "",
muteWhileHere: false,
muteUntil: 0
}
}
NotificationsManager.prototype.save = function () {
return Promise.all([
Utils.saveLocally(this.db),
Utils.saveRemotely(this.prefs)
]);
}
NotificationsManager.prototype.load = function () {
return Promise.all([
Utils.loadLocally(this.db),
Utils.loadRemotely(this.prefs)
]);
}
NotificationsManager.prototype.isCurrentlyBlocked = function () {
return !this.prefs.enabled || this.db.muteWhileHere || this.db.muteUntil - new Date().getTime() > 0;
}
NotificationsManager.prototype.getCurrentState = function () {
return {
currentlyBlocked: this.isCurrentlyBlocked(),
enabled: this.prefs.enabled,
audioVolume: this.prefs.audioVolume,
muttedUntil: this.db.muteUntil
}
}
NotificationsManager.prototype.postponeHourPeriod = function () {
this.db.muteUntil = new Date().getTime() + this.prefs.muteHourPeriod * 60 * 60 * 1000;
}
NotificationsManager.prototype.setEnabled = function (enabled) {
this.prefs.enabled = enabled;
this.db.muteUntil = 0;
}
NotificationsManager.prototype.playSound = function () {
var audio = new Audio('/audio/all-eyes-on-me.ogg');
audio.volume = this.prefs.audioVolume;
audio.play();
}
NotificationsManager.prototype.clearTopicsNotifications = function () {
return chromep.notifications.clear('unread');
}
NotificationsManager.prototype.notifyTopics = function (items) {
if (this.isCurrentlyBlocked()) return false;
if (!items || items.length === 0) return false;
this.playSound();
var postponePeriod = { title: "Não incomodar (" + this.prefs.muteHourPeriod + "h)", iconUrl: "/img/postpone.svg" };
// var naoIncomodarUMBtn = { title: "Não incomodar aqui", iconUrl: "img/UM.png" };
var buttonsToDisplay = [postponePeriod];
var opt = {
type: 'list',
title: 'Fórum de LEI',
message: 'Topicos por Ler',
iconUrl: '/img/unreadNotification.svg',
priority: 1,
items: items,
buttons: buttonsToDisplay,
isClickable: true,
contextMessage: items.length + " tópico" + (items.length > 1 ? 's' : '') + " por ler"
};
chromep.notifications.update('unread', opt).then(wasUpdated => {
if (wasUpdated) return;
chromep.notifications.create('unread', opt).then(id => {
// clear on timeout?
});
});
return true;
}
NotificationsManager.prototype.notifyUpdate = function (msg, newVersion, requireInt) {
if (this.isCurrentlyBlocked()) return false;
this.playSound();
var opt = {
type: 'basic',
title: 'Extensão Atualizada',
message: msg,
iconUrl: '/img/updateNotification.svg',
priority: 1,
isClickable: true,
contextMessage: newVersion,
requireInteraction: requireInt
};
chromep.notifications.create('update', opt);
return true;
}