forked from fair/matrix-puppeteer-line
Allow syncing pathless avatar images
But only if a path wasn't yet found for that image
This commit is contained in:
parent
14de373787
commit
ec14b90711
@ -447,17 +447,32 @@ class Portal(DBPortal, BasePortal):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
async def _update_icon(self, icon: Optional[PathImage], client: Client) -> bool:
|
async def _update_icon(self, icon: Optional[PathImage], client: Client) -> bool:
|
||||||
icon_path = icon.path if icon else None
|
if icon:
|
||||||
|
if icon.url and not icon.path:
|
||||||
|
self.log.warn(f"Using URL as path for room icon of {self.name}")
|
||||||
|
icon_path = icon_url = icon.url
|
||||||
|
else:
|
||||||
|
icon_path = icon.path
|
||||||
|
icon_url = icon.url
|
||||||
|
else:
|
||||||
|
icon_path = icon_url = None
|
||||||
|
|
||||||
if icon_path != self.icon_path:
|
if icon_path != self.icon_path:
|
||||||
|
self.log.info(f"Updating room icon of {self.name}")
|
||||||
self.icon_path = icon_path
|
self.icon_path = icon_path
|
||||||
if icon and icon.url:
|
if icon_url:
|
||||||
resp = await client.read_image(icon.url)
|
resp = await client.read_image(icon.url)
|
||||||
self.icon_mxc = await self.main_intent.upload_media(resp.data, mime_type=resp.mime)
|
self.icon_mxc = await self.main_intent.upload_media(resp.data, mime_type=resp.mime)
|
||||||
else:
|
else:
|
||||||
self.icon_mxc = ContentURI("")
|
self.icon_mxc = ContentURI("")
|
||||||
if self.mxid:
|
if self.mxid:
|
||||||
|
try:
|
||||||
await self.main_intent.set_room_avatar(self.mxid, self.icon_mxc)
|
await self.main_intent.set_room_avatar(self.mxid, self.icon_mxc)
|
||||||
|
except Exception as e:
|
||||||
|
self.log.exception(f"Failed to set room icon: {e}")
|
||||||
return True
|
return True
|
||||||
|
else:
|
||||||
|
self.log.debug(f"No need to update room icon of {self.name}, new icon has same path as old one")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def _update_participants(self, participants: List[Participant]) -> None:
|
async def _update_participants(self, participants: List[Participant]) -> None:
|
||||||
|
@ -85,14 +85,24 @@ class Puppet(DBPuppet, BasePuppet):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
async def _update_avatar(self, avatar: Optional[PathImage], client: Client) -> bool:
|
async def _update_avatar(self, avatar: Optional[PathImage], client: Client) -> bool:
|
||||||
if avatar and avatar.url and not avatar.path:
|
if avatar:
|
||||||
# Avatar exists, but in a form that cannot be uniquely identified.
|
if avatar.url and not avatar.path:
|
||||||
# Skip it for now.
|
if self.avatar_set and self.avatar_path:
|
||||||
|
self.log.warn(f"Not updating user avatar of {self.name}: new avatar exists, but in a form that cannot be uniquely identified")
|
||||||
return False
|
return False
|
||||||
avatar_path = avatar.path if avatar else None
|
else:
|
||||||
if avatar_path != self.avatar_path or not self.avatar_set:
|
self.log.warn(f"Using URL as path for user avatar of {self.name}: no previous avatar exists")
|
||||||
|
avatar_path = avatar_url = avatar.url
|
||||||
|
else:
|
||||||
|
avatar_path = avatar.path
|
||||||
|
avatar_url = avatar.url
|
||||||
|
else:
|
||||||
|
avatar_path = avatar_url = None
|
||||||
|
|
||||||
|
if not self.avatar_set or avatar_path != self.avatar_path:
|
||||||
|
self.log.info(f"Updating user avatar of {self.name}")
|
||||||
self.avatar_path = avatar_path
|
self.avatar_path = avatar_path
|
||||||
if avatar and avatar.url:
|
if avatar_url:
|
||||||
resp = await client.read_image(avatar.url)
|
resp = await client.read_image(avatar.url)
|
||||||
self.avatar_mxc = await self.intent.upload_media(resp.data, mime_type=resp.mime)
|
self.avatar_mxc = await self.intent.upload_media(resp.data, mime_type=resp.mime)
|
||||||
else:
|
else:
|
||||||
@ -100,10 +110,12 @@ class Puppet(DBPuppet, BasePuppet):
|
|||||||
try:
|
try:
|
||||||
await self.intent.set_avatar_url(self.avatar_mxc)
|
await self.intent.set_avatar_url(self.avatar_mxc)
|
||||||
self.avatar_set = True
|
self.avatar_set = True
|
||||||
except Exception:
|
except Exception as e:
|
||||||
self.log.exception("Failed to set user avatar")
|
self.log.exception(f"Failed to set user avatar: {e}")
|
||||||
self.avatar_set = False
|
self.avatar_set = False
|
||||||
return True
|
return True
|
||||||
|
else:
|
||||||
|
self.log.debug(f"No need to update user avatar of {self.name}, new avatar has same path as old one")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def _add_to_cache(self) -> None:
|
def _add_to_cache(self) -> None:
|
||||||
|
@ -26,7 +26,7 @@ class RPCError(Exception):
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class PathImage(SerializableAttrs['PathImage']):
|
class PathImage(SerializableAttrs['PathImage']):
|
||||||
path: str
|
path: Optional[str]
|
||||||
url: str
|
url: str
|
||||||
|
|
||||||
|
|
||||||
|
@ -166,7 +166,7 @@ class MautrixController {
|
|||||||
/**
|
/**
|
||||||
* Try to match a user against an entry in the friends list to get their ID.
|
* Try to match a user against an entry in the friends list to get their ID.
|
||||||
*
|
*
|
||||||
* @param {Element} element - The display name of the user to find the ID for.
|
* @param {string} senderName - The display name of the user to find the ID for.
|
||||||
* @return {?string} - The user's ID if found.
|
* @return {?string} - The user's ID if found.
|
||||||
*/
|
*/
|
||||||
getUserIdFromFriendsList(senderName) {
|
getUserIdFromFriendsList(senderName) {
|
||||||
@ -518,10 +518,14 @@ class MautrixController {
|
|||||||
/**
|
/**
|
||||||
* @typedef PathImage
|
* @typedef PathImage
|
||||||
* @type object
|
* @type object
|
||||||
* @property {string} path - The virtual path of the image (behaves like an ID)
|
* @property {?string} path - The virtual path of the image (behaves like an ID). Optional.
|
||||||
* @property {string} src - The URL of the image
|
* @property {string} src - The URL of the image. Mandatory.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {Element} img - The image element to get the URL and path of.
|
||||||
|
* @return {?PathImage} - The image URL and its path, if found.
|
||||||
|
*/
|
||||||
_getPathImage(img) {
|
_getPathImage(img) {
|
||||||
if (img && img.src.startsWith("blob:")) {
|
if (img && img.src.startsWith("blob:")) {
|
||||||
// NOTE Having a blob but no path means the image exists,
|
// NOTE Having a blob but no path means the image exists,
|
||||||
@ -540,7 +544,7 @@ class MautrixController {
|
|||||||
* @typedef Participant
|
* @typedef Participant
|
||||||
* @type object
|
* @type object
|
||||||
* @property {string} id - The member ID for the participant
|
* @property {string} id - The member ID for the participant
|
||||||
* @property {PathImage} avatar - The path and blob URL of the participant's avatar
|
* @property {?PathImage} avatar - The path and blob URL of the participant's avatar
|
||||||
* @property {string} name - The contact list name of the participant
|
* @property {string} name - The contact list name of the participant
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user