From ef104a7595478e55ae54b20af6642afc7903e07d Mon Sep 17 00:00:00 2001 From: Tanner Shaw Date: Tue, 5 Sep 2023 23:30:12 -0500 Subject: [PATCH] small changes --- src/data/db.ts | 35 +++++++++++++++++++++-------------- src/endpoints/index.ts | 1 + src/websockets/index.ts | 30 +++++++++++++++--------------- tests/express.test.ts | 2 +- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/data/db.ts b/src/data/db.ts index 0312c24..ea5114a 100644 --- a/src/data/db.ts +++ b/src/data/db.ts @@ -7,10 +7,10 @@ import type { RoomI } from 'discreetly-interfaces'; import { serverConfig } from '../config/serverConfig'; import { genMockUsers, genClaimCodeArray, pp } from '../utils'; import getRateCommitmentHash from '../crypto/rateCommitmentHasher'; - +import { Server as SocketIOServer } from 'socket.io'; const prisma = new PrismaClient(); -interface CodeStatus { +interface ClaimCodeI { roomIds: string[]; expiresAt: number; usesLeft: number; @@ -99,24 +99,26 @@ export async function getRoomsByIdentity(identity: string): Promise { * Finds a claim code in the database. * * @param {string} code - The code to find. - * @returns {Promise} - The claim code, if found. + * @returns {Promise} - The claim code, if found. */ -export function findClaimCode(code: string): Promise { +export function findClaimCode(code: string): Promise { return prisma.claimCodes.findUnique({ where: { claimcode: code } }); } -/** - * Update the claim_code table to mark the given code as claimed. - * @param {string} code - The code to update - * @returns {Promise} - The rooms associated with the claim code - */ +/** +* This function looks up a claim code and updates its usesLeft field. +* If the claim code is not found, then it returns undefined. +* Otherwise it returns a ClaimCodeI object. +* @param {string} code - The claim code to update +* @returns {Promise} - A promise that resolves to a ClaimCodeI object + */ export async function updateClaimCode( code: string -): Promise { +): Promise { const claimCode = await findClaimCode(code); if (!claimCode) { return; @@ -341,7 +343,8 @@ export async function findUpdatedRooms(roomIds: string[]): Promise { */ export function createSystemMessages( message: string, - roomId?: string + roomId?: string, + io?: SocketIOServer ): Promise { const query = roomId ? { where: { roomId } } : undefined; return prisma.rooms @@ -350,8 +353,8 @@ export function createSystemMessages( if (roomId && rooms.length === 0) { return Promise.reject('Room not found'); } - const createMessages = rooms.map((room) => { - return prisma.messages.create({ + const createMessagePromises = rooms.map((room) => { + const createMessage = prisma.messages.create({ data: { message, roomId: room.roomId, @@ -359,9 +362,13 @@ export function createSystemMessages( proof: JSON.stringify({}) } }); + if (io) { + io.to(room.roomId).emit('systemMessage', createMessage); + } + return createMessage; }); - return Promise.all(createMessages); + return Promise.all(createMessagePromises); }) .catch((err) => { console.error(err); diff --git a/src/endpoints/index.ts b/src/endpoints/index.ts index 23cb7fe..0aa327a 100644 --- a/src/endpoints/index.ts +++ b/src/endpoints/index.ts @@ -15,6 +15,7 @@ import { import { MessageI, RoomI } from 'discreetly-interfaces'; import { RLNFullProof } from 'rlnjs'; + const prisma = new PrismaClient(); function asyncHandler(fn: { diff --git a/src/websockets/index.ts b/src/websockets/index.ts index b34acb8..b5b4d46 100644 --- a/src/websockets/index.ts +++ b/src/websockets/index.ts @@ -1,7 +1,7 @@ import { MessageI, RoomI } from 'discreetly-interfaces'; import { Socket, Server as SocketIOServer } from 'socket.io'; import verifyProof from '../crypto/verifier'; -import { getRoomByID, createSystemMessages } from '../data/db'; +import { getRoomByID } from '../data/db'; import { pp } from '../utils'; import { createMessage } from '../data/messages'; import type { createMessageResult } from '../data/messages'; @@ -68,19 +68,19 @@ export function websocketSetup(io: SocketIOServer) { io.to(id).emit('Members', userCount[id] ? userCount[id] : 0); }); - socket.on('systemMessage', (msg: string, roomID: bigint) => { - const id = roomID.toString(); - createSystemMessages(msg, id) - .then(() => { - if (roomID) { - io.to(id).emit('systemMessage', msg); - } else { - io.emit('systemMessage', msg); - } - }) - .catch((err) => { - pp(err, 'error'); - }); - }); + // socket.on('systemMessage', (msg: string, roomID: bigint) => { + // const id = roomID.toString(); + // createSystemMessages(msg, id) + // .then(() => { + // if (roomID) { + // io.to(id).emit('systemMessage', msg); + // } else { + // io.emit('systemMessage', msg); + // } + // }) + // .catch((err) => { + // pp(err, 'error'); + // }); + // }); }); } diff --git a/tests/express.test.ts b/tests/express.test.ts index 735da49..faa2440 100644 --- a/tests/express.test.ts +++ b/tests/express.test.ts @@ -3,7 +3,7 @@ import _app from '../src/server'; import { genId } from 'discreetly-interfaces'; import { serverConfig } from '../src/config/serverConfig'; import { PrismaClient } from '@prisma/client'; -import { beforeAll, beforeEach, describe, expect, test } from '@jest/globals'; +import { beforeAll, beforeEach, afterAll, describe, expect, test } from '@jest/globals'; import { pp } from '../src/utils'; import { randBigint, randomRoomName } from './utils';