last updates (links)
This commit is contained in:
parent
3511ebd247
commit
8651efd578
|
|
@ -1,18 +1,14 @@
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from urllib.parse import urlencode
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
STARTAPP_SEPARATOR = '!'
|
|
||||||
STARTAPP_LIMIT = 64
|
STARTAPP_LIMIT = 64
|
||||||
|
|
||||||
|
|
||||||
def build_content_links(content_token: str, ref_id: Optional[str], *, project_host: str, bot_username: str):
|
def build_content_links(content_token: str, ref_id: Optional[str], *, project_host: str, bot_username: str):
|
||||||
"""Return tuple of (startapp_payload, telegram_url, web_url)."""
|
"""Return tuple of (startapp_payload, telegram_url, web_url)."""
|
||||||
payload = content_token
|
payload = (content_token or '').strip()
|
||||||
short_ref = (ref_id or '').strip()[:3]
|
if len(payload) > STARTAPP_LIMIT:
|
||||||
if short_ref:
|
payload = payload[:STARTAPP_LIMIT]
|
||||||
candidate = f"{content_token}{STARTAPP_SEPARATOR}{short_ref}"
|
|
||||||
if len(candidate) <= STARTAPP_LIMIT:
|
|
||||||
payload = candidate
|
|
||||||
|
|
||||||
telegram_url = f"https://t.me/{bot_username}/content?startapp={payload}"
|
telegram_url = f"https://t.me/{bot_username}/content?startapp={payload}"
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,7 @@ from app.core.models.user import User
|
||||||
from app.core.models.node_storage import StoredContent
|
from app.core.models.node_storage import StoredContent
|
||||||
from app.core._utils.resolve_content import resolve_content
|
from app.core._utils.resolve_content import resolve_content
|
||||||
from app.core.models.wallet_connection import WalletConnection
|
from app.core.models.wallet_connection import WalletConnection
|
||||||
from app.core._keyboards import get_inline_keyboard
|
|
||||||
from app.core.models._telegram import Wrapped_CBotChat
|
from app.core.models._telegram import Wrapped_CBotChat
|
||||||
from app.core._utils.share_links import build_content_links
|
|
||||||
|
|
||||||
|
|
||||||
MIN_ONCHAIN_INDEX = int(os.getenv("MIN_ONCHAIN_INDEX", "8"))
|
MIN_ONCHAIN_INDEX = int(os.getenv("MIN_ONCHAIN_INDEX", "8"))
|
||||||
|
|
@ -243,28 +241,19 @@ async def indexer_loop(memory, platform_found: bool, seqno: int) -> [bool, int]:
|
||||||
ref_id = user.ensure_ref_id()
|
ref_id = user.ensure_ref_id()
|
||||||
await session.commit()
|
await session.commit()
|
||||||
|
|
||||||
_, startapp_url, web_url = build_content_links(
|
|
||||||
encrypted_stored_content.cid.serialize_v2(),
|
|
||||||
ref_id,
|
|
||||||
project_host=PROJECT_HOST,
|
|
||||||
bot_username=CLIENT_TELEGRAM_BOT_USERNAME
|
|
||||||
)
|
|
||||||
|
|
||||||
message_text = user.translated('p_contentWasIndexed').format(
|
message_text = user.translated('p_contentWasIndexed').format(
|
||||||
item_address=item_address.to_string(1, 1, 1),
|
item_address=item_address.to_string(1, 1, 1),
|
||||||
item_index=item_index,
|
item_index=item_index,
|
||||||
)
|
)
|
||||||
message_text += f"\n\n<a href=\"{web_url}\">🔗 Открыть контент</a>"
|
|
||||||
|
|
||||||
await user_uploader_wrapper.send_message(
|
await user_uploader_wrapper.send_message(
|
||||||
message_text,
|
message_text,
|
||||||
message_type='notification',
|
message_type='notification'
|
||||||
reply_markup=get_inline_keyboard([
|
)
|
||||||
[{
|
|
||||||
'text': user.translated('viewTrackAsClient_button'),
|
await user_uploader_wrapper.send_content(
|
||||||
'url': startapp_url
|
session,
|
||||||
}]
|
encrypted_stored_content
|
||||||
])
|
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,9 @@ class PlayerTemplates:
|
||||||
template_kwargs = {}
|
template_kwargs = {}
|
||||||
inline_keyboard_array = []
|
inline_keyboard_array = []
|
||||||
text = ""
|
text = ""
|
||||||
|
content_metadata_json = {}
|
||||||
|
description_block = ""
|
||||||
|
status_hint = ""
|
||||||
if content:
|
if content:
|
||||||
assert content.type.startswith('onchain/content'), "Invalid nodeStorage content type"
|
assert content.type.startswith('onchain/content'), "Invalid nodeStorage content type"
|
||||||
cd_log = f"Content (SHA256: {content.hash}), Encrypted: {content.encrypted}, TelegramCID: {content.telegram_cid}. "
|
cd_log = f"Content (SHA256: {content.hash}), Encrypted: {content.encrypted}, TelegramCID: {content.telegram_cid}. "
|
||||||
|
|
@ -34,74 +37,74 @@ class PlayerTemplates:
|
||||||
cd_log += f"Decrypted: {local_content.hash}. "
|
cd_log += f"Decrypted: {local_content.hash}. "
|
||||||
else:
|
else:
|
||||||
cd_log += "Can't decrypt content. "
|
cd_log += "Can't decrypt content. "
|
||||||
|
|
||||||
user_wallet_address = await self.user.wallet_address_async(self.db_session)
|
user_wallet_address = await self.user.wallet_address_async(self.db_session)
|
||||||
user_existing_license = (await self.db_session.execute(select(UserContent).where(
|
user_existing_license = (await self.db_session.execute(select(UserContent).where(
|
||||||
and_(UserContent.user_id == self.user.id, UserContent.content_id == content.id)
|
and_(UserContent.user_id == self.user.id, UserContent.content_id == content.id)
|
||||||
))).scalars().first()
|
))).scalars().first()
|
||||||
|
|
||||||
|
content_meta = content.json_format() if content else {}
|
||||||
if local_content:
|
if local_content:
|
||||||
content_meta = content.json_format()
|
make_log("TG-Player", f"Content meta: {content_meta}. Local content meta: {local_content.json_format()}. ")
|
||||||
local_content_meta = local_content.json_format()
|
|
||||||
make_log("TG-Player", f"Content meta: {content_meta}. Local content meta: {local_content_meta}. ")
|
|
||||||
try:
|
try:
|
||||||
content_type, content_encoding = "audio", "aac"
|
content_type, content_encoding = "audio", "aac"
|
||||||
except:
|
except:
|
||||||
content_type, content_encoding = 'application', 'x-binary'
|
content_type, content_encoding = 'application', 'x-binary'
|
||||||
|
|
||||||
content_metadata = await StoredContent.from_cid_async(db_session, content_meta.get('metadata_cid') or None)
|
metadata_cid = content_meta.get('metadata_cid') if content_meta else None
|
||||||
with open(content_metadata.filepath, 'r') as f:
|
if metadata_cid:
|
||||||
content_metadata_json = json.loads(f.read())
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cover_content = await StoredContent.from_cid_async(self.db_session, content_meta.get('cover_cid') or None)
|
content_metadata = await StoredContent.from_cid_async(db_session, metadata_cid)
|
||||||
cd_log += f"Cover content: {cover_content.cid.serialize_v2()}. "
|
with open(content_metadata.filepath, 'r') as f:
|
||||||
|
content_metadata_json = json.loads(f.read())
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
cd_log += f"Can't get cover content: {e}. "
|
make_log("TG-Player", f"Can't get metadata content: {e}", level='warning')
|
||||||
cover_content = None
|
|
||||||
|
|
||||||
share_target = user_existing_license.onchain_address if user_existing_license else content.cid.serialize_v2()
|
try:
|
||||||
ref_id = (self.user.meta or {}).get('ref_id')
|
cover_content = await StoredContent.from_cid_async(self.db_session, content_meta.get('cover_cid') if content_meta else None)
|
||||||
if not ref_id:
|
cd_log += f"Cover content: {cover_content.cid.serialize_v2()}. "
|
||||||
ref_id = self.user.ensure_ref_id()
|
except BaseException as e:
|
||||||
if self.db_session:
|
cd_log += f"Can't get cover content: {e}. "
|
||||||
await self.db_session.commit()
|
cover_content = None
|
||||||
|
|
||||||
_, startapp_url, web_app_url = build_content_links(
|
share_target = user_existing_license.onchain_address if user_existing_license else content.cid.serialize_v2()
|
||||||
share_target,
|
ref_id = (self.user.meta or {}).get('ref_id')
|
||||||
ref_id,
|
if not ref_id:
|
||||||
project_host=PROJECT_HOST,
|
ref_id = self.user.ensure_ref_id()
|
||||||
bot_username=CLIENT_TELEGRAM_BOT_USERNAME
|
if self.db_session:
|
||||||
)
|
await self.db_session.commit()
|
||||||
|
|
||||||
content_share_link = {
|
_, startapp_url, web_app_url = build_content_links(
|
||||||
'text': self.user.translated('p_shareLinkContext').format(title=content_metadata_json.get('name', "")),
|
share_target,
|
||||||
'url': startapp_url,
|
ref_id,
|
||||||
'web_url': web_app_url,
|
project_host=PROJECT_HOST,
|
||||||
'ref_id': ref_id
|
bot_username=CLIENT_TELEGRAM_BOT_USERNAME
|
||||||
}
|
)
|
||||||
|
|
||||||
if cover_content:
|
content_share_link = {
|
||||||
template_kwargs['photo'] = URLInputFile(cover_content.web_url)
|
'text': self.user.translated('p_shareLinkContext').format(title=content_metadata_json.get('name', "")),
|
||||||
|
'url': startapp_url,
|
||||||
|
'web_url': web_app_url,
|
||||||
|
'ref_id': ref_id
|
||||||
|
}
|
||||||
|
|
||||||
|
if cover_content:
|
||||||
|
template_kwargs['photo'] = URLInputFile(cover_content.web_url)
|
||||||
|
|
||||||
if not local_content:
|
if not local_content:
|
||||||
text = self.user.translated('p_playerContext_unsupportedContent').format(
|
status_hint = self.user.translated('p_playerContext_contentNotReady')
|
||||||
content_type=content_type,
|
|
||||||
content_encoding=content_encoding
|
|
||||||
)
|
|
||||||
inline_keyboard_array = []
|
|
||||||
extra_buttons = []
|
|
||||||
else:
|
|
||||||
content_hashtags = content_metadata_json.get('description').strip()
|
|
||||||
if content_hashtags:
|
|
||||||
content_hashtags += '\n'
|
|
||||||
|
|
||||||
text = f"""<b>{content_metadata_json.get('name', 'Unnamed')}</b>
|
description = (content_metadata_json.get('description') or '').strip()
|
||||||
{content_hashtags}
|
if description:
|
||||||
Этот контент был загружен в MY
|
description_block = f"{description}\n"
|
||||||
|
|
||||||
|
title = content_metadata_json.get('name') or (local_content.filename if local_content else None) or (content.filename if content else None) or content.cid.serialize_v2()
|
||||||
|
|
||||||
|
status_block = f"{status_hint}\n" if status_hint else ""
|
||||||
|
|
||||||
|
text = f"""<b>{title}</b>
|
||||||
|
{description_block}{status_block}Этот контент был загружен в MY
|
||||||
\t/ p2p content market /
|
\t/ p2p content market /
|
||||||
<blockquote><a href="{content_share_link['url']}">🔴 «открыть в MY»</a></blockquote>
|
<blockquote><a href="{content_share_link['url']}">🔴 «открыть в MY»</a></blockquote>"""
|
||||||
<blockquote><a href="{content_share_link['web_url']}">🌐 «открыть в браузере»</a></blockquote>"""
|
|
||||||
|
|
||||||
make_log("TG-Player", f"Send content {content_type} ({content_encoding}) to chat {self._chat_id}. {cd_log}")
|
make_log("TG-Player", f"Send content {content_type} ({content_encoding}) to chat {self._chat_id}. {cd_log}")
|
||||||
kmsgs = (await self.db_session.execute(select(KnownTelegramMessage).where(
|
kmsgs = (await self.db_session.execute(select(KnownTelegramMessage).where(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue