Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
Updated the delete feature to only delete your own messages
Browse files Browse the repository at this point in the history
  • Loading branch information
emito-k committed Oct 14, 2023
1 parent 091e4f2 commit c349343
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
24 changes: 19 additions & 5 deletions backend/event-chats-service/routes/deleteMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ const db = require('../data-access/firebase');

async function deleteMessage(req, res) {
try {
const {event_id, chat_id } = req.params;
// Delete the document in the Event-Chats collection's subcollection "chats"
const { uid } = req.query;
const { event_id, chat_id } = req.params;

// Retrieve the message to be deleted
const messageRef = db.collection('Event-Chats').doc(event_id).collection('chats').doc(chat_id);
const messageDoc = await messageRef.get();

// Check if the message exists
if (!messageDoc.exists) {
return res.status(404).json({ error: 'Message not found' });
}

console.log("event_id::::", event_id);
console.log("chat_id:::", chat_id);
const messageData = messageDoc.data();

await db.collection('Event-Chats').doc(event_id).collection('chats').doc(chat_id).delete();
// Check if the uid in the query parameters matches the uid in the message data
if (uid !== messageData.uid) {
return res.status(403).json({ error: 'Unauthorized: You cannot delete this message' });
}

// Delete the document in the Event-Chats collection's subcollection "chats"
await messageRef.delete();

res.status(200).json({ message: 'Message deleted successfully' });
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/angular/xpose/src/app/event/event.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h2 class="chat-name">{{ message.displayName }} <b>{{ toDate(message.timestamp)
<p class="chat-message">{{ message.message }}</p>
<!-- <span class="chat-timestamp">{{ message.timestamp | date:'shortTime' }}</span> -->
</ion-label>
<ion-button fill="clear" slot="end" (click)="deleteMessage(message)">
<ion-button fill="clear" slot="end" (click)="deleteMessage(message)" *ngIf="user_id === message.uid">
<ion-icon name="trash"></ion-icon>
</ion-button>
</ion-item>
Expand Down
8 changes: 6 additions & 2 deletions frontend/angular/xpose/src/app/event/event.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,12 @@ export class EventPage {

deleteMessage(messageUID: any){
// console.log("Deleting message...",messageUID);
this.http.post(`${this.api.apiUrl}/c/chats/${this.current_event.code}/message_delete/${messageUID.id}`,{}).subscribe((res) => {
// console.log(res);
this.getCurrentUserId().subscribe((uid) => {
if(uid) {
this.http.post(`${this.api.apiUrl}/c/chats/${this.current_event.code}/message_delete/${messageUID.id}?uid=${uid}`,{}).subscribe((res) => {
// console.log(res);
});
}
});
}

Expand Down

0 comments on commit c349343

Please sign in to comment.