-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotifications.js
118 lines (98 loc) · 3.08 KB
/
notifications.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
var Notifications = (function() {
var Redis = require('ioredis');
var redis = new Redis();
var exports = {
register: registerEvent,
fetch: getEmployeeNotifications,
delete: deleteNotification
};
return exports;
function checkIfEventIsNewEmployee(event) {
return event === "App\\Events\\EmpleadoCreado";
}
function createNewList(message) {
var parsed_message = JSON.parse(message);
return new Promise(function(resolve, reject) {
if (checkIfEventIsNewEmployee(parsed_message.event)) {
var list_name = parsed_message.data.payload.extra.user + '-notifications';
redis.lpush(list_name, message).then(function(){
resolve(true);
});
} else {
reject(false);
}
});
}
function getListLengthOf(list) {
return redis.llen(list);
}
function iterateAndPush(message, lambda) {
return new Promise(function(resolve, reject) {
var parsed_message = JSON.parse(message);
var stream = redis.scanStream({match: '*-notifications'});
stream.on('data', function (lists) {
lambda(message, lists, parsed_message);
});
stream.on('end', function() {
resolve(true);
});
});
}
function updateAllExceptNewEmployee(message) {
function proc(message, lists, parsed_message) {
var new_employee_list = parsed_message.data.payload.extra.user + '-notifications';
var length = lists.length;
for (var i = 0; i < length; i++) {
if (lists[i] === new_employee_list) {
continue;
}
redis.lpush(lists[i], message);
}
}
iterateAndPush(message, proc);
}
function updateListsWithNewNotification(message) {
function proc(message, lists) {
var length = lists.length;
for (var i = 0; i < length; i++) {
redis.lpush(lists[i], message);
}
}
iterateAndPush(message, proc);
}
function informAllClients(io, channel, message) {
message = JSON.parse(message);
console.log("Emitting on (%s) -> %s", channel, message.event);
io.emit(channel, message);
}
function registerEvent(io, channel, message) {
createNewList(message).then(function() {
updateAllExceptNewEmployee(message);
}).catch(function(){
updateListsWithNewNotification(message);
});
informAllClients(io, channel, message);
}
function getLatestNotificationsFrom(list) {
return redis.lrange(list, 0, 9);
}
function getEmployeeNotifications(socket, employee) {
var user = employee.usuario;
var list = user + '-notifications';
getLatestNotificationsFrom(list).then(function (data) {
for (var i=data.length - 1; i >= 0; i--) {
var message = JSON.parse(data[i]);
message.data.payload.redis = { 'index': i};
socket.emit(message.data.payload.channel, message);
}
});
}
function deleteNotification(socket, payload) {
var list = payload.user + '-notifications';
var index = payload.index;
redis.lindex(list, index).then(function (element) {
redis.lrem(list, 1, element);
});
}
})();
module.exports = Notifications;