-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove.ts
More file actions
99 lines (93 loc) · 2.79 KB
/
Copy pathremove.ts
File metadata and controls
99 lines (93 loc) · 2.79 KB
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
import { PrismaClient } from '@prisma/client';
import { RoomI, getRateCommitmentHash } from 'discreetly-interfaces';
const prisma = new PrismaClient();
/**
* This function takes in an identity and a room and removes the identity from the room
* by setting its semaphoreIdentities to 0n and identities to 0n
* @param {string} idc - The identity of the user
* @param {RoomI} room - The room to remove the identity from
* @returns {Promise<void | RoomI>} - A promise that resolves to the room
*/
export function removeIdentityFromRoom(
idc: string,
room: RoomI
): Promise<void | RoomI> {
const rateCommitmentsToUpdate = getRateCommitmentHash(
BigInt(idc),
BigInt(room.userMessageLimit!)
).toString();
const updatedRateCommitments =
room.identities?.map((limiter) =>
limiter == rateCommitmentsToUpdate ? '0' : (limiter as string)
) ?? [];
return prisma.rooms
.update({
where: { id: room.id },
data: {
identities: updatedRateCommitments,
gateways: {
disconnect: {
semaphoreIdentity: idc
}
}
}
})
.then((room) => {
return room as RoomI;
})
.catch((err) => {
console.error(err);
});
}
/**
* This code removes a room from the database. It also removes any messages associated with that room.
* @param {string} roomId - The id of the room to remove
* @returns {Promise<boolean>} - A promise that resolves to true if the room was removed and false otherwise
* */
export function removeRoom(roomId: string): Promise<boolean> {
return prisma.messages
.deleteMany({
where: {
roomId: roomId
}
})
.then(() => {
return prisma.rooms
.delete({
where: {
roomId: roomId
}
})
.then(() => true)
.catch((err) => {
console.error(err);
return false;
});
})
.catch((err) => {
console.error(err);
return false;
});
}
/**
* This function removes a message from the database. It takes in a roomId and a messageId, and uses them to find the message in the database. It then deletes the message from the database and returns true if the message was successfully deleted. If there is an error, it will return false.
* @param {string} roomId - The id of the room the message is in
* @param {string} messageId - The id of the message to remove
* @returns {Promise<boolean>} - A promise that resolves to true if the message was removed and false otherwise
*/
export function removeMessage(roomId: string, messageId: string) {
return prisma.messages
.deleteMany({
where: {
roomId: roomId,
messageId: messageId
}
})
.then(() => {
return true;
})
.catch((err) => {
console.error(err);
return false;
});
}