dev #19

Merged
profitroll merged 98 commits from dev into master 2023-04-24 13:48:22 +03:00
5 changed files with 94 additions and 56 deletions
Showing only changes of commit 4cd37be5dc - Show all commits

View File

@ -30,7 +30,8 @@
"api_queue_error": "__TO_BE_ADDED__", "api_queue_error": "__TO_BE_ADDED__",
"post_low": "Low amount of content: `There are only {0} files left in the queue.`", "post_low": "Low amount of content: `There are only {0} files left in the queue.`",
"api_creds_invalid": "__TO_BE_ADDED__", "api_creds_invalid": "__TO_BE_ADDED__",
"sub_wip": "Post submission is now WIP. It will be available again in a few days. Thank you for your patience." "sub_wip": "Post submission is now WIP. It will be available again in a few days. Thank you for your patience.",
"sub_duplicates_found": "__TO_BE_ADDED__"
}, },
"button": { "button": {
"sub_yes": "✅ Accept", "sub_yes": "✅ Accept",
@ -49,7 +50,9 @@
"sub_unblock": "User {0} has been unblocked", "sub_unblock": "User {0} has been unblocked",
"sub_msg_unavail": "Submission message no longer exist", "sub_msg_unavail": "Submission message no longer exist",
"sub_media_unavail": "Could not download submission", "sub_media_unavail": "Could not download submission",
"sub_done": "You've already decided what to do with submission" "sub_done": "You've already decided what to do with submission",
"sub_upload_failed": "__TO_BE_ADDED__",
"sub_duplicates_found": "__TO_BE_ADDED__"
}, },
"console": { "console": {
"shutdown": "Shutting down bot with pid {0}", "shutdown": "Shutting down bot with pid {0}",

View File

@ -30,7 +30,8 @@
"api_queue_error": "__TO_BE_ADDED__", "api_queue_error": "__TO_BE_ADDED__",
"post_low": "Мала кількість контенту: `Залишилось всього {0} файлів в черзі.`", "post_low": "Мала кількість контенту: `Залишилось всього {0} файлів в черзі.`",
"api_creds_invalid": "__TO_BE_ADDED__", "api_creds_invalid": "__TO_BE_ADDED__",
"sub_wip": "Подання постів зараз знаходиться у розробці. Він буде знову доступний через кілька днів. Дякуємо за ваше терпіння." "sub_wip": "Подання постів зараз знаходиться у розробці. Він буде знову доступний через кілька днів. Дякуємо за ваше терпіння.",
"sub_duplicates_found": "__TO_BE_ADDED__"
}, },
"button": { "button": {
"sub_yes": "✅ Прийняти", "sub_yes": "✅ Прийняти",
@ -49,7 +50,9 @@
"sub_unblock": "Користувача {0} розблоковано", "sub_unblock": "Користувача {0} розблоковано",
"sub_msg_unavail": "Повідомлення більше не існує", "sub_msg_unavail": "Повідомлення більше не існує",
"sub_media_unavail": "Не вдалося завантажити подання", "sub_media_unavail": "Не вдалося завантажити подання",
"sub_done": "Ви вже обрали що зробити з цим поданням" "sub_done": "Ви вже обрали що зробити з цим поданням",
"sub_upload_failed": "__TO_BE_ADDED__",
"sub_duplicates_found": "__TO_BE_ADDED__"
}, },
"console": { "console": {
"shutdown": "Вимкнення бота з підом {0}", "shutdown": "Вимкнення бота з підом {0}",

View File

@ -55,11 +55,13 @@ async def upload_pic(filepath: str) -> Tuple[bool, list]:
try: try:
pic_name = path.basename(filepath) pic_name = path.basename(filepath)
files = {'file': (pic_name, open(filepath, 'rb'), 'image/jpeg')} files = {'file': (pic_name, open(filepath, 'rb'), 'image/jpeg')}
response = post(f'{configGet("address", "posting", "api")}/albums/{configGet("album", "posting", "api")}/photos&caption=queue', headers={"Authorization": f"Bearer {token}"}, files=files).json() response = post(f'{configGet("address", "posting", "api")}/albums/{configGet("album", "posting", "api")}/photos', params={"caption": "queue", "compress": False}, headers={"Authorization": f"Bearer {token}"}, files=files)
print(response, flush=True) if response.status_code != 200 and response.status_code != 409:
logWrite(f"Could not upload '{filepath}' to API: HTTP {response.status_code} with message '{response.content}'")
return False, []
duplicates = [] duplicates = []
if "duplicates" in response: if "duplicates" in response.json():
for duplicate in response["duplicates"]: for duplicate in response.json()["duplicates"]:
duplicates.append(f'{configGet("address", "posting", "api")}/photos/{duplicate["id"]}') duplicates.append(f'{configGet("address", "posting", "api")}/photos/{duplicate["id"]}')
return True, duplicates return True, duplicates
except: except:

View File

@ -14,7 +14,7 @@ def subLimited(user: User) -> bool:
db_record = col_users.find_one({"user": user.id}) db_record = col_users.find_one({"user": user.id})
if db_record is None: if db_record is None:
return False return False
return True if (datetime.now(tz=timezone.utc) - db_record["cooldown"]).total_seconds() < configGet("timeout", "submission") else False return True if (datetime.now(tz=timezone.utc) - db_record["cooldown"].astimezone(timezone.utc)).total_seconds() < configGet("timeout", "submission") else False
def subBlocked(user: User) -> bool: def subBlocked(user: User) -> bool:
return False if col_banned.find_one({"user": user.id}) is None else True return False if col_banned.find_one({"user": user.id}) is None else True

View File

@ -1,12 +1,14 @@
from os import path, sep from os import path, remove, sep
from pathlib import Path from pathlib import Path
from shutil import rmtree
from pyrogram import filters from pyrogram import filters
from pyrogram.client import Client from pyrogram.client import Client
from pyrogram.types import CallbackQuery from pyrogram.types import CallbackQuery, InlineKeyboardMarkup, InlineKeyboardButton
from modules.api_client import upload_pic from modules.api_client import upload_pic
from modules.app import app from modules.app import app
from modules.logger import logWrite
from modules.submissions import subBlock, subUnblock from modules.submissions import subBlock, subUnblock
from modules.utils import configGet, jsonLoad, jsonSave, locale from modules.utils import configGet, jsonLoad, jsonSave, locale
from modules.database import col_submitted from modules.database import col_submitted
@ -19,58 +21,86 @@ async def callback_query_yes(app: Client, clb: CallbackQuery):
fullclb = clb.data.split("_") fullclb = clb.data.split("_")
user_locale = clb.from_user.language_code user_locale = clb.from_user.language_code
# Check if submission is in DB and really exists db_entry = col_submitted.find_one({"_id": ObjectId(fullclb[2])})
submission = None
# Upload the file to the API server if db_entry is None:
await clb.answer(text=locale("sub_msg_unavail", "callback", locale=user_locale), show_alert=True)
return
else:
if db_entry["temp"]["uuid"] is not None:
if not path.exists(path.join(configGet("data", "locations"), "submissions", db_entry["temp"]["uuid"], db_entry["temp"]["file"])):
await clb.answer(text=locale("sub_msg_unavail", "callback", locale=user_locale), show_alert=True)
return
else:
filepath = path.join(configGet("data", "locations"), "submissions", db_entry["temp"]["uuid"], db_entry["temp"]["file"])
else:
try:
submission = await app.get_messages(db_entry["user"], db_entry["telegram"]["msg_id"])
filepath = await app.download_media(submission, file_name=configGet("tmp", "locations")+sep)
except:
await clb.answer(text=locale("sub_msg_unavail", "callback", locale=user_locale), show_alert=True)
return
# Modify submission in DB to state that it's accepted (or so called "done") response = await upload_pic(str(filepath))
if response[0] is False and len(response[1]) == 0:
await clb.answer(text=locale("sub_upload_failed", "callback", locale=user_locale), show_alert=True)
return
elif response[0] is False:
await clb.answer(text=locale("sub_duplicates_found", "callback", locale=user_locale), show_alert=True)
await clb.message.reply_text(locale("sub_media_duplicates_list", "message", locale=user_locale).format("\n".join(response[1])))
return
col_submitted.find_one_and_update({"_id": ObjectId(fullclb[2])}, {"$set": {"done": True}})
try:
if db_entry["temp"]["uuid"] is not None:
rmtree(path.join(configGet("data", "locations"), "submissions", db_entry["temp"]["uuid"]), ignore_errors=True)
else:
remove(str(filepath))
except (FileNotFoundError, NotADirectoryError):
logWrite(f"Could not delete '{filepath}' on submission accepted", debug=True)
if submission is not None:
await submission.reply_text(locale("sub_yes", "message", locale=submission.from_user.language_code), quote=True)
else:
await app.send_message(db_entry["user"], locale("sub_yes", "message"))
await clb.answer(text=locale("sub_yes", "callback", locale=user_locale).format(fullclb[2]), show_alert=True)
edited_markup = [[InlineKeyboardButton(text=str(locale("accepted", "button")), callback_data="nothing")], clb.message.reply_markup.inline_keyboard[1]] if len(clb.message.reply_markup.inline_keyboard) > 1 else [[InlineKeyboardButton(text=str(locale("accepted", "button")), callback_data="nothing")]]
await clb.message.edit(text=clb.message.text, reply_markup=InlineKeyboardMarkup(edited_markup))
# Change keyboard to a completed variant # Change keyboard to a completed variant
# Send replies to both user and admin about accepting the application # Send replies to both user and admin about accepting the application
db_entry = col_submitted.find_one({"_id": ObjectId(fullclb[2])}) # try:
# if configGet("api_based", "mode") is True:
if db_entry is None: # media = await app.download_media(submission, file_name=configGet("tmp", "locations")+sep)
await clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) # upload = upload_pic(media)
return # if upload[0] is False:
else: # await clb.answer(text=locale("sub_media_failed", "message", locale=user_locale), show_alert=True)
if db_entry["tmp"]["uuid"] is not None: # elif len(upload[1]) > 0:
if not path.exists(path.join(configGet("data", "locations"), "submissions", db_entry["tmp"]["uuid"], db_entry["tmp"]["file"])): # await clb.answer(text=locale("sub_media_duplicates", "message", locale=user_locale))
await clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) # await clb.message.reply_text(locale("sub_media_duplicates_list", "message", locale=user_locale).format("\n • ".join(upload[1])))
return # else:
else: # if clb.data.endswith("_caption"):
try: # index = jsonLoad(configGet("index", "locations"))
submission = await app.get_messages(db_entry["user"], db_entry["telegram"]["msg_id"]) # index["captions"][Path(media).name] = submission.caption
except: # jsonSave(index, configGet("index", "locations"))
await clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) # else:
return # media = await app.download_media(submission, file_name=configGet("queue", "locations")+sep)
# if clb.data.endswith("_caption"):
try: # index = jsonLoad(configGet("index", "locations"))
if configGet("api_based", "mode") is True: # index["captions"][Path(media).name] = submission.caption
media = await app.download_media(submission, file_name=configGet("tmp", "locations")+sep) # jsonSave(index, configGet("index", "locations"))
upload = upload_pic(media) # except:
if upload[0] is False: # await clb.answer(text=locale("sub_media_unavail", "message", locale=user_locale), show_alert=True)
await clb.answer(text=locale("sub_media_failed", "message", locale=user_locale), show_alert=True) # return
elif len(upload[1]) > 0: # await submission.reply_text(locale("sub_yes", "message", locale=submission.from_user.language_code), quote=True)
await clb.answer(text=locale("sub_media_duplicates", "message", locale=user_locale)) # await clb.answer(text=locale("sub_yes", "callback", locale=user_locale).format(fullclb[2]), show_alert=True)
await clb.message.reply_text(locale("sub_media_duplicates_list", "message", locale=user_locale).format("\n".join(upload[1])))
else:
if clb.data.endswith("_caption"):
index = jsonLoad(configGet("index", "locations"))
index["captions"][Path(media).name] = submission.caption
jsonSave(index, configGet("index", "locations"))
else:
media = await app.download_media(submission, file_name=configGet("queue", "locations")+sep)
if clb.data.endswith("_caption"):
index = jsonLoad(configGet("index", "locations"))
index["captions"][Path(media).name] = submission.caption
jsonSave(index, configGet("index", "locations"))
except:
await clb.answer(text=locale("sub_media_unavail", "message", locale=user_locale), show_alert=True)
return
await submission.reply_text(locale("sub_yes", "message", locale=submission.from_user.language_code), quote=True)
await clb.answer(text=locale("sub_yes", "callback", locale=user_locale).format(fullclb[2]), show_alert=True)
# edited_markup = [[InlineKeyboardButton(text=str(locale("accepted", "button")), callback_data="nothing")]] # edited_markup = [[InlineKeyboardButton(text=str(locale("accepted", "button")), callback_data="nothing")]]
# await clb.message.edit(text=clb.message.text, reply_markup=InlineKeyboardMarkup(edited_markup)) # await clb.message.edit(text=clb.message.text, reply_markup=InlineKeyboardMarkup(edited_markup))