From 567b0eab426eb28968986411beea60dcc6317c08 Mon Sep 17 00:00:00 2001 From: "berry :3" Date: Fri, 18 Apr 2025 09:28:31 +0300 Subject: livechat --- main.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'main.py') diff --git a/main.py b/main.py index 4b81068..f5a0209 100644 --- a/main.py +++ b/main.py @@ -540,6 +540,49 @@ async def handler(websocket): if str(websocket.id) not in client_data: await websocket.send(util.error("unauthorized", listener)) continue + if "chat" in r and r["chat"] == "livechat": + attachments = [] + for i in r["attachments"]: + if urlparse(i).hostname in attachment_whitelist: + attachments.append(i) + if len(r["content"]) == 0 and len(r["attachments"]) == 0: + await websocket.send(util.error("lengthInvalid", listener)) + continue + username = client_data[str(websocket.id)]["username"] + author = util.author_data(username) + replies = [] + for i in r["replies"]: + post = db.posts.get_by_id(i) + if type(post) == dict and i not in replies: + replies.append(post) + data = { + "_id": str(uuid.uuid4()), + "created": time.time(), + "content": r["content"], + "replies": replies, + "attachments": attachments, + "author": username + } + # posted = db.posts.add(data) + # if posted != True: + # await websocket.send(util.error(fc, listener)) + # continue + data["author"] = author + data["origin"] = "livechat" + incr = -1 + for j in data["replies"]: + incr += 1 + reply_author = db.acc.get_author(j["author"]) + if type(reply_author) != dict: + reply_author = {} + data["replies"][incr]["author"] = reply_author + broadcast(clients, json.dumps({ + "command": "new_post", + "origin": "livechat", + "data": data + })) + await websocket.send(json.dumps({"error": False, "listener": listener})) + continue attachments = [] for i in r["attachments"]: if urlparse(i).hostname in attachment_whitelist: -- cgit 1.4.1-2-gfad0