From c2106b2d73e85d63fc3d75ec71bd50eb39c5ac84 Mon Sep 17 00:00:00 2001 From: ZKWolf Date: Fri, 24 Nov 2023 00:28:03 +0100 Subject: [PATCH] Fixed message System --- src/endpoints/user_handeling.py | 17 +++++++++++------ src/logic/mongodb_handler.py | 16 +++++++++++----- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/endpoints/user_handeling.py b/src/endpoints/user_handeling.py index 884c4c2..253a865 100644 --- a/src/endpoints/user_handeling.py +++ b/src/endpoints/user_handeling.py @@ -396,11 +396,12 @@ def messages_count(): try: unread_message_ids = mongo.get_data_with_list(login=userid, login_steam=False, items={"unread_msg_ids"}) - if unread_message_ids is None: + if unread_message_ids["unread_msg_ids"] == "" or unread_message_ids is None: return jsonify({"Count": 0}) else: try: - return jsonify({"Count": len(unread_message_ids["unread_msg_ids"])}) + id_len = unread_message_ids["unread_msg_ids"].split(",") + return jsonify({"Count": len(id_len)}) except TypeError: return jsonify({"Count": unread_message_ids["unread_msg_ids"]}) except TimeoutError: @@ -465,12 +466,16 @@ def messages_mark_as(): userid = session_manager.get_user_id(session_cookie) data = request.get_json() message_list = data["messageList"] + if not message_list: + mongo.write_data_with_list(login=userid, login_steam=False, items_dict={"unread_msg_ids": ""}) + return jsonify({"List": [{"Received": get_time(), "Success": True, "RecipientId": userid}]}) message_id = message_list[0]["recipientId"] - print(message_id) unread_messages = mongo.get_data_with_list(login=userid, login_steam=False, items={"unread_msg_ids"}) - print(unread_messages) - unread_messages["unread_msg_ids"] = unread_messages["unread_msg_ids"].remove(f"{message_id},") - mongo.write_data_with_list(login=userid, login_steam=False, items={unread_messages}) + unread_messages = unread_messages["unread_msg_ids"].split(",") + unread_messages.remove(message_id) + unread_messages = ",".join(unread_messages) + data = {"unread_msg_ids": unread_messages} + mongo.write_data_with_list(login=userid, login_steam=False, items_dict=data) return jsonify({"List": [{"Received": get_time(), "Success": True, "RecipientId": userid}]}) except TimeoutError: return jsonify({"status": "Timeout error"}) diff --git a/src/logic/mongodb_handler.py b/src/logic/mongodb_handler.py index 1a0a0fe..be83d81 100644 --- a/src/logic/mongodb_handler.py +++ b/src/logic/mongodb_handler.py @@ -138,17 +138,23 @@ class Mongo: logger.graylog_logger(level="error", handler="mongo_get_data_with_list", message=e) return None - def write_data_with_list(self, steamid, items_dict): + def write_data_with_list(self, login, login_steam, items_dict): try: - steam_id = str(steamid) client = pymongo.MongoClient(self.dyn_server) dyn_client_db = client[self.dyn_db] dyn_collection = dyn_client_db[self.dyn_collection] - existing_document = dyn_collection.find_one({'steamid': steamid}) - + if login_steam: + steam_id = str(login) + existing_document = dyn_collection.find_one({'steamid': steam_id}) + else: + user_id = str(login) + existing_document = dyn_collection.find_one({"userId": user_id}) if existing_document: update_query = {'$set': items_dict} - dyn_collection.update_one({'steamid': steamid}, update_query) + if login_steam: + dyn_collection.update_one({'steamid': steam_id}, update_query) + else: + dyn_collection.update_one({'userId': user_id}, update_query) client.close() return {"status": "success", "message": "Data updated"} else: