2021-04-13 17:23:31 +03:00
|
|
|
from typing import (
|
|
|
|
List,
|
|
|
|
Optional,
|
|
|
|
)
|
2021-04-09 11:17:38 +03:00
|
|
|
|
2022-09-02 18:44:56 +03:00
|
|
|
from idm.api.proto import chat_manager_service_pb2
|
|
|
|
from library.telegram.common import close_button
|
2021-04-09 11:17:38 +03:00
|
|
|
from nexus.translations import t
|
2022-09-02 18:44:56 +03:00
|
|
|
from nexus.views.telegram.base_holder import BaseHolder
|
2021-04-09 11:17:38 +03:00
|
|
|
from telethon import Button
|
|
|
|
|
|
|
|
|
|
|
|
class DocumentListWidget:
|
|
|
|
def __init__(
|
|
|
|
self,
|
2022-09-02 18:44:56 +03:00
|
|
|
chat: chat_manager_service_pb2.Chat,
|
|
|
|
document_holders: List[BaseHolder],
|
|
|
|
bot_name,
|
|
|
|
header: Optional[str] = None,
|
|
|
|
has_next: bool = False,
|
|
|
|
session_id: Optional[str] = None,
|
|
|
|
message_id: Optional[int] = None,
|
|
|
|
request_id: Optional[str] = None,
|
|
|
|
cmd: str = None,
|
2021-04-09 11:17:38 +03:00
|
|
|
page: int = 0,
|
2021-04-13 17:23:31 +03:00
|
|
|
page_size: int = 5,
|
2021-04-09 11:17:38 +03:00
|
|
|
):
|
|
|
|
self.chat = chat
|
2022-09-02 18:44:56 +03:00
|
|
|
self.document_holders = document_holders
|
|
|
|
self.bot_name = bot_name
|
|
|
|
self.header = header
|
2021-04-13 17:23:31 +03:00
|
|
|
self.cmd = cmd
|
|
|
|
self.has_next = has_next
|
2021-04-09 11:17:38 +03:00
|
|
|
self.session_id = session_id
|
|
|
|
self.message_id = message_id
|
|
|
|
self.request_id = request_id
|
|
|
|
self.page = page
|
2021-04-13 17:23:31 +03:00
|
|
|
self.page_size = page_size
|
2021-04-09 11:17:38 +03:00
|
|
|
|
|
|
|
async def render(self) -> tuple[str, Optional[list]]:
|
2022-09-02 18:44:56 +03:00
|
|
|
if not len(self.document_holders):
|
|
|
|
return t('COULD_NOT_FIND_ANYTHING', self.chat.language), [close_button(self.session_id)]
|
2021-04-09 11:17:38 +03:00
|
|
|
|
2021-04-13 17:23:31 +03:00
|
|
|
serp_elements = []
|
2022-09-02 18:44:56 +03:00
|
|
|
for position, document_holder in enumerate(self.document_holders):
|
2021-04-09 11:17:38 +03:00
|
|
|
serp_elements.append(
|
2022-09-02 18:44:56 +03:00
|
|
|
document_holder
|
|
|
|
.view_builder(self.chat.language)
|
|
|
|
.add_short_description()
|
|
|
|
.add_new_line()
|
|
|
|
.add_links(bot_name=self.bot_name)
|
|
|
|
.build()
|
2021-04-09 11:17:38 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
serp = '\n\n'.join(serp_elements)
|
|
|
|
|
2022-09-02 18:44:56 +03:00
|
|
|
if self.header:
|
|
|
|
serp = f'**{self.header}**\n\n{serp}'
|
|
|
|
|
2021-04-09 11:17:38 +03:00
|
|
|
buttons = []
|
2022-09-02 18:44:56 +03:00
|
|
|
if self.cmd and self.message_id and self.session_id and (self.has_next or self.page > 0):
|
2021-04-09 11:17:38 +03:00
|
|
|
buttons = [
|
|
|
|
Button.inline(
|
|
|
|
text='<<1' if self.page > 1 else ' ',
|
2021-04-13 17:23:31 +03:00
|
|
|
data=f'/{self.cmd}_{self.session_id}_{self.message_id}_0'
|
2021-04-09 11:17:38 +03:00
|
|
|
if self.page > 1 else '/noop',
|
|
|
|
),
|
|
|
|
Button.inline(
|
|
|
|
text=f'<{self.page}' if self.page > 0 else ' ',
|
2021-04-13 17:23:31 +03:00
|
|
|
data=f'/{self.cmd}_{self.session_id}_{self.message_id}_{self.page - 1}'
|
2021-04-09 11:17:38 +03:00
|
|
|
if self.page > 0 else '/noop',
|
|
|
|
),
|
|
|
|
Button.inline(
|
|
|
|
text=f'{self.page + 2}>' if self.has_next else ' ',
|
2021-04-13 17:23:31 +03:00
|
|
|
data=f'/{self.cmd}_{self.session_id}_{self.message_id}_{self.page + 1}'
|
2021-04-09 11:17:38 +03:00
|
|
|
if self.has_next else '/noop',
|
|
|
|
)
|
|
|
|
]
|
|
|
|
buttons.append(close_button(self.session_id))
|
|
|
|
return serp, buttons
|