Ejemplo n.º 1
0
    async def update_avatar(self, source: 'AbstractUser',
                            photo: Union[UserProfilePhoto, UserProfilePhotoEmpty]) -> bool:
        if self.disable_updates:
            return False

        if isinstance(photo, UserProfilePhotoEmpty):
            photo_id = ""
        else:
            photo_id = str(photo.photo_id)
        if self.photo_id != photo_id:
            if not photo_id:
                self.photo_id = ""
                try:
                    await self.default_mxid_intent.set_avatar_url("")
                except MatrixRequestError:
                    self.log.exception("Failed to set avatar")
                    self.photo_id = ""
                return True

            loc = InputPeerPhotoFileLocation(
                peer=await self.get_input_entity(source),
                local_id=photo.photo_big.local_id,
                volume_id=photo.photo_big.volume_id,
                big=True
            )
            file = await util.transfer_file_to_matrix(source.client, self.default_mxid_intent, loc)
            if file:
                self.photo_id = photo_id
                try:
                    await self.default_mxid_intent.set_avatar_url(file.mxc)
                except MatrixRequestError:
                    self.log.exception("Failed to set avatar")
                    self.photo_id = ""
                return True
        return False
Ejemplo n.º 2
0
 async def _update_avatar(self,
                          user: '******',
                          photo: TypeChatPhoto,
                          save: bool = False) -> bool:
     if isinstance(photo, ChatPhoto):
         loc = InputPeerPhotoFileLocation(
             peer=await self.get_input_entity(user),
             local_id=photo.photo_big.local_id,
             volume_id=photo.photo_big.volume_id,
             big=True)
         photo_id = f"{loc.volume_id}-{loc.local_id}"
     elif isinstance(photo, Photo):
         loc, largest = self._get_largest_photo_size(photo)
         photo_id = f"{largest.location.volume_id}-{largest.location.local_id}"
     elif isinstance(photo, (ChatPhotoEmpty, PhotoEmpty)):
         photo_id = ""
         loc = None
     else:
         raise ValueError(f"Unknown photo type {type(photo)}")
     if self.photo_id != photo_id:
         if not photo_id:
             await self.main_intent.set_room_avatar(self.mxid, None)
             self.photo_id = ""
             if save:
                 self.save()
             return True
         file = await util.transfer_file_to_matrix(user.client,
                                                   self.main_intent, loc)
         if file:
             await self.main_intent.set_room_avatar(self.mxid, file.mxc)
             self.photo_id = photo_id
             if save:
                 self.save()
             return True
     return False
Ejemplo n.º 3
0
 async def _update_avatar(self,
                          user: '******',
                          photo: TypeChatPhoto,
                          sender: Optional['p.Puppet'] = None,
                          save: bool = False) -> bool:
     if isinstance(photo, (ChatPhoto, UserProfilePhoto)):
         loc = InputPeerPhotoFileLocation(
             peer=await self.get_input_entity(user),
             local_id=photo.photo_big.local_id,
             volume_id=photo.photo_big.volume_id,
             big=True)
         photo_id = (f"{loc.volume_id}-{loc.local_id}" if isinstance(
             photo, ChatPhoto) else photo.photo_id)
     elif isinstance(photo, Photo):
         loc, largest = self._get_largest_photo_size(photo)
         photo_id = f"{largest.location.volume_id}-{largest.location.local_id}"
     elif isinstance(
             photo,
         (UserProfilePhotoEmpty, ChatPhotoEmpty, PhotoEmpty, type(None))):
         photo_id = ""
         loc = None
     else:
         raise ValueError(f"Unknown photo type {type(photo)}")
     if self.peer_type == "user" and not photo_id and not config[
             "bridge.allow_avatar_remove"]:
         return False
     if self.photo_id != photo_id:
         if not photo_id:
             await self._try_set_state(
                 sender, EventType.ROOM_AVATAR,
                 RoomAvatarStateEventContent(url=None))
             self.photo_id = ""
             self.avatar_url = None
             if save:
                 await self.save()
             return True
         file = await util.transfer_file_to_matrix(user.client,
                                                   self.main_intent, loc)
         if file:
             await self._try_set_state(
                 sender, EventType.ROOM_AVATAR,
                 RoomAvatarStateEventContent(url=file.mxc))
             self.photo_id = photo_id
             self.avatar_url = file.mxc
             if save:
                 await self.save()
             return True
     return False
Ejemplo n.º 4
0
    async def update_avatar(
            self, source: 'AbstractUser',
            photo: Union[UserProfilePhoto, UserProfilePhotoEmpty]) -> bool:
        if self.disable_updates:
            return False

        if photo is None or isinstance(photo, UserProfilePhotoEmpty):
            photo_id = ""
        elif isinstance(photo, UserProfilePhoto):
            photo_id = str(photo.photo_id)
        else:
            self.log.warning(f"Unknown user profile photo type: {type(photo)}")
            return False
        if not photo_id and not config["bridge.allow_avatar_remove"]:
            return False
        if self.photo_id != photo_id:
            if not photo_id:
                self.photo_id = ""
                try:
                    await self.default_mxid_intent.set_avatar_url(
                        ContentURI(""))
                except MatrixError:
                    self.log.exception("Failed to set avatar")
                    self.photo_id = ""
                return True

            loc = InputPeerPhotoFileLocation(
                peer=await self.get_input_entity(source),
                local_id=photo.photo_big.local_id,
                volume_id=photo.photo_big.volume_id,
                big=True)
            file = await util.transfer_file_to_matrix(source.client,
                                                      self.default_mxid_intent,
                                                      loc)
            if file:
                self.photo_id = photo_id
                try:
                    await self.default_mxid_intent.set_avatar_url(file.mxc)
                except MatrixError:
                    self.log.exception("Failed to set avatar")
                    self.photo_id = ""
                return True
        return False
Ejemplo n.º 5
0
    async def update_avatar(
        self, source: au.AbstractUser, photo: TypeUserProfilePhoto | TypeChatPhoto
    ) -> bool:
        if self.disable_updates:
            return False

        if photo is None or isinstance(photo, (UserProfilePhotoEmpty, ChatPhotoEmpty)):
            photo_id = ""
        elif isinstance(photo, (UserProfilePhoto, ChatPhoto)):
            photo_id = str(photo.photo_id)
        else:
            self.log.warning(f"Unknown user profile photo type: {type(photo)}")
            return False
        if not photo_id and not self.config["bridge.allow_avatar_remove"]:
            return False
        if self.photo_id != photo_id or not self.avatar_set:
            if not photo_id:
                self.photo_id = ""
                self.avatar_url = None
            elif self.photo_id != photo_id or not self.avatar_url:
                file = await util.transfer_file_to_matrix(
                    client=source.client,
                    intent=self.default_mxid_intent,
                    location=InputPeerPhotoFileLocation(
                        peer=await self.get_input_entity(source), photo_id=photo.photo_id, big=True
                    ),
                    async_upload=self.config["homeserver.async_media"],
                )
                if not file:
                    return False
                self.photo_id = photo_id
                self.avatar_url = file.mxc
            try:
                await self.default_mxid_intent.set_avatar_url(self.avatar_url or "")
                self.avatar_set = True
            except Exception as e:
                self.log.warning(f"Failed to set avatar: {e}")
                self.avatar_set = False
            return True
        return False