2022-09-02 17:44:56 +02:00
|
|
|
import sys
|
2022-03-28 16:39:36 +02:00
|
|
|
|
2021-04-12 19:47:21 +02:00
|
|
|
from grpc import StatusCode
|
|
|
|
from idm.api.proto.chat_manager_service_pb2 import Chat as ChatPb
|
|
|
|
from idm.api.proto.chat_manager_service_pb2 import Chats as ChatsPb
|
|
|
|
from idm.api.proto.chat_manager_service_pb2_grpc import (
|
|
|
|
ChatManagerServicer,
|
|
|
|
add_ChatManagerServicer_to_server,
|
|
|
|
)
|
|
|
|
from library.aiogrpctools.base import (
|
|
|
|
BaseService,
|
|
|
|
aiogrpc_request_wrapper,
|
|
|
|
)
|
2022-09-02 17:44:56 +02:00
|
|
|
from psycopg.rows import dict_row
|
2021-04-12 19:47:21 +02:00
|
|
|
from pypika import (
|
|
|
|
PostgreSQLQuery,
|
|
|
|
Table,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ChatManagerService(ChatManagerServicer, BaseService):
|
|
|
|
chats_table = Table('chats')
|
|
|
|
|
|
|
|
async def start(self):
|
2022-09-02 17:44:56 +02:00
|
|
|
add_ChatManagerServicer_to_server(self, self.application.server)
|
2021-04-12 19:47:21 +02:00
|
|
|
|
|
|
|
@aiogrpc_request_wrapper()
|
|
|
|
async def create_chat(self, request, context, metadata):
|
|
|
|
chat = ChatPb(
|
|
|
|
chat_id=request.chat_id,
|
|
|
|
language=request.language,
|
|
|
|
username=request.username,
|
|
|
|
is_system_messaging_enabled=True,
|
|
|
|
is_discovery_enabled=True,
|
2022-09-02 17:44:56 +02:00
|
|
|
is_connectome_enabled=False,
|
2021-04-12 19:47:21 +02:00
|
|
|
)
|
|
|
|
query = (
|
|
|
|
PostgreSQLQuery
|
|
|
|
.into(self.chats_table)
|
|
|
|
.columns(
|
|
|
|
self.chats_table.chat_id,
|
|
|
|
self.chats_table.language,
|
|
|
|
self.chats_table.username,
|
|
|
|
self.chats_table.is_system_messaging_enabled,
|
|
|
|
self.chats_table.is_discovery_enabled,
|
2022-09-02 17:44:56 +02:00
|
|
|
self.chats_table.is_connectome_enabled,
|
2021-04-12 19:47:21 +02:00
|
|
|
)
|
|
|
|
.insert(
|
|
|
|
chat.chat_id,
|
|
|
|
chat.language,
|
|
|
|
chat.username,
|
|
|
|
chat.is_system_messaging_enabled,
|
|
|
|
chat.is_discovery_enabled,
|
2022-09-02 17:44:56 +02:00
|
|
|
chat.is_connectome_enabled,
|
2021-04-12 19:47:21 +02:00
|
|
|
)
|
2021-04-23 17:23:02 +02:00
|
|
|
.on_conflict('chat_id')
|
|
|
|
.do_nothing()
|
2021-04-12 19:47:21 +02:00
|
|
|
).get_sql()
|
2022-09-02 17:44:56 +02:00
|
|
|
await self.application.pool_holder['idm'].execute(query)
|
|
|
|
return await self._get_chat(chat_id=request.chat_id, context=context)
|
2021-04-12 19:47:21 +02:00
|
|
|
|
2022-09-02 17:44:56 +02:00
|
|
|
async def _get_chat(self, chat_id, context):
|
|
|
|
sql = (
|
2021-04-12 19:47:21 +02:00
|
|
|
PostgreSQLQuery
|
|
|
|
.from_(self.chats_table)
|
2022-09-02 17:44:56 +02:00
|
|
|
.select(
|
|
|
|
self.chats_table.chat_id,
|
|
|
|
self.chats_table.username,
|
|
|
|
self.chats_table.language,
|
|
|
|
self.chats_table.is_system_messaging_enabled,
|
|
|
|
self.chats_table.is_discovery_enabled,
|
|
|
|
self.chats_table.is_connectome_enabled,
|
|
|
|
self.chats_table.ban_until,
|
|
|
|
self.chats_table.ban_message,
|
|
|
|
self.chats_table.is_admin,
|
|
|
|
self.chats_table.created_at,
|
|
|
|
self.chats_table.updated_at,
|
|
|
|
)
|
2021-04-23 17:23:02 +02:00
|
|
|
.where(self.chats_table.chat_id == chat_id)
|
2021-04-12 19:47:21 +02:00
|
|
|
).get_sql()
|
2022-09-02 17:44:56 +02:00
|
|
|
|
|
|
|
chats = [ChatPb(**row) async for row in self.application.pool_holder['idm'].iterate(sql, row_factory=dict_row)]
|
|
|
|
if not chats:
|
2021-04-23 17:23:02 +02:00
|
|
|
await context.abort(StatusCode.NOT_FOUND, 'not_found')
|
2022-09-02 17:44:56 +02:00
|
|
|
return chats[0]
|
2021-04-23 17:23:02 +02:00
|
|
|
|
2022-09-02 17:44:56 +02:00
|
|
|
@aiogrpc_request_wrapper(log=False)
|
2021-04-23 17:23:02 +02:00
|
|
|
async def get_chat(self, request, context, metadata):
|
2022-09-02 17:44:56 +02:00
|
|
|
return await self._get_chat(chat_id=request.chat_id, context=context)
|
2021-04-12 19:47:21 +02:00
|
|
|
|
2022-09-02 17:44:56 +02:00
|
|
|
@aiogrpc_request_wrapper(log=False)
|
2021-04-12 19:47:21 +02:00
|
|
|
async def list_chats(self, request, context, metadata):
|
2022-09-02 17:44:56 +02:00
|
|
|
sql = (
|
2021-04-12 19:47:21 +02:00
|
|
|
PostgreSQLQuery
|
|
|
|
.from_(self.chats_table)
|
2022-09-02 17:44:56 +02:00
|
|
|
.select(
|
|
|
|
self.chats_table.chat_id,
|
|
|
|
self.chats_table.username,
|
|
|
|
self.chats_table.language,
|
|
|
|
self.chats_table.is_system_messaging_enabled,
|
|
|
|
self.chats_table.is_discovery_enabled,
|
|
|
|
self.chats_table.is_connectome_enabled,
|
|
|
|
self.chats_table.ban_until,
|
|
|
|
self.chats_table.ban_message,
|
|
|
|
self.chats_table.is_admin,
|
|
|
|
self.chats_table.created_at,
|
|
|
|
self.chats_table.updated_at,
|
|
|
|
)
|
2021-04-12 19:47:21 +02:00
|
|
|
.where(self.chats_table.ban_until > request.banned_at_moment)
|
|
|
|
.limit(10)
|
|
|
|
).get_sql()
|
2022-09-02 17:44:56 +02:00
|
|
|
return ChatsPb(chats=[ChatPb(**row) async for row in self.application.pool_holder['idm'].iterate(sql, row_factory=dict_row)])
|
2021-04-12 19:47:21 +02:00
|
|
|
|
|
|
|
@aiogrpc_request_wrapper()
|
|
|
|
async def update_chat(self, request, context, metadata):
|
2022-09-02 17:44:56 +02:00
|
|
|
sql = PostgreSQLQuery.update(self.chats_table)
|
2021-04-12 19:47:21 +02:00
|
|
|
for field in request.DESCRIPTOR.fields:
|
|
|
|
if field.containing_oneof and request.HasField(field.name):
|
|
|
|
field_value = getattr(request, field.name)
|
2022-09-02 17:44:56 +02:00
|
|
|
sql = sql.set(field.name, field_value)
|
|
|
|
sql = sql.where(self.chats_table.chat_id == request.chat_id).returning(
|
|
|
|
self.chats_table.chat_id,
|
|
|
|
self.chats_table.username,
|
|
|
|
self.chats_table.language,
|
|
|
|
self.chats_table.is_system_messaging_enabled,
|
|
|
|
self.chats_table.is_discovery_enabled,
|
|
|
|
self.chats_table.is_connectome_enabled,
|
|
|
|
self.chats_table.ban_until,
|
|
|
|
self.chats_table.ban_message,
|
|
|
|
self.chats_table.is_admin,
|
|
|
|
self.chats_table.created_at,
|
|
|
|
self.chats_table.updated_at,
|
|
|
|
).get_sql()
|
|
|
|
rows = []
|
|
|
|
async for row in self.application.pool_holder['idm'].iterate(sql, row_factory=dict_row):
|
|
|
|
rows.append(row)
|
|
|
|
if not rows:
|
|
|
|
return await context.abort(StatusCode.NOT_FOUND, 'not_found')
|
|
|
|
return ChatPb(**rows[0])
|