-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.js
150 lines (124 loc) · 3.73 KB
/
handlers.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
const WEBSITE_ROOM = "website";
const SITE_ROOM = "all";
function saashq_handlers(socket) {
socket.join(user_room(socket.user));
socket.join(WEBSITE_ROOM);
if (socket.user_type == "System User") {
socket.join(SITE_ROOM);
}
socket.has_permission = (doctype, name) => {
return new Promise((resolve) => {
socket
.saashq_request("/api/method/saashq.realtime.has_permission", {
doctype,
name: name || "",
})
.then((res) => res.json())
.then(({ message }) => {
if (message) {
resolve();
}
})
.catch((err) => console.log("Can't check permissions", err));
});
};
socket.on("ping", () => {
socket.emit("pong");
});
socket.on("doctype_subscribe", function (doctype) {
socket.has_permission(doctype).then(() => {
socket.join(doctype_room(doctype));
});
});
socket.on("doctype_unsubscribe", function (doctype) {
socket.leave(doctype_room(doctype));
});
socket.on("task_subscribe", function (task_id) {
const room = task_room(task_id);
socket.join(room);
});
socket.on("task_unsubscribe", function (task_id) {
const room = task_room(task_id);
socket.leave(room);
});
socket.on("progress_subscribe", function (task_id) {
const room = task_room(task_id);
socket.join(room);
});
socket.on("doc_subscribe", function (doctype, docname) {
socket.has_permission(doctype, docname).then(() => {
socket.join(doc_room(doctype, docname));
});
});
socket.on("doc_unsubscribe", function (doctype, docname) {
let room = doc_room(doctype, docname);
socket.leave(room);
});
socket.on("doc_open", function (doctype, docname) {
socket.has_permission(doctype, docname).then(() => {
let room = open_doc_room(doctype, docname);
socket.join(room);
if (!socket.subscribed_documents) socket.subscribed_documents = [];
socket.subscribed_documents.push([doctype, docname]);
// show who is currently viewing the form
notify_subscribed_doc_users({
socket: socket,
doctype: doctype,
docname: docname,
});
});
});
socket.on("doc_close", function (doctype, docname) {
// remove this user from the list of 'who is currently viewing the form'
let room = open_doc_room(doctype, docname);
socket.leave(room);
if (socket.subscribed_documents) {
socket.subscribed_documents = socket.subscribed_documents.filter(([dt, dn]) => {
!(dt == doctype && dn == docname);
});
}
notify_subscribed_doc_users({
socket: socket,
doctype: doctype,
docname: docname,
});
});
socket.on("disconnect", () => {
notify_disconnected_documents(socket);
});
}
function notify_disconnected_documents(socket) {
if (socket.subscribed_documents) {
socket.subscribed_documents.forEach(([doctype, docname]) => {
notify_subscribed_doc_users({ socket, doctype, docname });
});
}
}
function notify_subscribed_doc_users(args) {
if (!(args && args.doctype && args.docname)) {
return;
}
const socket = args.socket;
const room = open_doc_room(args.doctype, args.docname);
const clients = Array.from(socket.nsp.adapter.rooms.get(room) || []);
let users = [];
socket.nsp.sockets.forEach((sock) => {
if (clients.includes(sock.id)) {
users.push(sock.user);
}
});
// dont send update to self. meaningless.
if (users.length == 1 && users[0] == args.socket.user) return;
// notify
socket.nsp.to(room).emit("doc_viewers", {
doctype: args.doctype,
docname: args.docname,
users: Array.from(new Set(users)),
});
}
const doc_room = (doctype, docname) => "doc:" + doctype + "/" + docname;
const open_doc_room = (doctype, docname) => "open_doc:" + doctype + "/" + docname;
const user_room = (user) => "user:" + user;
const doctype_room = (doctype) => "doctype:" + doctype;
const task_room = (task_id) => "task_progress:" + task_id;
module.exports = saashq_handlers;