-
Notifications
You must be signed in to change notification settings - Fork 328
/
notification-controller.ts
226 lines (201 loc) · 7.25 KB
/
notification-controller.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
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
import { app, nativeImage, NativeImage, Notification, shell } from 'electron';
import os from 'os';
import path from 'path';
import { sprintf } from 'sprintf-js';
import config from '../config.json';
import { TunnelState } from '../shared/daemon-rpc-types';
import { messages } from '../shared/gettext';
export default class NotificationController {
private lastTunnelStateAnnouncement?: { body: string; notification: Notification };
private reconnecting = false;
private presentedNotifications: { [key: string]: boolean } = {};
private pendingNotifications: Notification[] = [];
private notificationTitle = process.platform === 'linux' ? app.name : '';
private notificationIcon?: NativeImage;
constructor() {
let usePngIcon;
if (process.platform === 'linux') {
usePngIcon = true;
} else if (process.platform === 'win32') {
usePngIcon = parseInt(os.release().split('.')[0], 10) >= 10;
} else {
usePngIcon = false;
}
if (usePngIcon) {
const basePath = path.resolve(path.join(__dirname, '../../assets/images'));
this.notificationIcon = nativeImage.createFromPath(
path.join(basePath, 'icon-notification.png'),
);
}
}
public notifyTunnelState(tunnelState: TunnelState) {
switch (tunnelState.state) {
case 'connecting':
if (!this.reconnecting) {
const details = tunnelState.details;
if (details && details.location && details.location.hostname) {
const msg = sprintf(
// TRANSLATORS: The message showed when a server is being connected to.
// TRANSLATORS: Available placeholder:
// TRANSLATORS: %(location) - name of the server location we're connecting to (e.g. "se-got-003")
messages.pgettext('notifications', 'Connecting to %(location)s'),
{
location: details.location.hostname,
},
);
this.showTunnelStateNotification(msg);
} else {
this.showTunnelStateNotification(messages.pgettext('notifications', 'Connecting'));
}
}
break;
case 'connected':
{
const details = tunnelState.details;
if (details.location && details.location.hostname) {
const msg = sprintf(
// TRANSLATORS: The message showed when a server has been connected to.
// TRANSLATORS: Available placeholder:
// TRANSLATORS: %(location) - name of the server location we're connected to (e.g. "se-got-003")
messages.pgettext('notifications', 'Connected to %(location)s'),
{
location: details.location.hostname,
},
);
this.showTunnelStateNotification(msg);
} else {
this.showTunnelStateNotification(messages.pgettext('notifications', 'Secured'));
}
}
break;
case 'disconnected':
this.showTunnelStateNotification(messages.pgettext('notifications', 'Unsecured'));
break;
case 'error':
if (tunnelState.details.isBlocking) {
this.showTunnelStateNotification(
messages.pgettext('notifications', 'Blocked all connections'),
);
} else {
this.showTunnelStateNotification(
messages.pgettext('notifications', 'Critical failure - Unsecured'),
);
}
break;
case 'disconnecting':
switch (tunnelState.details) {
case 'nothing':
case 'block':
// no-op
break;
case 'reconnect':
this.showTunnelStateNotification(messages.pgettext('notifications', 'Reconnecting'));
this.reconnecting = true;
return;
}
break;
}
this.reconnecting = false;
}
public notifyInconsistentVersion() {
this.presentNotificationOnce('inconsistent-version', () => {
const notification = new Notification({
title: this.notificationTitle,
body: messages.pgettext(
'notifications',
'Inconsistent internal version information, please restart the app',
),
silent: true,
icon: this.notificationIcon,
});
this.scheduleNotification(notification);
});
}
public notifyUnsupportedVersion(upgradeVersion: string) {
this.presentNotificationOnce('unsupported-version', () => {
const notification = new Notification({
title: this.notificationTitle,
body: sprintf(
// TRANSLATORS: The system notification displayed to the user when the running app becomes unsupported.
// TRANSLATORS: Available placeholder:
// TRANSLATORS: %(version) - the newest available version of the app
messages.pgettext(
'notifications',
'You are running an unsupported app version. Please upgrade to %(version)s now to ensure your security',
),
{
version: upgradeVersion,
},
),
silent: true,
icon: this.notificationIcon,
});
notification.on('click', () => {
shell.openExternal(config.links.download);
});
this.scheduleNotification(notification);
});
}
public notifyKeyGenerationFailed() {
const notification = new Notification({
title: this.notificationTitle,
body: messages.pgettext('notifications', 'Wireguard key generation failed'),
silent: true,
icon: this.notificationIcon,
});
this.scheduleNotification(notification);
}
public cancelPendingNotifications() {
for (const notification of this.pendingNotifications) {
notification.close();
}
}
public resetTunnelStateAnnouncements() {
this.lastTunnelStateAnnouncement = undefined;
}
private showTunnelStateNotification(message: string) {
const lastAnnouncement = this.lastTunnelStateAnnouncement;
const sameAsLastNotification = lastAnnouncement && lastAnnouncement.body === message;
if (sameAsLastNotification) {
return;
}
const newNotification = new Notification({
title: this.notificationTitle,
body: message,
silent: true,
icon: this.notificationIcon,
});
if (lastAnnouncement) {
lastAnnouncement.notification.close();
}
this.lastTunnelStateAnnouncement = {
body: message,
notification: newNotification,
};
this.scheduleNotification(newNotification);
}
private presentNotificationOnce(notificationName: string, presentNotification: () => void) {
const presented = this.presentedNotifications;
if (!presented[notificationName]) {
presented[notificationName] = true;
presentNotification();
}
}
private scheduleNotification(notification: Notification) {
this.addPendingNotification(notification);
notification.show();
setTimeout(() => notification.close(), 4000);
}
private addPendingNotification(notification: Notification) {
notification.on('close', () => {
this.removePendingNotification(notification);
});
this.pendingNotifications.push(notification);
}
private removePendingNotification(notification: Notification) {
const index = this.pendingNotifications.indexOf(notification);
if (index !== -1) {
this.pendingNotifications.splice(index, 1);
}
}
}