nodebb-plugin-web-push 0.6.2 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/library.js CHANGED
@@ -45,6 +45,12 @@ plugin.appendConfig = async (config) => {
45
45
  return config;
46
46
  };
47
47
 
48
+ plugin.registerServiceWorker = async (data) => {
49
+ const { scripts } = data;
50
+ scripts.add(`nodebb-plugin-web-push/static/web-push.js`);
51
+ return data;
52
+ };
53
+
48
54
  async function assertVapidConfiguration() {
49
55
  let { publicKey, privateKey } = await meta.settings.get('web-push');
50
56
  if (!publicKey || !privateKey) {
@@ -159,26 +165,30 @@ plugin.onNotificationRescind = async ({ nids }) => {
159
165
  subs = new Set(...Object.values(Object.fromEntries(subs))); // wtf
160
166
 
161
167
  if (subs.size) {
162
- subs.forEach(async (subscription) => {
163
- await webPush.sendNotification(subscription, JSON.stringify({ tag }));
164
- });
168
+ await Promise.all(Array.from(subs).map(async (subscription) => {
169
+ try {
170
+ await webPush.sendNotification(subscription, JSON.stringify({ tag }));
171
+ } catch (e) {
172
+ winston.info(`[plugins/web-push] Push failed: ${e.code}; ${e.message}; statusCode: ${e.statusCode}`);
173
+ }
174
+ }));
165
175
  }
166
- }));
176
+ })).catch(err => winston.error(err.stack));
167
177
  };
168
178
 
169
- plugin.addProfileItem = async (data) => {
170
- const title = await translator.translate('[[web-push:profile.label]]');
179
+ plugin.addProfileItem = (data) => {
171
180
  data.links.push({
172
181
  id: 'web-push',
173
182
  route: 'web-push',
174
183
  icon: 'fa-bell-o',
175
- name: title,
184
+ name: '[[web-push:profile.label]]',
176
185
  visibility: {
177
186
  self: true,
178
187
  other: false,
179
188
  moderator: false,
180
189
  globalMod: false,
181
190
  admin: false,
191
+ canViewInfo: false,
182
192
  },
183
193
  });
184
194
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-web-push",
3
- "version": "0.6.2",
3
+ "version": "0.7.2",
4
4
  "description": "A starter kit for quickly creating NodeBB plugins",
5
5
  "main": "library.js",
6
6
  "repository": {
@@ -31,16 +31,16 @@
31
31
  },
32
32
  "readmeFilename": "README.md",
33
33
  "nbbpm": {
34
- "compatibility": "^4.0.0"
34
+ "compatibility": "^3.10.x"
35
35
  },
36
36
  "devDependencies": {
37
- "@commitlint/cli": "19.5.0",
38
- "@commitlint/config-angular": "19.5.0",
37
+ "@commitlint/cli": "19.6.1",
38
+ "@commitlint/config-angular": "19.6.0",
39
39
  "eslint": "8.x",
40
40
  "eslint-config-nodebb": "0.2.1",
41
41
  "eslint-plugin-import": "2.x",
42
- "husky": "9.1.6",
43
- "lint-staged": "15.2.10"
42
+ "husky": "9.1.7",
43
+ "lint-staged": "15.3.0"
44
44
  },
45
45
  "dependencies": {
46
46
  "validator": "^13.12.0",
package/plugin.json CHANGED
@@ -9,12 +9,16 @@
9
9
  { "hook": "filter:config.get", "method": "appendConfig" },
10
10
  { "hook": "action:notification.pushed", "method": "onNotificationPush" },
11
11
  { "hook": "static:notifications.rescind", "method": "onNotificationRescind" },
12
- { "hook": "filter:user.profileMenu", "method": "addProfileItem" }
12
+ { "hook": "filter:user.profileMenu", "method": "addProfileItem" },
13
+ { "hook": "filter:service-worker.scripts", "method": "registerServiceWorker" }
13
14
  ],
14
15
  "languages": "public/languages",
15
16
  "modules": {
16
17
  "../client/account/web-push.js": "./public/lib/settings.js",
17
18
  "../admin/plugins/web-push.js": "./public/lib/admin.js"
18
19
  },
20
+ "staticDirs": {
21
+ "static": "./static"
22
+ },
19
23
  "templates": "templates"
20
24
  }
@@ -0,0 +1,56 @@
1
+ /* eslint-disable no-undef */
2
+
3
+ 'use strict';
4
+
5
+ // Register event listener for the 'push' event.
6
+ self.addEventListener('push', (event) => {
7
+ // Keep the service worker alive until the notification is created.
8
+ const { title, body, tag, data } = event.data.json();
9
+
10
+ if (title && body) {
11
+ const { icon } = data;
12
+ delete data.icon;
13
+ const { badge } = data;
14
+ delete data.badge;
15
+
16
+ event.waitUntil(
17
+ self.registration.showNotification(title, { body, tag, data, icon, badge })
18
+ );
19
+ } else if (tag) {
20
+ event.waitUntil(
21
+ self.registration.getNotifications({ tag }).then((notifications) => {
22
+ notifications.forEach((notification) => {
23
+ notification.close();
24
+ });
25
+ })
26
+ );
27
+ }
28
+ });
29
+
30
+ self.addEventListener('notificationclick', (event) => {
31
+ event.notification.close();
32
+ let target;
33
+ if (event.notification.data && event.notification.data.url) {
34
+ target = new URL(event.notification.data.url);
35
+ }
36
+
37
+ // This looks to see if the current is already open and focuses if it is
38
+ event.waitUntil(
39
+ self.clients
40
+ .matchAll({ type: 'window' })
41
+ .then((clientList) => {
42
+ // eslint-disable-next-line no-restricted-syntax
43
+ for (const client of clientList) {
44
+ const { hostname } = new URL(client.url);
45
+ if (target && hostname === target.hostname && 'focus' in client) {
46
+ client.postMessage({
47
+ action: 'ajaxify',
48
+ url: target.pathname,
49
+ });
50
+ return client.focus();
51
+ }
52
+ }
53
+ if (self.clients.openWindow) return self.clients.openWindow(target.pathname);
54
+ })
55
+ );
56
+ });
@@ -1,5 +0,0 @@
1
- <h1>Hello!</h1>
2
-
3
- <p>This file is served by nodebb at domain.com/assets/plugins/nodebb-plugin-web-push/static/samplefile.html</p>
4
-
5
- Check plugin.json for the "staticDirs" property if you want to change the path.