Repository navigation
Expand file tree
/
Copy pathlibrary.js
More file actions
executable file
·147 lines (121 loc) · 4.31 KB
/
Copy pathlibrary.js
File metadata and controls
executable file
·147 lines (121 loc) · 4.31 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'use strict';
const nconf = nodebb.require('nconf');
const NodeBB = require('./lib/nodebb');
const Config = require('./lib/config');
const Sockets = require('./lib/sockets');
const Hooks = require('./lib/hooks');
const Poll = require('./lib/poll');
const db = nodebb.require('./src/database');
const pagination = nodebb.require('./src/pagination');
const utils = nodebb.require('./src/utils');
const Plugin = module.exports;
Plugin.hooks = Hooks;
Plugin.load = async function (params) {
const routeHelpers = nodebb.require('./src/routes/helpers');
const controllerHelpers = nodebb.require('./src/controllers/helpers');
const { router, middleware } = params;
routeHelpers.setupAdminPageRoute(router, `/admin/plugins/${Config.plugin.id}`, async (req, res) => {
const page = Math.max(parseInt(req.query.page, 10) || 1, 1);
const itemsPerPage = 20;
const start = Math.max(0, (page - 1) * itemsPerPage);
const stop = start + itemsPerPage - 1;
const [pollIds, count] = await Promise.all([
db.getSortedSetRevRange('polls:createtime', start, stop),
db.sortedSetCard('polls:createtime'),
]);
const pollData = await db.getObjects(pollIds.map(pollId => `poll:${pollId}`));
const voteCounts = await Promise.all(pollIds.map(Poll.getVoteCount));
pollData.forEach((poll, index) => {
if (!poll) {
return;
}
poll.timestampISO = utils.toISOString(poll.timestamp);
poll.endISO = utils.toISOString(poll.end);
poll.voteCount = voteCounts[index];
});
await Promise.all(pollData.map(async (poll) => {
if (poll) {
poll.title = await Poll.getDisplayTitle(poll);
}
}));
const pageCount = Math.ceil(count / itemsPerPage);
res.render(`admin/plugins/${Config.plugin.id}`, {
title: 'Poll',
polls: pollData,
pagination: pagination.create(page, pageCount, req.query),
});
});
routeHelpers.setupApiRoute(router, 'delete', `/api/v3/admin/plugins/${Config.plugin.id}/poll/:pollId`, [middleware.ensureLoggedIn, middleware.admin.checkPrivileges], async (req, res) => {
const pollId = parseInt(req.params.pollId, 10);
if (!Number.isInteger(pollId) || pollId <= 0) {
return controllerHelpers.formatApiResponse(400, res, '[[poll:error.invalid_poll_id]]');
}
const poll = await db.getObject(`poll:${pollId}`);
if (!poll) {
return controllerHelpers.formatApiResponse(404, res, '[[poll:error.poll_not_found]]');
}
await Poll.deletePolls([pollId]);
// Remove the pollId from the post's pollIds list (or drop the field if it's now empty)
if (poll.pid) {
const remaining = (await Poll.getPollIdsByPid(poll.pid)).filter(id => String(id) !== String(pollId));
if (remaining.length) {
await db.setObjectField(`post:${poll.pid}`, 'pollIds', JSON.stringify(remaining));
} else {
await db.deleteObjectField(`post:${poll.pid}`, 'pollIds');
}
}
await controllerHelpers.formatApiResponse(200, res, { pollId });
});
NodeBB.PluginSockets[Config.plugin.id] = Sockets;
NodeBB.app = params.app;
if (nconf.get('runJobs')) {
await db.delete(`poll:voteLocks`);
}
};
Plugin.addAdminNavigation = function (adminHeader) {
adminHeader.plugins.push({
route: `/plugins/${Config.plugin.id}`,
icon: Config.plugin.icon,
name: Config.plugin.name,
});
return adminHeader;
};
Plugin.addPrivilege = function (hookData) {
hookData.privileges.set(
'poll:create', { label: '[[poll:admin.create-poll]]' },
);
};
Plugin.copyPrivilegesFrom = function (data) {
if (data.privileges.indexOf('poll:create') === -1) {
data.privileges.push('poll:create');
}
if (data.privileges.indexOf('groups:poll:create') === -1) {
data.privileges.push('groups:poll:create');
}
return data;
};
Plugin.defineWidgets = async function (widgets) {
const widget = {
widget: 'poll',
name: 'Poll',
description: 'Display a poll',
content: await NodeBB.app.renderAsync('admin/partials/widgets/poll', {}),
};
widgets.push(widget);
return widgets;
};
Plugin.renderPollWidget = async function (widget) {
const { pollId } = widget.data;
const Poll = require('./lib/poll');
const settings = await Config.getSettings();
const allowAnon = !!settings.allowGuestsToViewResults;
if (!widget.uid && !allowAnon) {
return null;
}
const pollData = await Poll.get(pollId, widget.uid, widget.req.loggedIn);
if (!pollData) {
return null;
}
widget.html = await NodeBB.app.renderAsync('poll/widget.tpl', { pollId });
return widget;
};