-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibrary.js
More file actions
127 lines (100 loc) · 3.14 KB
/
Copy pathlibrary.js
File metadata and controls
127 lines (100 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'use strict';
const posts = require.main.require('./src/posts');
const privileges = require.main.require('./src/privileges');
const SILENT_EDIT_PRIVILEGE = 'posts:silent-edit';
const SILENT_EDIT_CACHE_TTL = 60000;
const pendingSilentNotifications = new Map();
const plugin = module.exports;
plugin.addCategoryPrivilege = async function ({ privileges: categoryPrivileges }) {
const privilegeData = {
label: '[[silent-edit:admin.privilege.label]]',
type: 'posting',
};
const historyPrivilege = categoryPrivileges.get('posts:history');
const entries = Array.from(categoryPrivileges.entries())
.filter(([key]) => key !== SILENT_EDIT_PRIVILEGE && key !== 'posts:history');
const reordered = [];
let inserted = false;
entries.forEach((entry) => {
reordered.push(entry);
if (entry[0] === 'posts:edit') {
reordered.push([SILENT_EDIT_PRIVILEGE, privilegeData]);
if (historyPrivilege) {
reordered.push(['posts:history', historyPrivilege]);
}
inserted = true;
}
});
if (!inserted) {
reordered.push([SILENT_EDIT_PRIVILEGE, privilegeData]);
if (historyPrivilege) {
reordered.push(['posts:history', historyPrivilege]);
}
}
categoryPrivileges.clear();
reordered.forEach(([key, value]) => categoryPrivileges.set(key, value));
};
plugin.addComposerBuildData = async function (hookData) {
if (!hookData || !hookData.req || !hookData.templateData) {
return hookData;
}
const pid = parseInt(hookData.req.query.pid, 10);
if (!pid) {
return hookData;
}
hookData.templateData.canSilentEdit = await canUseSilentEdit(pid, hookData.req.uid);
return hookData;
};
plugin.addComposerPushData = async function (hookData) {
if (!hookData || !hookData.pid || !hookData.caller) {
return hookData;
}
hookData.canSilentEdit = await canUseSilentEdit(hookData.pid, hookData.caller.uid);
return hookData;
};
plugin.captureSilentEdit = async function (hookData) {
if (!hookData || !hookData.data || !hookData.data.silentEdit) {
return hookData;
}
const { pid, uid } = hookData.data;
const allowed = await canUseSilentEdit(pid, uid);
if (!allowed) {
throw new Error('[[error:no-privileges]]');
}
pendingSilentNotifications.set(buildNotificationId(pid, uid), Date.now());
prunePendingNotifications();
return hookData;
};
plugin.filterNotificationsCreate = async function (hookData) {
if (!hookData || !hookData.data || !hookData.data.nid) {
return hookData;
}
prunePendingNotifications();
if (!pendingSilentNotifications.has(hookData.data.nid)) {
return hookData;
}
pendingSilentNotifications.delete(hookData.data.nid);
hookData.data = null;
return hookData;
};
async function canUseSilentEdit(pid, uid) {
if (!parseInt(uid, 10) || !pid) {
return false;
}
const cid = await posts.getCidByPid(pid);
if (!cid) {
return false;
}
return await privileges.categories.can(SILENT_EDIT_PRIVILEGE, cid, uid);
}
function buildNotificationId(pid, uid) {
return `edit_post:${pid}:uid:${uid}`;
}
function prunePendingNotifications() {
const cutoff = Date.now() - SILENT_EDIT_CACHE_TTL;
for (const [nid, timestamp] of pendingSilentNotifications.entries()) {
if (timestamp < cutoff) {
pendingSilentNotifications.delete(nid);
}
}
}