This commit is contained in:
user committed 2025-02-28 13:27:37 +03:00
1 parent 8647352d0f
commit 758defee99
7 files changed
+183 -52

No files matched your search

+11 -1
View File
@@ -11,7 +11,7 @@ from app.core._blockchain.ton.platform import platform
from app.core._blockchain.ton.toncenter import toncenter
from app.core._utils.send_status import send_status
from app.core.logger import make_log
from app.core.models import UserContent
from app.core.models import UserContent, KnownTelegramMessage
from app.core.models.node_storage import StoredContent
from app.core._utils.resolve_content import resolve_content
from app.core.models.wallet_connection import WalletConnection
@@ -212,6 +212,16 @@ async def indexer_loop(memory, platform_found: bool, seqno: int) -> [bool, int]:
}]
])
)
user_uploader_wrapper = Wrapped_CBotChat(memory._telegram_bot, chat_id=user.telegram_id, user=user, db_session=session)
for hint_message in session.query(KnownTelegramMessage).filter(
and_(
KnownTelegramMessage.user_id == user.id,
KnownTelegramMessage.message_type == 'hint',
KnownTelegramMessage.meta.contains({'encrypted_content_hash': encrypted_stored_content.hash})
)
):
await user_uploader_wrapper.delete_message(hint_message.message_id)
elif encrypted_stored_content.type.startswith('onchain') and encrypted_stored_content.onchain_index == item_index:
encrypted_stored_content.type = "onchain/content" + ("_unknown" if (encrypted_stored_content.key_id is None) else "")
encrypted_stored_content.owner_address = item_owner_address.to_string(1, 1, 1)
+47
View File
@@ -20,6 +20,7 @@ from app.core.models._telegram import Wrapped_CBotChat
from app.core.storage import db_session
from app.core._config import CLIENT_TELEGRAM_API_KEY
from app.core.models.user import User
from app.core.models import StarsInvoice
import os
import traceback
@@ -27,6 +28,51 @@ import traceback
async def license_index_loop(memory, platform_found: bool, seqno: int) -> [bool, int]:
make_log("LicenseIndex", "Service running", level="debug")
with db_session() as session:
async def check_telegram_stars_transactions():
# Проверка звездных telegram транзакций, обновление paid
offset = {'desc': 'Статичное число заранее известного количества транзакций, которое даже не знает наш бот', 'value': 1}['value'] + \
session.query(StarsInvoice).count()
limit = 100
while True:
make_log("StarsProcessing", f"Star payments: offset={offset}, limit={limit}", level="DEBUG")
star_payments = (await memory._client_telegram_bot.get_star_transactions(offset, limit)).transactions
if not star_payments:
make_log("StarsProcessing", "No more star payments", level="DEBUG")
return
for star_payment in star_payments:
if star_payment.receiver:
continue
try:
existing_invoice = session.query(StarsInvoice).filter(
StarsInvoice.external_id == star_payment.source.invoice_payload
).first()
if not existing_invoice:
continue
if star_payment.amount == existing_invoice.amount:
if not existing_invoice.paid:
existing_invoice.paid = True
session.commit()
licensed_content = session.query(StoredContent).filter(StoredContent.hash == existing_invoice.content_hash).first()
user = session.query(User).filter(User.id == existing_invoice.user_id).first()
await (Wrapped_CBotChat(memory._client_telegram_bot, chat_id=user.telegram_id, user=user, db_session=session)).send_content(
session, licensed_content
)
except BaseException as e:
make_log("StarsProcessing", f"Local error: {e}" + '\n' + traceback.format_exc(), level="error")
offset += limit
try:
await check_telegram_stars_transactions()
except BaseException as e:
make_log("StarsProcessing", f"Error: {e}" + '\n' + traceback.format_exc(), level="error")
# Проверка кошельков пользователей на появление новых NFT, добавление их в базу как неопознанные
for user in session.query(User).filter(
User.last_use > datetime.now() - timedelta(minutes=10)
).all():
@@ -47,6 +93,7 @@ async def license_index_loop(memory, platform_found: bool, seqno: int) -> [bool,
except BaseException as e:
make_log("LicenseIndex", f"Error: {e}" + '\n' + traceback.format_exc(), level="error")
# Проверка NFT на актуальность данных, в том числе уже проверенные
process_content = session.query(UserContent).filter(
and_(
UserContent.type.startswith('nft/'),
+58 -15
View File
@@ -1,3 +1,4 @@
from sqlalchemy import and_
from app.core.models.node_storage import StoredContent
from app.core.models.content.user_content import UserContent, UserAction
from app.core.logger import make_log
@@ -7,6 +8,9 @@ from app.core._keyboards import get_inline_keyboard
from app.core.models.messages import KnownTelegramMessage
from aiogram.types import URLInputFile, Message
import json
import urllib
from app.core.models.transaction import StarsInvoice
class PlayerTemplates:
@@ -55,8 +59,25 @@ class PlayerTemplates:
local_content.meta['cover_cid'] = cover_content.cid.serialize_v2() if cover_content else 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)}"
local_content_url = f"{PROJECT_HOST}/api/v1.5/storage/{local_content_cid.serialize_v2()}"
converted_content = content.meta.get('converted_content')
if not converted_content:
r = await tg_process_template(
self, self.user.translated('p_playerContext_contentNotReady'),
message_id=message_id,
message_type='common'
)
return r
content_share_link = {
'text': self.user.translated('p_shareLinkContext').format(title=content_metadata_json.get('name', "")),
'url': f"https://t.me/{CLIENT_TELEGRAM_BOT_USERNAME}?start=C{content.cid.serialize_v2()}"
}
preview_content = db_session.query(StoredContent).filter(
StoredContent.hash == converted_content['low_preview']
).first()
local_content_preview_url = preview_content.web_url
if content_type == 'audio':
audio_title = content_metadata_json.get('name', "").split(' - ')
if len(audio_title) > 1:
@@ -65,15 +86,21 @@ class PlayerTemplates:
template_kwargs['title'] = audio_title[0].strip()
template_kwargs['protect_content'] = True
template_kwargs['audio'] = URLInputFile(local_content_url)
template_kwargs['audio'] = URLInputFile(local_content_preview_url)
if cover_content:
template_kwargs['thumbnail'] = URLInputFile(cover_content.web_url)
if self.bot_id == 1:
inline_keyboard_array.append([{
'text': self.user.translated('shareTrack_button'),
'switch_inline_query': f"C{content.cid.serialize_v2()}",
}])
inline_keyboard_array.append([
{
'text': self.user.translated('shareTrack_button'),
'switch_inline_query': f"C{content.cid.serialize_v2()}",
},
{
'text': self.user.translated('shareLink_button'),
'url': f"https://t.me/share/url?text={urllib.parse.quote(content_share_link['text'])}&url={urllib.parse.quote(content_share_link['url'])}"
}
])
inline_keyboard_array.append([{
'text': self.user.translated('openTrackInApp_button'),
'url': f"https://t.me/{CLIENT_TELEGRAM_BOT_USERNAME}/content?startapp={content.cid.serialize_v2()}"
@@ -90,7 +117,7 @@ class PlayerTemplates:
elif content_type == 'video':
# Processing video
video_title = content_metadata_json.get('name', "")
template_kwargs['video'] = URLInputFile(local_content_url)
template_kwargs['video'] = URLInputFile(local_content_preview_url)
template_kwargs['protect_content'] = True
if cover_content:
@@ -99,10 +126,16 @@ class PlayerTemplates:
if self.bot_id == 1:
# Buttons for sharing and opening in app
inline_keyboard_array.append([{
'text': self.user.translated('shareVideo_button'),
'switch_inline_query': f"C{content.cid.serialize_v2()}",
}])
inline_keyboard_array.append([
{
'text': self.user.translated('shareVideo_button'),
'switch_inline_query': f"C{content.cid.serialize_v2()}",
},
{
'text': self.user.translated('shareLink_button'),
'url': f"https://t.me/share/url?text={urllib.parse.quote(content_share_link['text'])}&url={urllib.parse.quote(content_share_link['url'])}"
}
])
inline_keyboard_array.append([{
'text': self.user.translated('openTrackInApp_button'),
'url': f"https://t.me/{CLIENT_TELEGRAM_BOT_USERNAME}/content?startapp={content.cid.serialize_v2()}"
@@ -134,14 +167,24 @@ class PlayerTemplates:
have_access = (
(content.owner_address == user_wallet_address)
or bool(self.db_session.query(UserContent).filter_by(owner_address=user_wallet_address, status='active', content_id=content.id).first())
or bool(self.db_session.query(UserContent).filter(
and_(
StarsInvoice.user_id == self.user,
StarsInvoice.content_hash == content['encrypted_content'].hash,
StarsInvoice.paid == True
)
))
)
if not have_access:
if have_access:
full_content = self.db_session.query(StoredContent).filter_by(
hash=content.meta.get('converted_content', {}).get('low') # TODO: support high quality
).first()
if content_type == 'audio':
# Restrict audio to 30 seconds if user does not have access
template_kwargs['audio'] = URLInputFile(local_content_url + '?seconds_limit=30')
template_kwargs['audio'] = URLInputFile(full_content.web_url)
elif content_type == 'video':
# Restrict video to 30 seconds if user does not have access
template_kwargs['video'] = URLInputFile(local_content_url + '?seconds_limit=30')
template_kwargs['video'] = URLInputFile(full_content.web_url)
make_log("TG-Player", f"Send content {content_type} ({content_encoding}) to chat {self._chat_id}. {cd_log}")
for kmsg in self.db_session.query(KnownTelegramMessage).filter_by(