Skip to content

Refactor endpoints #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 46 additions & 21 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ datasource db {
url = env("DATABASE_URL")
}

// Room Models
//-----------------------------------------------------------------------------------------
model Rooms {
id String @id @default(auto()) @map("_id") @db.ObjectId
roomId String @unique
Expand All @@ -31,12 +33,38 @@ model Rooms {
type String @default("PUBLIC")
ephemeral String @default("PERSISTENT")
encrypted String @default("PLAINTEXT")
sessionIds Boolean @default(true)
gatewayIds String[] @default([]) @db.ObjectId
gateways GateWayIdentity[] @relation(fields: [gatewayIds], references: [id])
ethereumGroups EthereumGroup[] @relation(fields: [ethereumGroupIds], references: [id])
ethereumGroupIds String[] @db.ObjectId
jubmojiGroups JubmojiGroup[] @relation(fields: [jubmojiGroupIds], references: [id])
jubmojiGroupIds String[] @db.ObjectId
}

model Epoch {
id String @id @default(auto()) @map("_id") @db.ObjectId
epoch String
messages Messages[]
rooms Rooms? @relation(fields: [roomsId], references: [id])
roomsId String? @db.ObjectId
}

model Messages {
id String @id @default(auto()) @map("_id") @db.ObjectId
messageId String // Internal Nullifier
message String
timeStamp DateTime @default(now())
roomId String
messageType String? @default("TEXT")
room Rooms @relation(fields: [roomId], references: [roomId])
proof String
epoch Epoch? @relation(fields: [epochId], references: [id])
epochId String? @db.ObjectId
}

// Gateway Models
//-----------------------------------------------------------------------------------------
model GateWayIdentity {
id String @id @default(auto()) @map("_id") @db.ObjectId
semaphoreIdentity String @unique
Expand All @@ -48,6 +76,7 @@ model GateWayIdentity {
usedClaimCodes String[] @default([]) @db.ObjectId
claimCodes ClaimCodes[] @relation(fields: [usedClaimCodes], references: [id])
ethereumAddress EthereumAddress[]
jubmojiAddress JubmojiAddress[]
}

model EthereumGroup {
Expand All @@ -65,6 +94,21 @@ model EthereumAddress {
gatewayId String @db.ObjectId
}

model JubmojiGroup {
id String @id @default(auto()) @map("_id") @db.ObjectId
name String @unique
jubmojiAddresses String[] @default([])
rooms Rooms[] @relation(fields: [roomIds], references: [id])
roomIds String[] @default([]) @db.ObjectId
}

model JubmojiAddress {
id String @id @default(auto()) @map("_id") @db.ObjectId
jubmojiAddress String @unique
gateways GateWayIdentity @relation(fields: [gatewayId], references: [id])
gatewayId String @db.ObjectId
}

model ClaimCodes {
id String @id @default(auto()) @map("_id") @db.ObjectId
claimcode String @unique
Expand All @@ -77,27 +121,8 @@ model ClaimCodes {
gateways GateWayIdentity[] @relation(fields: [gatewayIds], references: [id])
}

model Messages {
id String @id @default(auto()) @map("_id") @db.ObjectId
messageId String // Internal Nullifier
message String
timeStamp DateTime @default(now())
roomId String
messageType String? @default("TEXT")
room Rooms @relation(fields: [roomId], references: [roomId])
proof String
epoch Epoch? @relation(fields: [epochId], references: [id])
epochId String? @db.ObjectId
}

model Epoch {
id String @id @default(auto()) @map("_id") @db.ObjectId
epoch String
messages Messages[]
rooms Rooms? @relation(fields: [roomsId], references: [id])
roomsId String? @db.ObjectId
}

// Discord Bot Models
//-----------------------------------------------------------------------------------------
model Discord {
id String @id @default(auto()) @map("_id") @db.ObjectId
discordServerId String @unique
Expand Down
6 changes: 3 additions & 3 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createRoom } from '../src/data/db';
import { createEthGroup } from '../src/data/db';
import { createEthGroupForRoom } from '../src/data/db';
import addresses from './addresses';
async function main() {
// @param name — The name of the room.
Expand All @@ -17,8 +17,8 @@ async function main() {
await createRoom('The Word', 10000, 12, 0, 0, 'PUBLIC', [], '007001');
const bcgd = await createRoom('Beacon Chain Genesis Depositors', 10000, 12, 0 ,20, 'PUBLIC');
const sgf = await createRoom('Stateful Genesis Funders', 10000, 12, 0, 20, 'PUBLIC');
await createEthGroup('Beacon Chain Genesis Depositors', bcgd!.roomId, addresses.bcgd);
await createEthGroup('Stateful Genesis Funders', sgf!.roomId, addresses.sgf);
await createEthGroupForRoom('Beacon Chain Genesis Depositors', bcgd!.roomId, addresses.bcgd);
await createEthGroupForRoom('Stateful Genesis Funders', sgf!.roomId, addresses.sgf);
}

await main();
113 changes: 107 additions & 6 deletions src/data/db/create.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { PrismaClient } from '@prisma/client';
import { getRateCommitmentHash, MessageI, randomBigInt } from 'discreetly-interfaces';
import {
getRateCommitmentHash,
MessageI,
randomBigInt
} from 'discreetly-interfaces';
import { genClaimCodeArray, genMockUsers } from '../../utils';
import type { Server as SocketIOServer } from 'socket.io';
import { EthGroupI } from '../../types';

const prisma = new PrismaClient();

Expand Down Expand Up @@ -34,15 +39,17 @@ export async function createRoom(
bandadaAPIKey?: string,
membershipType?: string,
roomId?: string
): Promise<{ roomId: string ; claimCodes: { claimcode: string }[] } | undefined | null> {
): Promise<
{ roomId: string; claimCodes: { claimcode: string }[] } | undefined | null
> {
const claimCodes: { claimcode: string }[] = genClaimCodeArray(numClaimCodes);
const mockUsers: string[] = genMockUsers(approxNumMockUsers);
const identityCommitments: string[] = mockUsers.map((user) =>
getRateCommitmentHash(BigInt(user), BigInt(userMessageLimit)).toString()
);
const _roomId = roomId ? roomId : randomBigInt().toString();

const room = await prisma.rooms.findUnique({where: {roomId: _roomId}})
const room = await prisma.rooms.findUnique({ where: { roomId: _roomId } });
if (room) return null;

const roomData = {
Expand All @@ -67,7 +74,7 @@ export async function createRoom(
},
gateways: {
create: mockUsers.map((user) => ({
semaphoreIdentity: user,
semaphoreIdentity: user
}))
}
}
Expand All @@ -76,7 +83,7 @@ export async function createRoom(
return await prisma.rooms
.upsert(roomData)
.then(() => {
return {roomId: _roomId, claimCodes};
return { roomId: _roomId, claimCodes };
})
.catch((err) => {
console.error(err);
Expand Down Expand Up @@ -132,7 +139,10 @@ export function createSystemMessages(
* @param {MessageI} message - The message to add to the room.
* @returns {Promise<unknown>} - A promise that resolves when the message has been added to the room.
*/
export function createMessageInRoom(roomId: string, message: MessageI): Promise<unknown> {
export function createMessageInRoom(
roomId: string,
message: MessageI
): Promise<unknown> {
if (!message.epoch) {
throw new Error('Epoch not provided');
}
Expand All @@ -158,3 +168,94 @@ export function createMessageInRoom(roomId: string, message: MessageI): Promise<
}
});
}

export function createEthGroup(
name: string,
roomIds: string[]
): Promise<EthGroupI> {
return prisma.ethereumGroup.create({
data: {
name: name,
rooms: {
connect: roomIds.map((roomId) => ({ roomId }))
}
}
});
}

export function createClaimCode(
claimCode: string,
roomIds: string[],
expiresAt: number,
usesLeft: number,
discordId: string,
roomId?: string
) {
if (!roomId) {
return prisma.claimCodes.create({
data: {
claimcode: claimCode,
roomIds: roomIds,
expiresAt: expiresAt,
usesLeft: usesLeft,
discordId: discordId
}
});
} else {
return prisma.claimCodes.create({
data: {
claimcode: claimCode,
roomIds: roomIds,
expiresAt: expiresAt,
usesLeft: usesLeft,
discordId: discordId,
rooms: {
connect: {
roomId: roomId
}
}
}
});
}
}


export async function joinRoomsFromEthAddress(
recoveredAddress: string,
message: string
) {
const gatewayIdentity = await prisma.gateWayIdentity.upsert({
where: { semaphoreIdentity: message },
update: {},
create: {
semaphoreIdentity: message
}
});
await prisma.ethereumAddress.upsert({
where: { ethereumAddress: recoveredAddress },
update: {},
create: {
ethereumAddress: recoveredAddress,
gatewayId: gatewayIdentity.id
}
});
const roomsToJoin = await prisma.ethereumGroup.findMany({
where: {
ethereumAddresses: {
has: recoveredAddress
}
},
select: {
roomIds: true
}
});
const roomIdsSet = new Set(roomsToJoin.map((room) => room.roomIds).flat());
const roomIds = Array.from(roomIdsSet);

await prisma.gateWayIdentity.update({
where: { id: gatewayIdentity.id },
data: { roomIds: { set: roomIds } }
});

return roomIds;
}
Loading