Skip to content
Draft
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
37 changes: 30 additions & 7 deletions apps/ws-server/src/utils/handleMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ export const handleMessage = async (client: Client, data: any) => {

const { sessionId, role, participantId, userId } = payload;

// Verify if the client claims to be an organizer
let verifiedRole = role;
if (role === 'ORGANIZER') {
if (!userId) {
verifiedRole = 'PARTICIPANT';
} else {
const session = await getCachedSession(sessionId);
const quizId = session?.quizId;
const isOrganizer = quizId
? await prisma.quizOrganizer.findFirst({
where: {
quizId,
userId,
}
})
: null;

if (!isOrganizer) {
verifiedRole = 'PARTICIPANT';
}
}
}

// A reconnecting client re-joins with the same participantId while
// its previous socket may still be registered: the heartbeat can
// take up to two rounds to notice a dead peer, and a background
Expand All @@ -114,11 +137,11 @@ export const handleMessage = async (client: Client, data: any) => {
indexClient(client, sessionId);

client.sessionId = sessionId;
client.role = role;
client.role = verifiedRole;
client.participantId = participantId;
client.userId = userId;

if (role === 'ORGANIZER') {
if (verifiedRole === 'ORGANIZER') {

const session = await getCachedSession(sessionId);
const participants = await getCachedParticipants(sessionId);
Expand All @@ -136,7 +159,7 @@ export const handleMessage = async (client: Client, data: any) => {


console.log("JOIN CODE FROM WS:", payload);
} else if (role === 'PARTICIPANT' && participantId) {
} else if (verifiedRole === 'PARTICIPANT' && participantId) {
const participant = await prisma.participant.findUnique({
where: { id: participantId }
});
Expand Down Expand Up @@ -179,6 +202,7 @@ export const handleMessage = async (client: Client, data: any) => {


case 'quiz:start': {
if (client.role !== 'ORGANIZER' || client.sessionId !== payload.sessionId) break;
const { sessionId } = payload;
await prisma.quizSession.update({
where: { id: sessionId },
Expand All @@ -189,6 +213,7 @@ export const handleMessage = async (client: Client, data: any) => {
}

case 'quiz:next-question': {
if (client.role !== 'ORGANIZER' || client.sessionId !== payload.sessionId) break;
const { sessionId, questionIndex = 0 } = payload;

const questions = await getCachedQuestions(sessionId);
Expand Down Expand Up @@ -268,7 +293,7 @@ export const handleMessage = async (client: Client, data: any) => {


case 'quiz:end': {

if (client.role !== 'ORGANIZER' || client.sessionId !== payload.sessionId) break;
const { sessionId } = payload;

if (!sessionId) break;
Expand All @@ -293,6 +318,4 @@ export const handleMessage = async (client: Client, data: any) => {

}
}
}


}