-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.js
119 lines (95 loc) · 3.12 KB
/
routes.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
const express = require('express');
const redisClient = require('./redis');
const router = express.Router();
/**
* @description This route retirves the static channels
*/
router.get('/getChannels', async (req, res) => {
const channels = await redisClient.smembers('channels');
const response = channels.map((item, index) => ({
name: item,
participants: 0,
id: index + 1,
}));
const usernames = await redisClient.smembers('usernames');
let userRequests = [];
for (let user of usernames) {
userRequests.push(await redisClient.hgetall(`user:${user}`));
}
const users = await Promise.all(userRequests);
for (let item of users) {
if (item?.channel) {
const index = response.findIndex((chan) => chan.name === item.channel);
if (index > -1) response[index].participants += 1;
else response[index] = { participants: 1 };
}
}
res.json({
channels: response,
});
});
/**
* @description This route retirves the channel messages
*/
router.get('/getMessages/:user/:channel', async (req, res) => {
const { channel, user } = req.params;
const messageIds = await redisClient.xrange(`channel:${channel}`, '-', '+');
// Create promise chain to retreive all messages by messageId
const messageRequests = [];
for (let msg of messageIds) {
messageRequests.push(await redisClient.hgetall(`message:${msg[0]}`));
}
const messages = await Promise.all(messageRequests);
const allMessages = messages.map((item, index) => ({
...item,
timestamp: new Date(parseInt(messageIds[index][0])),
id: messageIds[index][0],
}));
// Extract last message of the current channel
const lastChannelMessage = allMessages[allMessages.length - 1];
// Retrieve user last seen message from redis
const userData = await redisClient.hgetall(`user:${user}`);
// Match last seen message with allMessages array
const lastMessageIndex = allMessages.findIndex((msg) => msg.id === userData[`lastMessageId:${channel}`]);
// If the last seen message is not the last message of channel then push 'new messages' message
if (lastMessageIndex > -1 && lastMessageIndex !== allMessages.length - 1) {
allMessages.splice(lastMessageIndex + 1, 0, {
message: '--- New Messages ---',
});
}
// Store last message Id in user redis object
await redisClient.hset(`user:${user}`, {
[`lastMessageId:${channel}`]: lastChannelMessage?.id,
});
res.json({ messages: allMessages });
});
/**
* @description This route is used for search functionality
*/
router.get('/search', async (req, res) => {
const { query, channel } = req.query;
const response = await redisClient.call(
'FT.SEARCH',
'idx:messages',
`@message:${query} @channel:{${channel}}`,
'LIMIT',
'0',
'999999'
);
res.json({ response });
});
/**
* @description This route add username to redis
*/
router.post('/add-user', async (req, res) => {
const { username } = req.body;
// Add a new user
const user = {
username,
channel: 'random',
};
await redisClient.sadd('usernames', username);
await redisClient.hset(`user:${username}`, user);
res.json({ user });
});
module.exports = router;