dev@locazia: implement message_types and clean trash

This commit is contained in:
user committed 2024-03-10 12:11:03 +03:00
1 parent 01d56dae42
commit dae4a6f123
10 files changed
+118 -31

No files matched your search

+23 -6
View File
@@ -12,12 +12,14 @@ from app.core._utils.send_status import send_status
from app.core.logger import make_log
from app.core.models.node_storage import StoredContent
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.storage import db_session
import os
import traceback
async def indexer_loop(platform_found: bool, seqno: int) -> [bool, int]:
async def indexer_loop(memory, platform_found: bool, seqno: int) -> [bool, int]:
if not platform_found:
platform_state = await toncenter.get_account(platform.address.to_string(1, 1, 1))
if not platform_state.get('code'):
@@ -120,14 +122,29 @@ async def indexer_loop(platform_found: bool, seqno: int) -> [bool, int]:
and encrypted_stored_content.onchain_index != item_index
if not is_duplicate:
if encrypted_stored_content.type.startswith('local'):
encrypted_stored_content.type = "onchain/content" + (
"_unknown" if not (encrypted_stored_content.key_id is None) else "")
encrypted_stored_content.type = "onchain/content" + ("_unknown" if (encrypted_stored_content.key_id is None) else "")
encrypted_stored_content.onchain_index = item_index
encrypted_stored_content.owner_address = item_owner_address.to_string(1, 1, 1)
user = None
if user_wallet_connection:
encrypted_stored_content.user_id = user_wallet_connection.user_id
user = user_wallet_connection.user
await (Wrapped_CBotChat(memory._telegram_bot, chat_id=user.telegram_id)).send_message(
user.translated('p_contentWasIndexed').format(
item_address=item_address.to_string(1, 1, 1),
item_index=item_index,
),
message_type='notification',
reply_markup=get_inline_keyboard([
[{
'text': user.translated('home_button'),
'callback_data': 'home'
}]
])
)
elif encrypted_stored_content.type.startswith('onchain') and encrypted_stored_content.onchain_index == item_index:
encrypted_stored_content.type = "onchain/content" + ("_unknown" if not (encrypted_stored_content.key_id is None) else "")
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)
if user_wallet_connection:
encrypted_stored_content.user_id = user_wallet_connection.user_id
@@ -168,13 +185,13 @@ async def indexer_loop(platform_found: bool, seqno: int) -> [bool, int]:
return platform_found, seqno
async def main_fn():
async def main_fn(memory, ):
make_log("Indexer", "Service started", level="info")
platform_found = False
seqno = 0
while True:
try:
platform_found, seqno = await indexer_loop(platform_found, seqno)
platform_found, seqno = await indexer_loop(memory, platform_found, seqno)
except BaseException as e:
make_log("Indexer", f"Error: {e}" + '\n' + traceback.format_exc(), level="error")
+1 -1
View File
@@ -21,7 +21,7 @@ async def get_sw_seqno():
return sw_seqno_value
async def main_fn():
async def main_fn(memory):
make_log("TON", f"Service started, SW = {service_wallet.address.to_string(1, 1, 1)}", level="info")
sw_seqno_value = await get_sw_seqno()
make_log("TON", f"Service wallet run seqno method: {sw_seqno_value}", level="info")
+1 -1
View File
@@ -8,7 +8,7 @@ async def uploader_loop():
pass
async def main_fn():
async def main_fn(memory):
make_log("Uploader", "Service started", level="info")
seqno = 0
while True:
+1 -1
View File
@@ -17,7 +17,7 @@ class ContentId:
version: int = None,
content_hash: bytes = None, # only SHA256
onchain_index: int = None,
accept_type: str = 'image/jpeg',
accept_type: str = None,
encryption_key_sha256: bytes = None,
):
self.version = version
+1
View File
@@ -6,3 +6,4 @@ from app.core.models.node_storage import StoredContent
from app.core.models.transaction import UserBalance, InternalTransaction
from app.core.models.user import User
from app.core.models.wallet_connection import WalletConnection
from app.core.models.messages import KnownTelegramMessage
+53 -14
View File
@@ -1,10 +1,12 @@
from aiogram import Bot
from aiogram import Bot, types
from datetime import datetime, timedelta
from app.core.logger import make_log
from app.core.models.messages import KnownTelegramMessage
class Wrapped_CBotChat:
def __init__(self, api_key: str, chat_id: int = None, **kwargs):
def __init__(self, api_key: str, chat_id: int = None, db_session=None, **kwargs):
if isinstance(api_key, Bot):
self._bot_key = api_key.token
self._bot = api_key
@@ -15,6 +17,7 @@ class Wrapped_CBotChat:
raise TypeError(f'api_key must be Bot or str, not {type(api_key)}')
self._chat_id = chat_id
self.db_session = db_session
self.options = kwargs
def __repr__(self):
@@ -23,16 +26,46 @@ class Wrapped_CBotChat:
return "Bot"
async def send_message(self, text: str, **kwargs):
async def return_result(self, result, message_type='common'):
if message_type == 'common':
ci = 0
for oc_msg in self.db_session.query(KnownTelegramMessage).filter(
KnownTelegramMessage.type == 'common',
KnownTelegramMessage.chat_id == self._chat_id,
).all():
await self.delete_message(oc_msg.message_id)
ci += 1
make_log(self, f"Deleted {ci} old messages", level='debug')
if isinstance(result, types.Message):
message_id = getattr(result, 'message_id', None)
assert message_id, "No message_id"
self.db_session.add(
KnownTelegramMessage(
type=message_type,
chat_id=self._chat_id,
message_id=message_id,
created=datetime.now()
)
)
self.db_session.commit()
else:
make_log(self, f"Unknown result type: {type(result)}", level='warning')
return result
async def send_message(self, text: str, message_type='common', **kwargs):
try:
make_log(self, f"Send message to {self._chat_id}. Text len: {len(text)}", level='debug')
return await self._bot.send_message(
r = await self._bot.send_message(
self._chat_id,
text,
parse_mode='html',
disable_web_page_preview=True,
**kwargs
)
return await self.return_result(r, message_type=message_type)
except BaseException as e:
make_log(self, f"Error sending message to {self._chat_id}. Error: {e}", level='warning')
return None
@@ -48,6 +81,7 @@ class Wrapped_CBotChat:
disable_web_page_preview=True,
**kwargs
)
except Exception as e:
make_log(self, f"Error editing message {self._chat_id}/{message_id}. Error: {e}", level='warning')
if 'exactly the same as a current content' in f'{e}':
@@ -66,64 +100,69 @@ class Wrapped_CBotChat:
make_log(self, f"Error deleting message {self._chat_id}/{message_id}. Error: {e}", level='warning')
return None
async def send_photo(self, file_id, **kwargs):
async def send_photo(self, file_id, message_type='common', **kwargs):
try:
make_log(self, f"Send photo to {self._chat_id}. File: {file_id}", level='debug')
return await self._bot.send_photo(
r = await self._bot.send_photo(
self._chat_id,
file_id,
**kwargs
)
return await self.return_result(r, message_type=message_type)
except Exception as e:
make_log(self, f"Error sending photo to {self._chat_id}. Error: {e}", level='warning')
return None
async def send_document(self, file_id, **kwargs):
async def send_document(self, file_id, message_type='common', **kwargs):
try:
make_log(self, f"Send document to {self._chat_id}. File: {file_id}", level='debug')
return await self._bot.send_document(
r = await self._bot.send_document(
self._chat_id,
file_id,
**kwargs
)
return await self.return_result(r, message_type=message_type)
except Exception as e:
make_log(self, f"Error sending document to {self._chat_id}. Error: {e}", level='warning')
return None
async def send_video(self, file_id, **kwargs):
async def send_video(self, file_id, message_type='common', **kwargs):
try:
make_log(self, f"Send video to {self._chat_id}. File: {file_id}", level='debug')
return await self._bot.send_video(
r = await self._bot.send_video(
self._chat_id,
file_id,
**kwargs
)
return await self.return_result(r, message_type=message_type)
except Exception as e:
make_log(self, f"Error sending video to {self._chat_id}. Error: {e}", level='warning')
return None
async def copy_message(self, from_chat_id, message_id, **kwargs):
async def copy_message(self, from_chat_id, message_id, message_type='common', **kwargs):
try:
make_log(self, f"Copy message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
return await self._bot.copy_message(
r = await self._bot.copy_message(
self._chat_id,
from_chat_id,
message_id,
**kwargs
)
return await self.return_result(r, message_type=message_type)
except Exception as e:
make_log(self, f"Error copying message from {from_chat_id}/{message_id} to {self._chat_id}. Error: {e}", level='warning')
return None
async def forward_message(self, from_chat_id, message_id, **kwargs):
async def forward_message(self, from_chat_id, message_id, message_type='common', **kwargs):
try:
make_log(self, f"Forward message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
return await self._bot.forward_message(
r = await self._bot.forward_message(
self._chat_id,
from_chat_id,
message_id,
**kwargs
)
return await self.return_result(r, message_type=message_type)
except Exception as e:
make_log(self, f"Error forwarding message from {from_chat_id}/{message_id} to {self._chat_id}. Error: {e}", level='warning')
return None
+17
View File
@@ -0,0 +1,17 @@
from base58 import b58decode
from sqlalchemy import Column, Integer, String, DateTime, JSON, BigInteger
from .base import AlchemyBase
# Only for clean old messages now
class KnownTelegramMessage(AlchemyBase):
__tablename__ = 'known_telegram_messages'
id = Column(Integer, autoincrement=True, primary_key=True)
type = Column(String(64), nullable=True)
chat_id = Column(BigInteger, nullable=False)
message_id = Column(BigInteger, nullable=False)
created = Column(DateTime, nullable=False, default=0)