nodebb-plugin-web-push 0.5.1 → 0.6.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
@@ -10,6 +10,7 @@ const user = require.main.require('./src/user');
10
10
  const meta = require.main.require('./src/meta');
11
11
  const utils = require.main.require('./src/utils');
12
12
  const translator = require.main.require('./src/translator');
13
+ const notifications = require.main.require('./src/notifications');
13
14
 
14
15
  const controllers = require('./lib/controllers');
15
16
  const subscriptions = require('./lib/subscriptions');
@@ -96,6 +97,12 @@ plugin.addRoutes = async ({ router, middleware, helpers }) => {
96
97
  await webPush.sendNotification(subscription, JSON.stringify({
97
98
  title: 'Test notification',
98
99
  body: 'This is a test message sent from NodeBB',
100
+ tag: 'web-push-test',
101
+ data: {
102
+ url: `${nconf.get('url')}/me/web-push`,
103
+ icon: `${nconf.get('url')}/apple-touch-icon`,
104
+ badge: `${nconf.get('url')}/apple-touch-icon`,
105
+ },
99
106
  }));
100
107
  });
101
108
  };
@@ -121,7 +128,7 @@ plugin.onNotificationPush = async ({ notification, uidsNotified: uids }) => {
121
128
  db.pexpire(refKey, 1000 * 60 * 60 * 48); // only track last 48 hours
122
129
 
123
130
  let payloads = await Promise.all(uids.map(async (uid, idx) => {
124
- const payload = await constructPayload(notification, userSettings[idx].userLang);
131
+ const payload = await constructPayload(notification, uid, userSettings[idx].userLang);
125
132
  return [uid, payload];
126
133
  }));
127
134
  payloads = new Map(payloads);
@@ -180,14 +187,25 @@ plugin.addProfileItem = async (data) => {
180
187
  return data;
181
188
  };
182
189
 
183
- async function constructPayload({ nid, mergeId, bodyShort, bodyLong, path }, language) {
184
- let { maxLength } = await meta.settings.get('web-push');
190
+ async function constructPayload(notification, uid, language) {
191
+ let { maxLength, icon, badge } = await meta.settings.get('web-push');
185
192
  maxLength = parseInt(maxLength, 10) || 256;
186
193
 
187
194
  if (!language) {
188
195
  language = meta.config.defaultLang || 'en-GB';
189
196
  }
190
197
 
198
+ // Merge with related unread notifications
199
+ if (notification.mergeId) {
200
+ const related = await notifications.findRelated([notification.mergeId], `uid:${uid}:notifications:unread`);
201
+ const merged = await notifications.getMultiple(related).then(notifications.merge);
202
+ if (merged.length) {
203
+ notification = merged.pop();
204
+ }
205
+ }
206
+
207
+ const { nid, mergeId, bodyShort, bodyLong, path } = notification;
208
+
191
209
  let [title, body] = await translator.translateKeys([bodyShort, bodyLong], language);
192
210
  ([title, body] = [title, body].map(str => validator.unescape(utils.stripHTMLTags(str))));
193
211
  const tag = mergeId || nid;
@@ -204,11 +222,14 @@ async function constructPayload({ nid, mergeId, bodyShort, bodyLong, path }, lan
204
222
  body = `${body.slice(0, maxLength)}…`;
205
223
  }
206
224
 
225
+ icon = icon || `${nconf.get('url')}/apple-touch-icon`;
226
+ badge = badge || `${nconf.get('url')}/apple-touch-icon`;
227
+
207
228
  return {
208
229
  title,
209
230
  body,
210
231
  tag,
211
- data: { url },
232
+ data: { url, icon, badge },
212
233
  };
213
234
  }
214
235
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-web-push",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "A starter kit for quickly creating NodeBB plugins",
5
5
  "main": "library.js",
6
6
  "repository": {
@@ -15,6 +15,24 @@
15
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
16
  </p>
17
17
  </div>
18
+
19
+ <div class="mb-3">
20
+ <label class="form-label" for="badge">Badge URL</label>
21
+ <input type="text" id="badge" name="badge" title="Badge" class="form-control" placeholder="https://...">
22
+ <p class="form-text">
23
+ Optional — overrides the badge for messages sent (usually seen in the notification bar on mobile devices)
24
+ By default, the site's configured "touch icon" is sent.
25
+ </p>
26
+ </div>
27
+
28
+ <div class="mb-3">
29
+ <label class="form-label" for="icon">Icon URL</label>
30
+ <input type="text" id="icon" name="icon" title="Icon" class="form-control" placeholder="https://...">
31
+ <p class="form-text">
32
+ Optional — overrides the icon for messages sent (can be used for branding, etc.)
33
+ By default, the site's configured "touch icon" is sent.
34
+ </p>
35
+ </div>
18
36
  </div>
19
37
  </form>
20
38