def validate_account_can_create_room_with_receiver( self, account: AccountInfoDto, receiver: AccountInfoDto): if not account: raise BaseApiException("Account not found!") if not receiver: raise BaseApiException("Receiver not found!")
def validate_account_can_view_stories_of_partner( self, account: Account, partner: Account ): if account not in partner.followers.all(): raise BaseApiException("Account can't view user story of this user.") if not partner.userstory_set.count(): raise BaseApiException("This user don't have any stories.")
def validate_account_can_report_receiver(self, account: Account, receiver: Account, related_post: Post): if account.id == receiver.id: raise BaseApiException(f"Can't report own account.") if related_post and related_post not in receiver.post_set.all(): raise BaseApiException( f"account {receiver.id} don't have post {related_post.id}")
def handle(self, command: RegisterAccountCommand) -> bool: if not Account.check_email(command.dto.email): raise BaseApiException("This email is existed in system.") if not Account.check_username(command.dto.username): raise BaseApiException("This username is existed in system.") with transaction.atomic(): new_account: Account = Account.new_normal_account(**command.dto.to_dict()) new_account.save() return True
async def get_list_room_chat(self, account_id: str) -> List[RoomChatResponseDto]: result: list try: async with self.get_connection() as channel: stub = ChatServiceStub(channel) res: GetListRoomChatReply = await stub.GetListRoomChat( GetListRoomChatRequest(account_id=account_id)) result = res.data except grpc.RpcError as ex: raise BaseApiException(ex.details()) return list( map( lambda x: RoomChatResponseDto( x.id, x.avt_icon_url, x.name, x.latest_msg, maya.parse(x.latest_msg_time).datetime(), x.num_un_read_msg, type=x.type, ), result, ))
async def get_matcher_info(self, account_id: str, matcher_id: str) -> MatcherInfoDto: result: Any try: async with self.get_connection() as channel: stub = MatchServiceV1Stub(channel) res: GetMatcherInfoReply = await stub.GetMatcherInfo( GetMatcherInfoRequest(account_id=account_id, matcher_id=matcher_id)) result = res except grpc.RpcError as ex: raise BaseApiException(ex.details()) return MatcherInfoDto( result.matcher_id, result.name, result.age, result.gender, result.address, result.job, result.reason, list(map(lambda x: MediaDto(x.url, x.type), result.medias)), status=result.status, )
def validate_user_can_remove_comment( self, account: Account, comment: UserCommentPost ): base_post: Post = comment.post base_account: Account = base_post.account owner_comment: Account = comment.sender if account not in [base_account, owner_comment]: raise BaseApiException("Account can't delete comment.")
def handle(self, query: GetAccountMatcherListQuery) -> List[MatcherDto]: account: AccountInfoDto = self._uc_gw.get_account_info(query.account_id) if not account: raise BaseApiException(f"Account with id: ${query.account_id} not found.") matcher_list: List[MatcherDto] = asyncio.run( self._match_service.get_matcher_list(account.id) ) return matcher_list
def handle(self, command: CreateUserMatchCommand): account: AccountInfoDto = self._user_content_gw.get_account_info( command.account_id ) if not account: raise BaseApiException(f"Account with id {command.account_id} not found!") asyncio.run(self._match_service.create_match(account.id, dto=command.dto)) return super().handle(command)
def handle(self, command: ChangeAccountPasswordCommand) -> bool: account: Account = Account.objects.find_account_by_id( command.account_id, raise_exception=True) if not check_password(command.dto.old_password, account.password): raise BaseApiException("Old password is incorrect.") account.change_password(command.dto.new_password) account.save() return True
def handle(self, command: UserUpdateAvatarCommand): account: Account = Account.objects.find_account_by_id( command.account_id, raise_exception=True) if not account.profile: raise BaseApiException("Please update personal information.") account_profile: Profile = account.profile account_profile.update_avatar(command.dto.media_url) account.save() return super().handle(command)
def handle(self, command: UpdateUserMatchSettingCommand): account: AccountInfoDto = self._user_content_gw.get_account_info( command.account_id) if not account: raise BaseApiException( f"Account with id {command.account_id} not found!") asyncio.run( self._match_service.update_account_match_setting( account.id, match_setting_dto=command.dto)) return
def handle(self, query: GetMessagesInRoomChatQuery) -> List[MessageChatReponseDto]: account: AccountInfoDto = self._user_content_gw.get_account_info( query.account_id ) if not account: raise BaseApiException(f"Account with id {query.account_id} not found!") messages: List[MessageChatReponseDto] = asyncio.run( self._chat_service.get_messages_in_room_chat(account.id, query.room_id) ) return messages
def handle(self, query: GetAccountRoomListQuery) -> List[RoomChatResponseDto]: account: AccountInfoDto = self._user_content_gw.get_account_info( query.account_id) if not account: raise BaseApiException( f"Account with id {query.account_id} not found.") rooms: List[RoomChatResponseDto] = asyncio.run( self._chat_service.get_list_room_chat(account.id)) return rooms
async def create_room_chat(self, account_id: str, receiver_id: str) -> CreateRoomResponseDto: try: result: CreateRoomChatReply async with self.get_connection() as channel: stub = ChatServiceStub(channel) res: UpdateAccountMatchSettingReply = await stub.CreateRoomChat( CreateRoomChatRequest(account_id=account_id, receiver_id=receiver_id)) result = res return CreateRoomResponseDto(result.room_id) except grpc.RpcError as ex: raise BaseApiException(ex.details())
def handle(self, command: LoginAccountCommand) -> LoginAccountResponseDto: existed_account: Account = ( Account.objects.find_account_by_username( command.dto.username, raise_exception=True ) or None ) if not check_password(command.dto.password, existed_account.password): raise BaseApiException("Wrong password.") return LoginAccountResponseDto( account_id=str(existed_account.id), **existed_account.generate_token() )
async def get_room_info(self, account_id: str, room_id: str) -> RoomInfoDto: result: Any try: async with self.get_connection() as channel: stub = ChatServiceStub(channel) res: GetRoomChatInfoReply = await stub.GetRoomChatInfo( GetRoomChatInfoRequest(account_id=account_id, room_id=room_id)) result = res except grpc.RpcError as ex: raise BaseApiException(es.details()) return RoomInfoDto(result.id, result.partner_id, result.partner_name) return super().get_room_info(account_id, room_id)
def handle(self, command: UserUpdateMediasCommand): account: Account = Account.objects.find_account_by_id( command.account_id) account_profile: Profile = account.profile if not account_profile: raise BaseApiException("Please update account profile.") with transaction.atomic(): collection: Collection = (getattr(account_profile, "collection", None) or account_profile.create_collection()) collection.update_medias( list( map( lambda x: MediaMapper(x.url, map_from_media_type(x.type )), command.medias, ))) return super().handle(command)
async def get_matching_data(self) -> MatchingDataDto: result: Any try: async with self.get_connection() as channel: stub = MatchServiceV1Stub(channel) res: GetMatchingDataReply = await stub.GetMatchingData( GetMatcherInfoRequest()) result = res except grpc.RpcError as ex: raise BaseApiException(ex.details()) return MatchingDataDto( num_smart_chat_users=result.num_smart_chat_users, num_traditional_match_users=result.num_traditional_match_users, nearly_users=list( map( lambda x: MatcherDataDto(x.id, x.avatar, x.name, x.bio, x. age, x.gender), result.nearly_users, )), )
async def get_matcher_list(self, account_id: str) -> List[MatcherDto]: result: list try: async with self.get_connection() as channel: stub = MatchServiceV1Stub(channel) res: GetMatcherListReply = await stub.GetMatcherList( GetMatcherListRequest(account_id=account_id)) result = res.data except grpc.RpcError as ex: raise BaseApiException(ex.details()) return list( map( lambda x: MatcherDto( x.matcher_id, x.name, x.age, x.bio, x.status, list(map(lambda _m: MediaDto(_m.url, _m.type), x.medias)), ), result, ))
def validate_user_can_view_story(self, account: Account, story: UserStory): if not story.is_visible(): raise BaseApiException("Story was disabled")
def validate_user_can_remove_story(cls, account: Account, story: UserStory): owner_story: Account = story.account if account != owner_story: raise BaseApiException("Can't remove story. Permission denied.")
def validate_user_can_remove_post(self, account: Account, post: Post): owner: Account = post.account if account != owner: raise BaseApiException("Account can't remove this post.")
def validate(self): if self.type and self.type not in ReactTypeEnum.values(): raise BaseApiException("Invalid data")
def validate(self): if self.gender and self.gender not in list( AccountGenderEnum.to_dict().values()): raise BaseApiException("Invalid gender")
def validate(self): if self.type not in ["LIKE", "LOVE", "HAHA"]: raise BaseApiException("Invalid react type")
def validate(self): if not self.username: raise BaseApiException("Username not found") if not self.password: raise BaseApiException("Password not found")
def validate(self): if self.type not in ["LOVE", "LIKE"]: raise BaseApiException("Invalid type.")