This commit closes #9

This commit is contained in:
Profitroll 2023-03-16 15:03:14 +01:00
parent 62e0a4986c
commit 4ec69c2a05
5 changed files with 30 additions and 15 deletions

View File

@ -1,6 +1,6 @@
from os import path, remove, sep from os import path, remove, sep
from shutil import rmtree from shutil import rmtree
from typing import Union from typing import Tuple, Union
from pyrogram.client import Client from pyrogram.client import Client
from pyrogram.types import Message from pyrogram.types import Message
from classes.exceptions import SubmissionDuplicatesError, SubmissionUnavailableError from classes.exceptions import SubmissionDuplicatesError, SubmissionUnavailableError
@ -18,7 +18,9 @@ class PosterClient(Client):
self.owner = configGet("owner") self.owner = configGet("owner")
self.admins = configGet("admins") + [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)}) db_entry = col_submitted.find_one({"_id": ObjectId(id)})
submission = None submission = None
@ -87,7 +89,7 @@ class PosterClient(Client):
f"Could not delete '{filepath}' on submission accepted", debug=True f"Could not delete '{filepath}' on submission accepted", debug=True
) )
return submission return submission, response[2]
async def ban_user(self, id: int) -> None: async def ban_user(self, id: int) -> None:
pass pass

View File

@ -96,6 +96,7 @@
"file_size": 15728640, "file_size": 15728640,
"tmp_size": 15728640, "tmp_size": 15728640,
"allow_duplicates": false, "allow_duplicates": false,
"send_uploaded_id": false,
"require_confirmation": { "require_confirmation": {
"users": true, "users": true,
"admins": true "admins": true

View File

@ -96,7 +96,7 @@ async def random_pic(token: Union[str, None] = None) -> Tuple[str, str]:
async def upload_pic( async def upload_pic(
filepath: str, allow_duplicates: bool = False, token: Union[str, None] = None 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 token = await authorize() if token is None else token
try: try:
pic_name = path.basename(filepath) pic_name = path.basename(filepath)
@ -117,6 +117,7 @@ async def upload_pic(
headers={"Authorization": f"Bearer {token}"}, headers={"Authorization": f"Bearer {token}"},
data=formdata, data=formdata,
) )
response_json = await response.json()
if response.status != 200 and response.status != 409: if response.status != 200 and response.status != 409:
logWrite( logWrite(
f"Could not upload '{filepath}' to API: HTTP {response.status} with message '{response.content}'" f"Could not upload '{filepath}' to API: HTTP {response.status} with message '{response.content}'"
@ -124,21 +125,22 @@ async def upload_pic(
raise SubmissionUploadError( raise SubmissionUploadError(
str(filepath), response.status, response.content str(filepath), response.status, response.content
) )
id = response_json["id"] if "id" in await response.json() else None
duplicates = [] duplicates = []
if "duplicates" in (await response.json()): if "duplicates" in response_json:
for index, duplicate in enumerate((await response.json())["duplicates"]): for index, duplicate in enumerate(response_json)["duplicates"]: # type: ignore
if (await response.json())["access_token"] is None: if response_json["access_token"] is None:
duplicates.append( duplicates.append(
f'`{duplicate["id"]}`:\n{configGet("address_external", "posting", "api")}/photos/{duplicate["id"]}' f'`{duplicate["id"]}`:\n{configGet("address_external", "posting", "api")}/photos/{duplicate["id"]}'
) )
else: else:
duplicates.append( 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: except Exception as exp:
print_exc() print_exc()
return False, [], "" return False, [], None
async def find_pic( async def find_pic(

View File

@ -40,9 +40,9 @@ async def callback_query_yes(app: PosterClient, clb: CallbackQuery):
) )
return return
if submission is not None: if submission[0] is not None:
await submission.reply_text( await submission[0].reply_text(
locale("sub_yes", "message", locale=submission.from_user.language_code), locale("sub_yes", "message", locale=submission[0].from_user.language_code),
quote=True, quote=True,
) )
elif db_entry is not None: 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( await clb.message.edit_reply_markup(
reply_markup=InlineKeyboardMarkup(edited_markup) reply_markup=InlineKeyboardMarkup(edited_markup)
) )

View File

@ -188,12 +188,14 @@ async def get_submission(app: PosterClient, msg: Message):
and configGet("admins", "submission", "require_confirmation") is False and configGet("admins", "submission", "require_confirmation") is False
): ):
try: try:
await app.submit_photo(str(inserted.inserted_id)) submitted = await app.submit_photo(str(inserted.inserted_id))
await msg.reply_text( await msg.reply_text(
locale("sub_yes_auto", "message", locale=user_locale), locale("sub_yes_auto", "message", locale=user_locale),
disable_notification=True, disable_notification=True,
quote=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) await msg.copy(app.owner, caption=caption, disable_notification=True)
return return
except SubmissionDuplicatesError as exp: except SubmissionDuplicatesError as exp:
@ -212,12 +214,14 @@ async def get_submission(app: PosterClient, msg: Message):
and configGet("users", "submission", "require_confirmation") is False and configGet("users", "submission", "require_confirmation") is False
): ):
try: try:
await app.submit_photo(str(inserted.inserted_id)) submitted = await app.submit_photo(str(inserted.inserted_id))
await msg.reply_text( await msg.reply_text(
locale("sub_yes_auto", "message", locale=user_locale), locale("sub_yes_auto", "message", locale=user_locale),
disable_notification=True, disable_notification=True,
quote=True, quote=True,
) )
if configGet("send_uploaded_id", "submission"):
caption += f"\n\nID: `{submitted[1]}`"
await msg.copy(app.owner, caption=caption) await msg.copy(app.owner, caption=caption)
return return
except SubmissionDuplicatesError as exp: except SubmissionDuplicatesError as exp: