Merge pull request #217 from ancho/fix/broken-apod-image

apod - fix broken image metadata and error response / logging on room…
This commit is contained in:
Ville Ranki 2022-09-16 10:09:53 +03:00 committed by GitHub
commit 8b248a53ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 24 deletions

37
bot.py
View File

@ -22,7 +22,9 @@ from io import BytesIO
from PIL import Image from PIL import Image
import requests import requests
from nio import AsyncClient, InviteEvent, JoinError, RoomMessageText, MatrixRoom, LoginError, RoomMemberEvent, RoomVisibility, RoomPreset, RoomCreateError, RoomResolveAliasResponse, UploadError, UploadResponse, SyncError from nio import AsyncClient, InviteEvent, JoinError, RoomMessageText, MatrixRoom, LoginError, RoomMemberEvent, \
RoomVisibility, RoomPreset, RoomCreateError, RoomResolveAliasResponse, UploadError, UploadResponse, SyncError, \
RoomPutStateError
from modules.common.exceptions import CommandRequiresAdmin, CommandRequiresOwner, UploadFailed from modules.common.exceptions import CommandRequiresAdmin, CommandRequiresOwner, UploadFailed
@ -258,7 +260,7 @@ class Bot:
"msgtype": "m.image", "msgtype": "m.image",
"info": { "info": {
"thumbnail_info": None, "thumbnail_info": None,
"thumbnail_url": None, "thumbnail_url": url,
}, },
} }
@ -271,37 +273,28 @@ class Bot:
if size: if size:
msg["info"]["size"] = size msg["info"]["size"] = size
self.logger.debug(f"send image room message: {msg}")
return await self.room_send(room.room_id, event, 'm.room.message', msg) return await self.room_send(room.room_id, event, 'm.room.message', msg)
async def set_room_avatar(self, room, uri, mimetype=None, width=None, height=None, size=None): async def set_room_avatar(self, room, uri):
""" """
:param room: A MatrixRoom the image should be send to :param room: A MatrixRoom the image should be send as room avatar event
:param uri: A MXC-Uri https://matrix.org/docs/spec/client_server/r0.6.0#mxc-uri :param uri: A MXC-Uri https://matrix.org/docs/spec/client_server/r0.6.0#mxc-uri
:param mimetype: The mimetype of the image
:param width: Width in pixel of the image
:param height: Height in pixel of the image
:param size: Size in bytes of the image
:return: :return:
""" """
msg = { msg = {
"url": uri, "url": uri
"info": {
"thumbnail_info": None,
"thumbnail_url": None,
},
} }
if mimetype: result = await self.client.room_put_state(room.room_id, 'm.room.avatar', msg)
msg["info"]["mimetype"] = mimetype
if width:
msg["info"]["w"] = width
if height:
msg["info"]["h"] = height
if size:
msg["info"]["size"] = size
return await self.client.room_put_state(room.room_id, 'm.room.avatar', msg) if isinstance(result, RoomPutStateError):
self.logger.warning(f"can't set room avatar. {result.message}")
await self.send_text(room, f"sorry. can't set room avatar. I need at least be a moderator")
return result
async def send_msg(self, mxid, roomname, message): async def send_msg(self, mxid, roomname, message):
""" """

View File

@ -107,10 +107,10 @@ class MatrixModule(BotModule):
matrix_uri, mimetype, w, h, size = await bot.upload_image(apod.hdurl) matrix_uri, mimetype, w, h, size = await bot.upload_image(apod.hdurl)
except (UploadFailed, TypeError, ValueError): except (UploadFailed, TypeError, ValueError):
await bot.send_text(room, f"Something went wrong uploading {apod.hdurl}.") await bot.send_text(room, f"Something went wrong uploading {apod.hdurl}.")
await bot.send_image(room, matrix_uri, apod.hdurl, mimetype, w, h, size) await bot.send_image(room, matrix_uri, apod.hdurl, None, mimetype, w, h, size)
await bot.send_text(room, f"{apod.explanation}") await bot.send_text(room, f"{apod.explanation}")
if matrix_uri and set_room_avatar: if matrix_uri and set_room_avatar:
await bot.set_room_avatar(room, matrix_uri, mimetype, w, h, size) await bot.set_room_avatar(room, matrix_uri, None, mimetype, w, h, size)
async def send_unknown_mediatype(self, room, bot, apod): async def send_unknown_mediatype(self, room, bot, apod):
self.logger.debug(f"unknown media_type: {apod.media_type}. sending raw information") self.logger.debug(f"unknown media_type: {apod.media_type}. sending raw information")