-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
575 lines (507 loc) · 15 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
const express = require("express");
const Client = require('pg').Client
const cors = require("cors");
const PORT = process.env.PORT || 3001;
var API = require('groupme').Stateless
awaitAsync = require('express-async-handler')
/**
* Retrieves user information from the GroupMe API using the provided access token.
* @param {string} TOKEN - The access token for the GroupMe API.
* @returns {Promise<Object>} - A promise that resolves to the user object.
*/
function getUserFromAPI(TOKEN) {
return new Promise((resolve, reject) => {
API.Users.me(TOKEN, function (err, ret) {
if (!err) {
resolve(ret);
}
else {
reject(err);
}
});
});
}
/**
* Retrieves groups information from the GroupMe API using the provided access token.
* @param {string} TOKEN - The access token for the GroupMe API.
* @returns {Promise<Object>} - A promise that resolves to an array of groups object.
*/
function getGroupsFromAPI(TOKEN) {
return new Promise((resolve, reject) => {
groups = null
API.Groups.index(TOKEN, function (err, ret) {
if (!err) {
resolve(ret)
}
else {
reject(err)
}
});
});
}
/**
* Recursively etrieves Messages from the GroupMe API using the provided access token
* @param {string} TOKEN - The access token for the GroupMe API.
* @param {int} groupid - The group id to get messages from.
* @param {Object} opts - The options object for the API call. Limit: (Max 100), before_id: (Message ID to get messages before)
* @returns {Promise<Object>} - A promise that resolves to an array of messages object.
*/
function recGetMessagesFromAPI(TOKEN, groupid, opts) {
return new Promise((resolve, reject) => {
API.Messages.index(TOKEN, groupid, opts, function (err, ret) {
if (!err) {
resolve(ret)
}
else {
reject(err)
}
});
});
}
/**
* Retrieves Messages from the GroupMe API using the provided access token, calls recGetMessagesFromAPI to get all messages.
* @param {string} TOKEN - User access token for the GroupMe API.
* @param {int} groupid - The group id to get messages from.
* @returns {Promise<Object>} - A promise that resolves to an array of messages object.
*/
async function getMessagesFromAPI(TOKEN, gID) {
let opts = { limit: 100 };
let messages = [];
do {
await recGetMessagesFromAPI(TOKEN, gID, opts).then((x, y) => {
if (y) {
batch = [];
} else {
batch = x.messages;
}
});
messages = messages.concat(batch);
if (batch.length < 100) {
break;
}
opts = { limit: 100, before_id: batch[batch.length - 1].id };
} while (true);
return messages;
}
/**
* Replaces all instances of ' with '' in a string, and then wraps the string in single quotes.
* @param {string} str - String to be sanitized
* @returns {string} - A sanitized string.
*/
function sanitizeString(str) {
if (str == null) {
return "Null";
}
return `\'${str.replaceAll('\'', "\'\'")}\'`;
}
/**
* Replaces all instances of ' with '' in a string, and DOES NOT wrap the string in single quotes.
* @param {string} str - String to be sanitized
* @returns {string} - A sanitized string.
*/
function sanitizeNoWrapString(str) {
if (str == null) {
return "Null";
}
return `${str.replaceAll('\'', "\'\'")}`;
}
/**
* Gets the number of likes on a message.
* @param {Object} element Messages to check likes on
* @returns {int} Number of likes
*/
function getLikes(element) {
if (!element.reactions) {
return 0;
}
return element.reactions[0].user_ids.length;
}
/**
* Registers messages in the database if they are not already registered.
* @param {Object[]} messages - An array of message objects to be added to DB
* @returns {Promise<void>} - A promise that resolves when the messages are registered.
*/
async function registerMessages(messages) {
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
})
let query = ""
query += `BEGIN;`
query += `DELETE FROM messages WHERE conv_id = ${messages[0].group_id};`
for (let i = 0; i < messages.length; i++) {
let element = messages[i];
if (isNaN(element.sender_id)) {
continue;
}
//Aprox 30 messages per second if you send each query here
query += (`INSERT INTO messages (msg_id, conv_id, sender_id, text, time_sent, num_likes, attachments) VALUES (${element.id}, ${element.group_id}, ${element.sender_id}, ${sanitizeString(element.text)}, ${element.created_at}, ${getLikes(element)}, \'${JSON.stringify(element.attachments)}\');`)
}
query += `COMMIT;`
await client.connect()
await client.query(query)
await client.end()
}
/**
* Registers a user in the Chatters table if they are not already registered.
* @param {Object} user - The user object to be added to the DB.
*/
async function registerChatters(user) {
try {
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
})
await client.connect()
const isReg = await client.query(`SELECT * FROM chatters WHERE id = \'${user.id}\';`)
if (isReg.rowCount == 0) {
await client.query(`INSERT INTO chatters (name, id) VALUES (\'${user.name}\', ${user.id});`)
}else {
await client.end();
}
} catch (error) {
console.log(error)
}
}
/**
* Marks a group as loaded in the database.
* @param {Object} group
*/
async function markGroupLoaded(group) {
try {
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
})
await client.connect()
const isReg = await client.query(`
SELECT * FROM conversations
WHERE conv_id = ${group} and loaded = true;`)
if (isReg.rowCount == 0) {
await client.query(`
UPDATE conversations
SET loaded = true
WHERE conv_id = ${group};`)
}
await client.end();
} catch (error) {
console.log(error)
}
}
/**
* Adds Group information to conversations table if it is not already registered.
* @param {groups} groups
*/
async function registerConversations(groups) {
try {
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
})
await client.connect()
for (let i = 0; i < groups.length; i++) {
let group = groups[i]
const isReg = await client.query(`SELECT * FROM conversations WHERE conv_id = ${group.id};`)
if (isReg.rowCount == 0) {
await client.query(`
INSERT INTO conversations (conv_id, name, msg_count, loaded)
VALUES (${group.id}, ${sanitizeString(group.name)}, ${group.messages.count}, false);`)
}
}
await client.end();
} catch (error) {
console.log(error)
}
}
async function pruneGroupsToLoad(groupids) {
let query = `SELECT c.conv_id FROM conversations c WHERE c.loaded = true;`;
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
});
await client.connect();
const response = await client.query(query);
await client.end();
//if a groupid appears in response remove it from the list
for (let i = 0; i < response.rows.length; i++) {
let group = response.rows[i];
let index = groupids.indexOf(group.conv_id.toString());
if (index > -1) {
groupids.splice(index, 1);
}
}
return groupids;
}
/**
* Registers user groups in the database if they are not already registered.
* @param {number} id - The user id.
* @param {Object[]} groups - An array of group objects containing group information.
* @returns {Promise<void>} - A promise that resolves when the user groups are registered.
*/
async function registerMemberOf(groups) {
try {
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
})
let query = ""
await client.connect()
for (let i = 0; i < groups.length; i++) {
let group = groups[i]
for (let j = 0; j < group.members_count; j++) {
let member = group.members[j]
await client.query(`SELECT 1 FROM member WHERE userid = ${parseInt(member.user_id)} and groupid = ${parseInt(group.id)};`).then(async (x) => {
if (x.rowCount == 0) {
console.log("Added Group " + group.name + " to user " + member.nickname)
try {
await client.query(`INSERT INTO member (userid, memid, groupid, nickname) VALUES (${member.user_id}, ${member.id} ,${group.id}, ${sanitizeString(member.nickname)});`)
} catch (error) {
console.log(error)
}
} else {
console.log("Group " + group.name + " already added to user " + member.nickname)
}
})
}
}
await client.end()
} catch (error) {
console.log(error)
}
}
const app = express();
app.use(cors());
/**
* Endpoint for user login.
* @name GET /api/login
* @function
* @async
* @param {Object} req - The request object.
* @param {Object} res - The response object.
*/
app.get("/api/login", awaitAsync(async (req, res) => {
const TOKEN = req.query["access_token"];
let user = await getUserFromAPI(TOKEN);
let convs = await getGroupsFromAPI(TOKEN)
await registerChatters(user)
await registerConversations(convs)
await registerMemberOf(convs)
//Return 200
res.status(200).send("Success")
}));
app.get("/api/loadmessages", awaitAsync(async (req, res) => {
const TOKEN = req.query["access_token"];
let groupids = req.query["groupids"].split(",");
let messages = []
groupids = await pruneGroupsToLoad(groupids);
for (let i = 0; i < groupids.length; i++) {
const group = await groupids[i]
console.log("Loading messages from group " + group + "...")
messages = await getMessagesFromAPI(TOKEN, group);
console.log("Registering Messages")
await registerMessages(messages)
markGroupLoaded(group)
}
console.log("Done")
res.status(200).send(`Added ${messages.length} to DB`)
}));
app.get("/api/getgroups", awaitAsync(async (req, res) => {
const TOKEN = req.query["access_token"];
let rawGroups = await getGroupsFromAPI(TOKEN)
let groups = []
rawGroups.forEach(group => {
groups.push({ id: group.id, name: group.name })
});
res.status(200).send(groups)
}));
app.get("/api/getloadedgroups", awaitAsync(async (req, res) => {
const TOKEN = req.query["access_token"];
const user = await getUserFromAPI(TOKEN);
const userID = user.id;
const query = `
SELECT * FROM conversations c
JOIN member m ON c.conv_id = m.groupid
WHERE c.loaded = true and m.userid = ${parseInt(userID)};`
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
})
await client.connect()
const response = await client.query(query)
await client.end()
res.status(200).send(response.rows)
}));
app.get("/api/avgLikes", awaitAsync(async (req, res) => {
const groupID = req.query["groupID"];
const query = `
SELECT m2.nickname as name, AVG(m.num_likes) as value
FROM messages m
JOIN member m2 ON m.sender_id = m2.userid and m.conv_id = m2.groupid
WHERE m.conv_id = ${parseInt(groupID)}
group by m2.nickname
order by AVG(m.num_likes) desc;`;
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
})
await client.connect()
const response = await client.query(query)
await client.end()
res.status(200).send(response.rows)
}));
app.get("/api/totalMessages", awaitAsync(async (req, res) => {
const groupID = req.query["groupID"];
const query = `
SELECT m2.nickname as name, COUNT(*) as value
FROM messages m
JOIN member m2 ON m.sender_id = m2.userid and m.conv_id = m2.groupid
WHERE m.conv_id = ${parseInt(groupID)}
group by m2.nickname
order by COUNT(*) desc;`;
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
})
await client.connect()
const response = await client.query(query)
await client.end()
res.status(200).send(response.rows)
}));
app.get("/api/totalLikes", awaitAsync(async (req, res) => {
const groupID = req.query["groupID"];
const query = `
SELECT m2.nickname as name, SUM(m.num_likes) as value
FROM messages m
JOIN member m2 ON m.sender_id = m2.userid and m.conv_id = m2.groupid
WHERE m.conv_id = ${parseInt(groupID)}
group by m2.nickname
order by SUM(m.num_likes) desc;`;
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
})
await client.connect()
const response = await client.query(query)
await client.end()
res.status(200).send(response.rows)
}));
app.get("/api/mostLiked", awaitAsync(async (req, res) => {
const groupID = req.query["groupID"];
const query = `
SELECT m2.nickname as name, m.num_likes as likes, m."text" as msg, m.attachments as atch
FROM messages m
JOIN member m2 ON m.sender_id = m2.userid and m.conv_id = m2.groupid
WHERE m.conv_id = ${parseInt(groupID)}
and m.num_likes = (SELECT MAX(m.num_likes) FROM messages m where m.conv_id = ${parseInt(groupID)})`;
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
})
await client.connect()
const response = await client.query(query)
await client.end()
res.status(200).send(response.rows)
}));
app.get("/api/random", awaitAsync(async (req, res) => {
const groupID = req.query["groupID"];
const query = `
SELECT m2.nickname as name, m.num_likes as likes, m."text" as msg, m.attachments as atch
FROM messages m
JOIN member m2 ON m.sender_id = m2.userid and m.conv_id = m2.groupid
WHERE m.conv_id = ${parseInt(groupID)}
ORDER BY random()
LIMIT 1;`;
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
})
await client.connect()
const response = await client.query(query)
await client.end()
res.status(200).send(response.rows)
}));
app.get("/api/custom", awaitAsync(async (req, res) => {
const groupID = parseInt(req.query["groupID"]);
const likes = parseInt(req.query["likes"]) || 0;
const likesValue = parseInt(req.query["likesValue"]) || 0;
const fromUser = parseInt(req.query["fromUser"]) || 0;
const fromUserValue = req.query['fromUserValue'] || "";
let likesQ = ""
let fromUserQ = ""
let likesSign = ""
if (likes > 0) {
switch (likes) {
case 1:
likesSign = ">"
break;
case 2:
likesSign = "<"
break;
case 3:
likesSign = "="
break;
case 4:
likesSign = ">="
break;
case 5:
likesSign = "<="
break;
}
likesQ = `and m.num_likes ${likesSign} ${likesValue}`
}
if (fromUser > 0) {
fromUserQ = `and m2.nickname like \'%${sanitizeNoWrapString(fromUserValue)}%\'`
}
const query = `SELECT m2.nickname as name, m.num_likes as likes, m.text as msg, m.attachments as atch FROM messages m JOIN member m2 ON m.sender_id = m2.userid and m.conv_id = m2.groupid WHERE m.conv_id = ${parseInt(groupID)} ${likesQ} ${fromUserQ} LIMIT 20;`;
const client = new Client({
user: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
database: 'groupme'
})
await client.connect()
const response = await client.query(query)
await client.end()
console.log(response.rows)
res.status(200).send(response.rows)
}));
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});