2021-04-13 16:23:31 +02:00
|
|
|
from typing import (
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
)
|
2021-04-09 10:17:38 +02:00
|
|
|
|
2021-04-12 19:47:21 +02:00
|
|
|
from idm.api.proto.chat_manager_service_pb2 import Chat as ChatPb
|
2021-04-09 10:17:38 +02:00
|
|
|
from nexus.bot.application import TelegramApplication
|
2021-04-13 16:23:31 +02:00
|
|
|
from nexus.models.proto.typed_document_pb2 import \
|
|
|
|
TypedDocument as TypedDocumentPb
|
2021-04-09 10:17:38 +02:00
|
|
|
from nexus.translations import t
|
|
|
|
from nexus.views.telegram.common import close_button
|
|
|
|
from nexus.views.telegram.registry import parse_typed_document_to_view
|
|
|
|
from telethon import Button
|
|
|
|
|
|
|
|
|
|
|
|
class DocumentListWidget:
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
application: TelegramApplication,
|
2021-04-12 19:47:21 +02:00
|
|
|
chat: ChatPb,
|
2021-04-13 16:23:31 +02:00
|
|
|
typed_documents: List[TypedDocumentPb],
|
|
|
|
cmd: str,
|
|
|
|
has_next: bool,
|
2021-04-09 10:17:38 +02:00
|
|
|
session_id: str,
|
|
|
|
message_id: int,
|
|
|
|
request_id: str,
|
|
|
|
page: int = 0,
|
2021-04-13 16:23:31 +02:00
|
|
|
page_size: int = 5,
|
2021-04-09 10:17:38 +02:00
|
|
|
):
|
|
|
|
self.application = application
|
|
|
|
self.chat = chat
|
2021-04-13 16:23:31 +02:00
|
|
|
self.typed_documents = typed_documents
|
|
|
|
self.cmd = cmd
|
|
|
|
self.has_next = has_next
|
2021-04-09 10:17:38 +02:00
|
|
|
self.session_id = session_id
|
|
|
|
self.message_id = message_id
|
|
|
|
self.request_id = request_id
|
|
|
|
self.page = page
|
2021-04-13 16:23:31 +02:00
|
|
|
self.page_size = page_size
|
2021-04-09 10:17:38 +02:00
|
|
|
|
|
|
|
async def render(self) -> tuple[str, Optional[list]]:
|
2021-04-13 16:23:31 +02:00
|
|
|
if not len(self.typed_documents):
|
2021-04-09 10:17:38 +02:00
|
|
|
return t('COULD_NOT_FIND_ANYTHING', language=self.chat.language), [close_button(self.session_id)]
|
|
|
|
|
2021-04-13 16:23:31 +02:00
|
|
|
serp_elements = []
|
|
|
|
for position, typed_document in enumerate(self.typed_documents):
|
|
|
|
view = parse_typed_document_to_view(typed_document)
|
2021-04-09 10:17:38 +02:00
|
|
|
serp_elements.append(
|
|
|
|
view.get_snippet(
|
|
|
|
language=self.chat.language,
|
|
|
|
limit=512 + 128,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
promo = self.application.promotioner.choose_promotion(language=self.chat.language).format(
|
|
|
|
related_channel=self.application.config['telegram']['related_channel'],
|
|
|
|
)
|
|
|
|
serp_elements.append(promo)
|
|
|
|
serp = '\n\n'.join(serp_elements)
|
|
|
|
|
|
|
|
buttons = []
|
|
|
|
if self.has_next or self.page > 0:
|
|
|
|
buttons = [
|
|
|
|
Button.inline(
|
|
|
|
text='<<1' if self.page > 1 else ' ',
|
2021-04-13 16:23:31 +02:00
|
|
|
data=f'/{self.cmd}_{self.session_id}_{self.message_id}_0'
|
2021-04-09 10:17:38 +02:00
|
|
|
if self.page > 1 else '/noop',
|
|
|
|
),
|
|
|
|
Button.inline(
|
|
|
|
text=f'<{self.page}' if self.page > 0 else ' ',
|
2021-04-13 16:23:31 +02:00
|
|
|
data=f'/{self.cmd}_{self.session_id}_{self.message_id}_{self.page - 1}'
|
2021-04-09 10:17:38 +02:00
|
|
|
if self.page > 0 else '/noop',
|
|
|
|
),
|
|
|
|
Button.inline(
|
|
|
|
text=f'{self.page + 2}>' if self.has_next else ' ',
|
2021-04-13 16:23:31 +02:00
|
|
|
data=f'/{self.cmd}_{self.session_id}_{self.message_id}_{self.page + 1}'
|
2021-04-09 10:17:38 +02:00
|
|
|
if self.has_next else '/noop',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
buttons.append(close_button(self.session_id))
|
|
|
|
|
|
|
|
return serp, buttons
|