dev@locazia: fix wrappers

This commit is contained in:
user 2024-03-10 18:42:12 +03:00
parent fafaef8915
commit dfb2ac7106
5 changed files with 40 additions and 25 deletions

View File

@ -5,6 +5,7 @@ from app.core._crypto.signer import Signer
from app.core._secrets import hot_seed
from app.core.logger import make_log
from app.core.models.keys import KnownKey
from app.core.models._telegram.wrapped_bot import Wrapped_CBotChat
from app.core.models.user import User
from app.core.storage import Session
@ -55,6 +56,9 @@ async def try_authorization(request):
request.ctx.user = user
request.ctx.user_key = known_key
request.ctx.user_uploader_wrapper = Wrapped_CBotChat(request.app.ctx.memory._telegram_bot, chat_id=user.telegram_id, db_session=request.ctx.db_session)
request.ctx.user_client_wrapper = Wrapped_CBotChat(request.app.ctx.memory._client_telegram_bot, chat_id=user.telegram_id, db_session=request.ctx.db_session)
async def try_service_authorization(request):
signature = request.headers.get('X-Service-Signature')
@ -75,6 +79,8 @@ async def attach_user_to_request(request):
request.ctx.verified_hash = None
request.ctx.user = None
request.ctx.user_key = None
request.ctx.user_uploader_wrapper = None
request.ctx.user_client_wrapper = None
request.ctx.db_session = Session()
await try_authorization(request)
await try_service_authorization(request)

View File

@ -139,7 +139,7 @@ async def s_api_v1_blockchain_send_new_content_message(request):
]
}))
await (Wrapped_CBotChat(request.app.ctx.memory._telegram_bot, chat_id=request.ctx.user.telegram_id)).send_message(
await request.ctx.user_uploader_wrapper.send_message(
request.ctx.user.translated('p_tonconnectTransactionRequested'),
reply_markup=get_inline_keyboard([
[{

View File

@ -70,7 +70,7 @@ class UserDataMiddleware(BaseMiddleware):
data['user'] = user
data['db_session'] = session
data['chat_wrap'] = Wrapped_CBotChat(data['bot'], chat_id=user_id)
data['chat_wrap'] = Wrapped_CBotChat(data['bot'], chat_id=user_id, db_session=session)
data['memory'] = dp._s_memory
result = await handler(event, data)
return result

View File

@ -28,7 +28,7 @@ handler3.setFormatter(
)
logger.addHandler(handler3)
IGNORED_ISSUERS = os.getenv('IGNORED_ISSUERS', '').split(',')
IGNORED_ISSUERS = [v for v in os.getenv('IGNORED_ISSUERS', '').split(',') if v]
def make_log(issuer, message, *args, level='INFO', **kwargs):

View File

@ -27,6 +27,7 @@ class Wrapped_CBotChat:
return "Bot"
async def return_result(self, result, message_type='common'):
if self.db_session:
if message_type == 'common':
ci = 0
for oc_msg in self.db_session.query(KnownTelegramMessage).filter(
@ -56,6 +57,7 @@ class Wrapped_CBotChat:
return result
async def send_message(self, text: str, message_type='common', **kwargs):
assert self._chat_id, "No chat_id"
try:
make_log(self, f"Send message to {self._chat_id}. Text len: {len(text)}", level='debug')
r = await self._bot.send_message(
@ -71,6 +73,7 @@ class Wrapped_CBotChat:
return None
async def edit_message(self, message_id, text, **kwargs):
assert self._chat_id, "No chat_id"
try:
make_log(self, f"Edit message {self._chat_id}/{message_id}. Text len: {len(text)}", level='debug')
return await self._bot.edit_message_text(
@ -90,6 +93,7 @@ class Wrapped_CBotChat:
return None
async def delete_message(self, message_id):
assert self._chat_id, "No chat_id"
try:
make_log(self, f"Delete message {self._chat_id}/{message_id}", level='debug')
return await self._bot.delete_message(
@ -101,6 +105,7 @@ class Wrapped_CBotChat:
return None
async def send_photo(self, file_id, message_type='common', **kwargs):
assert self._chat_id, "No chat_id"
try:
make_log(self, f"Send photo to {self._chat_id}. File: {file_id}", level='debug')
r = await self._bot.send_photo(
@ -114,6 +119,7 @@ class Wrapped_CBotChat:
return None
async def send_document(self, file_id, message_type='common', **kwargs):
assert self._chat_id, "No chat_id"
try:
make_log(self, f"Send document to {self._chat_id}. File: {file_id}", level='debug')
r = await self._bot.send_document(
@ -127,6 +133,7 @@ class Wrapped_CBotChat:
return None
async def send_video(self, file_id, message_type='common', **kwargs):
assert self._chat_id, "No chat_id"
try:
make_log(self, f"Send video to {self._chat_id}. File: {file_id}", level='debug')
r = await self._bot.send_video(
@ -140,6 +147,7 @@ class Wrapped_CBotChat:
return None
async def copy_message(self, from_chat_id, message_id, message_type='common', **kwargs):
assert self._chat_id, "No chat_id"
try:
make_log(self, f"Copy message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
r = await self._bot.copy_message(
@ -154,6 +162,7 @@ class Wrapped_CBotChat:
return None
async def forward_message(self, from_chat_id, message_id, message_type='common', **kwargs):
assert self._chat_id, "No chat_id"
try:
make_log(self, f"Forward message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
r = await self._bot.forward_message(