forked from NodeBB/nodebb-theme-quickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
107 lines (90 loc) · 2.99 KB
/
library.js
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
'use strict';
const library = module.exports;
const calendarEvent = require.main.require('nodebb-plugin-calendar/build/lib/event.js');
const calendarResponses = require.main.require('nodebb-plugin-calendar/build/lib/responses.js');
const groups = require.main.require('./src/groups');
const webserver = require.main.require('./src/webserver');
library.renderParticipationWidget = async function (widget) {
const daysDelta = 15 * 24 * 60 * 60 * 1000;
const { displayedGroups = [], relevantCategoryId = 0 } = widget.data;
const todayStart = new Date();
const [eventsByDate, groupData, groupMembers] = await Promise.all([
calendarEvent.getEventsByDate(+todayStart, +todayStart + daysDelta),
groups.getGroupsData(displayedGroups),
groups.getMemberUsers(displayedGroups, 0, -1),
]);
const {
pid = 0,
name: eventName = '',
} = eventsByDate.find(({ cid }) => cid === +relevantCategoryId) ?? {};
let positiveResponseCountByGroup;
if (pid && eventName) {
const currentResponses = await calendarResponses.getAll({
pid,
selection: ['yes', 'maybe'],
});
const yesUids = currentResponses.yes.map(response => response.uid);
const maybeUids = currentResponses.maybe.map(response => response.uid);
const positiveResponseUids = [...yesUids, ...maybeUids];
positiveResponseCountByGroup = displayedGroups.map((groupName, i) => {
const {
userTitle,
slug,
labelColor,
} = groupData[i];
const responseCount = groupMembers[i]?.filter(({ uid }) => positiveResponseUids.includes(uid)).length;
return {
groupName,
responseCount,
userTitle,
slug,
labelColor,
};
});
}
widget.html = await widget.req.app.renderAsync('widgets/eventparticipation', {
pid,
eventName,
positiveResponseCountByGroup,
});
return widget;
};
library.defineWidgets = async function (widgets) {
const groupsData = await groups.getGroups('groups:visible:createtime', 0, -1);
const html = await webserver.app.renderAsync('admin/partials/widgets/eventparticipation', { groups: groupsData });
widgets.push({
widget: 'eventparticipation',
name: 'Participation Counter',
description: 'ARF and SWORD Numbers with easy Register button',
content: html,
});
return widgets;
};
library.defineWidgetAreas = async function (areas) {
const locations = ['header', 'sidebar', 'footer'];
const templates = [
'categories.tpl', 'category.tpl', 'topic.tpl', 'users.tpl',
'unread.tpl', 'recent.tpl', 'popular.tpl', 'top.tpl', 'tags.tpl', 'tag.tpl',
'login.tpl', 'register.tpl',
];
function capitalizeFirst(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
templates.forEach((template) => {
locations.forEach((location) => {
areas.push({
name: `${capitalizeFirst(template.split('.')[0])} ${capitalizeFirst(location)}`,
template: template,
location: location,
});
});
});
areas = areas.concat([
{
name: 'Account Header',
template: 'account/profile.tpl',
location: 'header',
},
]);
return areas;
};