-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.js
More file actions
102 lines (88 loc) · 2.99 KB
/
library.js
File metadata and controls
102 lines (88 loc) · 2.99 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
(function(module) {
"use strict";
//var USER = require('../../src/user');
var whoisin = {},
SocketPlugins = module.parent.require('./socket.io/plugins'),
db = module.parent.require('./database'),
// meta = module.parent.require('./meta'),
user = module.parent.require('./user'),
// posts = module.parent.require('./posts'),
mainTemplate =
// TODO: get from'/templates/views/main';
'<div class="whoisin row">' +
' <h4>Who is in?</h4>' +
' <button class="iamin btn btn-primary">I am in!</button>' +
'</div>';
whoisin.init = function(params, callback) {
console.log('nodebb-plugin-whoisin: loaded');
// We create two routes for every view. One API call, and the actual route itself.
// Just add the buildHeader middleware to your route and NodeBB will take care of everything for you.
params.router.get('/admin/plugins/whoisin', params.middleware.admin.buildHeader, renderAdmin);
params.router.get('/api/admin/plugins/whoisin', renderAdmin);
SocketPlugins.whoisin = {
commit: whoisin.commit,
load: whoisin.load
};
callback();
};
whoisin.addAdminNavigation = function(header, callback) {
header.plugins.push({
route: '/plugins/whoisin',
icon: 'fa-child',
name: 'whoisin'
});
callback(null, header);
};
whoisin.parse = function(data, callback) {
if (!data || !data.postData || !data.postData.content) {
return callback(null, data);
}
data.postData.content = data.postData.content.replace(/Who is in\?/gi, mainTemplate);
callback(null, data);
};
whoisin.commit = function(socket, data, callback) {
if (socket.hasOwnProperty('uid') && socket.uid > 0) {
var topicid = data.url.match("topic/([0-9]*)")[1];
db.getObject('post-' + topicid + '-whoisin_participants', function(err, whoisin_participants) {
if (err) {
console.log('whoisin plugin: noone is in');
}
whoisin_participants[socket.uid] = {
isin: true,
timestamp: new Date()
};
db.setObject('post-' + topicid + '-whoisin_participants', whoisin_participants , function(err){
if (err) {
console.log('Whoisin Plugin: Error saving to db, ', err);
} else {
console.log('saved to db: ', JSON.stringify(whoisin_participants));
callback(null, "success");
}
});
});
} else {
callback(new Error('not-logged-in'));
}
}
whoisin.load = function(socket, data, callback) {
var topicid = data.url.match("topic/([0-9]*)")[1];
db.getObject('post-' + topicid + '-whoisin_participants', function(err, data) {
if (err) {
console.log('whoisin plugin: Error getting list of participants for topic');
}
console.log('got participant object: ', JSON.stringify(data));
var users_array = [];
for (var userid in data) {
users_array.push(userid);
}
console.log('got user array :-> ', users_array);
user.getMultipleUserFields(users_array, ['username', 'userslug', 'picture'], function(err, userData) {
callback(null, userData);
});
});
}
function renderAdmin(req, res, next) {
res.render('admin/plugins/whoisin', {});
}
module.exports = whoisin;
}(module));