2021-04-09 10:17:38 +02:00
|
|
|
import asyncio
|
2022-09-02 17:44:56 +02:00
|
|
|
import time
|
2021-04-09 10:17:38 +02:00
|
|
|
|
|
|
|
from library.telegram.base import RequestContext
|
2022-09-02 17:44:56 +02:00
|
|
|
from library.telegram.utils import safe_execution
|
2021-04-09 10:17:38 +02:00
|
|
|
from nexus.translations import t
|
2022-09-02 17:44:56 +02:00
|
|
|
from nexus.views.telegram.base_holder import BaseHolder
|
2021-04-09 10:17:38 +02:00
|
|
|
from telethon import (
|
|
|
|
events,
|
|
|
|
functions,
|
|
|
|
)
|
|
|
|
from telethon.errors import MessageIdInvalidError
|
|
|
|
|
|
|
|
from .base import BaseHandler
|
|
|
|
|
|
|
|
|
2022-09-02 17:44:56 +02:00
|
|
|
def is_earlier_than_2_days(message):
|
|
|
|
return time.time() - time.mktime(message.date.timetuple()) < 2 * 24 * 60 * 60 - 10
|
|
|
|
|
|
|
|
|
2021-04-09 10:17:38 +02:00
|
|
|
class ViewHandler(BaseHandler):
|
2022-09-02 17:44:56 +02:00
|
|
|
filter = events.NewMessage(incoming=True, pattern='^/v([ab])([sr])?_([A-Za-z0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)')
|
2021-04-09 10:17:38 +02:00
|
|
|
should_reset_last_widget = False
|
|
|
|
|
2021-04-13 11:03:44 +02:00
|
|
|
def parse_pattern(self, event: events.ChatAction):
|
2022-03-28 16:39:36 +02:00
|
|
|
short_index_alias = event.pattern_match.group(1)
|
|
|
|
index_alias = self.short_index_alias_to_index_alias(short_index_alias)
|
2021-04-09 10:17:38 +02:00
|
|
|
session_id = event.pattern_match.group(3)
|
|
|
|
old_message_id = int(event.pattern_match.group(4))
|
|
|
|
document_id = int(event.pattern_match.group(5))
|
|
|
|
position = int(event.pattern_match.group(6))
|
|
|
|
page = int(position / self.application.config['application']['page_size'])
|
|
|
|
|
2022-03-28 16:39:36 +02:00
|
|
|
return index_alias, session_id, old_message_id, document_id, position, page
|
2021-04-13 11:03:44 +02:00
|
|
|
|
2022-09-02 17:44:56 +02:00
|
|
|
async def get_message(self, message_id):
|
|
|
|
get_message_request = functions.messages.GetMessagesRequest(id=[message_id])
|
|
|
|
messages = await self.application.telegram_client(get_message_request)
|
|
|
|
return messages.messages[0]
|
|
|
|
|
|
|
|
async def process_widgeting(self, has_found_old_widget, old_message, request_context: RequestContext):
|
|
|
|
if has_found_old_widget and is_earlier_than_2_days(old_message):
|
|
|
|
message_id = old_message.id
|
2021-04-13 11:03:44 +02:00
|
|
|
else:
|
|
|
|
prefetch_message = await self.application.telegram_client.send_message(
|
|
|
|
request_context.chat.chat_id,
|
2022-09-02 17:44:56 +02:00
|
|
|
t("SEARCHING", request_context.chat.language),
|
2021-04-13 11:03:44 +02:00
|
|
|
reply_to=old_message.reply_to_msg_id,
|
|
|
|
)
|
|
|
|
self.application.user_manager.last_widget[request_context.chat.chat_id] = prefetch_message.id
|
|
|
|
message_id = prefetch_message.id
|
2022-09-02 17:44:56 +02:00
|
|
|
return message_id
|
|
|
|
|
|
|
|
async def compose_back_command(self, session_id, message_id, page):
|
2021-04-13 16:23:31 +02:00
|
|
|
return f'/search_{session_id}_{message_id}_{page}'
|
2021-04-13 11:03:44 +02:00
|
|
|
|
|
|
|
async def handler(self, event: events.ChatAction, request_context: RequestContext):
|
2022-03-28 16:39:36 +02:00
|
|
|
index_alias, session_id, old_message_id, document_id, position, page = self.parse_pattern(event)
|
2021-04-13 11:03:44 +02:00
|
|
|
|
2021-04-09 10:17:38 +02:00
|
|
|
request_context.add_default_fields(mode='view', session_id=session_id)
|
2022-09-02 17:44:56 +02:00
|
|
|
request_context.statbox(
|
|
|
|
action='view',
|
|
|
|
document_id=document_id,
|
|
|
|
position=position,
|
|
|
|
index_alias=index_alias
|
|
|
|
)
|
2021-04-13 11:03:44 +02:00
|
|
|
|
2022-09-02 17:44:56 +02:00
|
|
|
old_message = await self.get_message(old_message_id)
|
2021-04-13 11:03:44 +02:00
|
|
|
has_found_old_widget = old_message_id == self.application.user_manager.last_widget.get(request_context.chat.chat_id)
|
2022-09-02 17:44:56 +02:00
|
|
|
language = request_context.chat.language
|
2021-04-09 10:17:38 +02:00
|
|
|
|
|
|
|
try:
|
2022-09-02 17:44:56 +02:00
|
|
|
message_id = await self.process_widgeting(
|
2021-04-13 11:03:44 +02:00
|
|
|
has_found_old_widget=has_found_old_widget,
|
2022-09-02 17:44:56 +02:00
|
|
|
old_message=old_message,
|
|
|
|
request_context=request_context,
|
2021-04-13 11:03:44 +02:00
|
|
|
)
|
2022-09-02 17:44:56 +02:00
|
|
|
typed_document_pb = await self.resolve_document(
|
2022-03-28 16:39:36 +02:00
|
|
|
index_alias,
|
2021-04-09 10:17:38 +02:00
|
|
|
document_id,
|
|
|
|
position,
|
|
|
|
session_id,
|
|
|
|
request_context,
|
|
|
|
)
|
2022-09-02 17:44:56 +02:00
|
|
|
holder = BaseHolder.create(typed_document_pb=typed_document_pb)
|
|
|
|
back_command = await self.compose_back_command(session_id=session_id, message_id=message_id, page=page)
|
|
|
|
|
|
|
|
promo = self.application.promotioner.choose_promotion(language).format(
|
|
|
|
related_channel=self.application.config['telegram']['related_channel'],
|
|
|
|
twitter_contact_url=self.application.config['twitter']['contact_url'],
|
|
|
|
)
|
|
|
|
view_builder = holder.view_builder(language).add_view(
|
|
|
|
bot_name=self.application.config['telegram']['bot_name']
|
|
|
|
).add_new_line(2).add(promo, escaped=True)
|
|
|
|
buttons = holder.buttons_builder(language).add_back_button(back_command).add_default_layout(
|
2022-03-28 16:39:36 +02:00
|
|
|
bot_name=self.application.config['telegram']['bot_name'],
|
2022-09-02 17:44:56 +02:00
|
|
|
session_id=session_id,
|
2021-04-09 10:17:38 +02:00
|
|
|
position=position,
|
2022-09-02 17:44:56 +02:00
|
|
|
).build()
|
2021-04-09 10:17:38 +02:00
|
|
|
actions = [
|
|
|
|
self.application.telegram_client.edit_message(
|
2021-04-12 19:47:21 +02:00
|
|
|
request_context.chat.chat_id,
|
2021-04-09 10:17:38 +02:00
|
|
|
message_id,
|
2022-09-02 17:44:56 +02:00
|
|
|
view_builder.build(),
|
2021-04-09 10:17:38 +02:00
|
|
|
buttons=buttons,
|
2022-09-02 17:44:56 +02:00
|
|
|
link_preview=view_builder.has_cover,
|
2021-04-09 10:17:38 +02:00
|
|
|
),
|
|
|
|
event.delete(),
|
|
|
|
]
|
2021-04-13 11:03:44 +02:00
|
|
|
if not has_found_old_widget:
|
2022-09-02 17:44:56 +02:00
|
|
|
async with safe_execution(error_log=request_context.error_log):
|
|
|
|
await self.application.telegram_client.delete_messages(request_context.chat.chat_id, [old_message_id])
|
2021-04-09 10:17:38 +02:00
|
|
|
return await asyncio.gather(*actions)
|
|
|
|
except MessageIdInvalidError:
|
2022-09-02 17:44:56 +02:00
|
|
|
await event.reply(t("VIEWS_CANNOT_BE_SHARED", language))
|