nodebb-plugin-web-push 0.5.1 → 0.6.1

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');
@@ -92,11 +93,15 @@ plugin.addRoutes = async ({ router, middleware, helpers }) => {
92
93
  return helpers.notAllowed(req, res);
93
94
  }
94
95
 
96
+ const { userLang } = await user.getSettings(req.uid);
95
97
  const { subscription } = req.body;
96
- await webPush.sendNotification(subscription, JSON.stringify({
97
- title: 'Test notification',
98
- body: 'This is a test message sent from NodeBB',
99
- }));
98
+ const payload = await constructPayload({
99
+ nid: utils.generateUUID(),
100
+ bodyShort: 'Test notification',
101
+ bodyLong: 'This is a test message sent from NodeBB',
102
+ path: `${nconf.get('url')}/me/web-push`,
103
+ }, req.uid, userLang);
104
+ await webPush.sendNotification(subscription, JSON.stringify(payload));
100
105
  });
101
106
  };
102
107
 
@@ -121,7 +126,7 @@ plugin.onNotificationPush = async ({ notification, uidsNotified: uids }) => {
121
126
  db.pexpire(refKey, 1000 * 60 * 60 * 48); // only track last 48 hours
122
127
 
123
128
  let payloads = await Promise.all(uids.map(async (uid, idx) => {
124
- const payload = await constructPayload(notification, userSettings[idx].userLang);
129
+ const payload = await constructPayload(notification, uid, userSettings[idx].userLang);
125
130
  return [uid, payload];
126
131
  }));
127
132
  payloads = new Map(payloads);
@@ -180,15 +185,28 @@ plugin.addProfileItem = async (data) => {
180
185
  return data;
181
186
  };
182
187
 
183
- async function constructPayload({ nid, mergeId, bodyShort, bodyLong, path }, language) {
184
- let { maxLength } = await meta.settings.get('web-push');
188
+ async function constructPayload(notification, uid, lang) {
189
+ let { maxLength, icon, badge } = await meta.settings.get('web-push');
185
190
  maxLength = parseInt(maxLength, 10) || 256;
186
191
 
187
- if (!language) {
188
- language = meta.config.defaultLang || 'en-GB';
192
+ // i18n/rtl
193
+ if (!lang) {
194
+ lang = meta.config.defaultLang || 'en-GB';
195
+ }
196
+ const dir = await translator.translate('[[language:dir]]', lang);
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
+ }
189
205
  }
190
206
 
191
- let [title, body] = await translator.translateKeys([bodyShort, bodyLong], language);
207
+ const { nid, mergeId, bodyShort, bodyLong, path } = notification;
208
+
209
+ let [title, body] = await translator.translateKeys([bodyShort, bodyLong], lang);
192
210
  ([title, body] = [title, body].map(str => validator.unescape(utils.stripHTMLTags(str))));
193
211
  const tag = mergeId || nid;
194
212
  const url = `${nconf.get('url')}${path}`;
@@ -204,11 +222,16 @@ 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
+ lang,
233
+ dir,
234
+ data: { url, icon, badge },
212
235
  };
213
236
  }
214
237
 
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.1",
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