This repository has been archived by the owner on May 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.ts
91 lines (77 loc) · 1.67 KB
/
users.ts
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
/*
Add user to room
----
socketID: the socket id
userName: the name of the user
roomID: the id of the room
RoomDB: the db
token: the user token
id: the id of the user
*/
let addUser = ({ socketID, userName, roomID, RoomDB, token, id }) => {
// Finds the room
RoomDB.findById(roomID)
.catch((err) => console.log(err))
.then((result) => {
// Delets the filler iser
delete result.users["null"]
// Makes a new var and adds it to it
let users = {...result.users, [token]: {name: userName, id: id, socket: socketID}}
// Updates the new room
RoomDB.findByIdAndUpdate(roomID, {"users": users})
.catch((err) => console.log(err))
})
};
/*
Removes a user from a room
----
roomID: the id of the room
RoomDB: the db
token: the token of the user
*/
let removeUser = ({roomID, RoomDB, token }) => {
// Finds the room
RoomDB.findById(roomID)
.catch(() => console.log("error"))
.then((result) => {
// sets the var
let users = result.users
// Deletes the user
delete users.token
// Updates the room
RoomDB.findByIdAndUpdate(roomID, {"users": users})
.then(result => console.log(result))
.catch(() => console.log("error"))
})
};
/*
Make Room
----
RoomName: The name of the room
RoomDB: The db
type: The type of the room
roomID: the id of the room
token: the user token
*/
let makeRoom = ({
RoomName,
RoomDB,
type,
roomID,
token
}) => {
// Makes a new docs
let room = new RoomDB({
_id: roomID,
name: RoomName,
users: {null: null},
owner: token,
messages: [1],
type: "chat",
slowmode: 1
});
// Saves the db
room.save()
};
// exports all funcs
module.exports = { addUser, removeUser, makeRoom };