dev@locazia: fix wrappers
This commit is contained in:
parent
fafaef8915
commit
dfb2ac7106
|
|
@ -5,6 +5,7 @@ from app.core._crypto.signer import Signer
|
||||||
from app.core._secrets import hot_seed
|
from app.core._secrets import hot_seed
|
||||||
from app.core.logger import make_log
|
from app.core.logger import make_log
|
||||||
from app.core.models.keys import KnownKey
|
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.models.user import User
|
||||||
from app.core.storage import Session
|
from app.core.storage import Session
|
||||||
|
|
||||||
|
|
@ -55,6 +56,9 @@ async def try_authorization(request):
|
||||||
request.ctx.user = user
|
request.ctx.user = user
|
||||||
request.ctx.user_key = known_key
|
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):
|
async def try_service_authorization(request):
|
||||||
signature = request.headers.get('X-Service-Signature')
|
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.verified_hash = None
|
||||||
request.ctx.user = None
|
request.ctx.user = None
|
||||||
request.ctx.user_key = None
|
request.ctx.user_key = None
|
||||||
|
request.ctx.user_uploader_wrapper = None
|
||||||
|
request.ctx.user_client_wrapper = None
|
||||||
request.ctx.db_session = Session()
|
request.ctx.db_session = Session()
|
||||||
await try_authorization(request)
|
await try_authorization(request)
|
||||||
await try_service_authorization(request)
|
await try_service_authorization(request)
|
||||||
|
|
|
||||||
|
|
@ -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'),
|
request.ctx.user.translated('p_tonconnectTransactionRequested'),
|
||||||
reply_markup=get_inline_keyboard([
|
reply_markup=get_inline_keyboard([
|
||||||
[{
|
[{
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ class UserDataMiddleware(BaseMiddleware):
|
||||||
|
|
||||||
data['user'] = user
|
data['user'] = user
|
||||||
data['db_session'] = session
|
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
|
data['memory'] = dp._s_memory
|
||||||
result = await handler(event, data)
|
result = await handler(event, data)
|
||||||
return result
|
return result
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ handler3.setFormatter(
|
||||||
)
|
)
|
||||||
logger.addHandler(handler3)
|
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):
|
def make_log(issuer, message, *args, level='INFO', **kwargs):
|
||||||
|
|
|
||||||
|
|
@ -27,35 +27,37 @@ class Wrapped_CBotChat:
|
||||||
return "Bot"
|
return "Bot"
|
||||||
|
|
||||||
async def return_result(self, result, message_type='common'):
|
async def return_result(self, result, message_type='common'):
|
||||||
if message_type == 'common':
|
if self.db_session:
|
||||||
ci = 0
|
if message_type == 'common':
|
||||||
for oc_msg in self.db_session.query(KnownTelegramMessage).filter(
|
ci = 0
|
||||||
KnownTelegramMessage.type == 'common',
|
for oc_msg in self.db_session.query(KnownTelegramMessage).filter(
|
||||||
KnownTelegramMessage.chat_id == self._chat_id,
|
KnownTelegramMessage.type == 'common',
|
||||||
).all():
|
KnownTelegramMessage.chat_id == self._chat_id,
|
||||||
await self.delete_message(oc_msg.message_id)
|
).all():
|
||||||
ci += 1
|
await self.delete_message(oc_msg.message_id)
|
||||||
|
ci += 1
|
||||||
|
|
||||||
make_log(self, f"Deleted {ci} old messages", level='debug')
|
make_log(self, f"Deleted {ci} old messages", level='debug')
|
||||||
|
|
||||||
if isinstance(result, types.Message):
|
if isinstance(result, types.Message):
|
||||||
message_id = getattr(result, 'message_id', None)
|
message_id = getattr(result, 'message_id', None)
|
||||||
assert message_id, "No message_id"
|
assert message_id, "No message_id"
|
||||||
self.db_session.add(
|
self.db_session.add(
|
||||||
KnownTelegramMessage(
|
KnownTelegramMessage(
|
||||||
type=message_type,
|
type=message_type,
|
||||||
chat_id=self._chat_id,
|
chat_id=self._chat_id,
|
||||||
message_id=message_id,
|
message_id=message_id,
|
||||||
created=datetime.now()
|
created=datetime.now()
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
self.db_session.commit()
|
||||||
self.db_session.commit()
|
else:
|
||||||
else:
|
make_log(self, f"Unknown result type: {type(result)}", level='warning')
|
||||||
make_log(self, f"Unknown result type: {type(result)}", level='warning')
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
async def send_message(self, text: str, message_type='common', **kwargs):
|
async def send_message(self, text: str, message_type='common', **kwargs):
|
||||||
|
assert self._chat_id, "No chat_id"
|
||||||
try:
|
try:
|
||||||
make_log(self, f"Send message to {self._chat_id}. Text len: {len(text)}", level='debug')
|
make_log(self, f"Send message to {self._chat_id}. Text len: {len(text)}", level='debug')
|
||||||
r = await self._bot.send_message(
|
r = await self._bot.send_message(
|
||||||
|
|
@ -71,6 +73,7 @@ class Wrapped_CBotChat:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def edit_message(self, message_id, text, **kwargs):
|
async def edit_message(self, message_id, text, **kwargs):
|
||||||
|
assert self._chat_id, "No chat_id"
|
||||||
try:
|
try:
|
||||||
make_log(self, f"Edit message {self._chat_id}/{message_id}. Text len: {len(text)}", level='debug')
|
make_log(self, f"Edit message {self._chat_id}/{message_id}. Text len: {len(text)}", level='debug')
|
||||||
return await self._bot.edit_message_text(
|
return await self._bot.edit_message_text(
|
||||||
|
|
@ -90,6 +93,7 @@ class Wrapped_CBotChat:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def delete_message(self, message_id):
|
async def delete_message(self, message_id):
|
||||||
|
assert self._chat_id, "No chat_id"
|
||||||
try:
|
try:
|
||||||
make_log(self, f"Delete message {self._chat_id}/{message_id}", level='debug')
|
make_log(self, f"Delete message {self._chat_id}/{message_id}", level='debug')
|
||||||
return await self._bot.delete_message(
|
return await self._bot.delete_message(
|
||||||
|
|
@ -101,6 +105,7 @@ class Wrapped_CBotChat:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def send_photo(self, file_id, message_type='common', **kwargs):
|
async def send_photo(self, file_id, message_type='common', **kwargs):
|
||||||
|
assert self._chat_id, "No chat_id"
|
||||||
try:
|
try:
|
||||||
make_log(self, f"Send photo to {self._chat_id}. File: {file_id}", level='debug')
|
make_log(self, f"Send photo to {self._chat_id}. File: {file_id}", level='debug')
|
||||||
r = await self._bot.send_photo(
|
r = await self._bot.send_photo(
|
||||||
|
|
@ -114,6 +119,7 @@ class Wrapped_CBotChat:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def send_document(self, file_id, message_type='common', **kwargs):
|
async def send_document(self, file_id, message_type='common', **kwargs):
|
||||||
|
assert self._chat_id, "No chat_id"
|
||||||
try:
|
try:
|
||||||
make_log(self, f"Send document to {self._chat_id}. File: {file_id}", level='debug')
|
make_log(self, f"Send document to {self._chat_id}. File: {file_id}", level='debug')
|
||||||
r = await self._bot.send_document(
|
r = await self._bot.send_document(
|
||||||
|
|
@ -127,6 +133,7 @@ class Wrapped_CBotChat:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def send_video(self, file_id, message_type='common', **kwargs):
|
async def send_video(self, file_id, message_type='common', **kwargs):
|
||||||
|
assert self._chat_id, "No chat_id"
|
||||||
try:
|
try:
|
||||||
make_log(self, f"Send video to {self._chat_id}. File: {file_id}", level='debug')
|
make_log(self, f"Send video to {self._chat_id}. File: {file_id}", level='debug')
|
||||||
r = await self._bot.send_video(
|
r = await self._bot.send_video(
|
||||||
|
|
@ -140,6 +147,7 @@ class Wrapped_CBotChat:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def copy_message(self, from_chat_id, message_id, message_type='common', **kwargs):
|
async def copy_message(self, from_chat_id, message_id, message_type='common', **kwargs):
|
||||||
|
assert self._chat_id, "No chat_id"
|
||||||
try:
|
try:
|
||||||
make_log(self, f"Copy message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
|
make_log(self, f"Copy message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
|
||||||
r = await self._bot.copy_message(
|
r = await self._bot.copy_message(
|
||||||
|
|
@ -154,6 +162,7 @@ class Wrapped_CBotChat:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
async def forward_message(self, from_chat_id, message_id, message_type='common', **kwargs):
|
async def forward_message(self, from_chat_id, message_id, message_type='common', **kwargs):
|
||||||
|
assert self._chat_id, "No chat_id"
|
||||||
try:
|
try:
|
||||||
make_log(self, f"Forward message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
|
make_log(self, f"Forward message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
|
||||||
r = await self._bot.forward_message(
|
r = await self._bot.forward_message(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue