-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoints.js
88 lines (69 loc) · 2 KB
/
endpoints.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
if( Meteor.isServer ) {
api = new Restivus({
useDefaultAuth: true,
prettyJson: true
});
Meteor.startup(function() {
Future = Npm.require('fibers/future');
});
api.addRoute('messageWaiting', {authRequired: false}, {
post: {
action: function() {
if( this.bodyParams.user === undefined ) {
return false;
}
var messageFuture = new Future();
var user = this.bodyParams.user.name;
//Since there's no connection open to a client, we can't actually properly "log in"
Meteor.call('plone.login', this.bodyParams.token, this.bodyParams.user.name, function(err, res) {
if( err ) {
messageFuture.return(false);
}else if( res.userId ){
messageFuture.return(getMessages(user));
}
});
return messageFuture.wait();
}
}
});
}
var getMessages = function(username) {
var id = Accounts.findUserByUsername(username);
if( id === undefined ) {
return false;
}
var rooms = RocketChat.models.Subscriptions.findByType(['c']).fetch();
var roommap = {};
for( var room in rooms ) {
roommap[ rooms[room].rid ] = rooms[room].name;
}
var missed = {};
var count = 0;
var messages = RocketChat.models.Messages.findVisibleCreatedOrEditedAfterTimestamp(id.lastLogin).fetch();
for( var msg in messages ) {
if( messages[msg].u.username === username ) {
continue;
}
count += 1;
var message = messages[msg];
var rid = message.rid;
message.name = roommap[rid];
var name = message.name;
if( missed[name] === undefined ) {
missed[name] = [];
}
missed[name].push(message);
}
if( count > 0 ) {
return missed;
}
return false;
};
//api.addRoute('removeperson/:id', {authRequired: false}, {
// get: function() {
// Meteor.users.remove(this.urlParams.id);
// var user = Accounts.findUserByUsername(this.urlParams.id);
// RocketChat.deleteUser(user._id);
// return true;
// }
//});