Single status code for MatrixReuploader

This commit is contained in:
Dawid Rejowski 2022-09-11 18:48:11 +02:00
parent 0f41c6f0b7
commit f5529744f6
2 changed files with 29 additions and 31 deletions

View file

@ -46,31 +46,31 @@ class Command:
pack_name = self.args[0]
reuploader = MatrixReuploader(self.client, self.room, exporter=self.tg_exporter)
async for status in reuploader.import_stickerset_to_room(pack_name):
text = 'Warning: Unknown status'
if status == MatrixReuploader.STATUS_DOWNLOADING:
text = f'Downloading stickerpack {pack_name}...'
if status == MatrixReuploader.STATUS_UPLOADING:
text = f'Uploading stickerpack {pack_name}...'
if status == MatrixReuploader.STATUS_UPDATING_ROOM_STATE:
text = f'Updating room state...'
await send_text_to_room(self.client, self.room.room_id, text)
if reuploader.result == MatrixReuploader.RESULT_OK:
text = 'Done 😄'
if reuploader.result == MatrixReuploader.RESULT_NO_PERMISSION:
text = (
'I do not have permissions to create any stickerpack in this room\n'
'Please, give me mod 🙏'
)
if reuploader.result == MatrixReuploader.RESULT_PACK_EXISTS:
text = (
f"Stickerpack '{pack_name}' already exists.\n"
'Please delete it first.'
)
if reuploader.result == MatrixReuploader.RESULT_PACK_EMPTY:
text = (
f'Warning: Telegram pack {pack_name} find out empty or not existing.'
)
await send_text_to_room(self.client, self.room.room_id, text)
if status == MatrixReuploader.STATUS_OK:
text = 'Done 😄'
if status == MatrixReuploader.STATUS_NO_PERMISSION:
text = (
'I do not have permissions to create any stickerpack in this room\n'
'Please, give me mod 🙏'
)
if status == MatrixReuploader.STATUS_PACK_EXISTS:
text = (
f"Stickerpack '{pack_name}' already exists.\n"
'Please delete it first.'
)
if status == MatrixReuploader.STATUS_PACK_EMPTY:
text = (
f'Warning: Telegram pack {pack_name} find out empty or not existing.'
)
await send_text_to_room(self.client, self.room.room_id, text)
async def _unknown_command(self):
await send_text_to_room(

View file

@ -9,14 +9,14 @@ from telegram_exporter import TelegramExporter
class MatrixReuploader:
RESULT_OK = 0
RESULT_NO_PERMISSION = 1
RESULT_PACK_EXISTS = 2
RESULT_PACK_EMPTY = 3
STATUS_OK = 0
STATUS_NO_PERMISSION = 1
STATUS_PACK_EXISTS = 2
STATUS_PACK_EMPTY = 3
STATUS_DOWNLOADING = 1
STATUS_UPLOADING = 2
STATUS_UPDATING_ROOM_STATE = 3
STATUS_DOWNLOADING = 4
STATUS_UPLOADING = 5
STATUS_UPDATING_ROOM_STATE = 6
def __init__(self, client: AsyncClient, room: MatrixRoom, exporter: TelegramExporter = None,
pack: list[Sticker] = None):
@ -29,19 +29,17 @@ class MatrixReuploader:
self.exporter = exporter
self.pack = pack
self.result = -1
async def _has_permission_to_upload(self) -> bool:
return await has_permission(self.client, self.room.room_id, 'state_default')
async def import_stickerset_to_room(self, pack_name: str):
if not await self._has_permission_to_upload():
self.result = self.RESULT_NO_PERMISSION
yield self.STATUS_NO_PERMISSION
return
stickerset = MatrixStickerset(pack_name)
if await is_stickerpack_existing(self.client, self.room.room_id, stickerset.name()):
self.result = self.RESULT_PACK_EXISTS
yield self.STATUS_PACK_EXISTS
return
yield self.STATUS_DOWNLOADING
@ -54,10 +52,10 @@ class MatrixReuploader:
stickerset.add_sticker(sticker_mxc, sticker.alt_text)
if not stickerset.count():
self.result = self.RESULT_PACK_EMPTY
yield self.STATUS_PACK_EMPTY
return
yield self.STATUS_UPDATING_ROOM_STATE
await upload_stickerpack(self.client, self.room.room_id, stickerset)
self.result = self.RESULT_OK
yield self.STATUS_OK