nodebb-plugin-web-push 0.4.0 → 0.5.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/lib/controllers.js +1 -1
- package/library.js +30 -1
- package/package.json +1 -1
- package/plugin.json +1 -0
package/lib/controllers.js
CHANGED
|
@@ -9,7 +9,7 @@ const helpers = require.main.require('./src/controllers/helpers');
|
|
|
9
9
|
const Controllers = module.exports;
|
|
10
10
|
|
|
11
11
|
Controllers.renderSettings = async function (req, res) {
|
|
12
|
-
if (res.locals.uid !== req.
|
|
12
|
+
if (res.locals.uid !== req.uid) {
|
|
13
13
|
return helpers.notAllowed(req, res);
|
|
14
14
|
}
|
|
15
15
|
|
package/library.js
CHANGED
|
@@ -5,6 +5,7 @@ const winston = require.main.require('winston');
|
|
|
5
5
|
const webPush = require('web-push');
|
|
6
6
|
const validator = require('validator');
|
|
7
7
|
|
|
8
|
+
const db = require.main.require('./src/database');
|
|
8
9
|
const user = require.main.require('./src/user');
|
|
9
10
|
const meta = require.main.require('./src/meta');
|
|
10
11
|
const utils = require.main.require('./src/utils');
|
|
@@ -114,6 +115,11 @@ plugin.onNotificationPush = async ({ notification, uidsNotified: uids }) => {
|
|
|
114
115
|
uids = uids.filter(uid => subs.get(uid).size);
|
|
115
116
|
const userSettings = await user.getMultipleUserSettings(uids);
|
|
116
117
|
|
|
118
|
+
// Save recipients by nid (for use by .rescind)
|
|
119
|
+
const refKey = `web-push:nid:${notification.mergeId || notification.nid}:uids`;
|
|
120
|
+
await db.setAdd(refKey, uids);
|
|
121
|
+
db.pexpire(refKey, 1000 * 60 * 60 * 48); // only track last 48 hours
|
|
122
|
+
|
|
117
123
|
let payloads = await Promise.all(uids.map(async (uid, idx) => {
|
|
118
124
|
const payload = await constructPayload(notification, userSettings[idx].userLang);
|
|
119
125
|
return [uid, payload];
|
|
@@ -134,6 +140,27 @@ plugin.onNotificationPush = async ({ notification, uidsNotified: uids }) => {
|
|
|
134
140
|
});
|
|
135
141
|
};
|
|
136
142
|
|
|
143
|
+
plugin.onNotificationRescind = async ({ nids }) => {
|
|
144
|
+
const notificationKeys = nids.map(nid => `notifications:${nid}`);
|
|
145
|
+
let mergeIds = await db.getObjectsFields(notificationKeys, ['mergeId']);
|
|
146
|
+
mergeIds = mergeIds.map(o => o.mergeId);
|
|
147
|
+
|
|
148
|
+
// Favour mergeIds over nids, then eliminate dupes
|
|
149
|
+
const tags = new Set(notificationKeys.map((key, i) => mergeIds[i] || key));
|
|
150
|
+
const recipients = await db.getSetsMembers(Array.from(tags).map(tag => `web-push:nid:${tag}:uids`));
|
|
151
|
+
|
|
152
|
+
Promise.all(Array.from(tags).map(async (tag, idx) => {
|
|
153
|
+
let subs = await subscriptions.list(recipients[idx]);
|
|
154
|
+
subs = new Set(...Object.values(Object.fromEntries(subs))); // wtf
|
|
155
|
+
|
|
156
|
+
if (subs.size) {
|
|
157
|
+
subs.forEach(async (subscription) => {
|
|
158
|
+
await webPush.sendNotification(subscription, JSON.stringify({ tag }));
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}));
|
|
162
|
+
};
|
|
163
|
+
|
|
137
164
|
plugin.addProfileItem = async (data) => {
|
|
138
165
|
const title = await translator.translate('[[web-push:profile.label]]');
|
|
139
166
|
data.links.push({
|
|
@@ -153,7 +180,7 @@ plugin.addProfileItem = async (data) => {
|
|
|
153
180
|
return data;
|
|
154
181
|
};
|
|
155
182
|
|
|
156
|
-
async function constructPayload({ bodyShort, bodyLong, path }, language) {
|
|
183
|
+
async function constructPayload({ nid, mergeId, bodyShort, bodyLong, path }, language) {
|
|
157
184
|
let { maxLength } = await meta.settings.get('web-push');
|
|
158
185
|
maxLength = parseInt(maxLength, 10) || 256;
|
|
159
186
|
|
|
@@ -163,6 +190,7 @@ async function constructPayload({ bodyShort, bodyLong, path }, language) {
|
|
|
163
190
|
|
|
164
191
|
let [title, body] = await translator.translateKeys([bodyShort, bodyLong], language);
|
|
165
192
|
([title, body] = [title, body].map(str => validator.unescape(utils.stripHTMLTags(str))));
|
|
193
|
+
const tag = mergeId || nid;
|
|
166
194
|
const url = `${nconf.get('url')}${path}`;
|
|
167
195
|
|
|
168
196
|
// Handle empty bodyLong
|
|
@@ -179,6 +207,7 @@ async function constructPayload({ bodyShort, bodyLong, path }, language) {
|
|
|
179
207
|
return {
|
|
180
208
|
title,
|
|
181
209
|
body,
|
|
210
|
+
tag,
|
|
182
211
|
data: { url },
|
|
183
212
|
};
|
|
184
213
|
}
|
package/package.json
CHANGED
package/plugin.json
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
{ "hook": "filter:admin.header.build", "method": "addAdminNavigation" },
|
|
9
9
|
{ "hook": "filter:config.get", "method": "appendConfig" },
|
|
10
10
|
{ "hook": "action:notification.pushed", "method": "onNotificationPush" },
|
|
11
|
+
{ "hook": "static:notifications.rescind", "method": "onNotificationRescind" },
|
|
11
12
|
{ "hook": "filter:user.profileMenu", "method": "addProfileItem" }
|
|
12
13
|
],
|
|
13
14
|
"languages": "public/languages",
|