-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser-feedback-server.js
233 lines (222 loc) · 8.12 KB
/
user-feedback-server.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
UserFeedback = new Mongo.Collection("userfeedback");
UserFeedbackMessages = new Mongo.Collection("userfeedbackmessages");
UserFeedbackChats = new Mongo.Collection("userfeedbackchats");
var moderators = {};
// check if user is a moderator - specified in settings - accepts userId or email
// looks up once and then caches for future
function isModerator(user){
if(user in moderators)
return moderators[user];
else
{
var ismod = false, email = '---';
if(user){
var uRec = Meteor.users.findOne(user);
if(uRec.emails && uRec.emails.length > 0)
email = uRec.emails[0].address;
if(Meteor.settings && Meteor.settings.userfeedback
&& Meteor.settings.userfeedback.moderators
&& (Meteor.settings.userfeedback.moderators[user] || Meteor.settings.userfeedback.moderators[email])
)
ismod = true;
}
moderators[user] = ismod;
return ismod;
}
}
Meteor.publish("userfeedbackchats", function() {
var uid = this.userId;
if(isModerator(uid)){
return UserFeedbackChats.find();
}
else
return UserFeedbackChats.find('support');
});
Meteor.publish("userfeedbackmessages", function(userId) {
var uid = this.userId;
if(isModerator(uid) && userId)
uid = userId;
return UserFeedbackMessages.find({ $and: [ {status: {$gt: 0}}, {$or: [ {from: uid}, {to: uid} ]} ] }, {multi: true});
});
Meteor.startup(function(){
// text index search requires that index is present
try{
UserFeedback._ensureIndex({"head":"text", "desc":"text"},{weights: {"head": 10, "desc": 5,}});
console.log('index exists');
}catch(e){
console.log(e);
console.log('seems like index could not be reated - this is needed to do search over topic descriptions. '+
'You can create the index on mongo command line as db.userfeedback.createIndex({"head":"text", "desc":"text"},{"weights": {"head": 10, "desc": 5,}})');
}
});
Meteor.methods({
initUFBChat: function(){ // this function is called on chat open
var stats = {};
stats.isChatModerator = isModerator(Meteor.userId());
console.log('ischatmoderator '+stats.isChatModerator);
return stats;
},
modUpdateStatus: function(){
if(isModerator(Meteor.userId())){
var supRec = UserFeedbackChats.find('support');
// this should be called to update the status id moderator is present
// not implemented yet
//if(supRec && supRec.status)
}
},
ufbArchiveUserMessages: function(uid){
if(isModerator(Meteor.userId())){
var msgs = UserFeedbackMessages.find({ $and: [ {status: 1}, {$or: [ {from: uid}, {to: uid} ]} ] }).fetch();
console.log('archiving '+uid+' msgs: '+msgs.length);
for(var v in msgs){
var msg = msgs[v];
UserFeedbackMessages.update(msg._id, { $set : {status: 0}});
}
}
},
ufbSendMessage: function(text, to){
var sender = Meteor.userId();
var receiver = 'support';
var fromName = 'you';
if(to && isModerator(sender)){
fromName = 'support';
receiver = to;
var ufChat = UserFeedbackChats.findOne(to);
if(ufChat.live === 1)
UserFeedbackChats.upsert(to, {$set:{live:0}});
}
else
UserFeedbackChats.upsert(sender, {$set: {lastUpdate: new Date(), live:1, name: Meteor.user().username}} );
console.log(sender + 'chatting with ' + receiver+' isModerator: '+isModerator(Meteor.userId())+' called:'+fromName);
UserFeedbackMessages.insert({ from: sender, fromName: fromName, message: text, at: new Date(), to: receiver, status: 1 });
},
ufbChatAcknowledged: function(to){
if(to && isModerator(Meteor.userId())){
UserFeedbackChats.upsert(to, {$set:{live:0}});
}
},
initUFB: function(){ // this function is called on opens - returns statistics
var topicStatus = UserFeedback.find({},{fields:{"status":1}}).fetch();
var stats = _.countBy(topicStatus,'status');
stats.atTime = new Date();
var uId = Meteor.userId();
stats.isModerator = isModerator(uId);
var statInfo = JSON.stringify(stats);
var sort_order = {};
sort_order["likes"] = -1;
//return articles.find({}, {sort: sort_order, limit: 1});
stats.topics = UserFeedback.find({}, {sort:sort_order, limit: 15, fields:{'head':1, 'date':1, 'likes':1, 'unlikes':1, 'category':1, '_id':1, 'status':1, 'commentCount':1, 'username':1}}).fetch();
console.log('ufb: got stats : '+ statInfo + ' topics: '+stats.topics.length);
return stats;
},
findTopic: function (text, pageNo) {
check(text, String);
var res = UserFeedback.find({"$text":{"$search":text}},
{ fields: {'head':1, 'date':1, 'likes':1, 'unlikes':1, 'category':1, '_id':1, 'status':1, 'commentCount':1, 'username':1, 'date':1 }}).fetch();
console.log('ufb: findToipc '+ text+ ' got '+res.length);
return res;
},
setTopic: function (head, typ, desc, topicId) {
if (! Meteor.userId())
throw new Meteor.Error("log in to create new topic");
check(head, String);
check(typ, String);
check(desc, String);
if(!topicId){
var topic = {head: head, date: new Date(), category: typ, desc: desc, likes:0, unlikes:0, category:typ, commentCount: 0, comments:[], userSet:{}, status: "New" };
topic.owner = Meteor.userId();
topic.username = Meteor.user().username;
topicId = UserFeedback.insert(topic);
console.log('ufb: new topic '+head+' '+desc + ' '+typ);
}
else{
var topic = UserFeedback.findOne({'_id': topicId});
if(topic.owner === Meteor.userId() || isModerator(Meteor.userId())){
topic.head = head;
topic.category = typ;
topic.desc = desc;
topic.updated = new Date();
UserFeedback.update({'_id': topicId}, topic);
console.log('ufb: updating topic '+head+' '+desc + ' '+typ+ ' '+topicId);
}
}
return UserFeedback.findOne({'_id': topicId});
},
getTopicDetails: function (topicId) {
check(topicId, String);
return UserFeedback.findOne({_id: topicId});
},
updateTopic: function (topicId, type, comment) {
if (! Meteor.userId())
throw new Meteor.Error("log in to type on topic");
check(comment, String);
check(type, String);
var ufb = UserFeedback.findOne({'_id': topicId}, {fields:{'likes':1, 'unlikes':1, userSet:1, commentCount:1, 'owner':1,'date':1}});
var uId = Meteor.userId();
console.log('ufb: updaing topic id:'+topicId+' type:'+type+ ' comment:'+comment+ ' user:'+uId);
var updated = false;
if(type == 'likes'){
if(!ufb.userSet[uId]){
var updateSet = {"likes": ufb.likes + 1};
updateSet["userSet."+uId] = 1;
UserFeedback.update({'_id': topicId},{"$set":updateSet});
updated = true;
}
}
else if(type == 'unlikes'){
if(!ufb.userSet[uId]){
var updateSet = {"unlikes": ufb.unlikes + 1};
updateSet["userSet."+uId] = 1;
UserFeedback.update({'_id': topicId},{"$set": updateSet});
updated = true;
}
}
else if(type == 'clikes'){
if(!ufb.userSet[uId+'-'+comment]){
var updateSet = {};
updateSet["userSet."+uId+'-'+comment]=1;
UserFeedback.update({_id: topicId, "comments.id" : parseInt(comment)},
{ "$inc": { "comments.$.rating" : 1 }, "$set":updateSet });
updated = true;
}
}
else if(type == 'cunlikes'){
if(!ufb.userSet[uId+'-'+comment]){
var updateSet = {};
updateSet["userSet."+uId+'-'+comment]= -1;
UserFeedback.update({_id: topicId, "comments.id" : parseInt(comment)},
{ "$inc": { "comments.$.rating" : -1 }, "$set":updateSet });
updated = true;
}
}
else if(type == 'accept-answer'){
UserFeedback.update({_id: topicId, "comments.id" : parseInt(comment)},
{ "$set": {"comments.$.accepted" : true, "status":"Solved"} });
updated = true;
}
else if(type == 'remove-comment'){
UserFeedback.update({_id: topicId, "comments.id" : parseInt(comment)},
{ "$set": {"comments.$.removed" : true} });
updated = true;
}
else if(type == 'comment'){
var cmt = {"desc":comment, "date": new Date(), "rating": 0};
cmt.user = Meteor.userId();
cmt.uName = Meteor.user().username;
cmt.id = ufb.commentCount + 1;
UserFeedback.update({_id: topicId},{"$push":{"comments": cmt}, "$set":{ commentCount: cmt.id}});
updated = true;
}
else if(type == 'status'){
if(ufb.owner === Meteor.userId() || isModerator(Meteor.userId())){
UserFeedback.update({'_id': topicId},{"$set":{"status": comment}});
updated = true;
}
}
if(updated)
console.log("ufb: updateTopic - successfully updated");
else
console.log("ufb: updateTopic - did not update for some reason");
return UserFeedback.findOne({'_id' : topicId});
}
});