Skip to content

Commit

Permalink
Update message controller to validate room membership before sending …
Browse files Browse the repository at this point in the history
…a message
  • Loading branch information
xuelink committed Apr 11, 2024
1 parent 992fbb6 commit 6256995
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/controllers/message.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ interface UserDocument extends Models.Document {
blockedUsers?: string[];
}

interface RoomDocument extends Models.Document {
users: string[];
}

export default class MessageController {
async create(req: Request, res: Response) {
try {
Expand Down Expand Up @@ -89,6 +93,23 @@ export default class MessageController {

const database = new Databases(client);

// Check Room has this user or not
const roomDoc = (await database.getDocument(
env.APP_DATABASE,
env.ROOMS_COLLECTION,
roomId
)) as RoomDocument;

// console.log(`roomDoc.users: ${roomDoc.users}`);
// console.log(`sender: ${sender}`);
// console.log(`to: ${to}`);

// Check if the user is in the room
if (!roomDoc.users.includes(sender) || !roomDoc.users.includes(to)) {
res.status(403).json({ message: "You are not in this room." });
return;
}

// Check user blocked or not
const currentUserDoc = (await database.getDocument(
env.APP_DATABASE,
Expand Down

0 comments on commit 6256995

Please sign in to comment.