2021-04-09 10:17:38 +02:00
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from izihawa_utils.pb_to_json import MessageToDict
|
|
|
|
from library.telegram.base import RequestContext
|
|
|
|
from nexus.models.proto.operation_pb2 import \
|
|
|
|
DocumentOperation as DocumentOperationPb
|
|
|
|
from nexus.models.proto.operation_pb2 import Vote as VotePb
|
|
|
|
from nexus.translations import t
|
|
|
|
from telethon import events
|
|
|
|
|
|
|
|
from .base import BaseCallbackQueryHandler
|
|
|
|
|
|
|
|
|
|
|
|
class VoteHandler(BaseCallbackQueryHandler):
|
2021-04-13 09:39:00 +02:00
|
|
|
filter = events.CallbackQuery(pattern='^/vote([ab])?_([A-Za-z0-9]+)_([0-9]+)_([bo])$')
|
2021-04-09 10:17:38 +02:00
|
|
|
|
2021-04-13 11:03:44 +02:00
|
|
|
def parse_pattern(self, event: events.ChatAction):
|
2022-03-28 16:39:36 +02:00
|
|
|
short_index_alias = event.pattern_match.group(1)
|
|
|
|
index_alias = self.short_index_alias_to_index_alias(short_index_alias.decode()) if short_index_alias else None
|
2021-04-13 09:39:00 +02:00
|
|
|
session_id = event.pattern_match.group(2).decode()
|
|
|
|
document_id = int(event.pattern_match.group(3).decode())
|
|
|
|
vote = event.pattern_match.group(4).decode()
|
2021-04-09 10:17:38 +02:00
|
|
|
vote_value = {'b': -1, 'o': 1}[vote]
|
2021-04-13 11:03:44 +02:00
|
|
|
|
2022-03-28 16:39:36 +02:00
|
|
|
return index_alias, session_id, document_id, vote, vote_value
|
2021-04-13 11:03:44 +02:00
|
|
|
|
|
|
|
async def handler(self, event: events.ChatAction, request_context: RequestContext):
|
2022-03-28 16:39:36 +02:00
|
|
|
index_alias, session_id, document_id, vote, vote_value = self.parse_pattern(event)
|
2021-04-13 11:03:44 +02:00
|
|
|
|
2021-04-09 10:17:38 +02:00
|
|
|
request_context.add_default_fields(mode='vote', session_id=session_id)
|
2021-04-13 11:03:44 +02:00
|
|
|
request_context.statbox(
|
|
|
|
action='vote',
|
|
|
|
document_id=document_id,
|
|
|
|
query=vote,
|
2022-03-28 16:39:36 +02:00
|
|
|
index_alias=index_alias,
|
2021-04-13 11:03:44 +02:00
|
|
|
)
|
2021-04-09 10:17:38 +02:00
|
|
|
|
|
|
|
document_operation_pb = DocumentOperationPb(
|
|
|
|
vote=VotePb(
|
|
|
|
document_id=document_id,
|
|
|
|
value=vote_value,
|
2021-04-12 19:47:21 +02:00
|
|
|
voter_id=request_context.chat.chat_id,
|
2021-04-09 10:17:38 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
logging.getLogger('operation').info(
|
2022-03-28 16:39:36 +02:00
|
|
|
msg=MessageToDict(document_operation_pb, preserving_proto_field_name=True),
|
2021-04-09 10:17:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
message = await event.get_message()
|
|
|
|
|
|
|
|
# ToDo: Generalize nexus.views.telegram.common.remove_button and use it here
|
|
|
|
return await asyncio.gather(
|
|
|
|
self.application.telegram_client.edit_message(
|
2021-04-12 19:47:21 +02:00
|
|
|
request_context.chat.chat_id,
|
2021-04-09 10:17:38 +02:00
|
|
|
message.id,
|
|
|
|
message.text,
|
|
|
|
buttons=None,
|
|
|
|
),
|
|
|
|
event.answer(t('TANKS_BRUH')),
|
|
|
|
)
|