From 4a22ca8a9226fe0e162012461980cc42a92c81ef Mon Sep 17 00:00:00 2001 From: n9k Date: Tue, 2 Aug 2022 04:46:38 +0000 Subject: [PATCH] Minor: `add_chat_message` returns seq (or None) It now returns the seq of the just-added message if one was added, and None otherwise. The previous behaviour was to return True and False respectively. --- anonstream/chat.py | 6 +++--- anonstream/routes/nojs.py | 3 ++- anonstream/websocket.py | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/anonstream/chat.py b/anonstream/chat.py index 7075411..667f591 100644 --- a/anonstream/chat.py +++ b/anonstream/chat.py @@ -31,9 +31,9 @@ def get_all_messages_for_websocket(): )) def add_chat_message(user, nonce, comment, ignore_empty=False): - # Special case: if the comment is empty, do nothing and return + # Special case: if the comment is empty, do nothing and return None if ignore_empty and len(comment) == 0: - return False + return None timestamp_ms = time.time_ns() // 1_000_000 timestamp = timestamp_ms // 1000 @@ -137,7 +137,7 @@ def add_chat_message(user, nonce, comment, ignore_empty=False): }, ) - return True + return seq def delete_chat_messages(seqs): seq_set = set(seqs) diff --git a/anonstream/routes/nojs.py b/anonstream/routes/nojs.py index 6f89666..e34c52e 100644 --- a/anonstream/routes/nojs.py +++ b/anonstream/routes/nojs.py @@ -135,12 +135,13 @@ async def nojs_submit_message(timestamp, user): try: # If the comment is empty but the captcha was just solved, # be lenient: don't raise an exception and don't create a notice - message_was_added = add_chat_message( + seq = add_chat_message( user, nonce, comment, ignore_empty=verification_happened, ) + message_was_added = seq is not None except Rejected as e: notice, *_ = e.args state_id = add_state( diff --git a/anonstream/websocket.py b/anonstream/websocket.py index 35be036..48d8467 100644 --- a/anonstream/websocket.py +++ b/anonstream/websocket.py @@ -140,12 +140,13 @@ def handle_inbound_message(timestamp, queue, user, nonce, comment, digest, answe message_was_added = False else: try: - message_was_added = add_chat_message( + seq = add_chat_message( user, nonce, comment, ignore_empty=verification_happened, ) + message_was_added = seq is not None except Rejected as e: notice, *_ = e.args message_was_added = False