forked from arasbm/nodebb-plugin-whoisin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.js
More file actions
163 lines (146 loc) · 4.83 KB
/
library.js
File metadata and controls
163 lines (146 loc) · 4.83 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
(function(module) {
"use strict";
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 =
'<div id="whoisin-{postData.pid}" class="whoisin" data-pid="{postData.pid}">' +
' <h4>{title}</h4>' +
' <div class="participants"></div>' +
' <div class="whoisin-btn-wrapper">' +
' <button title="add yourself to the list" class="iamin btn btn-primary">' +
' <i class="fa fa-plus"></i> {iamin}</button>' +
' <button title="remove yourself from this list" class="iamnotin btn btn-danger">' +
' <i class="fa fa-times"></i></button>' +
' </div>' +
'</div>' +
'<br />';
whoisin.init = function(params, callback) {
// 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) {
var settings = /(\[whoisin )(.*)(\])/.exec(data.postData.content);
var title = "Who is in?";
var iamin = "I am in!";
if (!!settings && settings.length > 2) {
var title_attr = /(title=\()(.*?)(\))/.exec(settings[2]);
var iamin_attr = /(iamin=\()(.*?)(\))/.exec(settings[2]);
if (title_attr) {
title = title_attr[2];
}
if (iamin_attr) {
iamin = iamin_attr[2];
}
}
var tmp =
mainTemplate.replace(/\{postData.pid\}/gi, data.postData.pid)
.replace(/\{title\}/gi, title)
.replace(/\{iamin\}/gi, iamin);
data.postData.content =
data.postData.content.replace(/\[whoisin(.*?)\]/gi, tmp);
}
callback(null, data);
};
whoisin.commit = function(socket, data, callback) {
if (socket.hasOwnProperty('uid') && socket.uid > 0) {
db.getObject('whoisin-post-' + data.pid + '-participants', function(err, whoisin_participants) {
if (err) {
console.log('whoisin plugin: noone is in');
}
whoisin_participants = whoisin_participants || {};
if (data.action === 'add') {
whoisin_participants[socket.uid] = {
isin: true,
timestamp: new Date()
};
} else if (data.action === 'remove' && whoisin_participants[socket.uid]) {
whoisin_participants[socket.uid].isin = false;
}
//serialize
whoisin_participants[socket.uid] = JSON.stringify(whoisin_participants[socket.uid]);
db.setObject('whoisin-post-' + data.pid + '-participants', whoisin_participants, function(err){
if (err) {
console.log('Whoisin Plugin: Error saving to db, ', err);
} else {
callback(null, "success");
}
});
});
} else {
callback(new Error('not-logged-in'));
}
}
whoisin.load = function(socket, data, callback) {
db.getObject('whoisin-post-' + data.pid + '-participants', function(err, participants) {
var users_array = [];
var whoisin_data = {};
if (err) {
console.log('whoisin plugin: Error getting list of participants for topic');
}
whoisin_data.current_user_is_in = false;
whoisin_data.current_user_id = socket.uid;
whoisin_data.users = participants;
for (var userid in whoisin_data.users) {
// FIXME
whoisin_data.users[userid] = JSON.parse(whoisin_data.users[userid]);
users_array.push(userid);
if (userid == socket.uid && whoisin_data.users[userid].isin) {
whoisin_data.current_user_is_in = true;
}
}
user.getMultipleUserFields(users_array, ['username', 'userslug', 'picture', 'uid'], function(err, users_data) {
// Add user data to whoisin data
var sortable = [], user;
for (var id in users_data) {
user = users_data[id];
whoisin_data.users[user.uid].userslug = user.userslug;
whoisin_data.users[user.uid].picture = user.picture;
whoisin_data.users[user.uid].username = user.username;
}
for (user in whoisin_data.users){
if(whoisin_data.users.hasOwnProperty(user)){
var u = whoisin_data.users[user];
u.uid = user;
sortable.push(u);
}
}
whoisin_data.users = sortable.sort(function(a,b){
var an = new Date(a.timestamp),
bn = new Date(b.timestamp);
if(an > bn || b.uid == socket.uid) {
return 1;
}
if(an < bn || a.uid == socket.uid) {
return -1;
}
return 0;
});
callback(null, whoisin_data);
});
});
}
function renderAdmin(req, res, next) {
res.render('admin/plugins/whoisin', {});
}
module.exports = whoisin;
}(module));