-
Notifications
You must be signed in to change notification settings - Fork 4
/
library.js
93 lines (74 loc) · 2.39 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
'use strict';
var async = require('async');
var db = require.main.require('./src/database');
var meta = require.main.require('./src/meta');
var user = require.main.require('./src/user');
var pagination = require.main.require('./src/pagination');
var controllers = require('./lib/controllers');
const routeHelpers = require.main.require('./src/routes/helpers');
var plugin = {};
plugin.init = async ({ router }) => {
var SocketPlugins = require.main.require('./src/socket.io/plugins');
SocketPlugins.gdpr = {};
SocketPlugins.gdpr.refresh = function (socket, data, callback) {
db.getObjectField('user:' + socket.uid, 'gdpr_consent', callback);
};
routeHelpers.setupAdminPageRoute(router, '/admin/plugins/gdpr', controllers.renderAdminPage);
};
plugin.getUsers = function (page, callback) {
page = parseInt(page, 10) || 1;
const resultsPerPage = 20;
const start = Math.max(0, page - 1) * resultsPerPage;
const stop = start + resultsPerPage - 1;
let count;
let pageCount;
async.waterfall([
function (next) {
db.sortedSetCard('users:joindate', function (err, numUsers) {
count = numUsers;
pageCount = Math.ceil(count / resultsPerPage);
next(err);
});
},
async.apply(db.getSortedSetRange.bind(db), 'users:joindate', start, stop),
function (uids, next) {
db.getObjectsFields(uids.map(uid => 'user:' + uid), ['uid', 'gdpr_consent'], next);
},
function (users, next) {
const uids = users.map(user => parseInt(user.uid, 10));
user.getUsersFields(uids, ['username', 'userslug', 'picture'], function (err, profiles) {
next(err, users, profiles);
});
},
], function (err, users, profiles) {
if (err) {
return callback(err);
}
users = users.map((user, idx) => {
user.gdpr_consent = parseInt(user.gdpr_consent, 10) === 1;
return Object.assign(user, profiles[idx]);
});
callback(null, {
users: users,
pagination: pagination.create(page, pageCount),
});
});
};
plugin.addAdminNavigation = function (header, callback) {
header.plugins.push({
route: '/plugins/gdpr',
icon: 'fa-eur',
name: 'GDPR',
});
callback(null, header);
};
plugin.appendConfig = async (config) => {
const consented = await db.getObjectField('user:' + config.uid, 'gdpr_consent');
const { require_consent } = await meta.settings.get('gdpr');
config.gdpr = {
require: require_consent === 'on',
given: !!parseInt(consented, 10),
};
return config;
};
module.exports = plugin;