From 5b1afe2c9ec1ed5a6633567b187fdc83c74d2266 Mon Sep 17 00:00:00 2001 From: profitroll Date: Wed, 15 Feb 2023 14:06:06 +0100 Subject: [PATCH] WIP: API usage as main --- classes/enums/submission_types.py | 8 ++ config.json | 1 + data/blocked.json | 1 - data/index.json | 5 - data/submit.json | 1 - locale/en.json | 7 +- locale/uk.json | 7 +- modules/database.py | 3 +- modules/submissions.py | 43 +++---- plugins/callbacks/submission.py | 41 ++++-- plugins/commands/mode_submit.py | 5 +- plugins/handlers/submission.py | 199 ++++++++++++++++++++---------- 12 files changed, 211 insertions(+), 110 deletions(-) create mode 100644 classes/enums/submission_types.py delete mode 100644 data/blocked.json delete mode 100644 data/index.json delete mode 100644 data/submit.json diff --git a/classes/enums/submission_types.py b/classes/enums/submission_types.py new file mode 100644 index 0000000..20494f9 --- /dev/null +++ b/classes/enums/submission_types.py @@ -0,0 +1,8 @@ +from enum import Enum + + +class SubmissionType(Enum): + DOCUMENT = "document" + VIDEO = "video" + ANIMATION = "animation" + PHOTO = "photo" \ No newline at end of file diff --git a/config.json b/config.json index 1d86c56..8d4cf3c 100644 --- a/config.json +++ b/config.json @@ -46,6 +46,7 @@ "channel": 0, "silent": false, "move_sent": false, + "interval": 1, "extensions": { "photo": [ "jpg", diff --git a/data/blocked.json b/data/blocked.json deleted file mode 100644 index 0637a08..0000000 --- a/data/blocked.json +++ /dev/null @@ -1 +0,0 @@ -[] \ No newline at end of file diff --git a/data/index.json b/data/index.json deleted file mode 100644 index 25f21fd..0000000 --- a/data/index.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "last_id": 0, - "sent": [], - "captions": {} -} \ No newline at end of file diff --git a/data/submit.json b/data/submit.json deleted file mode 100644 index 9e26dfe..0000000 --- a/data/submit.json +++ /dev/null @@ -1 +0,0 @@ -{} \ No newline at end of file diff --git a/locale/en.json b/locale/en.json index 4ea91cb..eb56b16 100644 --- a/locale/en.json +++ b/locale/en.json @@ -26,10 +26,11 @@ "mime_not_allowed": "File type not allowed. Please, consider using one of these: {0}", "post_exception": "Could not send content due to `{0}`\n\nTraceback:\n```{1}```", "post_invalid_pic": "__TO_BE_ADDED__", - "api_queue_empty": "Could not send content: `Queue folder is empty or contains only unsupported or already sent files.`", + "api_queue_empty": "Could not send content: `Queue is empty or contains only unsupported files.`", "api_queue_error": "__TO_BE_ADDED__", "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." }, "button": { "sub_yes": "✅ Accept", @@ -58,7 +59,7 @@ "post_sent": "Sent {0} to {1} with caption {2} and silently {3}", "post_exception": "Could not send content due to {0}. Traceback: {1}", "post_invalid_pic": "__TO_BE_ADDED__", - "post_empty": "Could not send content due to queue folder empty with allowed extensions", + "post_empty": "Could not send content due to queue empty or contains only forbidden extensions", "sub_mime_not_allowed": "Got submission from {0} but type of {1} which is not allowed", "sub_document_too_large": "Got submission from {0} but but file is too large ({1} > {2})", "sub_received": "Got submission from {0} with a caption {1}", diff --git a/locale/uk.json b/locale/uk.json index a6c7f39..a644403 100644 --- a/locale/uk.json +++ b/locale/uk.json @@ -26,10 +26,11 @@ "mime_not_allowed": "Тип файлу не дозволений. Розгляньте можливість використання одного з цих: {0}", "post_exception": "Не вдалося надіслати контент через `{0}`\n\nTraceback:\n```{1}```", "post_invalid_pic": "__TO_BE_ADDED__", - "api_queue_empty": "Не вдалося надіслати контент: `Папка черги порожня або містить лише непідтримувані або вже надіслані файли`.", + "api_queue_empty": "Не вдалося надіслати контент: `Черга порожня або містить лише непідтримувані файли`.", "api_queue_error": "__TO_BE_ADDED__", "post_low": "Мала кількість контенту: `Залишилось всього {0} файлів в черзі.`", - "api_creds_invalid": "__TO_BE_ADDED__" + "api_creds_invalid": "__TO_BE_ADDED__", + "sub_wip": "Подання постів зараз знаходиться у розробці. Він буде знову доступний через кілька днів. Дякуємо за ваше терпіння." }, "button": { "sub_yes": "✅ Прийняти", @@ -58,7 +59,7 @@ "post_sent": "Надіслано {0} у {1} з підписом {2} та без звуку {3}", "post_exception": "Не вдалося надіслати контент через {0}. Traceback: {1}", "post_invalid_pic": "__TO_BE_ADDED__", - "post_empty": "Не вдалося надіслати контент через порожню папку черги з дозволеними розширеннями", + "post_empty": "Не вдалося надіслати контент адже черга з дозволеними розширеннями порожня", "sub_mime_not_allowed": "Отримано подання від {0} але типу {1} який не є дозволеним", "sub_document_too_large": "Отримано подання від {0} але файл завеликий({1} > {2})", "sub_received": "Отримано подання від {0} з підписом {1}", diff --git a/modules/database.py b/modules/database.py index 7d734ec..97e5dc1 100644 --- a/modules/database.py +++ b/modules/database.py @@ -27,10 +27,11 @@ db = db_client.get_database(name=db_config["name"]) collections = db.list_collection_names() -for collection in ["sent", "banned", "submitted"]: +for collection in ["sent", "users", "banned", "submitted"]: if not collection in collections: db.create_collection(collection) col_sent = db.get_collection("sent") +col_users = db.get_collection("users") col_banned = db.get_collection("banned") col_submitted = db.get_collection("submitted") \ No newline at end of file diff --git a/modules/submissions.py b/modules/submissions.py index f75c27e..c42749a 100644 --- a/modules/submissions.py +++ b/modules/submissions.py @@ -1,32 +1,27 @@ -from time import time -from modules.utils import jsonLoad, jsonSave, configGet +from datetime import datetime, timezone +from modules.utils import configGet +from modules.database import col_users, col_banned +from pyrogram.types.user_and_chats import User -def subLimit(user): - submit = jsonLoad(configGet("submit", "locations")) - submit[str(user.id)] = time() - jsonSave(submit, configGet("submit", "locations")) +def subLimit(user: User) -> None: + if col_users.find_one_and_update({"user": user.id}, {"$set": {"cooldown": datetime.now(tz=timezone.utc)}}) is None: + col_users.insert_one({"user": user.id, "cooldown": datetime.now(tz=timezone.utc)}) -def subLimited(user): +def subLimited(user: User) -> bool: if user.id == configGet("admin"): return False else: - submit = jsonLoad(configGet("submit", "locations")) - if str(user.id) in submit: - if (time() - submit[str(user.id)]) < configGet("timeout", "submission"): - return True - else: - return False - else: + db_record = col_users.find_one({"user": user.id}) + if db_record is None: return False + return True if (datetime.now(tz=timezone.utc) - db_record["cooldown"]).total_seconds() < configGet("timeout", "submission") else False -def subBlock(user): - blocked = jsonLoad(configGet("blocked", "locations")) - if user not in blocked: - blocked.append(user) - jsonSave(blocked, configGet("blocked", "locations")) +def subBlocked(user: User) -> bool: + return False if col_banned.find_one({"user": user.id}) is None else True -def subUnblock(user): - blocked = jsonLoad(configGet("blocked", "locations")) - if user in blocked: - blocked.remove(user) - jsonSave(blocked, configGet("blocked", "locations")) \ No newline at end of file +def subBlock(user: User) -> None: + if col_banned.find_one({"user": user.id}) is None: + col_banned.insert_one({"user": user.id, "date": datetime.now(tz=timezone.utc)}) + +def subUnblock(user: User) -> None: + col_banned.find_one_and_delete({"user": user.id}) \ No newline at end of file diff --git a/plugins/callbacks/submission.py b/plugins/callbacks/submission.py index a586e37..185b0ab 100644 --- a/plugins/callbacks/submission.py +++ b/plugins/callbacks/submission.py @@ -1,26 +1,51 @@ -from os import sep +from os import path, sep from pathlib import Path from pyrogram import filters from pyrogram.client import Client -from pyrogram.types import (CallbackQuery, InlineKeyboardButton, - InlineKeyboardMarkup) +from pyrogram.types import CallbackQuery from modules.api_client import upload_pic from modules.app import app from modules.submissions import subBlock, subUnblock from modules.utils import configGet, jsonLoad, jsonSave, locale +from modules.database import col_submitted +from bson import ObjectId -@app.on_callback_query(filters.regex("sub_yes_[\s\S]*_[\s\S]*")) +@app.on_callback_query(filters.regex("sub_yes_[\s\S]*")) async def callback_query_yes(app: Client, clb: CallbackQuery): + fullclb = clb.data.split("_") user_locale = clb.from_user.language_code - try: - submission = await app.get_messages(int(fullclb[2]), int(fullclb[3])) - except: + + # Check if submission is in DB and really exists + + # Upload the file to the API server + + # Modify submission in DB to state that it's accepted (or so called "done") + + # Change keyboard to a completed variant + + # Send replies to both user and admin about accepting the application + + db_entry = col_submitted.find_one({"_id": ObjectId(fullclb[2])}) + + if db_entry is None: await clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) return + else: + if db_entry["tmp"]["uuid"] is not None: + if not path.exists(path.join(configGet("data", "locations"), "submissions", db_entry["tmp"]["uuid"], db_entry["tmp"]["file"])): + await clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) + return + else: + try: + submission = await app.get_messages(db_entry["user"], db_entry["telegram"]["msg_id"]) + except: + await clb.answer(text=locale("sub_msg_unavail", "message", locale=user_locale), show_alert=True) + return + try: if configGet("api_based", "mode") is True: media = await app.download_media(submission, file_name=configGet("tmp", "locations")+sep) @@ -51,7 +76,7 @@ async def callback_query_yes(app: Client, clb: CallbackQuery): # await clb.message.edit(text=clb.message.text, reply_markup=InlineKeyboardMarkup(edited_markup)) -@app.on_callback_query(filters.regex("sub_no_[\s\S]*_[\s\S]*")) +@app.on_callback_query(filters.regex("sub_no_[\s\S]*")) async def callback_query_no(app: Client, clb: CallbackQuery): fullclb = clb.data.split("_") user_locale = clb.from_user.language_code diff --git a/plugins/commands/mode_submit.py b/plugins/commands/mode_submit.py index 39819f3..ae81d60 100644 --- a/plugins/commands/mode_submit.py +++ b/plugins/commands/mode_submit.py @@ -3,15 +3,16 @@ from pyrogram.client import Client from pyrogram.types import Message from modules.app import app +from modules.submissions import subBlocked from modules.utils import configGet, jsonLoad, locale @app.on_message(~ filters.scheduled & filters.command(["start"], prefixes="/")) async def cmd_start(app: Client, msg: Message): - if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")): + if subBlocked(msg.from_user) is False: await msg.reply_text(locale("start", "message", locale=msg.from_user.language_code)) @app.on_message(~ filters.scheduled & filters.command(["rules", "help"], prefixes="/")) async def cmd_rules(app: Client, msg: Message): - if msg.from_user.id not in jsonLoad(configGet("blocked", "locations")): + if subBlocked(msg.from_user) is False: await msg.reply_text(locale("rules", "message", locale=msg.from_user.language_code)) \ No newline at end of file diff --git a/plugins/handlers/submission.py b/plugins/handlers/submission.py index a1acf93..b2c0278 100644 --- a/plugins/handlers/submission.py +++ b/plugins/handlers/submission.py @@ -1,87 +1,162 @@ +from datetime import datetime, timezone +from os import makedirs, path, sep +from uuid import uuid4 + from pyrogram import filters from pyrogram.client import Client from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message from modules.app import app +from modules.database import col_banned, col_submitted from modules.logger import logWrite from modules.submissions import subLimit, subLimited -from modules.utils import configGet, jsonLoad, locale -from modules.database import col_banned +from modules.utils import configGet, locale +from classes.enums.submission_types import SubmissionType -@app.on_message(~ filters.scheduled & filters.photo) # | filters.video | filters.animation | filters.document) +@app.on_message(~ filters.scheduled & filters.photo | filters.video | filters.animation | filters.document) async def get_submission(_: Client, msg: Message): - try: + locale("sub_wip", "message", locale=msg.from_user.language_code) - if col_banned.find_one( {"user": msg.from_user.id} ) is not None: - return + # try: - user_locale = msg.from_user.language_code + # if col_banned.find_one( {"user": msg.from_user.id} ) is not None: + # return - if not subLimited(msg.from_user): + # user_locale = msg.from_user.language_code + # save_tmp = True + # contents = None - if msg.document != None: - if msg.document.mime_type not in configGet("mime_types", "submission"): - await msg.reply_text(locale("mime_not_allowed", "message", locale=user_locale), quote=True) - return - if msg.document.file_size > configGet("file_size", "submission"): - await msg.reply_text(locale("document_too_large", "message", locale=user_locale).format(str(configGet("file_size", "submission")/1024/1024)), quote=True) - return + # if subLimited(msg.from_user): + # await msg.reply_text(locale("sub_cooldown", "message", locale=user_locale).format(str(configGet("timeout", "submission")))) + # return - if msg.video != None: - if msg.video.file_size > configGet("file_size", "submission"): - await msg.reply_text(locale("document_too_large", "message", locale=user_locale).format(str(configGet("file_size", "submission")/1024/1024)), quote=True) - return + # if msg.document is not None: + # if msg.document.mime_type not in configGet("mime_types", "submission"): + # await msg.reply_text(locale("mime_not_allowed", "message", locale=user_locale), quote=True) + # return + # if msg.document.file_size > configGet("file_size", "submission"): + # await msg.reply_text(locale("document_too_large", "message", locale=user_locale).format(str(configGet("file_size", "submission")/1024/1024)), quote=True) + # return + # if msg.document.file_size > configGet("tmp_size", "submission"): + # save_tmp = False + # contents = msg.document.file_id, SubmissionType.DOCUMENT #, msg.document.file_name - buttons = [ - [ - InlineKeyboardButton(text=locale("sub_yes", "button", locale=configGet("locale")), callback_data=f"sub_yes_{msg.from_user.id}_{msg.id}") - ] - ] + # if msg.video is not None: + # if msg.video.file_size > configGet("file_size", "submission"): + # await msg.reply_text(locale("document_too_large", "message", locale=user_locale).format(str(configGet("file_size", "submission")/1024/1024)), quote=True) + # return + # if msg.video.file_size > configGet("tmp_size", "submission"): + # save_tmp = False + # contents = msg.video.file_id, SubmissionType.VIDEO #, msg.video.file_name - if msg.caption != None: - caption = str(msg.caption) - buttons[0].append( - InlineKeyboardButton(text=locale("sub_yes_caption", "button", locale=configGet("locale")), callback_data=f"sub_yes_{msg.from_user.id}_{msg.id}_caption") - ) - buttons[0].append( - InlineKeyboardButton(text=locale("sub_no", "button", locale=configGet("locale")), callback_data=f"sub_no_{msg.from_user.id}_{msg.id}") - ) - else: - caption = "" - buttons[0].append( - InlineKeyboardButton(text=locale("sub_no", "button", locale=configGet("locale")), callback_data=f"sub_no_{msg.from_user.id}_{msg.id}") - ) + # if msg.animation is not None: + # if msg.animation.file_size > configGet("file_size", "submission"): + # await msg.reply_text(locale("document_too_large", "message", locale=user_locale).format(str(configGet("file_size", "submission")/1024/1024)), quote=True) + # return + # if msg.animation.file_size > configGet("tmp_size", "submission"): + # save_tmp = False + # contents = msg.animation.file_id, SubmissionType.ANIMATION #, msg.animation.file_name - caption += locale("sub_by", "message", locale=locale(configGet("locale"))) + # if msg.photo is not None: + # contents = msg.photo.file_id, SubmissionType.PHOTO #, "please_generate" - if msg.from_user.first_name != None: - caption += f" {msg.from_user.first_name}" - if msg.from_user.last_name != None: - caption += f" {msg.from_user.last_name}" - if msg.from_user.username != None: - caption += f" (@{msg.from_user.username})" - if msg.from_user.phone_number != None: - caption += f" ({msg.from_user.phone_number})" + # if save_tmp is not None: - if msg.from_user.id != configGet("admin"): - buttons += [ - [ - InlineKeyboardButton(text=locale("sub_block", "button", locale=configGet("locale")), callback_data=f"sub_block_{msg.from_user.id}") - ], - [ - InlineKeyboardButton(text=locale("sub_unblock", "button", locale=configGet("locale")), callback_data=f"sub_unblock_{msg.from_user.id}") - ] - ] + # if contents is None: + # return - await msg.reply_text(locale("sub_sent", "message", locale=user_locale), quote=True) - subLimit(msg.from_user) + # tmp_id = str(uuid4()) + # # filename = tmp_id if contents[1] == "please_generate" else contents[1] + # makedirs(path.join(configGet("data", "locations"), "submissions", tmp_id), exist_ok=True) + # downloaded = await app.download_media(msg, path.join(configGet("data", "locations"), "submissions", tmp_id)+sep) + # inserted = col_submitted.insert_one( + # { + # "user": msg.from_user.id, + # "date": datetime.now(tz=timezone.utc), + # "done": False, + # "type": contents[1].value, + # "temp": { + # "uuid": tmp_id, + # "file": path.basename(str(downloaded)) + # }, + # "telegram": { + # "msg_id": msg.id, + # "file_id": contents[0] + # }, + # "caption": str(msg.caption) if msg.caption is not None else None + # } + # ) + + # else: - await msg.copy(configGet("admin"), caption=caption, reply_markup=InlineKeyboardMarkup(buttons)) + # if contents is None: + # return - else: - await msg.reply_text(locale("sub_cooldown", "message", locale=user_locale).format(str(configGet("timeout", "submission")))) + # inserted = col_submitted.insert_one( + # { + # "user": msg.from_user.id, + # "date": datetime.now(tz=timezone.utc), + # "done": False, + # "type": contents[1].value, + # "temp": { + # "uuid": None, + # "file": None + # }, + # "telegram": { + # "msg_id": msg.id, + # "file_id": contents[0] + # }, + # "caption": str(msg.caption) if msg.caption is not None else None + # } + # ) + + # buttons = [ + # [ + # InlineKeyboardButton(text=locale("sub_yes", "button", locale=configGet("locale")), callback_data=f"sub_yes_{str(inserted.inserted_id)}") + # ] + # ] - except AttributeError: - logWrite(f"from_user in function get_submission does not seem to contain id") \ No newline at end of file + # if msg.caption is not None: + # caption = str(msg.caption) + # buttons[0].append( + # InlineKeyboardButton(text=locale("sub_yes_caption", "button", locale=configGet("locale")), callback_data=f"sub_yes_{str(inserted.inserted_id)}_caption") + # ) + # buttons[0].append( + # InlineKeyboardButton(text=locale("sub_no", "button", locale=configGet("locale")), callback_data=f"sub_no_{str(inserted.inserted_id)}") + # ) + # else: + # caption = "" + # buttons[0].append( + # InlineKeyboardButton(text=locale("sub_no", "button", locale=configGet("locale")), callback_data=f"sub_no_{str(inserted.inserted_id)}") + # ) + + # caption += locale("sub_by", "message", locale=locale(configGet("locale"))) + + # if msg.from_user.first_name is not None: + # caption += f" {msg.from_user.first_name}" + # if msg.from_user.last_name is not None: + # caption += f" {msg.from_user.last_name}" + # if msg.from_user.username is not None: + # caption += f" (@{msg.from_user.username})" + # if msg.from_user.phone_number is not None: + # caption += f" ({msg.from_user.phone_number})" + + # if msg.from_user.id != configGet("admin"): + # buttons += [ + # [ + # InlineKeyboardButton(text=locale("sub_block", "button", locale=configGet("locale")), callback_data=f"sub_block_{msg.from_user.id}") + # ] + # # [ + # # InlineKeyboardButton(text=locale("sub_unblock", "button", locale=configGet("locale")), callback_data=f"sub_unblock_{msg.from_user.id}") + # # ] + # ] + + # await msg.reply_text(locale("sub_sent", "message", locale=user_locale), quote=True) + # subLimit(msg.from_user) + + # await msg.copy(configGet("admin"), caption=caption, reply_markup=InlineKeyboardMarkup(buttons)) + + # except AttributeError: + # logWrite(f"from_user in function get_submission does not seem to contain id") \ No newline at end of file