From e8d7a533b2c4bd99fb943f934f87ae46649afb34 Mon Sep 17 00:00:00 2001 From: Tanner Shaw Date: Fri, 13 Oct 2023 15:56:43 -0500 Subject: [PATCH] refactor(gateways): findRoomsByIdentity refactored for gateways --- prisma/schema.prisma | 77 +++++++++++++++++++++--------------------- src/data/db/create.ts | 14 +++++--- src/data/db/find.ts | 16 +++++---- src/data/db/remove.ts | 10 +++--- src/data/db/update.ts | 4 +-- src/endpoints/index.ts | 21 +++++------- 6 files changed, 73 insertions(+), 69 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7bd9ed0..a7451c6 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -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]) } diff --git a/src/data/db/create.ts b/src/data/db/create.ts index 4ee4a0b..0d912ae 100644 --- a/src/data/db/create.ts +++ b/src/data/db/create.ts @@ -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} - 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(() => { diff --git a/src/data/db/find.ts b/src/data/db/find.ts index de68786..82b12cf 100644 --- a/src/data/db/find.ts +++ b/src/data/db/find.ts @@ -60,16 +60,20 @@ https://github.com/Discreetly/IdentityCommitmentNullifierCircuit <- Circuit and export async function findRoomsByIdentity(identity: string): Promise { 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); diff --git a/src/data/db/remove.ts b/src/data/db/remove.ts index 4cf999d..9e1e2ac 100644 --- a/src/data/db/remove.ts +++ b/src/data/db/remove.ts @@ -13,10 +13,10 @@ export function removeIdentityFromRoom( idc: string, room: RoomI ): Promise { - 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) => { diff --git a/src/data/db/update.ts b/src/data/db/update.ts index 462b6e6..66cbdfa 100644 --- a/src/data/db/update.ts +++ b/src/data/db/update.ts @@ -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 } + } }); diff --git a/src/endpoints/index.ts b/src/endpoints/index.ts index e478a6a..2076b87 100644 --- a/src/endpoints/index.ts +++ b/src/endpoints/index.ts @@ -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) => {