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
57import { ajv, logger, userRoom, channelRoom } from "../util.js";
const validate = ajv.compile({
type: "object",
properties: {
userIds: {
type: "array",
items: { type: "string", format: "uuid" },
minItems: 1,
maxItems: 1,
},
},
required: ["userIds"],
additionalProperties: false,
});
export function reachUser({ io, socket, db }) {
return async (payload, callback) => {
if (typeof callback !== "function") {
return;
}
if (!validate(payload)) {
return callback({
status: "ERROR",
errors: validate.errors,
});
}
let channel;
try {
channel = await db.createPrivateChannel(socket.userId, payload.userIds);
} catch (e) {
return callback({
status: "ERROR",
});
}
logger.info(
"private channel [%s] was created by user [%s]",
channel.id,
socket.userId,
);
// broadcast to other tabs of the same user
socket.to(userRoom(socket.userId)).emit("channel:created", channel);
io.in(userRoom(socket.userId)).socketsJoin(channelRoom(channel.id));
callback({
status: "OK",
data: channel,
});
};
}