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
示例#2
0
    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 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 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"
            },
        )