-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
53 lines (46 loc) · 1.61 KB
/
index.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
var sanitaze = require('validator').sanitize;
exports.name = 'kabamPluginPrivateMessage';
exports.routes = function (kabam) {
kabam.app.get('/api/messages', function (request, response) {
if (request.user) {
var mesgLimit = request.query['limit'] ? request.query['limit'] : 10,
mesgOffset = request.query['offset'] ? request.query['offset'] : 0;
request.user.getRecentMessages(mesgLimit, mesgOffset, function(err,messages){
if(err) throw err;
response.json(messages);
});
} else {
response.send(400);
}
});
kabam.app.get('/api/messages/:username', function (request, response) {
if (request.user) {
var mesgLimit = request.query['limit'] ? request.query['limit'] : 10,
mesgOffset = request.query['offset'] ? request.query['offset'] : 0;
request.user.getDialog(request.params.username, mesgLimit, mesgOffset, function(err,messages){
if(err && err.message === 'User do not exists!'){
response.send(404);
} else {
if(err) throw err;
response.json(messages);
}
});
} else {
response.send(400);
}
});
kabam.app.post('/api/messages/:username', function (request, response) {
if (request.user) {
request.user.sendMessage(request.params.username, request.body.title, request.body.message, function(err){
if(err) throw err;
if(request.is('json')){
response.json(201,{'status':201,'description':'Message is send!'});
} else {
response.redirect('back');
}
});
} else {
response.send(400);
}
});
};