small changes

This commit is contained in:
Tanner Shaw
2023-09-05 23:30:12 -05:00
parent 683a408d3c
commit ef104a7595
4 changed files with 38 additions and 30 deletions

View File

@@ -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<string[]> {
* Finds a claim code in the database.
*
* @param {string} code - The code to find.
* @returns {Promise<CodeStatus | null>} - The claim code, if found.
* @returns {Promise<ClaimCodeI | null>} - The claim code, if found.
*/
export function findClaimCode(code: string): Promise<CodeStatus | null> {
export function findClaimCode(code: string): Promise<ClaimCodeI | null> {
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<RoomsFromClaimCode>} - 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<ClaimCodeI | void>} - A promise that resolves to a ClaimCodeI object
*/
export async function updateClaimCode(
code: string
): Promise<CodeStatus | void> {
): Promise<ClaimCodeI | void> {
const claimCode = await findClaimCode(code);
if (!claimCode) {
return;
@@ -341,7 +343,8 @@ export async function findUpdatedRooms(roomIds: string[]): Promise<RoomI[]> {
*/
export function createSystemMessages(
message: string,
roomId?: string
roomId?: string,
io?: SocketIOServer
): Promise<unknown> {
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);

View File

@@ -15,6 +15,7 @@ import {
import { MessageI, RoomI } from 'discreetly-interfaces';
import { RLNFullProof } from 'rlnjs';
const prisma = new PrismaClient();
function asyncHandler(fn: {

View File

@@ -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');
// });
// });
});
}

View File

@@ -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';