def new_message(data): logger.info(f"SEND_MESSAGE Recieved {data}") thread = ThreadEntry.from_id(data["thread"]) sender = UserEntry.from_user_id(data["sender"]) try: content = data["content"] except KeyError: logger.error("No content found in message") logger.debug(f"Contents of payload: {data}") content = "" try: logger.info(f"Creating MessageEntry with id: {MessageEntry.next_id()}") message = MessageEntry(MessageEntry.next_id(incr=True), thread.thread_id, sender.user_id, content) logger.info("Made MessageEntry object. Commiting...") message.commit() logger.info(f"Emitting NEW_MESSAGE id: {message.message_id}") emit( "NEW_MESSAGE", message.to_dict(), room=thread.room_name, namespace="/message", ) except KeyError as e: logger.error(e) return jsonify(error="Malformed request"), 400
def _get_threads(self): from server.redis_cache.message_cache import ThreadEntry raw_thread_ids = self._R.smembers(f"user:{self.user_id}:threads") # If redis loads something, fix the response and set threads if raw_thread_ids: thread_ids = list( map(lambda th: int(th.decode("utf-8")), raw_thread_ids)) self._threads = [ ThreadEntry.from_id(thread_id) for thread_id in thread_ids ] # Else return empty list else: self._threads = []
def new_thread(data): logger.info(f"REQUEST_NEW_THREAD recieved with payload {data}") try: full_members_list = [data["sender"], *data["users"]] existing = ThreadEntry.check_if_thread_exists(full_members_list) logger.debug("After geting fullmembers list") # TODO Return something more useful if existing: logger.info("Thread Exists") return existing.to_dict() logger.info("Creating Thread") thread = ThreadEntry(ThreadEntry.next_id(incr=True), users=full_members_list) thread.commit() for user in thread.users: logger.debug(f"User: {user}") logger.info(f"Getting user: {user.user_id} sid: {user.sid}") if user.sid != UserEntry.NO_SID: logger.info( f"User {user.user_id} online. Sending thread request") logger.info(f"User {user.user_id} has sid: {user.sid}") socketio.emit("SERVER_THREAD_REQUEST", thread.to_dict(), room=user.sid) logger.debug("CALLING JOIN ROOM") join_room(thread.room_name, sid=user.sid, namespace="/message") else: logger.info(f"User {user.user_id} not online") logger.debug(f"Rooms: {rooms()}") emit( "NEW_THREAD_CREATED", { "content": data["content"], **thread.to_dict() }, room=thread.room_name, namesapce="/message", ) except Exception as e: traceback.print_tb(e.__traceback__) logger.error(f"ERROR: {e}") socketio.emit("SERVER ERROR", {"msg": "Could not create new thread"})
def request_new_thread(): logger.info("/message/requestnewthread accessed") try: payload = request.get_json() logger.info(f"Payload: {payload}") full_members_list = [payload["sender"], *payload["users"]] existing = ThreadEntry.check_if_thread_exists(full_members_list) logger.debug("After geting fullmembers list") # TODO Return something more useful if existing: logger.info("Thread Exists") return existing.to_dict() logger.info("Creating Thread") thread = ThreadEntry(ThreadEntry.next_id(incr=True), users=full_members_list) thread.commit() for user in thread.users: logger.debug(f"User: {user}") logger.info(f"Getting user: {user.user_id} sid: {user.sid}") if user.sid != UserEntry.NO_SID: logger.info( f"User {user.user_id} online. Sending thread request") logger.info(f"User {user.user_id} has sid: {user.sid}") socketio.emit("SERVER_THREAD_REQUEST", thread.to_dict(), room=user.sid) logger.info( f"Adding user:<sid>:{user.sid} to room: {thread.room_name}" ) logger.debug("CALLING JOIN ROOM") join_room(thread.room_name, sid=user.sid) else: logger.info(f"User {user.user_id} not online") return jsonify(thread.to_dict()), 200 except Exception as e: traceback.print_tb(e.__traceback__) logger.error(f"ERROR: {e}") return jsonify({"error": "ERROR!"}), 503
def client_thread_request(data): logger.info("CLIENT_THREAD_REQUEST") logger.info(f"Client requested thread {data}") logger.info(data["thread"]) thread = ThreadEntry.from_id(data["thread"]) user = UserEntry.from_sid(request.sid) if thread.is_user_in_thread(user): logger.debug("CALLING JOIN ROOM (CLIENT THREAD REQUEST)") join_room(thread.room_name) logger.debug(f"Rooms: {rooms()}") emit("THREAD_JOINED", {"thread": thread.thread_id}) else: emit( "THREAD_JOIN_FAILED", { "thread": thread.thread_id, "error": "USER NOT IN THREAD" }, )
def load_thread_messages(): logger.info("Loading messages") try: payload = request.get_json() logger.info(f"Payload sent: {payload}") thread_id = payload["thread"] thread = ThreadEntry(thread_id) return_payload = { "messages": { message.message_id: message.to_dict() for message in thread.messages } } logger.info(f"Returning: {return_payload}") return jsonify(return_payload) except Exception as e: logger.error(f"ERROR: {e}") traceback.print_tb(e.__traceback__) return jsonify(error="Error loading thread messages")
def join_thread_request(data): logger.info("JOIN_THREAD_REQUEST") logger.info(f"Client requested thread {data}") thread = ThreadEntry.from_id(data["thread"]) sender = UserEntry.from_user_id(data["sender"]) if thread.check_if_user_in_thread(sender): logger.info(f"Adding {sender} to thread {thread}") # join_room(thread.thread_name, namespace="/message") logger.debug("CALLING JOIN ROOM (JOIN_THREAD_REQUEST)") join_room(thread.thread_name) logger.debug(f"Rooms: {rooms()}") emit("THREAD_JOINED", {"thread": thread.to_dict()}) else: emit( "THREAD_JOIN_FAILED", { "thread": data["thread"], "error": "USER NOT IN THREAD" }, )
from redis import Redis, DataError from server.logging import make_logger from server.redis_cache.user_cache import UserEntry from server.redis_cache.message_cache import ThreadEntry, MessageEntry from server.utils.data_generators import FlexDict logger = make_logger(__name__) mock_threads = {0: ThreadEntry("0", "mockthread2")} mock_users = { 0: {"id": 0, "username": "******", "threads": []}, 1: {"id": 1, "username": "******", "threads": [0]}, 2: {"id": 2, "username": "******", "threads": [0, 1]}, 3: {"id": 3, "username": "******", "threads": [1]}, 4: {"id": 4, "username": "******", "threads": [2]}, 5: {"id": 5, "username": "******", "threads": [2]}, } mock_threads = { 0: { "id": 0, "name": "mockthread1", "members": [1, 2, 0], "messages": [1, 2, 3, 4, 12], }, 1: {"id": 1, "name": "mockthread2", "members": [2, 3], "messages": [5, 6, 7]}, 2: {"id": 2, "name": "mockthread3", "members": [4, 5], "messages": [8, 9, 10, 11]}, }