nodebb-plugin-web-push 0.2.1 → 0.4.0

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
@@ -85,6 +85,18 @@ plugin.addRoutes = async ({ router, middleware, helpers }) => {
85
85
  await subscriptions.remove(req.uid, subscription);
86
86
  helpers.formatApiResponse(200, res);
87
87
  });
88
+
89
+ routeHelpers.setupApiRoute(router, 'post', '/web-push/test', middlewares, async (req, res) => {
90
+ if (!req.uid) {
91
+ return helpers.notAllowed(req, res);
92
+ }
93
+
94
+ const { subscription } = req.body;
95
+ await webPush.sendNotification(subscription, JSON.stringify({
96
+ title: 'Test notification',
97
+ body: 'This is a test message sent from NodeBB',
98
+ }));
99
+ });
88
100
  };
89
101
 
90
102
  plugin.addAdminNavigation = (header) => {
@@ -115,7 +127,8 @@ plugin.onNotificationPush = async ({ notification, uidsNotified: uids }) => {
115
127
  await webPush.sendNotification(subscription, JSON.stringify(payload));
116
128
  } catch (e) {
117
129
  // Errored — remove subscription from user
118
- subscriptions.remove(uid, subscription);
130
+ winston.info(`[plugins/web-push] Push failed: ${e.code}; ${e.message}; statusCode: ${e.statusCode}`);
131
+ // subscriptions.remove(uid, subscription);
119
132
  }
120
133
  });
121
134
  });
@@ -141,6 +154,9 @@ plugin.addProfileItem = async (data) => {
141
154
  };
142
155
 
143
156
  async function constructPayload({ bodyShort, bodyLong, path }, language) {
157
+ let { maxLength } = await meta.settings.get('web-push');
158
+ maxLength = parseInt(maxLength, 10) || 256;
159
+
144
160
  if (!language) {
145
161
  language = meta.config.defaultLang || 'en-GB';
146
162
  }
@@ -155,6 +171,11 @@ async function constructPayload({ bodyShort, bodyLong, path }, language) {
155
171
  title = meta.config.title || 'NodeBB';
156
172
  }
157
173
 
174
+ // Truncate body if needed
175
+ if (body.length > maxLength) {
176
+ body = `${body.slice(0, maxLength)}…`;
177
+ }
178
+
158
179
  return {
159
180
  title,
160
181
  body,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-web-push",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "description": "A starter kit for quickly creating NodeBB plugins",
5
5
  "main": "library.js",
6
6
  "repository": {
@@ -3,5 +3,9 @@
3
3
  "profile.introduction": "In addition to in-application and email notifications, you may opt-in to receive push notifications as well. This will allow you to be notified even if the app is not open on your device.",
4
4
  "profile.option": "Enable push notifications on this device",
5
5
  "profile.devices": "Currently notifying <strong>%1</strong> device(s).",
6
- "profile.permissionBlocked": "Your device is currently disallowing notifications from this site. Please grant the notification permission to continue."
6
+ "profile.permissionBlocked": "Your device is currently disallowing notifications from this site. Please grant the notification permission to continue.",
7
+ "profile.send-test": "Send Test Notification",
8
+
9
+ "toast.test_success": "Test notification sent.",
10
+ "toast.test_unavailable": "Cannot send test notification as push notifications are not enabled on this device."
7
11
  }
@@ -1,7 +1,9 @@
1
+ /* eslint-disable import/no-unresolved */
2
+
1
3
  'use strict';
2
4
 
3
5
  import { post, del } from 'api';
4
- import { success } from 'alerts';
6
+ import { success, warning } from 'alerts';
5
7
 
6
8
  // eslint-disable-next-line import/prefer-default-export
7
9
  export async function init() {
@@ -16,11 +18,15 @@ export async function init() {
16
18
  const action = e.target.getAttribute('data-action');
17
19
 
18
20
  switch (action) {
19
- // case 'test': {
20
- // await post('/plugins/ntfy/test');
21
- // success('[[ntfy:toast.test_success]]');
22
- // break;
23
- // }
21
+ case 'test': {
22
+ if (subscription) {
23
+ await post('/plugins/web-push/test', { subscription });
24
+ success('[[web-push:toast.test_success]]');
25
+ } else {
26
+ warning('[[web-push:toast.test_unavailable]]');
27
+ }
28
+ break;
29
+ }
24
30
 
25
31
  case 'toggle': {
26
32
  const countEl = document.querySelector('#deviceCount strong');
@@ -77,7 +83,7 @@ export async function init() {
77
83
  function urlBase64ToUint8Array(base64String) {
78
84
  var padding = '='.repeat((4 - (base64String.length % 4)) % 4);
79
85
  var base64 = (base64String + padding)
80
- .replace(/\-/g, '+')
86
+ .replace(/-/g, '+')
81
87
  .replace(/_/g, '/');
82
88
 
83
89
  var rawData = window.atob(base64);
@@ -7,11 +7,14 @@
7
7
  <div class="alert alert-warning d-none" id="permission-warning">[[web-push:profile.permissionBlocked]]</div>
8
8
 
9
9
  <form role="form">
10
- <div class="form-check form-switch">
10
+ <div class="form-check form-switch mb-3">
11
11
  <input type="checkbox" class="form-check-input" id="enabled" name="enabled" autocomplete="off" data-action="toggle">
12
12
  <label for="enabled" class="form-check-label">[[web-push:profile.option]]</label>
13
13
  <p class="form-text" id="deviceCount">[[web-push:profile.devices, {count}]]</p>
14
14
  </div>
15
+ <div class="mb-3">
16
+ <button type="button" class="btn btn-primary" data-action="test">[[web-push:profile.send-test]]</button>
17
+ </div>
15
18
  </form>
16
19
 
17
20
  <!-- IMPORT partials/account/footer.tpl -->
@@ -7,9 +7,14 @@
7
7
  <div class="mb-4">
8
8
  <h5 class="fw-bold tracking-tight settings-header">Settings</h5>
9
9
 
10
- <p class="fst-italic">
11
- There are no settings to adjust for this plugin — at this time.
12
- </p>
10
+ <div class="mb-3">
11
+ <label class="form-label" for="maxLength">Maximum length</label>
12
+ <input type="number" min="0" max="4096" id="maxLength" name="maxLength" title="Maximum message length" class="form-control" placeholder="256">
13
+ <p class="form-text">
14
+ Additional characters beyond this specified length will be truncated.
15
+ Due to a software limitation, if the message body is greater than 4096 bytes, the message itself will be an attachment in the push notification.
16
+ </p>
17
+ </div>
13
18
  </div>
14
19
  </form>
15
20