75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
import base58
|
|
from aiogram import types, Router, F
|
|
|
|
from app.core._config import WEB_APP_URLS
|
|
from app.core._keyboards import get_inline_keyboard
|
|
from app.core._utils.tg_process_template import tg_process_template
|
|
from app.core.logger import make_log
|
|
from app.core.models.node_storage import StoredContent
|
|
import json
|
|
|
|
router = Router()
|
|
|
|
|
|
def chunks(lst, n):
|
|
"""Yield successive n-sized chunks from lst."""
|
|
for i in range(0, len(lst), n):
|
|
yield lst[i:i + n]
|
|
|
|
|
|
async def t_callback_owned_content(query: types.CallbackQuery, memory=None, user=None, db_session=None, chat_wrap=None, **extra):
|
|
message_text = user.translated("ownedContent_menu")
|
|
content_list = []
|
|
for content in db_session.query(StoredContent).filter_by(
|
|
owner_address=user.wallet_address(db_session),
|
|
).all():
|
|
try:
|
|
metadata_content = StoredContent.from_cid(db_session, content.json_format()['metadata_cid'])
|
|
make_log("OwnedContent", f"Metadata content: {metadata_content.filepath}")
|
|
with open(metadata_content.filepath, 'r') as f:
|
|
metadata_content_json = json.loads(f.read())
|
|
|
|
make_log("OwnedContent", f"Metadata content: {metadata_content_json}")
|
|
except BaseException as e:
|
|
make_log("OwnedContent", f"Can't get metadata content: {e}", level='warning')
|
|
continue
|
|
|
|
content_list.append([
|
|
{
|
|
'text': metadata_content_json['name'],
|
|
'callback_data': f'NC_{content.cid.content_hash.hex()}'
|
|
}
|
|
])
|
|
|
|
return await tg_process_template(
|
|
chat_wrap, message_text,
|
|
keyboard=get_inline_keyboard([
|
|
*content_list,
|
|
[{
|
|
'text': user.translated('webApp_uploadContent_button'),
|
|
'web_app': types.WebAppInfo(
|
|
url=WEB_APP_URLS['uploadContent']
|
|
)
|
|
}],
|
|
[{
|
|
'text': user.translated('back_button'),
|
|
'callback_data': 'home'
|
|
}]
|
|
]), message_id=query.message.message_id
|
|
)
|
|
|
|
|
|
async def t_callback_node_content(query: types.CallbackQuery, memory=None, user=None, db_session=None, chat_wrap=None, **extra):
|
|
content_hash = query.data.split('_')[1]
|
|
return await chat_wrap.send_player(
|
|
db_session.query(StoredContent).filter_by(
|
|
hash=base58.b58encode(bytes.fromhex(content_hash)).decode()
|
|
).first(),
|
|
message_id=query.message.message_id
|
|
)
|
|
|
|
|
|
|
|
router.callback_query.register(t_callback_owned_content, F.data == 'ownedContent')
|
|
router.callback_query.register(t_callback_node_content, F.data.startswith('NC_'))
|