def on_message_not_spam(message): print(f"Processed message with id: {message['id']} from \"{message['sender-name']}\". Not spam") message_key = message['id'] sender_key = f"user:{message['sender-name']}" r.zincrby('sent-count', 1, sender_key) r.hincrby(sender_key, 'sent-count', 1) r.hset(message_key, 'status', MessageState.SENT)
def on_message_spam(message): print(f"Found spam message with id: {message['id']} from {message['sender-name']}") message_key = message['id'] sender_key = f"user:{message['sender-name']}" r.hset(message_key, 'status', MessageState.BLOCKED_BY_SPAM) r.hincrby(sender_key, 'spam-count', 1) r.zincrby('spam-count', 1, sender_key) r.publish('spam', message['sender-name'])
def send_message(text, username_from, username_to, tags=","): massage_id = get_next_message_id_save() message_key = f"message:{massage_id}" user_key = f"user:{username_from}" r.sadd(f'messages-sent-to:{username_to}', massage_id) message_dict = { 'id': massage_id, 'sender-name': username_from, 'receiver-name': username_to, 'text': text, 'status': MessageState.CREATED, 'tags': tags, } r.hmset(message_key, message_dict) g.add_message(message_dict) r.hincrby(user_key, 'created-count', 1) r.hincrby(user_key, 'total-count', 1) r.rpush(MESSAGES_QUEUE_KEY, f"message:{massage_id}") r.hmset(message_key, { 'status': MessageState.IN_QUEUE, }) r.hincrby(user_key, 'created-count', -1) r.hincrby(user_key, 'in-queue-count', 1) r.zincrby('sent-count', 1, username_from)
def spam_message_check(message): message_key = message['id'] r.hset(message_key, 'status', MessageState.IN_SPAM_CHECKING) sender_key = f"user:{message['sender-name']}" r.hincrby(sender_key, 'in-queue-count', -1) r.hincrby(sender_key, 'spam-checking-count', 1) spam_checking_result = is_spam(message) r.hincrby(sender_key, 'spam-checking-count', -1) return spam_checking_result