refactor(gateways): joining rooms now adds a discordId if the code is from discord

This commit is contained in:
Tanner Shaw
2023-10-14 14:35:18 -05:00
parent abdee0d522
commit 8a3d609d6c
5 changed files with 94 additions and 52 deletions

View File

@@ -55,6 +55,7 @@ model ClaimCodes {
expiresAt Int @default(0)
usesLeft Int @default(-1)
rooms Rooms[] @relation(fields: [roomIds], references: [id])
discordId String?
gatewayIds String[] @default([]) @db.ObjectId
gateways GateWayIdentity[] @relation(fields: [gatewayIds], references: [id])
}

View File

@@ -60,8 +60,8 @@ export async function createRoom(
bandadaAddress,
bandadaGroupId,
bandadaAPIKey,
membershipType,
type,
membershipType,
claimCodes: {
create: claimCodes
},

View File

@@ -20,6 +20,7 @@ const prisma = new PrismaClient();
export async function updateRoomIdentities(
idc: string,
roomIds: string[],
discordId?: string
): Promise<string[] | void> {
try {
const identityCommitment: string = sanitizeIDC(idc);
@@ -33,11 +34,13 @@ export async function updateRoomIdentities(
const identityRooms: string[] = await addIdentityToIdentityListRooms(
rooms,
identityCommitment
identityCommitment,
discordId
);
const bandadaRooms: string[] = await addIdentityToBandadaRooms(
rooms,
identityCommitment
identityCommitment,
discordId
);
return [...identityRooms, ...bandadaRooms];
@@ -68,7 +71,7 @@ export async function updateClaimCode(
const gateway = await findGatewayByIdentity(idc);
if (gateway) {
return await prisma.claimCodes.update({
where: {claimcode: code},
where: { claimcode: code },
data: {
usesLeft: newUsesLeft,
gateways: {
@@ -77,19 +80,19 @@ export async function updateClaimCode(
}
}
}
})
});
} else {
return await prisma.claimCodes.update({
where: {claimcode: code},
where: { claimcode: code },
data: {
usesLeft: newUsesLeft,
gateways: {
create: {
semaphoreIdentity: idc,
semaphoreIdentity: idc
}
}
}
})
});
}
}
}
@@ -102,21 +105,28 @@ export async function updateClaimCode(
*/
export async function addIdentityToIdentityListRooms(
rooms: RoomI[] | RoomWithSecretsI[],
identityCommitment: string
identityCommitment: string,
discordId?: string
): Promise<string[]> {
const identityListRooms = rooms
.filter(
(room: RoomI) =>
room.membershipType === 'IDENTITY_LIST' &&
!room.identities?.includes(getRateCommitmentHash(BigInt(identityCommitment), BigInt(room.userMessageLimit! ?? 1)).toString()
))
!room.identities?.includes(
getRateCommitmentHash(
BigInt(identityCommitment),
BigInt(room.userMessageLimit! ?? 1)
).toString()
)
)
.map((room) => room.roomId as string);
const addedRooms: string[] = [];
for(const roomId of identityListRooms) {
for (const roomId of identityListRooms) {
console.log(roomId);
const room = rooms.find((r) => r.roomId === roomId);
console.log(room);
if (room) {
try {
const gateway = await findGatewayByIdentity(identityCommitment);
@@ -137,7 +147,17 @@ export async function addIdentityToIdentityListRooms(
}
}
});
console.debug(`Successfully added user to Identity List room ${room.roomId}`);
if (discordId) {
await prisma.gateWayIdentity.update({
where: { semaphoreIdentity: identityCommitment },
data: {
discordId: discordId
}
})
}
console.debug(
`Successfully added user to Identity List room ${room.roomId}`
);
addedRooms.push(roomId);
} else {
await prisma.rooms.update({
@@ -152,10 +172,11 @@ export async function addIdentityToIdentityListRooms(
gateways: {
create: {
semaphoreIdentity: identityCommitment,
discordId: discordId
}
}
}
})
});
}
} catch (err) {
console.error(err);
@@ -178,13 +199,19 @@ export async function addIdentityToIdentityListRooms(
export async function addIdentityToBandadaRooms(
rooms: RoomWithSecretsI[],
identityCommitment: string,
discordId?: string
): Promise<string[]> {
const bandadaGroupRooms = rooms
.filter(
(room: RoomI) =>
room.membershipType === 'BANDADA_GROUP' &&
!room.identities?.includes(getRateCommitmentHash(BigInt(identityCommitment), BigInt(room.userMessageLimit! ?? 1)).toString()
))
!room.identities?.includes(
getRateCommitmentHash(
BigInt(identityCommitment),
BigInt(room.userMessageLimit! ?? 1)
).toString()
)
)
.map((room) => room);
const addedRooms: string[] = [];
@@ -225,6 +252,12 @@ export async function addIdentityToBandadaRooms(
}
}
});
await prisma.gateWayIdentity.update({
where: { semaphoreIdentity: identityCommitment },
data: {
discordId: discordId
}
})
} else {
await prisma.rooms.update({
where: { id: room.id },
@@ -235,6 +268,7 @@ export async function addIdentityToBandadaRooms(
gateways: {
create: {
semaphoreIdentity: identityCommitment,
discordId: discordId
}
}
}

View File

@@ -166,9 +166,9 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
return;
}
const roomIds = foundCode.roomIds;
const addedRooms = await updateRoomIdentities(idc, roomIds);
console.log(roomIds);
const addedRooms = await updateRoomIdentities(idc, roomIds, foundCode.discordId!);
console.log(addedRooms);
const updatedRooms = await findUpdatedRooms(addedRooms as string[]);
// Return the room ids of the updated rooms
@@ -411,18 +411,20 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
* "all": boolean,
* "expiresAt": number, // optional
* "usesLeft": number // optional
* "discordId": string // optional
* }
*/
app.post(
['/addcode', '/api/addcode'],
adminAuth,
asyncHandler(async (req: Request, res: Response) => {
const { numCodes, rooms, all, expiresAt, usesLeft } = req.body as {
const { numCodes, rooms, all, expiresAt, usesLeft, discordId } = req.body as {
numCodes: number;
rooms: string[];
all: boolean;
expiresAt: number;
usesLeft: number;
discordId: string;
};
const currentDate = new Date();
@@ -443,7 +445,8 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
claimcode: code.claimcode,
roomIds: roomIds,
expiresAt: codeExpires,
usesLeft: usesLeft
usesLeft: usesLeft,
discordId: discordId
}
})
.then((newCode) => {
@@ -733,32 +736,36 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
* @returns {string[]} - An array of room ids
* */
app.post('/api/discord/getrooms', adminAuth, (req, res) => {
const { roleId } = req.body as { roleId: string };
app.post('/api/discord/getrooms', adminAuth, asyncHandler(async (req: Request, res: Response) => {
const { roleId, discordId } = req.body as { roleId: string, discordId: string };
if (!roleId) {
res.status(400).json({ error: 'Bad Request' });
return;
}
prisma.discordRoleRoomMapping
.findMany({
where: {
roles: {
has: roleId
const rooms = await prisma.gateWayIdentity.findFirst({
where: {
discordId: discordId
},
include: {
rooms: true
}
})
if (rooms) {
const discordRoleRoomMapping = await prisma.discordRoleRoomMapping.findMany({
where: {
roles: {
has: roleId
}
}
},
select: {
roomId: true
}
})
.then((rooms) => {
res.status(200).json(rooms);
return rooms;
})
.catch((err) => {
console.error(err);
res.status(500).json({ error: 'Internal Server Error' });
});
});
})
const roomIds = discordRoleRoomMapping.map((mapping) => mapping.roomId);
const filteredRooms = rooms.rooms.filter((room) => !roomIds.includes(room.roomId));
console.log(rooms);
res.status(200).json(filteredRooms);
}
// console.log(filteredRooms)
}));
/**
* This endpoint takes a discord user id and returns all rooms that the user is a part of.
@@ -772,19 +779,18 @@ export function initEndpoints(app: Express, adminAuth: RequestHandler) {
.findFirst({
where: {
discordId: discordUserId
}
})
},
include: {
rooms: true,
}
})
.then((identity) => {
if (!identity) {
res.status(404).json({ error: 'Identity not found' });
return;
}
return res.status(200).json(identity.roomIds);
return identity
}).catch((err) => {
console.error(err);
res.status(500).json({ error: 'Internal Server Error' });
})
});
});
});
/**
* This endpoint gets all the rooms that a user is allowed to access based on their discordId.
* @params {string} discordId - The id of the discord user to get rooms for

View File

@@ -17,6 +17,7 @@ export interface ClaimCodeI {
roomIds: string[];
expiresAt: number;
usesLeft: number;
discordId: string | null;
}
export interface GateWayIdentityI {