big update
This commit is contained in:
1 parent
7d05a3f2c0
commit
f45d593a84
32 files changed
+726
-320
No files matched your search
@@ -0,0 +1,70 @@
|
||||
from app.core.models.node_storage import StoredContent
|
||||
from app.core.models.content.user_content import UserContent
|
||||
from app.core.logger import make_log
|
||||
from app.core._utils.tg_process_template import tg_process_template
|
||||
from app.core._config import PROJECT_HOST
|
||||
from app.core._keyboards import get_inline_keyboard
|
||||
|
||||
|
||||
class PlayerTemplates:
|
||||
async def send_content(self, content: StoredContent, extra_buttons=None, message_id=None):
|
||||
assert content.type.startswith('onchain/content'), "Invalid nodeStorage content type"
|
||||
inline_keyboard_array = []
|
||||
cd_log = f"Content (SHA256: {content.hash}), Encrypted: {content.encrypted}, TelegramCID: {content.telegram_cid}. "
|
||||
if not content.encrypted:
|
||||
local_content = content
|
||||
else:
|
||||
local_content = content.decrypted_content
|
||||
# TODO: add check decrypted_content by .format_json()['content_cid']
|
||||
if local_content:
|
||||
cd_log += f"Decrypted: {local_content.hash}. "
|
||||
else:
|
||||
cd_log += "Can't decrypt content. "
|
||||
|
||||
if local_content:
|
||||
content_meta = content.json_format()
|
||||
local_content_meta = local_content.json_format()
|
||||
try:
|
||||
content_type, content_encoding = local_content_meta["content_type"].split('/')
|
||||
except:
|
||||
content_type, content_encoding = 'application', 'x-binary'
|
||||
|
||||
try:
|
||||
cover_content = StoredContent.from_cid(self.db_session, content_meta.get('cover_cid') or None)
|
||||
except BaseException as e:
|
||||
cd_log += f"Can't get cover content: {e}. "
|
||||
cover_content = None
|
||||
|
||||
local_content_cid = local_content.cid
|
||||
local_content_cid.content_type = 'audio/mpeg'
|
||||
local_content_url = f"{PROJECT_HOST}/api/v1/storage/{local_content_cid.serialize_v2(include_accept_type=True)}"
|
||||
|
||||
template_kwargs = {}
|
||||
if content_type[0] == 'audio':
|
||||
template_kwargs['title'] = 'title'
|
||||
template_kwargs['performer'] = 'performer'
|
||||
template_kwargs['protect_content'] = True
|
||||
template_kwargs['audio'] = local_content_url
|
||||
if cover_content:
|
||||
template_kwargs['thumbnail'] = cover_content.web_url
|
||||
|
||||
|
||||
else:
|
||||
local_content = None
|
||||
|
||||
if not local_content:
|
||||
text = self.user.translated('p_playerContext_unsupportedContent').format(
|
||||
content_type=content_type,
|
||||
content_encoding=content_encoding
|
||||
)
|
||||
inline_keyboard_array = []
|
||||
extra_buttons = []
|
||||
|
||||
make_log("TG-Player", f"Send content {content_type} ({content_encoding}) to chat {self._chat_id}. {cd_log}")
|
||||
return await tg_process_template(
|
||||
self, text, message_id=message_id, **template_kwargs,
|
||||
keyboard=get_inline_keyboard([*inline_keyboard_array, *extra_buttons]) if inline_keyboard_array else None,
|
||||
message_type=f'content/{content_type}', message_meta={'content_sha256': content_meta['hash']}
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user