예제 #1
0
    async def create_attachment(self, user_id: int, message_id: str,
                                query: CreateAttachmentQuery,
                                db: Session) -> Message:
        if query.group_id is None and query.receiver_id is None:
            raise QueryValidationError(
                "both group_id and receiver_id is empty")
        elif query.group_id is not None and query.receiver_id is not None:
            raise QueryValidationError(
                "can't use both group_id AND receiver_id, choose one")

        group_id = query.group_id

        if group_id is None:
            group_id = self._get_or_create_group_for_1v1(
                user_id, query.receiver_id, db)

        attachment = self.env.storage.store_attachment(group_id, user_id,
                                                       message_id, query)

        update_last_message = True
        if query.action_log is not None:
            update_last_message = query.action_log.update_last_message

        self._user_sends_a_message(group_id,
                                   user_id=user_id,
                                   message=attachment,
                                   db=db,
                                   should_increase_unread=True,
                                   event_type=EventTypes.ATTACHMENT,
                                   update_last_message=update_last_message)

        return message_base_to_message(attachment)
예제 #2
0
    async def get_attachment_info(self, group_id: str, query: AttachmentQuery,
                                  db: Session) -> Message:
        group = self.env.db.get_group_from_id(group_id, db)

        message_base = self.env.storage.get_attachment_from_file_id(
            group_id, group.created_at, query)

        return message_base_to_message(message_base)
예제 #3
0
    async def get_message_info(self, user_id: int, message_id: str,
                               query: MessageInfoQuery) -> Message:
        message_base = self.env.storage.get_message_with_id(
            group_id=query.group_id,
            user_id=user_id,
            message_id=message_id,
            created_at=query.created_at)

        return message_base_to_message(message_base)
예제 #4
0
    async def messages_in_group(self, group_id: str,
                                query: MessageQuery) -> List[Message]:
        raw_messages = self.env.storage.get_messages_in_group(group_id, query)
        messages = list()

        for message_base in raw_messages:
            message = message_base_to_message(message_base)
            messages.append(message)

        return messages
예제 #5
0
    async def get_attachments_in_group_for_user(
        self, group_id: str, user_id: int, query: MessageQuery, db: Session
    ) -> List[Message]:
        if query.since is None and query.until is None:
            raise InvalidRangeException("both 'since' and 'until' was empty, need one")
        if query.since is not None and query.until is not None:
            raise InvalidRangeException("only one of parameters 'since' and 'until' can be used at the same time")

        user_stats = self.env.db.get_user_stats_in_group(group_id, user_id, db)
        attachments = self.env.storage.get_attachments_in_group_for_user(group_id, user_stats, query)

        return [
            message_base_to_message(attachment)
            for attachment in attachments
        ]
예제 #6
0
        def get_messages():
            # need to batch query cassandra, can't filter by user id
            if query.only_sender:
                _messages = self.env.storage.get_messages_in_group_only_from_user(
                    group_id, user_stats, query
                )
            else:
                _messages = self.env.storage.get_messages_in_group_for_user(
                    group_id, user_stats, query
                )

            return [
                message_base_to_message(message)
                for message in _messages
            ]
예제 #7
0
    async def messages_for_user(self, group_id: str, user_id: int,
                                query: MessageQuery,
                                db: Session) -> List[Message]:
        user_stats = self.env.db.get_user_stats_in_group(group_id, user_id, db)

        if user_stats.hide:
            return list()

        raw_messages = self.env.storage.get_messages_in_group_for_user(
            group_id, user_stats, query)
        messages = list()

        for message_base in raw_messages:
            message = message_base_to_message(message_base)
            messages.append(message)

        return messages
예제 #8
0
    async def send_message_to_group(self, group_id: str, user_id: int,
                                    query: SendMessageQuery,
                                    db: Session) -> Message:
        message = self.env.storage.store_message(group_id, user_id, query)

        self._user_sends_a_message(group_id,
                                   user_id=user_id,
                                   message=message,
                                   db=db,
                                   should_increase_unread=True,
                                   event_type=EventTypes.MESSAGE)
        """
        message_amount = self.env.storage.count_messages_in_group_since(group_id, group_base.created_at)
        _, join_times, n_users = self.env.db.get_users_in_group(group_id, db, include_group=False)
        group = MessageResource.group_base_to_group(
            group_base, users=join_times, user_count=n_users, message_amount=message_amount,
        )
        """

        return message_base_to_message(message)
예제 #9
0
    def create_action_log(
        self,
            query: ActionLogQuery,
            db: Session,
            user_id: int = None,
            group_id: str = None,
    ) -> Message:
        # creating an action log is optional for the caller
        if query is None:
            return None  # noqa

        if query.user_id is not None:
            user_id = query.user_id

        if user_id is None and group_id is None:
            raise ValueError("either receiver_id or group_id is required in CreateActionLogQuery")

        if user_id is None:
            raise ValueError("no user_id in api path(?) and no user_id on ActionLogQuery; need one of them")

        if query.group_id is not None and len(query.group_id.strip()):
            group_id = query.group_id
        elif query.receiver_id is not None and query.receiver_id > 0:
            group_id = self._get_or_create_group_for_1v1(user_id, query.receiver_id, db)

        log = self.env.storage.create_action_log(user_id, group_id, query)
        self._user_sends_a_message(
            group_id,
            user_id=user_id,
            message=log,
            db=db,
            should_increase_unread=query.update_unread_count,
            update_last_message=query.update_last_message,
            event_type=EventTypes.ACTION_LOG
        )

        return message_base_to_message(log)