This repository has been archived on 2024-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
TelegramPoster/plugins/callbacks/submission.py

275 lines
9.0 KiB
Python
Raw Normal View History

2023-02-17 22:55:38 +02:00
from os import path
from shutil import rmtree
2023-01-10 13:52:44 +02:00
from pyrogram import filters
2023-02-16 17:41:01 +02:00
from pyrogram.types import CallbackQuery, InlineKeyboardMarkup, InlineKeyboardButton
2023-02-17 17:44:56 +02:00
from classes.exceptions import SubmissionDuplicatesError, SubmissionUnavailableError
from classes.poster_client import PosterClient
2023-02-17 22:55:38 +02:00
from classes.user import PosterUser
2023-02-14 12:38:54 +02:00
2023-01-10 13:52:44 +02:00
from modules.app import app
2023-03-17 15:13:28 +02:00
from modules.logger import logWrite
2023-02-17 17:44:56 +02:00
from modules.utils import configGet, locale
2023-02-15 15:06:06 +02:00
from modules.database import col_submitted
from bson import ObjectId
2023-01-10 13:52:44 +02:00
2023-01-10 14:06:24 +02:00
2023-02-15 15:06:06 +02:00
@app.on_callback_query(filters.regex("sub_yes_[\s\S]*"))
2023-02-17 17:44:56 +02:00
async def callback_query_yes(app: PosterClient, clb: CallbackQuery):
fullclb = str(clb.data).split("_")
2023-01-10 13:52:44 +02:00
user_locale = clb.from_user.language_code
2023-02-17 17:44:56 +02:00
2023-02-15 15:06:06 +02:00
db_entry = col_submitted.find_one({"_id": ObjectId(fullclb[2])})
2023-02-17 17:44:56 +02:00
try:
submission = await app.submit_photo(fullclb[2])
except SubmissionUnavailableError:
2023-03-09 12:33:02 +02:00
await clb.answer(
text=locale("sub_msg_unavail", "callback", locale=user_locale),
show_alert=True,
)
2023-01-10 13:52:44 +02:00
return
2023-02-17 17:44:56 +02:00
except SubmissionDuplicatesError as exp:
2023-03-09 12:33:02 +02:00
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(exp.duplicates)
),
quote=True,
)
2023-03-17 15:13:28 +02:00
logWrite(
2023-03-17 15:51:41 +02:00
locale(
"submission_duplicate",
"console",
locale=configGet("locale_log").format(
fullclb[2],
str(exp.duplicates),
),
),
2023-03-17 15:13:28 +02:00
debug=True,
)
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(
locale("sub_yes", "message", locale=submission[0].from_user.language_code),
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-02-16 17:41:01 +02:00
await app.send_message(db_entry["user"], locale("sub_yes", "message"))
2023-03-09 12:33:02 +02:00
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", locale=user_locale)),
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", locale=user_locale)),
callback_data="nothing",
)
]
]
)
2023-03-16 16:03:14 +02:00
if configGet("send_uploaded_id", "submission"):
await clb.message.edit_caption(
clb.message.caption + f"\n\nID: `{submission[1]}`"
)
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
2023-03-17 15:13:28 +02:00
logWrite(
2023-03-17 15:51:41 +02:00
locale(
"submission_accepted",
"console",
locale=configGet("locale_log").format(fullclb[2], submission[1]),
),
2023-03-17 15:13:28 +02:00
debug=True,
)
2023-02-16 17:41:01 +02:00
# try:
# if configGet("api_based", "mode") is True:
# media = await app.download_media(submission, file_name=configGet("tmp", "locations")+sep)
# upload = upload_pic(media)
# if upload[0] is False:
# await clb.answer(text=locale("sub_media_failed", "message", locale=user_locale), show_alert=True)
# elif len(upload[1]) > 0:
# await clb.answer(text=locale("sub_media_duplicates", "message", locale=user_locale))
# 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
2023-01-17 15:38:21 +02:00
2023-01-10 13:52:44 +02:00
2023-02-15 15:06:06 +02:00
@app.on_callback_query(filters.regex("sub_no_[\s\S]*"))
2023-02-17 17:44:56 +02:00
async def callback_query_no(app: PosterClient, clb: CallbackQuery):
2023-02-17 22:55:38 +02:00
fullclb = str(clb.data).split("_")
2023-01-10 13:52:44 +02:00
user_locale = clb.from_user.language_code
2023-02-17 22:55:38 +02:00
db_entry = col_submitted.find_one_and_delete({"_id": ObjectId(fullclb[2])})
if db_entry["temp"]["uuid"] is not None:
2023-03-09 12:33:02 +02:00
if path.exists(
path.join(
configGet("data", "locations"), "submissions", db_entry["temp"]["uuid"]
)
):
rmtree(
path.join(
configGet("data", "locations"),
"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-01-10 13:52:44 +02:00
except:
2023-03-09 12:33:02 +02:00
await clb.answer(
text=locale("sub_msg_unavail", "message", locale=user_locale),
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(
locale("sub_no", "message", locale=submission.from_user.language_code),
quote=True,
)
await clb.answer(
text=locale("sub_no", "callback", locale=user_locale).format(fullclb[2]),
show_alert=True,
)
edited_markup = (
[
[
InlineKeyboardButton(
text=str(locale("declined", "button", locale=user_locale)),
callback_data="nothing",
)
],
clb.message.reply_markup.inline_keyboard[1],
]
if len(clb.message.reply_markup.inline_keyboard) > 1
else [
[
InlineKeyboardButton(
text=str(locale("declined", "button", locale=user_locale)),
callback_data="nothing",
)
]
]
)
await clb.message.edit_reply_markup(
reply_markup=InlineKeyboardMarkup(edited_markup)
)
2023-03-17 15:13:28 +02:00
logWrite(
2023-03-17 15:51:41 +02:00
locale(
"submission_rejected",
"console",
locale=configGet("locale_log").format(fullclb[2]),
),
2023-03-17 15:13:28 +02:00
debug=True,
)
2023-01-17 15:38:21 +02:00
2023-01-10 13:52:44 +02:00
@app.on_callback_query(filters.regex("sub_block_[\s\S]*"))
2023-02-17 17:44:56 +02:00
async def callback_query_block(app: PosterClient, clb: CallbackQuery):
2023-02-17 22:55:38 +02:00
fullclb = str(clb.data).split("_")
2023-01-10 13:52:44 +02:00
user_locale = clb.from_user.language_code
2023-03-09 12:33:02 +02:00
await app.send_message(
int(fullclb[2]), locale("sub_blocked", "message", locale=configGet("locale"))
)
2023-02-17 22:55:38 +02:00
PosterUser(int(fullclb[2])).block()
2023-03-09 12:33:02 +02:00
await clb.answer(
text=locale("sub_block", "callback", locale=user_locale).format(fullclb[2]),
show_alert=True,
)
edited_markup = [
clb.message.reply_markup.inline_keyboard[0],
[
InlineKeyboardButton(
text=str(locale("sub_unblock", "button", locale=user_locale)),
callback_data=f"sub_unblock_{fullclb[2]}",
)
],
]
await clb.message.edit_reply_markup(
reply_markup=InlineKeyboardMarkup(edited_markup)
)
2023-03-17 15:13:28 +02:00
logWrite(
2023-03-17 15:51:41 +02:00
locale(
"user_blocked",
"console",
locale=configGet("locale_log").format(fullclb[2]),
),
2023-03-17 15:13:28 +02:00
debug=True,
)
2023-03-09 12:33:02 +02:00
@app.on_callback_query(filters.regex("sub_unblock_[\s\S]*"))
2023-02-17 17:44:56 +02:00
async def callback_query_unblock(app: PosterClient, clb: CallbackQuery):
2023-02-17 22:55:38 +02:00
fullclb = str(clb.data).split("_")
2023-01-10 13:52:44 +02:00
user_locale = clb.from_user.language_code
2023-03-09 12:33:02 +02:00
await app.send_message(
int(fullclb[2]), locale("sub_unblocked", "message", locale=configGet("locale"))
)
2023-02-17 22:55:38 +02:00
PosterUser(int(fullclb[2])).unblock()
2023-03-09 12:33:02 +02:00
await clb.answer(
text=locale("sub_unblock", "callback", locale=user_locale).format(fullclb[2]),
show_alert=True,
)
edited_markup = [
clb.message.reply_markup.inline_keyboard[0],
[
InlineKeyboardButton(
text=str(locale("sub_block", "button", locale=user_locale)),
callback_data=f"sub_block_{fullclb[2]}",
)
],
]
await clb.message.edit_reply_markup(
reply_markup=InlineKeyboardMarkup(edited_markup)
)
2023-03-17 15:13:28 +02:00
logWrite(
2023-03-17 15:51:41 +02:00
locale(
"user_unblocked",
"console",
locale=configGet("locale_log").format(fullclb[2]),
),
2023-03-17 15:13:28 +02:00
debug=True,
)