forked from psychobunny/nodebb-widget-board-stats
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlibrary.js
More file actions
128 lines (105 loc) · 3.95 KB
/
library.js
File metadata and controls
128 lines (105 loc) · 3.95 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
'use strict';
const nconf = require.main.require('nconf');
const db = require.main.require('./src/database');
const user = require.main.require('./src/user');
const meta = require.main.require('./src/meta');
const utils = require.main.require('./src/utils');
const socketPlugins = require.main.require('./src/socket.io/plugins');
const adminRooms = require.main.require('./src/socket.io/admin/rooms');
let app;
const Widget = module.exports;
const relativePath = nconf.get('relative_path');
Widget.init = function (params, callback) {
app = params.app;
callback();
};
socketPlugins.boardStats = {};
socketPlugins.boardStats.get = async function (socket, { userLang }) {
return await getWidgetData(socket.uid, userLang);
};
async function getUserLang(uid) {
const settings = await user.getSettings(uid);
return settings.userLang;
}
async function getWidgetData(uid, userLang) {
const [global, latestUser, activeUsers, onlineUsers] = await Promise.all([
db.getObjectFields('global', ['topicCount', 'postCount', 'userCount']),
getLatestUser(uid),
getActiveUsers(),
Widget.updateAndGetOnlineUsers(),
]);
const dateStr = (new Date(parseInt(onlineUsers.timestamp, 10)))
.toLocaleDateString(userLang, {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric',
});
return {
count: utils.makeNumberHumanReadable(onlineUsers.onlineCount + onlineUsers.guestCount),
members: utils.makeNumberHumanReadable(onlineUsers.onlineCount),
guests: utils.makeNumberHumanReadable(onlineUsers.guestCount),
list: joinUsers(activeUsers),
posts: utils.makeNumberHumanReadable(global.postCount ? global.postCount : 0),
topics: utils.makeNumberHumanReadable(global.topicCount ? global.topicCount : 0),
registered: utils.makeNumberHumanReadable(global.userCount ? global.userCount : 0),
latest: latestUser,
relative_path: nconf.get('relative_path'),
mostUsers: {
date: dateStr.replace(/,/g, ','),
total: onlineUsers.total,
},
};
}
async function getActiveUsers() {
const uids = await user.getUidsFromSet('users:online', 0, 19);
const userData = await user.getUsersFields(uids, ['username', 'userslug', 'status']);
return userData.filter(user => user && user.status !== 'offline');
}
async function getLatestUser(uid) {
const uids = await user.getUidsFromSet('users:joindate', 0, 0);
const userData = await user.getUsersWithFields(uids, ['username', 'userslug'], uid);
return userData.length ? userData[0] : null;
}
function joinUsers(usersData) {
return usersData
.map(user => `<a class="fw-bold" href="${relativePath}/user/${user.userslug}">${user.username}</a>`)
.join(', ');
}
Widget.updateAndGetOnlineUsers = async function () {
const now = Date.now();
const [onlineUserCount, onlineGuestCount, widgetData] = await Promise.all([
db.sortedSetCount('users:online', now - (meta.config.onlineCutoff * 60000), '+inf'),
adminRooms.getTotalGuestCount(),
db.getObjectFields('plugin:widget-board-stats', ['total', 'timestamp']),
]);
const totalUsers = onlineUserCount + onlineGuestCount;
widgetData.timestamp = widgetData.timestamp || Date.now();
if (parseInt(widgetData.total || 0, 10) <= totalUsers) {
widgetData.timestamp = Date.now();
widgetData.total = totalUsers;
await db.setObject('plugin:widget-board-stats', widgetData);
}
widgetData.onlineCount = onlineUserCount;
widgetData.guestCount = onlineGuestCount;
return widgetData;
};
Widget.renderWidget = async function (widget) {
const lang = widget.req.query.lang || await getUserLang(widget.uid);
const data = await getWidgetData(widget.uid, lang);
const html = await app.renderAsync('widgets/board-stats', data);
widget.html = html;
return widget;
};
Widget.defineWidgets = async function (widgets) {
const widget = {
widget: 'board-stats',
name: 'Board Stats',
description: 'Classical board stats widget in real-time.',
content: 'admin/board-stats',
};
const html = await app.renderAsync(widget.content, {});
widget.content = html;
widgets.push(widget);
return widgets;
};