TelegramPoster/plugins/callbacks/submission.py

267 lines
7.7 KiB
Python
Raw Normal View History

import logging
2023-02-17 22:55:38 +02:00
from os import path
from pathlib import Path
2023-02-17 22:55:38 +02:00
from shutil import rmtree
from bson import ObjectId
from libbot import config_get
2023-01-10 13:52:44 +02:00
from pyrogram import filters
from pyrogram.client import Client
from pyrogram.types import CallbackQuery, InlineKeyboardButton, InlineKeyboardMarkup
2023-02-14 12:38:54 +02:00
from classes.exceptions import (
SubmissionDuplicatesError,
SubmissionUnavailableError,
SubmissionUnsupportedError,
)
from classes.pyroclient import PyroClient
2023-02-15 15:06:06 +02:00
from modules.database import col_submitted
logger = logging.getLogger(__name__)
2023-01-10 13:52:44 +02:00
2023-01-10 14:06:24 +02:00
@Client.on_callback_query(filters.regex("sub_yes_[\s\S]*"))
async def callback_query_yes(app: PyroClient, clb: CallbackQuery):
2023-06-28 11:37:18 +03:00
user = await app.find_user(clb.from_user)
2023-02-17 17:44:56 +02:00
fullclb = str(clb.data).split("_")
2023-08-14 15:52:02 +03:00
db_entry = await col_submitted.find_one({"_id": ObjectId(fullclb[2])})
2023-02-15 15:06:06 +02:00
2023-02-17 17:44:56 +02:00
try:
2023-06-28 11:37:18 +03:00
submission = await app.submit_media(fullclb[2])
2023-02-17 17:44:56 +02:00
except SubmissionUnavailableError:
2023-03-09 12:33:02 +02:00
await clb.answer(
2023-06-28 11:37:18 +03:00
text=app._("sub_msg_unavail", "callback", locale=user.locale),
show_alert=True,
)
return
except SubmissionUnsupportedError:
await clb.answer(
2023-06-28 11:37:18 +03:00
text=app._("mime_not_allowed", "message", locale=user.locale).format(
", ".join(app.config["submission"]["mime_types"]), quote=True
),
2023-03-09 12:33:02 +02:00
show_alert=True,
)
2023-01-10 13:52:44 +02:00
return
2023-08-16 14:27:23 +03:00
except SubmissionDuplicatesError as exc:
2023-03-09 12:33:02 +02:00
await clb.answer(
2023-06-28 11:37:18 +03:00
text=app._("sub_duplicates_found", "callback", locale=user.locale),
2023-03-09 12:33:02 +02:00
show_alert=True,
)
await clb.message.reply_text(
2023-06-28 11:37:18 +03:00
app._("sub_media_duplicates_list", "message", locale=user.locale).format(
2023-08-16 14:27:23 +03:00
"\n".join(exc.duplicates)
2023-03-09 12:33:02 +02:00
),
quote=True,
)
logger.info(
2023-07-03 12:42:28 +03:00
"Submission with ID '%s' could not be accepted because of the duplicates: %s",
fullclb[2],
2023-08-16 14:27:23 +03:00
str(exc.duplicates),
2023-03-17 15:13:28 +02:00
)
2023-02-16 17:41:01 +02:00
return
2023-03-16 16:03:14 +02:00
if submission[0] is not None:
await submission[0].reply_text(
2023-06-28 11:37:18 +03:00
app._(
"sub_yes",
"message",
locale=(await app.find_user(submission[0].from_user)).locale,
),
2023-03-09 12:33:02 +02:00
quote=True,
)
2023-02-17 17:44:56 +02:00
elif db_entry is not None:
2023-06-28 11:37:18 +03:00
await app.send_message(
db_entry["user"],
app._(
"sub_yes",
"message",
locale=(await app.find_user(db_entry["user"])).locale,
),
)
2023-02-16 17:41:01 +02:00
2023-03-09 12:33:02 +02:00
await clb.answer(
2023-06-28 11:37:18 +03:00
text=app._("sub_yes", "callback", locale=user.locale).format(fullclb[2]),
2023-03-09 12:33:02 +02:00
show_alert=True,
)
edited_markup = (
[
[
InlineKeyboardButton(
2023-06-28 11:37:18 +03:00
text=str(app._("accepted", "button", locale=user.locale)),
2023-03-09 12:33:02 +02:00
callback_data="nothing",
)
],
clb.message.reply_markup.inline_keyboard[1],
]
if len(clb.message.reply_markup.inline_keyboard) > 1
else [
[
InlineKeyboardButton(
2023-06-28 11:37:18 +03:00
text=str(app._("accepted", "button", locale=user.locale)),
2023-03-09 12:33:02 +02:00
callback_data="nothing",
)
]
]
)
2023-03-16 16:03:14 +02:00
if await config_get("send_uploaded_id", "submission"):
2023-03-16 16:03:14 +02:00
await clb.message.edit_caption(
2023-06-28 11:37:18 +03:00
f"{clb.message.caption}\n\nID: `{submission[1]}`"
2023-03-16 16:03:14 +02:00
)
2023-03-09 12:33:02 +02:00
await clb.message.edit_reply_markup(
reply_markup=InlineKeyboardMarkup(edited_markup)
)
2023-02-16 17:41:01 +02:00
logger.info(
2023-07-03 12:42:28 +03:00
"Submission with ID '%s' accepted and uploaded with ID '%s'",
fullclb[2],
submission[1],
2023-03-17 15:13:28 +02:00
)
2023-01-10 13:52:44 +02:00
@Client.on_callback_query(filters.regex("sub_no_[\s\S]*"))
async def callback_query_no(app: PyroClient, clb: CallbackQuery):
2023-06-28 11:37:18 +03:00
user = await app.find_user(clb.from_user)
2023-02-17 22:55:38 +02:00
fullclb = str(clb.data).split("_")
2023-08-14 15:52:02 +03:00
db_entry = await col_submitted.find_one_and_delete({"_id": ObjectId(fullclb[2])})
2023-02-17 22:55:38 +02:00
if (
db_entry["temp"]["uuid"] is not None
and Path(
f"{app.config['locations']['data']}/submissions/{db_entry['temp']['uuid']}"
).exists()
):
rmtree(
Path(
f"{app.config['locations']['data']}/submissions/{db_entry['temp']['uuid']}"
),
ignore_errors=True,
)
2023-02-17 22:55:38 +02:00
2023-01-10 13:52:44 +02:00
try:
2023-03-09 12:33:02 +02:00
submission = await app.get_messages(
db_entry["user"], db_entry["telegram"]["msg_id"]
)
2023-08-16 14:27:23 +03:00
except Exception as exc:
2023-03-09 12:33:02 +02:00
await clb.answer(
2023-06-28 11:37:18 +03:00
text=app._("sub_msg_unavail", "message", locale=user.locale),
2023-03-09 12:33:02 +02:00
show_alert=True,
)
2023-01-10 13:52:44 +02:00
return
2023-01-10 14:06:24 +02:00
2023-03-09 12:33:02 +02:00
await submission.reply_text(
2023-06-28 11:37:18 +03:00
app._(
"sub_no",
"message",
locale=(await app.find_user(submission.from_user)).locale,
),
2023-03-09 12:33:02 +02:00
quote=True,
)
await clb.answer(
2023-06-28 11:37:18 +03:00
text=app._("sub_no", "callback", locale=user.locale).format(fullclb[2]),
2023-03-09 12:33:02 +02:00
show_alert=True,
)
edited_markup = (
[
[
InlineKeyboardButton(
2023-06-28 11:37:18 +03:00
text=str(app._("declined", "button", locale=user.locale)),
2023-03-09 12:33:02 +02:00
callback_data="nothing",
)
],
clb.message.reply_markup.inline_keyboard[1],
]
if len(clb.message.reply_markup.inline_keyboard) > 1
else [
[
InlineKeyboardButton(
2023-06-28 11:37:18 +03:00
text=str(app._("declined", "button", locale=user.locale)),
2023-03-09 12:33:02 +02:00
callback_data="nothing",
)
]
]
)
await clb.message.edit_reply_markup(
reply_markup=InlineKeyboardMarkup(edited_markup)
)
logger.info(
2023-07-03 12:42:28 +03:00
"Submission with ID '%s' rejected",
fullclb[2],
2023-03-17 15:13:28 +02:00
)
2023-01-17 15:38:21 +02:00
2023-01-10 13:52:44 +02:00
@Client.on_callback_query(filters.regex("sub_block_[\s\S]*"))
async def callback_query_block(app: PyroClient, clb: CallbackQuery):
2023-06-28 11:37:18 +03:00
user = await app.find_user(clb.from_user)
2023-02-17 22:55:38 +02:00
fullclb = str(clb.data).split("_")
2023-03-09 12:33:02 +02:00
await app.send_message(
int(fullclb[2]),
2023-06-28 11:37:18 +03:00
app._(
"sub_blocked",
"message",
locale=(await app.find_user(int(fullclb[2]))).locale,
),
2023-03-09 12:33:02 +02:00
)
2023-06-28 11:37:18 +03:00
await user.block()
2023-03-09 12:33:02 +02:00
await clb.answer(
2023-06-28 11:37:18 +03:00
text=app._("sub_block", "callback", locale=user.locale).format(fullclb[2]),
2023-03-09 12:33:02 +02:00
show_alert=True,
)
edited_markup = [
clb.message.reply_markup.inline_keyboard[0],
[
InlineKeyboardButton(
2023-06-28 11:37:18 +03:00
text=str(app._("sub_unblock", "button", locale=user.locale)),
2023-03-09 12:33:02 +02:00
callback_data=f"sub_unblock_{fullclb[2]}",
)
],
]
await clb.message.edit_reply_markup(
reply_markup=InlineKeyboardMarkup(edited_markup)
)
2023-07-03 12:42:28 +03:00
logger.info("User %s has been blocked", fullclb[2])
2023-03-09 12:33:02 +02:00
@Client.on_callback_query(filters.regex("sub_unblock_[\s\S]*"))
async def callback_query_unblock(app: PyroClient, clb: CallbackQuery):
2023-06-28 11:37:18 +03:00
user = await app.find_user(clb.from_user)
2023-02-17 22:55:38 +02:00
fullclb = str(clb.data).split("_")
2023-06-28 11:37:18 +03:00
await app.send_message(
int(fullclb[2]),
app._(
"sub_unblocked",
"message",
locale=(await app.find_user(int(fullclb[2]))).locale,
),
)
2023-06-28 11:37:18 +03:00
await user.unblock()
2023-03-09 12:33:02 +02:00
await clb.answer(
2023-06-28 11:37:18 +03:00
text=app._("sub_unblock", "callback", locale=user.locale).format(fullclb[2]),
2023-03-09 12:33:02 +02:00
show_alert=True,
)
edited_markup = [
clb.message.reply_markup.inline_keyboard[0],
[
InlineKeyboardButton(
2023-06-28 11:37:18 +03:00
text=str(app._("sub_block", "button", locale=user.locale)),
2023-03-09 12:33:02 +02:00
callback_data=f"sub_block_{fullclb[2]}",
)
],
]
await clb.message.edit_reply_markup(
reply_markup=InlineKeyboardMarkup(edited_markup)
)
2023-07-03 12:42:28 +03:00
logger.info("User %s has been unblocked", fullclb[2])