From 4f171c87e5ed92b64b0f63da2b66532287b6c6c8 Mon Sep 17 00:00:00 2001 From: Aevann Date: Fri, 15 Nov 2024 14:30:05 +0200 Subject: [PATCH] fix bug causing ppl to get notified of their own chat messages --- files/routes/chat.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/files/routes/chat.py b/files/routes/chat.py index 44208d0e4..ad58965e6 100644 --- a/files/routes/chat.py +++ b/files/routes/chat.py @@ -36,6 +36,10 @@ typing = {} cache.set('loggedin_chat', 0, timeout=86400) +def get_chatroom(): + if request.referrer: + return request.referrer.split('?')[0] + def auth_required_socketio(f): def wrapper(*args, **kwargs): if not hasattr(g, 'db'): g.db = db_session() @@ -175,9 +179,9 @@ def speak(data, v): if v.shadowbanned: emit('speak', data) else: - emit('speak', data, room=request.referrer.split('?')[0]) + emit('speak', data, room=get_chatroom()) - typing[request.referrer.split('?')[0]] = [] + typing[get_chatroom()] = [] if membership and membership.is_mod: added_users = [] @@ -194,7 +198,7 @@ def speak(data, v): g.db.flush() added_users.append((user.username, user.name_color, user.patron, user.id, bool(user.has_badge(303)))) if added_users: - emit("add", added_users, room=request.referrer.split('?')[0]) + emit("add", added_users, room=get_chatroom()) kicked_users = [] for i in chat_kicking_regex.finditer(text): @@ -207,7 +211,7 @@ def speak(data, v): send_notification(user.id, f"@{v.username} kicked you from their chat [{chat.name}](/chat/{chat.id})") kicked_users.append(user.id) if kicked_users: - emit("kick", kicked_users, room=request.referrer.split('?')[0]) + emit("kick", kicked_users, room=get_chatroom()) if v.id == chat.owner_id: for i in chat_jannying_regex.finditer(text): @@ -227,7 +231,7 @@ def speak(data, v): send_notification(user.id, f"@{v.username} has removed you as a mod of their chat [{chat.name}](/chat/{chat.id})") if chat.id != 1: - alrdy_here = set(online[request.referrer.split('?')[0]].keys()) + alrdy_here = set(online[get_chatroom()].keys()) memberships = g.db.query(ChatMembership).options(load_only(ChatMembership.user_id)).filter( ChatMembership.chat_id == chat_id, ChatMembership.user_id.notin_(alrdy_here), @@ -277,8 +281,8 @@ def refresh_online(room): @socketio.on('connect') @auth_required_socketio def connect(v): - if not request.referrer.split('?')[0]: stop(400, "Invalid referrer!") - room = request.referrer.split('?')[0] + if not get_chatroom(): stop(400, "Invalid referrer!") + room = get_chatroom() if room.startswith(f'{SITE_FULL}/notifications/messages'): join_room(v.id) @@ -303,8 +307,8 @@ def connect(v): @socketio.on('disconnect') @auth_required_socketio def disconnect(v): - if not request.referrer.split('?')[0]: stop(400, "Invalid referrer!") - room = request.referrer.split('?')[0] + if not get_chatroom(): stop(400, "Invalid referrer!") + room = get_chatroom() if room.startswith(f'{SITE_FULL}/notifications/messages'): leave_room(v.id) @@ -327,8 +331,8 @@ def disconnect(v): @socketio.on('heartbeat') @auth_required_socketio def heartbeat(v): - if not request.referrer.split('?')[0]: stop(400, "Invalid referrer!") - room = request.referrer.split('?')[0] + if not get_chatroom(): stop(400, "Invalid referrer!") + room = get_chatroom() if not online.get(room): online[room] = {} @@ -346,8 +350,8 @@ def heartbeat(v): def typing_indicator(data, v): if v.is_suspended or v.shadowbanned: return '' - if not request.referrer.split('?')[0]: stop(400, "Invalid referrer!") - room = request.referrer.split('?')[0] + if not get_chatroom(): stop(400, "Invalid referrer!") + room = get_chatroom() if not typing.get(room): typing[room] = []