mirror of
https://github.com/Discreetly/server.git
synced 2026-05-09 03:00:03 -04:00
refactor(gateways): findRoomsByIdentity refactored for gateways
This commit is contained in:
@@ -11,40 +11,40 @@ datasource db {
|
||||
}
|
||||
|
||||
model Rooms {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
roomId String @unique
|
||||
name String
|
||||
rateLimit Int @default(10000) // epoch length in ms
|
||||
banRateLimit Int @default(1000000) // starting number of epochs banned for
|
||||
userMessageLimit Int @default(12) // per epoch
|
||||
membershipType String @default("IDENTITY_LIST")
|
||||
adminIdentities String[] @default([])
|
||||
identities String[] @default([])
|
||||
contractAddress String? // RLN_CONTRACT as "chainID:0xADDRESS"
|
||||
bandadaAddress String? // BANDADA as "url:groupID"
|
||||
bandadaGroupId String? // Bandada Group ID
|
||||
bandadaAPIKey String? // Bandada API Key
|
||||
epochs Epoch[]
|
||||
messages Messages[]
|
||||
claimCodes ClaimCodes[] @relation(fields: [claimCodeIds], references: [id])
|
||||
claimCodeIds String[] @default([]) @db.ObjectId
|
||||
type String @default("PUBLIC")
|
||||
ephemeral String @default("PERSISTENT")
|
||||
encrypted String @default("PLAINTEXT")
|
||||
gatewayIds String[] @default([]) @db.ObjectId
|
||||
gateways GateWayIdentity[] @relation(fields: [gatewayIds], references: [id])
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
roomId String @unique
|
||||
name String
|
||||
rateLimit Int @default(10000) // epoch length in ms
|
||||
banRateLimit Int @default(1000000) // starting number of epochs banned for
|
||||
userMessageLimit Int @default(12) // per epoch
|
||||
membershipType String @default("IDENTITY_LIST")
|
||||
adminIdentities String[] @default([])
|
||||
identities String[] @default([])
|
||||
contractAddress String? // RLN_CONTRACT as "chainID:0xADDRESS"
|
||||
bandadaAddress String? // BANDADA as "url:groupID"
|
||||
bandadaGroupId String? // Bandada Group ID
|
||||
bandadaAPIKey String? // Bandada API Key
|
||||
epochs Epoch[]
|
||||
messages Messages[]
|
||||
claimCodes ClaimCodes[] @relation(fields: [claimCodeIds], references: [id])
|
||||
claimCodeIds String[] @default([]) @db.ObjectId
|
||||
type String @default("PUBLIC")
|
||||
ephemeral String @default("PERSISTENT")
|
||||
encrypted String @default("PLAINTEXT")
|
||||
gatewayIds String[] @default([]) @db.ObjectId
|
||||
gateways GateWayIdentity[] @relation(fields: [gatewayIds], references: [id])
|
||||
}
|
||||
|
||||
model GateWayIdentity {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
semaphoreIdentity String
|
||||
discordId String
|
||||
jubmojii String[]
|
||||
steamId64 String
|
||||
discordId String
|
||||
jubmojii String[]
|
||||
steamId64 String
|
||||
ethereumAddresses String[]
|
||||
roomIds String[] @default([]) @db.ObjectId
|
||||
rooms Rooms[] @relation(fields: [roomIds], references: [id])
|
||||
usedClaimCodes String[]
|
||||
roomIds String[] @default([]) @db.ObjectId
|
||||
rooms Rooms[] @relation(fields: [roomIds], references: [id])
|
||||
usedClaimCodes String[]
|
||||
}
|
||||
|
||||
model ClaimCodes {
|
||||
@@ -77,16 +77,15 @@ model Epoch {
|
||||
}
|
||||
|
||||
model Discord {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
discordId String @unique
|
||||
admins String[] @default([])
|
||||
roomsMapping RoleRoomMapping[]
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
discordServerId String @unique
|
||||
roomsMapping DiscordRoleRoomMapping[]
|
||||
}
|
||||
|
||||
model RoleRoomMapping {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
discordId String
|
||||
roomId String @unique
|
||||
roles String[]
|
||||
discord Discord @relation(fields: [discordId], references: [discordId])
|
||||
model DiscordRoleRoomMapping {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
discordServerId String
|
||||
roomId String @unique
|
||||
roles String[]
|
||||
discord Discord @relation(fields: [discordServerId], references: [discordServerId])
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ const prisma = new PrismaClient();
|
||||
* @param {string} [bandadaAPIKey] - The API key for the bandada server.
|
||||
* @param {string} [membershipType] - The membership type of the room.
|
||||
* @param {string} [roomId] - The ID of the room to create.
|
||||
* @param {string[]} [discordIds=[]] - The discord IDs of the users in the room.
|
||||
* @returns {Promise<boolean>} - A promise that resolves to true if the room was created successfully.
|
||||
*/
|
||||
export async function createRoom(
|
||||
@@ -34,8 +33,7 @@ export async function createRoom(
|
||||
bandadaGroupId?: string,
|
||||
bandadaAPIKey?: string,
|
||||
membershipType?: string,
|
||||
roomId?: string,
|
||||
discordIds: string[] = []
|
||||
roomId?: string
|
||||
): Promise<{ roomId: string ; claimCodes: { claimcode: string }[] } | undefined | null> {
|
||||
const claimCodes: { claimcode: string }[] = genClaimCodeArray(numClaimCodes);
|
||||
const mockUsers: string[] = genMockUsers(approxNumMockUsers);
|
||||
@@ -58,19 +56,25 @@ export async function createRoom(
|
||||
banRateLimit: rateLimit,
|
||||
userMessageLimit: userMessageLimit,
|
||||
adminIdentities: adminIdentities,
|
||||
semaphoreIdentities: mockUsers,
|
||||
identities: identityCommitments,
|
||||
bandadaAddress,
|
||||
bandadaGroupId,
|
||||
bandadaAPIKey,
|
||||
membershipType,
|
||||
type,
|
||||
discordIds,
|
||||
claimCodes: {
|
||||
create: claimCodes
|
||||
},
|
||||
gateways: {
|
||||
create: mockUsers.map((user) => ({
|
||||
semaphoreIdentity: user,
|
||||
discordId: '',
|
||||
steamId64: ''
|
||||
}))
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return await prisma.rooms
|
||||
.upsert(roomData)
|
||||
.then(() => {
|
||||
|
||||
@@ -60,16 +60,20 @@ https://github.com/Discreetly/IdentityCommitmentNullifierCircuit <- Circuit and
|
||||
export async function findRoomsByIdentity(identity: string): Promise<string[]> {
|
||||
const r: string[] = [];
|
||||
try {
|
||||
const rooms = await prisma.rooms.findMany({
|
||||
const gateway = await prisma.gateWayIdentity.findFirst({
|
||||
where: {
|
||||
semaphoreIdentities: {
|
||||
has: identity
|
||||
}
|
||||
semaphoreIdentity: identity
|
||||
},
|
||||
include: {
|
||||
rooms: true
|
||||
}
|
||||
});
|
||||
rooms.forEach((room) => {
|
||||
if (!gateway) {
|
||||
return [];
|
||||
}
|
||||
gateway.rooms.forEach((room) => {
|
||||
r.push(room.roomId);
|
||||
});
|
||||
})
|
||||
return r;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
@@ -13,10 +13,10 @@ export function removeIdentityFromRoom(
|
||||
idc: string,
|
||||
room: RoomI
|
||||
): Promise<void | RoomI> {
|
||||
const updateSemaphoreIdentities =
|
||||
room.semaphoreIdentities?.map((identity) =>
|
||||
identity === idc ? '0' : (identity as string)
|
||||
) ?? [];
|
||||
// const updateSemaphoreIdentities =
|
||||
// room.semaphoreIdentities?.map((identity) =>
|
||||
// identity === idc ? '0' : (identity as string)
|
||||
// ) ?? [];
|
||||
|
||||
const rateCommitmentsToUpdate = getRateCommitmentHash(
|
||||
BigInt(idc),
|
||||
@@ -33,7 +33,7 @@ export function removeIdentityFromRoom(
|
||||
where: { id: room.id },
|
||||
data: {
|
||||
identities: updatedRateCommitments,
|
||||
semaphoreIdentities: updateSemaphoreIdentities
|
||||
|
||||
}
|
||||
})
|
||||
.then((room) => {
|
||||
|
||||
@@ -96,7 +96,7 @@ export async function addIdentityToIdentityListRooms(
|
||||
BigInt(room.userMessageLimit! ?? 1)
|
||||
).toString()
|
||||
},
|
||||
semaphoreIdentities: { push: identityCommitment }
|
||||
|
||||
}
|
||||
});
|
||||
console.debug(`Successfully added user to Identity List room ${room.roomId}`);
|
||||
@@ -162,7 +162,7 @@ export async function addIdentityToBandadaRooms(
|
||||
identities: {
|
||||
push: rateCommitment
|
||||
},
|
||||
semaphoreIdentities: { push: identityCommitment }
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -66,7 +66,6 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
userMessageLimit,
|
||||
membershipType,
|
||||
identities,
|
||||
semaphoreIdentities,
|
||||
bandadaAddress,
|
||||
bandadaGroupId
|
||||
} = room || {};
|
||||
@@ -83,11 +82,11 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
if (membershipType === 'BANDADA_GROUP') {
|
||||
roomResult.bandadaAddress = bandadaAddress;
|
||||
roomResult.bandadaGroupId = bandadaGroupId;
|
||||
roomResult.semaphoreIdentities = semaphoreIdentities;
|
||||
|
||||
}
|
||||
if (membershipType === 'IDENTITY_LIST') {
|
||||
roomResult.identities = identities;
|
||||
roomResult.semaphoreIdentities = semaphoreIdentities;
|
||||
|
||||
}
|
||||
|
||||
res.status(200).json(roomResult);
|
||||
@@ -253,7 +252,6 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
const bandadaAPIKey = roomMetadata.bandadaAPIKey;
|
||||
const membershipType = roomMetadata.membershipType;
|
||||
const roomId = roomMetadata.roomId;
|
||||
const discordIds = roomMetadata.discordIds;
|
||||
createRoom(
|
||||
roomName,
|
||||
rateLimit,
|
||||
@@ -267,7 +265,6 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
bandadaAPIKey,
|
||||
membershipType,
|
||||
roomId,
|
||||
discordIds
|
||||
)
|
||||
.then((result) => {
|
||||
const response =
|
||||
@@ -663,11 +660,11 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
prisma.discord
|
||||
.upsert({
|
||||
where: {
|
||||
discordId: guildId
|
||||
discordServerId: guildId
|
||||
},
|
||||
update: {},
|
||||
create: {
|
||||
discordId: guildId
|
||||
discordServerId: guildId
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
@@ -699,7 +696,7 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
res.status(400).json({ error: 'Bad Request' });
|
||||
return;
|
||||
}
|
||||
prisma.roleRoomMapping
|
||||
prisma.discordRoleRoomMapping
|
||||
.upsert({
|
||||
where: {
|
||||
roomId: roomId
|
||||
@@ -711,7 +708,7 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
},
|
||||
create: {
|
||||
roomId: roomId,
|
||||
discordId: guildId,
|
||||
discordServerId: guildId,
|
||||
roles: {
|
||||
set: roles
|
||||
}
|
||||
@@ -743,7 +740,7 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
res.status(400).json({ error: 'Bad Request' });
|
||||
return;
|
||||
}
|
||||
prisma.roleRoomMapping
|
||||
prisma.discordRoleRoomMapping
|
||||
.findMany({
|
||||
where: {
|
||||
roles: {
|
||||
@@ -797,10 +794,10 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
|
||||
|
||||
app.post('/api/discord/checkrooms', adminAuth, (req, res) => {
|
||||
const { discordId } = req.body as { discordId: string };
|
||||
prisma.roleRoomMapping
|
||||
prisma.discordRoleRoomMapping
|
||||
.findMany({
|
||||
where: {
|
||||
discordId: discordId
|
||||
discordServerId: discordId
|
||||
}
|
||||
})
|
||||
.then((rooms) => {
|
||||
|
||||
Reference in New Issue
Block a user