Skip to content

Commit

Permalink
cleanup and improve logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmohsin7 committed Jan 14, 2025
1 parent fc50742 commit 2cbda09
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
20 changes: 8 additions & 12 deletions backend/database/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def get_messages(
return messages


def get_message(uid: str, message_id: str) -> Message | None:
def get_message(uid: str, message_id: str) -> tuple[Message, str] | None:
user_ref = db.collection('users').document(uid)
message_ref = user_ref.collection('messages').where('id', '==', message_id).limit(1).stream()
message_doc = next(message_ref, None)
Expand All @@ -154,22 +154,18 @@ def get_message(uid: str, message_id: str) -> Message | None:
if message.deleted is True:
return None

return message
return message, message_doc.id


def report_message(uid: str, msg_doc_id: str):
user_ref = db.collection('users').document(uid)
message_ref = user_ref.collection('messages').document(msg_doc_id)
message = message_ref.get().to_dict()

if not message:
return {"message": "Message not found"}

if message.get('deleted') is True:
return {"message": "Message already deleted"}

message_ref.update({'deleted': True, 'reported': True})
return None
try:
message_ref.update({'deleted': True, 'reported': True})
return {"message": "Message reported"}
except Exception as e:
print("Update failed:", e)
return {"message": f"Update failed: {e}"}


def batch_delete_messages(parent_doc_ref, batch_size=450, plugin_id: Optional[str] = None):
Expand Down
6 changes: 3 additions & 3 deletions backend/routers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ async def generate_stream():
def report_message(
message_id: str, uid: str = Depends(auth.get_current_user_uid)
):
print('report_message', message_id, uid)
message = chat_db.get_message(uid, message_id)

message, msg_doc_id = chat_db.get_message(uid, message_id)
if message is None:
raise HTTPException(status_code=404, detail='Message not found')
if message.sender != 'ai':
raise HTTPException(status_code=400, detail='Only AI messages can be reported')
if message.reported:
raise HTTPException(status_code=400, detail='Message already reported')
chat_db.report_message(uid, message.id)
chat_db.report_message(uid, msg_doc_id)
return {'message': 'Message reported'}


Expand Down

0 comments on commit 2cbda09

Please sign in to comment.