forked from sclorg/nodejs-ex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatModule.js
123 lines (106 loc) · 3.68 KB
/
chatModule.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
function chatModule(){
var that = this;
var database = require('./database');
/**
* Retrieves all chat messages that belong to the chat between user_a and user_b from the database and sends the results to the mobile app
* req.params must contain friendid
*/
that.getchat = function (req, res, next){
var friendid= req.params.friendid;
if( !friendid ){
res.json({'error': 'Insufficient Parameters'});
} else {
database.connection.query( 'DELETE FROM notifications WHERE username IN (SELECT username FROM users WHERE id = ?) AND CONCAT("New message from ", (SELECT username FROM users WHERE id = ?)) = message;'+
'SELECT * FROM user_chat WHERE (user_a = ? and user_b = ?) or (user_a = ? and user_b = ?) ORDER BY changed ASC',
[req.user.id, friendid, req.user.id, friendid, friendid, req.user.id], function (error, results, fields) {
if (!error){
var messages = [];
for(var i=0; i<results[1].length; i++){
var message = {
timestamp: results[1][i].timestamp,
text: results[1][i].message,
owncomment: results[1][i].user_a == req.user.id
};
messages.push(message);
}
res.send(200, {error: "false", chat: messages});
} else {
console.log(error.code);
console.log(error);
res.send(500, {error: "Could not find the chat with your friend"});
}
});
}
return next();
}
/**
* Inserts a chat message and an appropriate notification into the database and sends a status response to the mobile app
* req.body must contain friendid, text and timestamp
*/
that.postchat = function (req, res, next){
if( !req.body.hasOwnProperty('friendid') || !req.body.hasOwnProperty('text') || !req.body.hasOwnProperty('timestamp')){
res.json({'error': 'Insufficient Parameters'});
} else {
var post = {
'user_a': req.user.id,
'user_b': req.body.friendid,
'message': req.body.text,
'timestamp': req.body.timestamp
};
var query = database.connection.query('INSERT INTO user_chat SET ?', post, function (error, results, fields) {
if (!error){
console.log('Last insert ', results);
response = {
"error": 'false'
};
res.send(response);
} else {
console.log(error);
console.log(error.code)
response = null;
if(error.code === 'ER_DUP_ENTRY')
response = {
"error": "There was an error with the chat"
};
res.send(response);
}
});
//Add a notification to the notification table
//get the username of the user
var sendUsername;
database.connection.query('SELECT username FROM users WHERE id = ?',
[req.user.id], function (error, results, fields) {
if (!error){
for(var i=0; i<results.length; i++){
sendUsername = results[i].username
};
}
//get the username of the friend
var receiveUsername;
database.connection.query('SELECT username FROM users WHERE id = ?',
[req.body.friendid], function (error, results, fields) {
if (!error){
for(var i=0; i<results.length; i++){
receiveUsername = results[i].username
};
}
//insert the notification
post = {
'username': receiveUsername,
'message': "New message from " + sendUsername
};
var query = database.connection.query('INSERT INTO notifications SET ?', post, function (error, results, fields) {
if (!error){
console.log('notification inserted');
} else {
console.log('error while inserting the notification');
console.log(error);
console.log(error.code)
}
});
});
});
}
}
}
module.exports = new chatModule();