Ejemplo n.º 1
0
 def is_user_in_thread(self, user):
     if isinstance(user, str) or isinstance(user, int):
         user = UserEntry.from_user_id(user)
     for member in self.users:
         if member == user:
             return True
     return False
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
Ejemplo n.º 3
0
def load_user_list():
    logger.info("Loading user list")
    try:
        # logger.info(f"Return value of get_online_users: {UserEntry.get_online_users()}")
        return_payload = {"friends": {}, "online": UserEntry.get_online_users()}
        # 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 users")
Ejemplo n.º 4
0
def load_threads():
    logger.info("Loading Thread")
    try:
        payload = request.get_json()
        logger.info(f"Payload sent: {payload}")
        user = UserEntry.from_user_id(payload["user"])
        logger.debug(f"Loaded user: {user}")
        # thread_list = get_user_threads(user.user_id)
        thread_list = user.threads
        return_payload = {
            "threads": {thread.thread_id: thread.to_dict() for thread in thread_list}
        }
        logger.info(f"Returning: {return_payload}")
        return jsonify(return_payload)
    except Exception as e:
        logger.error(f"ERROR: ({type(e)}) {e}")
        traceback.print_tb(e)
        return jsonify(error="Could not load 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"
            },
        )
Ejemplo n.º 6
0
    def __init__(
        self,
        thread_id: int,
        thread_name: str = None,
        users: list = None,
        messages: list = None,
    ):
        self._messages = messages
        self.thread_id = thread_id
        if users is None:
            self._users = users

        elif all(isinstance(u, int) for u in users):
            self._users = [UserEntry.from_user_id(u_id) for u_id in users]
        else:
            self._users = users

        self.room_name = f"thread-{thread_id}"
        self.thread_name = (
            thread_name if thread_name else self.create_default_thread_name()
        )
        super().__init__(thread_id)
        logger.info(f"Created ThreadEntry: {self}")
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"
            },
        )
Ejemplo n.º 8
0
 def _read_users_from_cache(self):
     member_ids = self._R.smembers(f"thread:{self.thread_id}:members")
     self._users = [
         UserEntry.from_user_id(int(member_id)) for member_id in member_ids
     ]
     return self._users
Ejemplo n.º 9
0
 def sender(self):
     if self._sender is None:
         logger.debug(f"self.sender_id: {self.sender_id}")
         self._sender = UserEntry.from_user_id(self.sender_id)
     return self._sender
def user_identification_sent(data):
    logger.info(f'User identification sent by {data["user"]}')
    user = UserEntry.from_user_id(data["user"]["id"])
    user.sid = request.sid
    user._set_user_online()
Ejemplo n.º 11
0
def request_userlist():
    """Gets mapping of users -> usernames for online users
    """
    return UserEntry.get_online_users()