From 4ec69c2a05d87dd8232cf99e4b7f59ce22d6a54a Mon Sep 17 00:00:00 2001 From: profitroll Date: Thu, 16 Mar 2023 15:03:14 +0100 Subject: [PATCH] This commit closes #9 --- classes/poster_client.py | 8 +++++--- config.json | 1 + modules/api_client.py | 16 +++++++++------- plugins/callbacks/submission.py | 12 +++++++++--- plugins/handlers/submission.py | 8 ++++++-- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/classes/poster_client.py b/classes/poster_client.py index 642bede..ba0aace 100644 --- a/classes/poster_client.py +++ b/classes/poster_client.py @@ -1,6 +1,6 @@ from os import path, remove, sep from shutil import rmtree -from typing import Union +from typing import Tuple, Union from pyrogram.client import Client from pyrogram.types import Message from classes.exceptions import SubmissionDuplicatesError, SubmissionUnavailableError @@ -18,7 +18,9 @@ class PosterClient(Client): self.owner = configGet("owner") self.admins = configGet("admins") + [configGet("owner")] - async def submit_photo(self, id: str) -> Union[Message, None]: + async def submit_photo( + self, id: str + ) -> Tuple[Union[Message, None], Union[str, None]]: db_entry = col_submitted.find_one({"_id": ObjectId(id)}) submission = None @@ -87,7 +89,7 @@ class PosterClient(Client): f"Could not delete '{filepath}' on submission accepted", debug=True ) - return submission + return submission, response[2] async def ban_user(self, id: int) -> None: pass diff --git a/config.json b/config.json index 191f736..692e452 100644 --- a/config.json +++ b/config.json @@ -96,6 +96,7 @@ "file_size": 15728640, "tmp_size": 15728640, "allow_duplicates": false, + "send_uploaded_id": false, "require_confirmation": { "users": true, "admins": true diff --git a/modules/api_client.py b/modules/api_client.py index cb7e750..cd8af06 100644 --- a/modules/api_client.py +++ b/modules/api_client.py @@ -96,7 +96,7 @@ async def random_pic(token: Union[str, None] = None) -> Tuple[str, str]: async def upload_pic( filepath: str, allow_duplicates: bool = False, token: Union[str, None] = None -) -> Tuple[bool, list, str]: +) -> Tuple[bool, list, Union[str, None]]: token = await authorize() if token is None else token try: pic_name = path.basename(filepath) @@ -117,6 +117,7 @@ async def upload_pic( headers={"Authorization": f"Bearer {token}"}, data=formdata, ) + response_json = await response.json() if response.status != 200 and response.status != 409: logWrite( f"Could not upload '{filepath}' to API: HTTP {response.status} with message '{response.content}'" @@ -124,21 +125,22 @@ async def upload_pic( raise SubmissionUploadError( str(filepath), response.status, response.content ) + id = response_json["id"] if "id" in await response.json() else None duplicates = [] - if "duplicates" in (await response.json()): - for index, duplicate in enumerate((await response.json())["duplicates"]): - if (await response.json())["access_token"] is None: + if "duplicates" in response_json: + for index, duplicate in enumerate(response_json)["duplicates"]: # type: ignore + if response_json["access_token"] is None: duplicates.append( f'`{duplicate["id"]}`:\n{configGet("address_external", "posting", "api")}/photos/{duplicate["id"]}' ) else: duplicates.append( - f'`{duplicate["id"]}`:\n{configGet("address_external", "posting", "api")}/token/photo/{(await response.json())["access_token"]}?id={index}' + f'`{duplicate["id"]}`:\n{configGet("address_external", "posting", "api")}/token/photo/{response_json["access_token"]}?id={index}' ) - return True, duplicates, (await response.json())["id"] + return True, duplicates, id except Exception as exp: print_exc() - return False, [], "" + return False, [], None async def find_pic( diff --git a/plugins/callbacks/submission.py b/plugins/callbacks/submission.py index 0f902c8..f182b76 100644 --- a/plugins/callbacks/submission.py +++ b/plugins/callbacks/submission.py @@ -40,9 +40,9 @@ async def callback_query_yes(app: PosterClient, clb: CallbackQuery): ) return - if submission is not None: - await submission.reply_text( - locale("sub_yes", "message", locale=submission.from_user.language_code), + if submission[0] is not None: + await submission[0].reply_text( + locale("sub_yes", "message", locale=submission[0].from_user.language_code), quote=True, ) elif db_entry is not None: @@ -73,6 +73,12 @@ async def callback_query_yes(app: PosterClient, clb: CallbackQuery): ] ] ) + + if configGet("send_uploaded_id", "submission"): + await clb.message.edit_caption( + clb.message.caption + f"\n\nID: `{submission[1]}`" + ) + await clb.message.edit_reply_markup( reply_markup=InlineKeyboardMarkup(edited_markup) ) diff --git a/plugins/handlers/submission.py b/plugins/handlers/submission.py index 759a2c0..112f767 100644 --- a/plugins/handlers/submission.py +++ b/plugins/handlers/submission.py @@ -188,12 +188,14 @@ async def get_submission(app: PosterClient, msg: Message): and configGet("admins", "submission", "require_confirmation") is False ): try: - await app.submit_photo(str(inserted.inserted_id)) + submitted = await app.submit_photo(str(inserted.inserted_id)) await msg.reply_text( locale("sub_yes_auto", "message", locale=user_locale), disable_notification=True, quote=True, ) + if configGet("send_uploaded_id", "submission"): + caption += f"\n\nID: `{submitted[1]}`" await msg.copy(app.owner, caption=caption, disable_notification=True) return except SubmissionDuplicatesError as exp: @@ -212,12 +214,14 @@ async def get_submission(app: PosterClient, msg: Message): and configGet("users", "submission", "require_confirmation") is False ): try: - await app.submit_photo(str(inserted.inserted_id)) + submitted = await app.submit_photo(str(inserted.inserted_id)) await msg.reply_text( locale("sub_yes_auto", "message", locale=user_locale), disable_notification=True, quote=True, ) + if configGet("send_uploaded_id", "submission"): + caption += f"\n\nID: `{submitted[1]}`" await msg.copy(app.owner, caption=caption) return except SubmissionDuplicatesError as exp: