From 5455a6b359f6dbf3a09aecf15804a088d31a935c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 6 Jan 2023 14:14:37 +0100 Subject: [PATCH 01/36] Added first graphene models --- kdb-bot/src/bot/bot.json | 4 ++- kdb-bot/src/bot/config | 2 +- kdb-bot/src/bot_api/api.py | 11 ++++++ kdb-bot/src/bot_api/api_module.py | 1 + .../bot_api/controller/graphql_controller.py | 32 +++++++++++++++++ .../src/bot_data/graphql/query/__init__.py | 0 .../bot_data/graphql/query/client_query.py | 19 +++++++++++ .../bot_data/graphql/query/server_query.py | 19 +++++++++++ kdb-bot/src/bot_data/graphql/root_query.py | 12 +++++++ kdb-bot/src/bot_data/graphql/schema.py | 5 +++ .../graphql/types/client_query_type.py | 17 ++++++++++ .../graphql/types/server_query_type.py | 30 ++++++++++++++++ .../service/client_repository_service.py | 34 +++++++++++++------ .../service/server_repository_service.py | 16 ++++++--- 14 files changed, 184 insertions(+), 18 deletions(-) create mode 100644 kdb-bot/src/bot_api/controller/graphql_controller.py create mode 100644 kdb-bot/src/bot_data/graphql/query/__init__.py create mode 100644 kdb-bot/src/bot_data/graphql/query/client_query.py create mode 100644 kdb-bot/src/bot_data/graphql/query/server_query.py create mode 100644 kdb-bot/src/bot_data/graphql/root_query.py create mode 100644 kdb-bot/src/bot_data/graphql/schema.py create mode 100644 kdb-bot/src/bot_data/graphql/types/client_query_type.py create mode 100644 kdb-bot/src/bot_data/graphql/types/server_query_type.py diff --git a/kdb-bot/src/bot/bot.json b/kdb-bot/src/bot/bot.json index c9461eb6..7fea125a 100644 --- a/kdb-bot/src/bot/bot.json +++ b/kdb-bot/src/bot/bot.json @@ -28,7 +28,9 @@ "Flask-SocketIO==5.3.2", "eventlet==0.33.2", "requests-oauthlib==1.3.1", - "icmplib==3.0.3" + "icmplib==3.0.3", + "graphene==3.2.1", + "graphql-server==3.0.0b5" ], "DevDependencies": [ "cpl-cli==2022.12.0" diff --git a/kdb-bot/src/bot/config b/kdb-bot/src/bot/config index f15e3ff8..64a551bc 160000 --- a/kdb-bot/src/bot/config +++ b/kdb-bot/src/bot/config @@ -1 +1 @@ -Subproject commit f15e3ff82709bbd274b0645c2b13a482f67f75ff +Subproject commit 64a551bcee7fbfb06ee724355b48baec1c8e8e3e diff --git a/kdb-bot/src/bot_api/api.py b/kdb-bot/src/bot_api/api.py index b31eee19..b44b92b3 100644 --- a/kdb-bot/src/bot_api/api.py +++ b/kdb-bot/src/bot_api/api.py @@ -12,6 +12,7 @@ from eventlet import wsgi from flask import Flask, request, jsonify, Response from flask_cors import CORS from flask_socketio import SocketIO +from graphql_server.flask import GraphQLView from werkzeug.exceptions import NotFound from bot_api.configuration.api_settings import ApiSettings @@ -22,6 +23,7 @@ from bot_api.exception.service_exception import ServiceException from bot_api.logging.api_logger import ApiLogger from bot_api.model.error_dto import ErrorDTO from bot_api.route.route import Route +from bot_data.graphql.schema import schema class Api(Flask): @@ -62,6 +64,15 @@ class Api(Flask): self._requests = {} + self.add_url_rule( + '/api/graphql', + view_func=GraphQLView.as_view( + 'graphql', + schema=schema, + graphiql=True # for having the GraphiQL interface + ) + ) + @staticmethod def _get_methods_from_registered_route() -> Union[list[str], str]: methods = ['Unknown'] diff --git a/kdb-bot/src/bot_api/api_module.py b/kdb-bot/src/bot_api/api_module.py index b4c1735a..f95aa58c 100644 --- a/kdb-bot/src/bot_api/api_module.py +++ b/kdb-bot/src/bot_api/api_module.py @@ -47,6 +47,7 @@ class ApiModule(ModuleABC): services.add_transient(GuiController) services.add_transient(DiscordService) services.add_transient(ServerController) + #services.add_transient(GraphQLController) # cpl-discord self._dc.add_event(DiscordEventTypesEnum.on_ready.value, BotApiOnReadyEvent) diff --git a/kdb-bot/src/bot_api/controller/graphql_controller.py b/kdb-bot/src/bot_api/controller/graphql_controller.py new file mode 100644 index 00000000..a1cc5425 --- /dev/null +++ b/kdb-bot/src/bot_api/controller/graphql_controller.py @@ -0,0 +1,32 @@ +import os + +from cpl_core.configuration import ConfigurationABC +from cpl_core.environment import ApplicationEnvironmentABC +from cpl_core.mailing import EMail, EMailClientABC, EMailClientSettings +from cpl_translation import TranslatePipe +from flask import jsonify, request + +from bot_api.api import Api +from bot_api.configuration.authentication_settings import AuthenticationSettings +from bot_api.logging.api_logger import ApiLogger +from bot_api.model.settings_dto import SettingsDTO +from bot_api.model.version_dto import VersionDTO +from bot_api.route.route import Route + + +class GraphQLController: + BasePath = f'/api/graphql' + + def __init__( + self, + config: ConfigurationABC, + env: ApplicationEnvironmentABC, + logger: ApiLogger, + ): + self._config = config + self._env = env + self._logger = logger + + @Route.get(f'{BasePath}') + async def graphql(self): + return '', 200 diff --git a/kdb-bot/src/bot_data/graphql/query/__init__.py b/kdb-bot/src/bot_data/graphql/query/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/kdb-bot/src/bot_data/graphql/query/client_query.py b/kdb-bot/src/bot_data/graphql/query/client_query.py new file mode 100644 index 00000000..2e34fbdd --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/query/client_query.py @@ -0,0 +1,19 @@ +import graphene +from cpl_core.dependency_injection import ServiceProviderABC +from graphene import ObjectType + +from bot_data.graphql.types.client_query_type import ClientQueryType +from bot_data.service.client_repository_service import ClientRepositoryService + + +class ClientQuery(ObjectType): + clients = graphene.List(ClientQueryType) + + @staticmethod + @ServiceProviderABC.inject + def _get_clients(clients: ClientRepositoryService): + return clients.get_clients() + + @staticmethod + def resolve_clients(root, info): + return ClientQuery._get_clients() diff --git a/kdb-bot/src/bot_data/graphql/query/server_query.py b/kdb-bot/src/bot_data/graphql/query/server_query.py new file mode 100644 index 00000000..d0fa2acc --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/query/server_query.py @@ -0,0 +1,19 @@ +import graphene +from cpl_core.dependency_injection import ServiceProviderABC +from graphene import ObjectType + +from bot_data.graphql.types.server_query_type import ServerQueryType +from bot_data.service.server_repository_service import ServerRepositoryService + + +class ServerQuery(ObjectType): + servers = graphene.List(ServerQueryType) + + @staticmethod + @ServiceProviderABC.inject + def _get_servers(servers: ServerRepositoryService): + return servers.get_servers() + + @staticmethod + def resolve_servers(root, info): + return ServerQuery._get_servers() diff --git a/kdb-bot/src/bot_data/graphql/root_query.py b/kdb-bot/src/bot_data/graphql/root_query.py new file mode 100644 index 00000000..057820bb --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/root_query.py @@ -0,0 +1,12 @@ +from graphene import ObjectType, relay + +from bot_data.graphql.query.client_query import ClientQuery +from bot_data.graphql.query.server_query import ServerQuery + + +class RootQuery( + ServerQuery, + ClientQuery, + ObjectType +): + node = relay.Node.Field() diff --git a/kdb-bot/src/bot_data/graphql/schema.py b/kdb-bot/src/bot_data/graphql/schema.py new file mode 100644 index 00000000..bf335aee --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/schema.py @@ -0,0 +1,5 @@ +import graphene + +from bot_data.graphql.root_query import RootQuery + +schema = graphene.Schema(query=RootQuery) diff --git a/kdb-bot/src/bot_data/graphql/types/client_query_type.py b/kdb-bot/src/bot_data/graphql/types/client_query_type.py new file mode 100644 index 00000000..3d62c3f8 --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/types/client_query_type.py @@ -0,0 +1,17 @@ +from graphene import ObjectType, Int + +from bot_data.model.client import Client + + +class ClientQueryType(ObjectType): + id = Int() + discord_client_id = Int() + sent_message_count = Int() + received_message_count = Int() + deleted_message_count = Int() + received_command_count = Int() + moved_users_count = Int() + + @staticmethod + def resolve_id(client: Client, info): + return client.client_id diff --git a/kdb-bot/src/bot_data/graphql/types/server_query_type.py b/kdb-bot/src/bot_data/graphql/types/server_query_type.py new file mode 100644 index 00000000..1ca96651 --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/types/server_query_type.py @@ -0,0 +1,30 @@ +from cpl_core.dependency_injection import ServiceProviderABC +from graphene import ObjectType, Int, DateTime, String, Field + +from bot_data.graphql.types.client_query_type import ClientQueryType +from bot_data.model.server import Server +from bot_data.service.client_repository_service import ClientRepositoryService + + +class ServerQueryType(ObjectType): + id = Int() + discord_server_id = String() + + created_at = DateTime() + modified_at = DateTime() + + client = Field(ClientQueryType) + + @staticmethod + def resolve_id(server: Server, info): + return server.server_id + + @staticmethod + @ServiceProviderABC.inject + def _get_clients(clients: ClientRepositoryService): + return clients + + @staticmethod + def resolve_client(server: Server, info): + clients: ClientRepositoryService = ServerQueryType._get_clients() + return clients.find_client_by_server_id(server.server_id) diff --git a/kdb-bot/src/bot_data/service/client_repository_service.py b/kdb-bot/src/bot_data/service/client_repository_service.py index ba875a25..102758e5 100644 --- a/kdb-bot/src/bot_data/service/client_repository_service.py +++ b/kdb-bot/src/bot_data/service/client_repository_service.py @@ -33,6 +33,8 @@ class ClientRepositoryService(ClientRepositoryABC): result[5], result[6], self._servers.get_server_by_id(result[7]), + result[8], + result[9], id=result[0] )) @@ -49,6 +51,8 @@ class ClientRepositoryService(ClientRepositoryABC): result[5], result[6], self._servers.get_server_by_id(result[7]), + result[8], + result[9], id=result[0] ) @@ -63,6 +67,8 @@ class ClientRepositoryService(ClientRepositoryABC): result[5], result[6], self._servers.get_server_by_id(result[7]), + result[8], + result[9], id=result[0] ) @@ -71,9 +77,9 @@ class ClientRepositoryService(ClientRepositoryABC): result = self._context.select(Client.get_select_by_discord_id_string(discord_id)) if result is None or len(result) == 0: return None - + result = result[0] - + return Client( result[1], result[2], @@ -82,6 +88,8 @@ class ClientRepositoryService(ClientRepositoryABC): result[5], result[6], self._servers.get_server_by_id(result[7]), + result[8], + result[9], id=result[0] ) @@ -90,9 +98,9 @@ class ClientRepositoryService(ClientRepositoryABC): result = self._context.select(Client.get_select_by_server_id_string(discord_id)) if result is None or len(result) == 0: return None - + result = result[0] - + return Client( result[1], result[2], @@ -101,6 +109,8 @@ class ClientRepositoryService(ClientRepositoryABC): result[5], result[6], self._servers.get_server_by_id(result[7]), + result[8], + result[9], id=result[0] ) @@ -109,9 +119,9 @@ class ClientRepositoryService(ClientRepositoryABC): result = self._context.select(Client.get_select_by_discord_id_and_server_id_string(discord_id, server_id)) if result is None or len(result) == 0: return None - + result = result[0] - + return Client( result[1], result[2], @@ -120,17 +130,19 @@ class ClientRepositoryService(ClientRepositoryABC): result[5], result[6], self._servers.get_server_by_id(result[7]), + result[8], + result[9], id=result[0] ) - + def add_client(self, client: Client): self._logger.trace(__name__, f'Send SQL command: {client.insert_string}') self._context.cursor.execute(client.insert_string) - + def update_client(self, client: Client): self._logger.trace(__name__, f'Send SQL command: {client.udpate_string}') self._context.cursor.execute(client.udpate_string) - + def delete_client(self, client: Client): self._logger.trace(__name__, f'Send SQL command: {client.delete_string}') self._context.cursor.execute(client.delete_string) @@ -140,12 +152,12 @@ class ClientRepositoryService(ClientRepositoryABC): if server is None: self._logger.warn(__name__, f'Cannot find server by id {server_id}') raise Exception('Value not found') - + client = self.find_client_by_discord_id_and_server_id(id, server.server_id) if client is None: self._logger.warn(__name__, f'Cannot find client by ids {id}@{server.server_id}') raise Exception('Value not found') - + return client def append_sent_message_count(self, client_id: int, server_id: int, value: int): diff --git a/kdb-bot/src/bot_data/service/server_repository_service.py b/kdb-bot/src/bot_data/service/server_repository_service.py index 210a6dee..5ae268ea 100644 --- a/kdb-bot/src/bot_data/service/server_repository_service.py +++ b/kdb-bot/src/bot_data/service/server_repository_service.py @@ -25,6 +25,8 @@ class ServerRepositoryService(ServerRepositoryABC): for result in results: servers.append(Server( result[1], + result[2], + result[3], id=result[0] )) @@ -55,6 +57,8 @@ class ServerRepositoryService(ServerRepositoryABC): result = self._context.select(Server.get_select_by_id_string(server_id))[0] return Server( result[1], + result[2], + result[3], id=result[0] ) @@ -63,6 +67,8 @@ class ServerRepositoryService(ServerRepositoryABC): result = self._context.select(Server.get_select_by_discord_id_string(discord_id))[0] return Server( result[1], + result[2], + result[3], id=result[0] ) @@ -71,24 +77,24 @@ class ServerRepositoryService(ServerRepositoryABC): result = self._context.select(Server.get_select_by_discord_id_string(discord_id)) if result is None or len(result) == 0: return None - + result = result[0] - + return Server( result[1], result[2], result[3], id=result[0] ) - + def add_server(self, server: Server): self._logger.trace(__name__, f'Send SQL command: {server.insert_string}') self._context.cursor.execute(server.insert_string) - + def update_server(self, server: Server): self._logger.trace(__name__, f'Send SQL command: {server.udpate_string}') self._context.cursor.execute(server.udpate_string) - + def delete_server(self, server: Server): self._logger.trace(__name__, f'Send SQL command: {server.delete_string}') self._context.cursor.execute(server.delete_string) From 75500076a7c03a26b43bd0d994e927bf74fe4841 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 6 Jan 2023 14:47:45 +0100 Subject: [PATCH 02/36] Improved gql data layer --- kdb-bot/src/bot_api/api.py | 5 +- kdb-bot/src/bot_api/api_module.py | 1 - .../bot_api/controller/graphql_controller.py | 32 ----------- kdb-bot/src/bot_data/abc/queryABC.py | 53 +++++++++++++++++++ kdb-bot/src/bot_data/graphql/graphql.py | 16 ++++++ .../graphql/{query => queries}/__init__.py | 0 .../bot_data/graphql/queries/client_query.py | 12 +++++ .../{query => queries}/server_query.py | 10 ++-- .../bot_data/graphql/query/client_query.py | 19 ------- kdb-bot/src/bot_data/graphql/root_query.py | 4 +- kdb-bot/src/bot_data/graphql/schema.py | 5 -- .../graphql/types/client_query_type.py | 7 +-- .../graphql/types/server_query_type.py | 21 +++----- 13 files changed, 101 insertions(+), 84 deletions(-) delete mode 100644 kdb-bot/src/bot_api/controller/graphql_controller.py create mode 100644 kdb-bot/src/bot_data/abc/queryABC.py create mode 100644 kdb-bot/src/bot_data/graphql/graphql.py rename kdb-bot/src/bot_data/graphql/{query => queries}/__init__.py (100%) create mode 100644 kdb-bot/src/bot_data/graphql/queries/client_query.py rename kdb-bot/src/bot_data/graphql/{query => queries}/server_query.py (72%) delete mode 100644 kdb-bot/src/bot_data/graphql/query/client_query.py delete mode 100644 kdb-bot/src/bot_data/graphql/schema.py diff --git a/kdb-bot/src/bot_api/api.py b/kdb-bot/src/bot_api/api.py index b44b92b3..26b4bae6 100644 --- a/kdb-bot/src/bot_api/api.py +++ b/kdb-bot/src/bot_api/api.py @@ -23,7 +23,7 @@ from bot_api.exception.service_exception import ServiceException from bot_api.logging.api_logger import ApiLogger from bot_api.model.error_dto import ErrorDTO from bot_api.route.route import Route -from bot_data.graphql.schema import schema +from bot_data.graphql.graphql import GraphQL class Api(Flask): @@ -64,11 +64,12 @@ class Api(Flask): self._requests = {} + gql = GraphQL() self.add_url_rule( '/api/graphql', view_func=GraphQLView.as_view( 'graphql', - schema=schema, + schema=gql.schema, graphiql=True # for having the GraphiQL interface ) ) diff --git a/kdb-bot/src/bot_api/api_module.py b/kdb-bot/src/bot_api/api_module.py index f95aa58c..b4c1735a 100644 --- a/kdb-bot/src/bot_api/api_module.py +++ b/kdb-bot/src/bot_api/api_module.py @@ -47,7 +47,6 @@ class ApiModule(ModuleABC): services.add_transient(GuiController) services.add_transient(DiscordService) services.add_transient(ServerController) - #services.add_transient(GraphQLController) # cpl-discord self._dc.add_event(DiscordEventTypesEnum.on_ready.value, BotApiOnReadyEvent) diff --git a/kdb-bot/src/bot_api/controller/graphql_controller.py b/kdb-bot/src/bot_api/controller/graphql_controller.py deleted file mode 100644 index a1cc5425..00000000 --- a/kdb-bot/src/bot_api/controller/graphql_controller.py +++ /dev/null @@ -1,32 +0,0 @@ -import os - -from cpl_core.configuration import ConfigurationABC -from cpl_core.environment import ApplicationEnvironmentABC -from cpl_core.mailing import EMail, EMailClientABC, EMailClientSettings -from cpl_translation import TranslatePipe -from flask import jsonify, request - -from bot_api.api import Api -from bot_api.configuration.authentication_settings import AuthenticationSettings -from bot_api.logging.api_logger import ApiLogger -from bot_api.model.settings_dto import SettingsDTO -from bot_api.model.version_dto import VersionDTO -from bot_api.route.route import Route - - -class GraphQLController: - BasePath = f'/api/graphql' - - def __init__( - self, - config: ConfigurationABC, - env: ApplicationEnvironmentABC, - logger: ApiLogger, - ): - self._config = config - self._env = env - self._logger = logger - - @Route.get(f'{BasePath}') - async def graphql(self): - return '', 200 diff --git a/kdb-bot/src/bot_data/abc/queryABC.py b/kdb-bot/src/bot_data/abc/queryABC.py new file mode 100644 index 00000000..af49f42b --- /dev/null +++ b/kdb-bot/src/bot_data/abc/queryABC.py @@ -0,0 +1,53 @@ +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_query.extension import List +from graphene import ObjectType + +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.client_repository_abc import ClientRepositoryABC +from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC +from bot_data.abc.level_repository_abc import LevelRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC +from bot_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.model.auto_role import AutoRole +from bot_data.model.client import Client +from bot_data.model.known_user import KnownUser +from bot_data.model.level import Level +from bot_data.model.server import Server +from bot_data.model.user import User +from bot_data.model.user_joined_server import UserJoinedServer +from bot_data.model.user_joined_voice_channel import UserJoinedVoiceChannel + + +class QueryABC(ObjectType): + _auto_roles: List[AutoRole] + _clients: List[Client] + _known_users: List[KnownUser] + _levels: List[Level] + _servers: List[Server] + _user_joined_servers: List[UserJoinedServer] + _user_joined_voice_channel: List[UserJoinedVoiceChannel] + _users: List[User] + + @classmethod + @ServiceProviderABC.inject + def init( + cls, + auto_roles: AutoRoleRepositoryABC, + clients: ClientRepositoryABC, + known_users: KnownUserRepositoryABC, + levels: LevelRepositoryABC, + servers: ServerRepositoryABC, + user_joined_servers: UserJoinedServerRepositoryABC, + user_joined_voice_channel: UserJoinedVoiceChannelRepositoryABC, + users: UserRepositoryABC, + ): + cls._auto_roles = auto_roles.get_auto_roles() + cls._clients = clients.get_clients() + cls._known_users = known_users.get_users() + cls._levels = levels.get_levels() + cls._servers = servers.get_servers() + cls._user_joined_servers = user_joined_servers.get_user_joined_servers() + cls._user_joined_voice_channel = user_joined_voice_channel.get_user_joined_voice_channels() + cls._users = users.get_users() diff --git a/kdb-bot/src/bot_data/graphql/graphql.py b/kdb-bot/src/bot_data/graphql/graphql.py new file mode 100644 index 00000000..23df08dc --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/graphql.py @@ -0,0 +1,16 @@ +import graphene +from graphene import Schema + +from bot_data.abc.queryABC import QueryABC +from bot_data.graphql.root_query import RootQuery + + +class GraphQL: + + def __init__(self): + QueryABC.init() + self._schema = graphene.Schema(query=RootQuery) + + @property + def schema(self) -> Schema: + return self._schema diff --git a/kdb-bot/src/bot_data/graphql/query/__init__.py b/kdb-bot/src/bot_data/graphql/queries/__init__.py similarity index 100% rename from kdb-bot/src/bot_data/graphql/query/__init__.py rename to kdb-bot/src/bot_data/graphql/queries/__init__.py diff --git a/kdb-bot/src/bot_data/graphql/queries/client_query.py b/kdb-bot/src/bot_data/graphql/queries/client_query.py new file mode 100644 index 00000000..a900cd58 --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/queries/client_query.py @@ -0,0 +1,12 @@ +import graphene + +from bot_data.abc.queryABC import QueryABC +from bot_data.graphql.types.client_query_type import ClientQueryType + + +class ClientQuery(QueryABC): + clients = graphene.List(ClientQueryType) + + @classmethod + def resolve_clients(cls, root, info): + return cls._clients diff --git a/kdb-bot/src/bot_data/graphql/query/server_query.py b/kdb-bot/src/bot_data/graphql/queries/server_query.py similarity index 72% rename from kdb-bot/src/bot_data/graphql/query/server_query.py rename to kdb-bot/src/bot_data/graphql/queries/server_query.py index d0fa2acc..9092ddd2 100644 --- a/kdb-bot/src/bot_data/graphql/query/server_query.py +++ b/kdb-bot/src/bot_data/graphql/queries/server_query.py @@ -1,12 +1,12 @@ import graphene from cpl_core.dependency_injection import ServiceProviderABC -from graphene import ObjectType +from bot_data.abc.queryABC import QueryABC from bot_data.graphql.types.server_query_type import ServerQueryType from bot_data.service.server_repository_service import ServerRepositoryService -class ServerQuery(ObjectType): +class ServerQuery(QueryABC): servers = graphene.List(ServerQueryType) @staticmethod @@ -14,6 +14,6 @@ class ServerQuery(ObjectType): def _get_servers(servers: ServerRepositoryService): return servers.get_servers() - @staticmethod - def resolve_servers(root, info): - return ServerQuery._get_servers() + @classmethod + def resolve_servers(cls, root, info): + return cls._servers diff --git a/kdb-bot/src/bot_data/graphql/query/client_query.py b/kdb-bot/src/bot_data/graphql/query/client_query.py deleted file mode 100644 index 2e34fbdd..00000000 --- a/kdb-bot/src/bot_data/graphql/query/client_query.py +++ /dev/null @@ -1,19 +0,0 @@ -import graphene -from cpl_core.dependency_injection import ServiceProviderABC -from graphene import ObjectType - -from bot_data.graphql.types.client_query_type import ClientQueryType -from bot_data.service.client_repository_service import ClientRepositoryService - - -class ClientQuery(ObjectType): - clients = graphene.List(ClientQueryType) - - @staticmethod - @ServiceProviderABC.inject - def _get_clients(clients: ClientRepositoryService): - return clients.get_clients() - - @staticmethod - def resolve_clients(root, info): - return ClientQuery._get_clients() diff --git a/kdb-bot/src/bot_data/graphql/root_query.py b/kdb-bot/src/bot_data/graphql/root_query.py index 057820bb..40590f12 100644 --- a/kdb-bot/src/bot_data/graphql/root_query.py +++ b/kdb-bot/src/bot_data/graphql/root_query.py @@ -1,12 +1,10 @@ from graphene import ObjectType, relay -from bot_data.graphql.query.client_query import ClientQuery -from bot_data.graphql.query.server_query import ServerQuery +from bot_data.graphql.queries.server_query import ServerQuery class RootQuery( ServerQuery, - ClientQuery, ObjectType ): node = relay.Node.Field() diff --git a/kdb-bot/src/bot_data/graphql/schema.py b/kdb-bot/src/bot_data/graphql/schema.py deleted file mode 100644 index bf335aee..00000000 --- a/kdb-bot/src/bot_data/graphql/schema.py +++ /dev/null @@ -1,5 +0,0 @@ -import graphene - -from bot_data.graphql.root_query import RootQuery - -schema = graphene.Schema(query=RootQuery) diff --git a/kdb-bot/src/bot_data/graphql/types/client_query_type.py b/kdb-bot/src/bot_data/graphql/types/client_query_type.py index 3d62c3f8..30630989 100644 --- a/kdb-bot/src/bot_data/graphql/types/client_query_type.py +++ b/kdb-bot/src/bot_data/graphql/types/client_query_type.py @@ -1,11 +1,12 @@ -from graphene import ObjectType, Int +from graphene import Int, String +from bot_data.abc.queryABC import QueryABC from bot_data.model.client import Client -class ClientQueryType(ObjectType): +class ClientQueryType(QueryABC): id = Int() - discord_client_id = Int() + discord_id = String() sent_message_count = Int() received_message_count = Int() deleted_message_count = Int() diff --git a/kdb-bot/src/bot_data/graphql/types/server_query_type.py b/kdb-bot/src/bot_data/graphql/types/server_query_type.py index 1ca96651..ca719507 100644 --- a/kdb-bot/src/bot_data/graphql/types/server_query_type.py +++ b/kdb-bot/src/bot_data/graphql/types/server_query_type.py @@ -1,30 +1,23 @@ -from cpl_core.dependency_injection import ServiceProviderABC -from graphene import ObjectType, Int, DateTime, String, Field +from graphene import Int, DateTime, String, List +from bot_data.abc.queryABC import QueryABC from bot_data.graphql.types.client_query_type import ClientQueryType from bot_data.model.server import Server -from bot_data.service.client_repository_service import ClientRepositoryService -class ServerQueryType(ObjectType): +class ServerQueryType(QueryABC): id = Int() discord_server_id = String() created_at = DateTime() modified_at = DateTime() - client = Field(ClientQueryType) + clients = List(ClientQueryType) @staticmethod def resolve_id(server: Server, info): return server.server_id - @staticmethod - @ServiceProviderABC.inject - def _get_clients(clients: ClientRepositoryService): - return clients - - @staticmethod - def resolve_client(server: Server, info): - clients: ClientRepositoryService = ServerQueryType._get_clients() - return clients.find_client_by_server_id(server.server_id) + @classmethod + def resolve_clients(cls, server: Server, info): + return cls._clients.where(lambda c: c.server.server_id == server.server_id) From b13695b018bd5367e1c5870f49701af8a73872ef Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 6 Jan 2023 16:21:21 +0100 Subject: [PATCH 03/36] Moved to ariadne --- kdb-bot/src/bot/bot.json | 4 +- kdb-bot/src/bot_api/api.py | 13 ---- kdb-bot/src/bot_api/api_module.py | 2 + .../bot_api/controller/grahpql_controller.py | 44 +++++++++++++ .../abc/{queryABC.py => query_abc.py} | 13 +++- kdb-bot/src/bot_data/graphql/graphql.py | 16 ----- kdb-bot/src/bot_data/graphql/model.gql | 64 +++++++++++++++++++ .../bot_data/graphql/queries/client_query.py | 12 ---- .../bot_data/graphql/queries/server_query.py | 19 ------ kdb-bot/src/bot_data/graphql/query.py | 17 +++++ kdb-bot/src/bot_data/graphql/root_query.py | 10 --- .../graphql/{queries => types}/__init__.py | 0 .../graphql/types/client_query_type.py | 26 ++++---- .../graphql/types/level_query_type.py | 21 ++++++ .../graphql/types/server_query_type.py | 41 ++++++++---- .../bot_data/graphql/types/user_query_type.py | 39 +++++++++++ 16 files changed, 245 insertions(+), 96 deletions(-) create mode 100644 kdb-bot/src/bot_api/controller/grahpql_controller.py rename kdb-bot/src/bot_data/abc/{queryABC.py => query_abc.py} (85%) delete mode 100644 kdb-bot/src/bot_data/graphql/graphql.py create mode 100644 kdb-bot/src/bot_data/graphql/model.gql delete mode 100644 kdb-bot/src/bot_data/graphql/queries/client_query.py delete mode 100644 kdb-bot/src/bot_data/graphql/queries/server_query.py create mode 100644 kdb-bot/src/bot_data/graphql/query.py delete mode 100644 kdb-bot/src/bot_data/graphql/root_query.py rename kdb-bot/src/bot_data/graphql/{queries => types}/__init__.py (100%) create mode 100644 kdb-bot/src/bot_data/graphql/types/level_query_type.py create mode 100644 kdb-bot/src/bot_data/graphql/types/user_query_type.py diff --git a/kdb-bot/src/bot/bot.json b/kdb-bot/src/bot/bot.json index 7fea125a..f7b034a1 100644 --- a/kdb-bot/src/bot/bot.json +++ b/kdb-bot/src/bot/bot.json @@ -29,8 +29,8 @@ "eventlet==0.33.2", "requests-oauthlib==1.3.1", "icmplib==3.0.3", - "graphene==3.2.1", - "graphql-server==3.0.0b5" + "ariadne==0.17.0", + "ariadne-graphql-modules==0.7.0" ], "DevDependencies": [ "cpl-cli==2022.12.0" diff --git a/kdb-bot/src/bot_api/api.py b/kdb-bot/src/bot_api/api.py index 26b4bae6..5d91c75e 100644 --- a/kdb-bot/src/bot_api/api.py +++ b/kdb-bot/src/bot_api/api.py @@ -1,4 +1,3 @@ -import re import sys import textwrap import uuid @@ -12,7 +11,6 @@ from eventlet import wsgi from flask import Flask, request, jsonify, Response from flask_cors import CORS from flask_socketio import SocketIO -from graphql_server.flask import GraphQLView from werkzeug.exceptions import NotFound from bot_api.configuration.api_settings import ApiSettings @@ -23,7 +21,6 @@ from bot_api.exception.service_exception import ServiceException from bot_api.logging.api_logger import ApiLogger from bot_api.model.error_dto import ErrorDTO from bot_api.route.route import Route -from bot_data.graphql.graphql import GraphQL class Api(Flask): @@ -64,16 +61,6 @@ class Api(Flask): self._requests = {} - gql = GraphQL() - self.add_url_rule( - '/api/graphql', - view_func=GraphQLView.as_view( - 'graphql', - schema=gql.schema, - graphiql=True # for having the GraphiQL interface - ) - ) - @staticmethod def _get_methods_from_registered_route() -> Union[list[str], str]: methods = ['Unknown'] diff --git a/kdb-bot/src/bot_api/api_module.py b/kdb-bot/src/bot_api/api_module.py index b4c1735a..7448dcb3 100644 --- a/kdb-bot/src/bot_api/api_module.py +++ b/kdb-bot/src/bot_api/api_module.py @@ -14,6 +14,7 @@ from bot_api.api_thread import ApiThread from bot_api.controller.auth_controller import AuthController from bot_api.controller.auth_discord_controller import AuthDiscordController from bot_api.controller.discord.server_controller import ServerController +from bot_api.controller.grahpql_controller import GraphQLController from bot_api.controller.gui_controller import GuiController from bot_api.event.bot_api_on_ready_event import BotApiOnReadyEvent from bot_api.service.auth_service import AuthService @@ -47,6 +48,7 @@ class ApiModule(ModuleABC): services.add_transient(GuiController) services.add_transient(DiscordService) services.add_transient(ServerController) + services.add_transient(GraphQLController) # cpl-discord self._dc.add_event(DiscordEventTypesEnum.on_ready.value, BotApiOnReadyEvent) diff --git a/kdb-bot/src/bot_api/controller/grahpql_controller.py b/kdb-bot/src/bot_api/controller/grahpql_controller.py new file mode 100644 index 00000000..477b843f --- /dev/null +++ b/kdb-bot/src/bot_api/controller/grahpql_controller.py @@ -0,0 +1,44 @@ +from ariadne import graphql_sync +from ariadne.constants import PLAYGROUND_HTML +from ariadne_graphql_modules import make_executable_schema +from cpl_core.configuration import ConfigurationABC +from cpl_core.environment import ApplicationEnvironmentABC +from flask import request, jsonify + +from bot_api.logging.api_logger import ApiLogger +from bot_api.route.route import Route +from bot_data.abc.query_abc import QueryABC +from bot_data.graphql.query import Query + + +class GraphQLController: + BasePath = f'/api/graphql' + + def __init__( + self, + config: ConfigurationABC, + env: ApplicationEnvironmentABC, + logger: ApiLogger, + ): + self._config = config + self._env = env + self._logger = logger + + @Route.get(f'{BasePath}/playground') + async def playground(self): + return PLAYGROUND_HTML, 200 + + @Route.post(f'{BasePath}') + async def graphql(self): + QueryABC.init() + data = request.get_json() + + # Note: Passing the request to the context is optional. + # In Flask, the current request is always accessible as flask.request + success, result = graphql_sync( + make_executable_schema(Query), + data, + context_value=request + ) + + return jsonify(result), 200 if success else 400 diff --git a/kdb-bot/src/bot_data/abc/queryABC.py b/kdb-bot/src/bot_data/abc/query_abc.py similarity index 85% rename from kdb-bot/src/bot_data/abc/queryABC.py rename to kdb-bot/src/bot_data/abc/query_abc.py index af49f42b..9ee57ecc 100644 --- a/kdb-bot/src/bot_data/abc/queryABC.py +++ b/kdb-bot/src/bot_data/abc/query_abc.py @@ -1,6 +1,7 @@ +from ariadne_graphql_modules import ObjectType from cpl_core.dependency_injection import ServiceProviderABC +from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List -from graphene import ObjectType from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.client_repository_abc import ClientRepositoryABC @@ -18,9 +19,12 @@ from bot_data.model.server import Server from bot_data.model.user import User from bot_data.model.user_joined_server import UserJoinedServer from bot_data.model.user_joined_voice_channel import UserJoinedVoiceChannel +from modules.level.service.level_service import LevelService class QueryABC(ObjectType): + __abstract__ = True + _auto_roles: List[AutoRole] _clients: List[Client] _known_users: List[KnownUser] @@ -30,6 +34,9 @@ class QueryABC(ObjectType): _user_joined_voice_channel: List[UserJoinedVoiceChannel] _users: List[User] + _bot: DiscordBotServiceABC + _level_service: LevelService + @classmethod @ServiceProviderABC.inject def init( @@ -42,6 +49,8 @@ class QueryABC(ObjectType): user_joined_servers: UserJoinedServerRepositoryABC, user_joined_voice_channel: UserJoinedVoiceChannelRepositoryABC, users: UserRepositoryABC, + bot: DiscordBotServiceABC, + level_service: LevelService, ): cls._auto_roles = auto_roles.get_auto_roles() cls._clients = clients.get_clients() @@ -51,3 +60,5 @@ class QueryABC(ObjectType): cls._user_joined_servers = user_joined_servers.get_user_joined_servers() cls._user_joined_voice_channel = user_joined_voice_channel.get_user_joined_voice_channels() cls._users = users.get_users() + cls._bot = bot + cls._level_service = level_service diff --git a/kdb-bot/src/bot_data/graphql/graphql.py b/kdb-bot/src/bot_data/graphql/graphql.py deleted file mode 100644 index 23df08dc..00000000 --- a/kdb-bot/src/bot_data/graphql/graphql.py +++ /dev/null @@ -1,16 +0,0 @@ -import graphene -from graphene import Schema - -from bot_data.abc.queryABC import QueryABC -from bot_data.graphql.root_query import RootQuery - - -class GraphQL: - - def __init__(self): - QueryABC.init() - self._schema = graphene.Schema(query=RootQuery) - - @property - def schema(self) -> Schema: - return self._schema diff --git a/kdb-bot/src/bot_data/graphql/model.gql b/kdb-bot/src/bot_data/graphql/model.gql new file mode 100644 index 00000000..dba3d2af --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/model.gql @@ -0,0 +1,64 @@ +type Query { + servers: [Server] + known_users: [User] +} + +type Server { + id: ID + discord_id: String + clients: [Client] + members: [User] + level: [Level] +} + +type Client { + id: ID + discord_id: String + sent_message_count: Int + received_message_count: Int + deleted_message_count: Int + received_command_count: Int + moved_users_count: Int + + server: Server +} + +type User { + id: ID + discord_id: String + name: String + xp: Int + ontime: Int + level: Level + + joined_servers: [UserJoinedServer] + joined_voice_channel: [UserJoinedVoiceChannel] + + server: Server +} + +type UserJoinedServer { + id: ID + user: User + server: Server + joined_on: String + leaved_on: String +} + +type UserJoinedVoiceChannel { + id: ID + channel_id: String + user: User + joined_on: String + leaved_on: String +} + +type Level { + id: ID + name: String + color: String + min_xp: Int + permissions: String + + server: Server +} \ No newline at end of file diff --git a/kdb-bot/src/bot_data/graphql/queries/client_query.py b/kdb-bot/src/bot_data/graphql/queries/client_query.py deleted file mode 100644 index a900cd58..00000000 --- a/kdb-bot/src/bot_data/graphql/queries/client_query.py +++ /dev/null @@ -1,12 +0,0 @@ -import graphene - -from bot_data.abc.queryABC import QueryABC -from bot_data.graphql.types.client_query_type import ClientQueryType - - -class ClientQuery(QueryABC): - clients = graphene.List(ClientQueryType) - - @classmethod - def resolve_clients(cls, root, info): - return cls._clients diff --git a/kdb-bot/src/bot_data/graphql/queries/server_query.py b/kdb-bot/src/bot_data/graphql/queries/server_query.py deleted file mode 100644 index 9092ddd2..00000000 --- a/kdb-bot/src/bot_data/graphql/queries/server_query.py +++ /dev/null @@ -1,19 +0,0 @@ -import graphene -from cpl_core.dependency_injection import ServiceProviderABC - -from bot_data.abc.queryABC import QueryABC -from bot_data.graphql.types.server_query_type import ServerQueryType -from bot_data.service.server_repository_service import ServerRepositoryService - - -class ServerQuery(QueryABC): - servers = graphene.List(ServerQueryType) - - @staticmethod - @ServiceProviderABC.inject - def _get_servers(servers: ServerRepositoryService): - return servers.get_servers() - - @classmethod - def resolve_servers(cls, root, info): - return cls._servers diff --git a/kdb-bot/src/bot_data/graphql/query.py b/kdb-bot/src/bot_data/graphql/query.py new file mode 100644 index 00000000..ae8c4fcc --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/query.py @@ -0,0 +1,17 @@ +from ariadne import gql + +from bot_data.abc.query_abc import QueryABC +from bot_data.graphql.types.server_query_type import ServerQueryType + + +class Query(QueryABC): + __schema__ = gql(""" + type Query { + servers: [Server] + } + """) + __requires__ = [ServerQueryType] + + @classmethod + def resolve_servers(cls, *_): + return cls._servers diff --git a/kdb-bot/src/bot_data/graphql/root_query.py b/kdb-bot/src/bot_data/graphql/root_query.py deleted file mode 100644 index 40590f12..00000000 --- a/kdb-bot/src/bot_data/graphql/root_query.py +++ /dev/null @@ -1,10 +0,0 @@ -from graphene import ObjectType, relay - -from bot_data.graphql.queries.server_query import ServerQuery - - -class RootQuery( - ServerQuery, - ObjectType -): - node = relay.Node.Field() diff --git a/kdb-bot/src/bot_data/graphql/queries/__init__.py b/kdb-bot/src/bot_data/graphql/types/__init__.py similarity index 100% rename from kdb-bot/src/bot_data/graphql/queries/__init__.py rename to kdb-bot/src/bot_data/graphql/types/__init__.py diff --git a/kdb-bot/src/bot_data/graphql/types/client_query_type.py b/kdb-bot/src/bot_data/graphql/types/client_query_type.py index 30630989..da278f7f 100644 --- a/kdb-bot/src/bot_data/graphql/types/client_query_type.py +++ b/kdb-bot/src/bot_data/graphql/types/client_query_type.py @@ -1,18 +1,22 @@ -from graphene import Int, String +from ariadne import gql -from bot_data.abc.queryABC import QueryABC +from bot_data.abc.query_abc import QueryABC from bot_data.model.client import Client class ClientQueryType(QueryABC): - id = Int() - discord_id = String() - sent_message_count = Int() - received_message_count = Int() - deleted_message_count = Int() - received_command_count = Int() - moved_users_count = Int() + __schema__ = gql(""" + type Client { + id: ID + discord_id: String + sent_message_count: Int + received_message_count: Int + deleted_message_count: Int + received_command_count: Int + moved_users_count: Int + } + """) - @staticmethod - def resolve_id(client: Client, info): + @classmethod + def resolve_id(cls, client: Client, *_): return client.client_id diff --git a/kdb-bot/src/bot_data/graphql/types/level_query_type.py b/kdb-bot/src/bot_data/graphql/types/level_query_type.py new file mode 100644 index 00000000..4e9d6a5c --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/types/level_query_type.py @@ -0,0 +1,21 @@ +from ariadne import gql + +from bot_data.abc.query_abc import QueryABC +from bot_data.model.client import Client +from bot_data.model.level import Level + + +class LevelQueryType(QueryABC): + __schema__ = gql(""" + type Level { + id: ID + name: String + color: String + min_xp: Int + permissions: String + } + """) + + # @classmethod + # def resolve_id(cls, level: Level, *_): + # return level.id diff --git a/kdb-bot/src/bot_data/graphql/types/server_query_type.py b/kdb-bot/src/bot_data/graphql/types/server_query_type.py index ca719507..cb5e7939 100644 --- a/kdb-bot/src/bot_data/graphql/types/server_query_type.py +++ b/kdb-bot/src/bot_data/graphql/types/server_query_type.py @@ -1,23 +1,40 @@ -from graphene import Int, DateTime, String, List +from ariadne import gql -from bot_data.abc.queryABC import QueryABC +from bot_data.abc.query_abc import QueryABC from bot_data.graphql.types.client_query_type import ClientQueryType +from bot_data.graphql.types.level_query_type import LevelQueryType +from bot_data.graphql.types.user_query_type import UserQueryType from bot_data.model.server import Server class ServerQueryType(QueryABC): - id = Int() - discord_server_id = String() + __schema__ = gql(""" + type Server { + id: ID + discord_id: String + clients: [Client] + users: [User] + levels: [Level] + } + """) + __requires__ = [ClientQueryType, UserQueryType, LevelQueryType] - created_at = DateTime() - modified_at = DateTime() - - clients = List(ClientQueryType) - - @staticmethod - def resolve_id(server: Server, info): + @classmethod + def resolve_id(cls, server: Server, *_): return server.server_id @classmethod - def resolve_clients(cls, server: Server, info): + def resolve_discord_id(cls, server: Server, *_): + return server.discord_server_id + + @classmethod + def resolve_clients(cls, server: Server, *_): return cls._clients.where(lambda c: c.server.server_id == server.server_id) + + @classmethod + def resolve_users(cls, server: Server, *_): + return cls._users.where(lambda u: u.server.server_id == server.server_id) + + @classmethod + def resolve_levels(cls, server: Server, *_): + return cls._levels.where(lambda l: l.server.server_id == server.server_id) diff --git a/kdb-bot/src/bot_data/graphql/types/user_query_type.py b/kdb-bot/src/bot_data/graphql/types/user_query_type.py new file mode 100644 index 00000000..fe526e07 --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/types/user_query_type.py @@ -0,0 +1,39 @@ +from ariadne import gql + +from bot_data.abc.query_abc import QueryABC +from bot_data.graphql.types.level_query_type import LevelQueryType +from bot_data.model.user import User + + +class UserQueryType(QueryABC): + __schema__ = gql(""" + type User { + id: ID + discord_id: String + name: String + xp: Int + ontime: Int + level: Level + } + """) + __requires__ = [LevelQueryType] + + @classmethod + def resolve_id(cls, user: User, *_): + return user.user_id + + @classmethod + def resolve_name(cls, user: User, *_): + return cls._bot.get_user(user.discord_id).name + + @classmethod + def resolve_ontime(cls, user: User, *_): + return cls._user_joined_voice_channel.where(lambda ujvc: ujvc.user.user_id == user.user_id) \ + .where(lambda x: x.leaved_on is not None and x.joined_on is not None) \ + .sum(lambda join: round((join.leaved_on - join.joined_on).total_seconds() / 3600, 2)) + + @classmethod + def resolve_level(cls, user: User, *_): + levels = cls._levels.where(lambda l: l.server.server_id == user.server.server_id).order_by(lambda l: l.min_xp) + return levels.where(lambda l: user.xp >= l.min_xp).last_or_default() + \ No newline at end of file From 77e18027a09537f6a40ee0fe729c43a59c33d8f8 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Fri, 6 Jan 2023 23:18:40 +0100 Subject: [PATCH 04/36] Improved ariadne with DI --- kdb-bot/src/bot_api/api.py | 2 + .../bot_api/controller/grahpql_controller.py | 7 ++- kdb-bot/src/bot_data/abc/query_abc.py | 61 +------------------ kdb-bot/src/bot_data/data_module.py | 12 ++++ .../src/bot_data/graphql/graphql_service.py | 7 +++ kdb-bot/src/bot_data/graphql/query.py | 27 ++++---- kdb-bot/src/bot_data/graphql/schema.py | 31 ++++++++++ kdb-bot/src/bot_data/graphql/server_query.py | 20 ++++++ .../graphql/types/client_query_type.py | 22 ------- .../graphql/types/level_query_type.py | 21 ------- .../graphql/types/server_query_type.py | 41 ++++--------- .../bot_data/graphql/types/user_query_type.py | 39 ------------ 12 files changed, 102 insertions(+), 188 deletions(-) create mode 100644 kdb-bot/src/bot_data/graphql/graphql_service.py create mode 100644 kdb-bot/src/bot_data/graphql/schema.py create mode 100644 kdb-bot/src/bot_data/graphql/server_query.py delete mode 100644 kdb-bot/src/bot_data/graphql/types/client_query_type.py delete mode 100644 kdb-bot/src/bot_data/graphql/types/level_query_type.py delete mode 100644 kdb-bot/src/bot_data/graphql/types/user_query_type.py diff --git a/kdb-bot/src/bot_api/api.py b/kdb-bot/src/bot_api/api.py index 5d91c75e..700019c6 100644 --- a/kdb-bot/src/bot_api/api.py +++ b/kdb-bot/src/bot_api/api.py @@ -21,6 +21,7 @@ from bot_api.exception.service_exception import ServiceException from bot_api.logging.api_logger import ApiLogger from bot_api.model.error_dto import ErrorDTO from bot_api.route.route import Route +from bot_data.graphql.graphql_service import GraphQLService class Api(Flask): @@ -32,6 +33,7 @@ class Api(Flask): api_settings: ApiSettings, frontend_settings: FrontendSettings, auth_settings: AuthenticationSettings, + graphql: GraphQLService, *args, **kwargs ): if not args: diff --git a/kdb-bot/src/bot_api/controller/grahpql_controller.py b/kdb-bot/src/bot_api/controller/grahpql_controller.py index 477b843f..b30f052f 100644 --- a/kdb-bot/src/bot_api/controller/grahpql_controller.py +++ b/kdb-bot/src/bot_api/controller/grahpql_controller.py @@ -7,8 +7,8 @@ from flask import request, jsonify from bot_api.logging.api_logger import ApiLogger from bot_api.route.route import Route -from bot_data.abc.query_abc import QueryABC from bot_data.graphql.query import Query +from bot_data.graphql.schema import Schema class GraphQLController: @@ -19,10 +19,12 @@ class GraphQLController: config: ConfigurationABC, env: ApplicationEnvironmentABC, logger: ApiLogger, + schema: Schema, ): self._config = config self._env = env self._logger = logger + self._schema = schema @Route.get(f'{BasePath}/playground') async def playground(self): @@ -30,13 +32,12 @@ class GraphQLController: @Route.post(f'{BasePath}') async def graphql(self): - QueryABC.init() data = request.get_json() # Note: Passing the request to the context is optional. # In Flask, the current request is always accessible as flask.request success, result = graphql_sync( - make_executable_schema(Query), + self._schema.schema, data, context_value=request ) diff --git a/kdb-bot/src/bot_data/abc/query_abc.py b/kdb-bot/src/bot_data/abc/query_abc.py index 9ee57ecc..8685c163 100644 --- a/kdb-bot/src/bot_data/abc/query_abc.py +++ b/kdb-bot/src/bot_data/abc/query_abc.py @@ -1,64 +1,5 @@ -from ariadne_graphql_modules import ObjectType -from cpl_core.dependency_injection import ServiceProviderABC -from cpl_discord.service import DiscordBotServiceABC -from cpl_query.extension import List - -from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC -from bot_data.abc.client_repository_abc import ClientRepositoryABC -from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC -from bot_data.abc.level_repository_abc import LevelRepositoryABC -from bot_data.abc.server_repository_abc import ServerRepositoryABC -from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC -from bot_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC -from bot_data.abc.user_repository_abc import UserRepositoryABC -from bot_data.model.auto_role import AutoRole -from bot_data.model.client import Client -from bot_data.model.known_user import KnownUser -from bot_data.model.level import Level -from bot_data.model.server import Server -from bot_data.model.user import User -from bot_data.model.user_joined_server import UserJoinedServer -from bot_data.model.user_joined_voice_channel import UserJoinedVoiceChannel -from modules.level.service.level_service import LevelService +from ariadne import ObjectType class QueryABC(ObjectType): __abstract__ = True - - _auto_roles: List[AutoRole] - _clients: List[Client] - _known_users: List[KnownUser] - _levels: List[Level] - _servers: List[Server] - _user_joined_servers: List[UserJoinedServer] - _user_joined_voice_channel: List[UserJoinedVoiceChannel] - _users: List[User] - - _bot: DiscordBotServiceABC - _level_service: LevelService - - @classmethod - @ServiceProviderABC.inject - def init( - cls, - auto_roles: AutoRoleRepositoryABC, - clients: ClientRepositoryABC, - known_users: KnownUserRepositoryABC, - levels: LevelRepositoryABC, - servers: ServerRepositoryABC, - user_joined_servers: UserJoinedServerRepositoryABC, - user_joined_voice_channel: UserJoinedVoiceChannelRepositoryABC, - users: UserRepositoryABC, - bot: DiscordBotServiceABC, - level_service: LevelService, - ): - cls._auto_roles = auto_roles.get_auto_roles() - cls._clients = clients.get_clients() - cls._known_users = known_users.get_users() - cls._levels = levels.get_levels() - cls._servers = servers.get_servers() - cls._user_joined_servers = user_joined_servers.get_user_joined_servers() - cls._user_joined_voice_channel = user_joined_voice_channel.get_user_joined_voice_channels() - cls._users = users.get_users() - cls._bot = bot - cls._level_service = level_service diff --git a/kdb-bot/src/bot_data/data_module.py b/kdb-bot/src/bot_data/data_module.py index e32d8039..39b07a54 100644 --- a/kdb-bot/src/bot_data/data_module.py +++ b/kdb-bot/src/bot_data/data_module.py @@ -10,11 +10,17 @@ from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC from bot_data.abc.level_repository_abc import LevelRepositoryABC +from bot_data.abc.query_abc import QueryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.statistic_repository_abc import StatisticRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_data.graphql.graphql_service import GraphQLService +from bot_data.graphql.query import Query +from bot_data.graphql.schema import Schema +from bot_data.graphql.server_query import ServerQuery +from bot_data.graphql.types.server_query_type import ServerQueryType from bot_data.service.auth_user_repository_service import AuthUserRepositoryService from bot_data.service.auto_role_repository_service import AutoRoleRepositoryService from bot_data.service.client_repository_service import ClientRepositoryService @@ -48,4 +54,10 @@ class DataModule(ModuleABC): services.add_transient(LevelRepositoryABC, LevelRepositoryService) services.add_transient(StatisticRepositoryABC, StatisticRepositoryService) + services.add_singleton(Schema) + services.add_singleton(GraphQLService) + services.add_singleton(Query) + # services.add_transient(QueryABC, ServerQuery) + services.add_transient(QueryABC, ServerQueryType) + services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_data/graphql/graphql_service.py b/kdb-bot/src/bot_data/graphql/graphql_service.py new file mode 100644 index 00000000..34e1d5e8 --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/graphql_service.py @@ -0,0 +1,7 @@ +from bot_data.abc.query_abc import QueryABC + + +class GraphQLService: + + def __init__(self, queries: list[QueryABC]): + self._queries = queries diff --git a/kdb-bot/src/bot_data/graphql/query.py b/kdb-bot/src/bot_data/graphql/query.py index ae8c4fcc..b839fce9 100644 --- a/kdb-bot/src/bot_data/graphql/query.py +++ b/kdb-bot/src/bot_data/graphql/query.py @@ -1,17 +1,18 @@ -from ariadne import gql +from ariadne import QueryType -from bot_data.abc.query_abc import QueryABC -from bot_data.graphql.types.server_query_type import ServerQueryType +from bot_data.service.server_repository_service import ServerRepositoryService -class Query(QueryABC): - __schema__ = gql(""" - type Query { - servers: [Server] - } - """) - __requires__ = [ServerQueryType] +class Query(QueryType): - @classmethod - def resolve_servers(cls, *_): - return cls._servers + def __init__( + self, + servers: ServerRepositoryService + ): + QueryType.__init__(self) + self._servers = servers + + self.set_field('servers', self.resolve_servers) + + def resolve_servers(self, *_): + return self._servers.get_servers() diff --git a/kdb-bot/src/bot_data/graphql/schema.py b/kdb-bot/src/bot_data/graphql/schema.py new file mode 100644 index 00000000..e9c7a72a --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/schema.py @@ -0,0 +1,31 @@ +from ariadne import make_executable_schema, gql +from graphql import GraphQLSchema + +from bot_data.graphql.query import Query +from bot_data.graphql.server_query import ServerQuery +from bot_data.graphql.types.server_query_type import ServerQueryType + + +class Schema: + + def __init__( + self, + query: Query, + server_query: ServerQuery, + server_query_type: ServerQueryType + ): + type_defs = gql(""" + type Query { + servers: [Server] + } + + type Server { + id: Int + discord_id: String + } + """) + self._schema = make_executable_schema(type_defs, query, server_query_type) + + @property + def schema(self) -> GraphQLSchema: + return self._schema diff --git a/kdb-bot/src/bot_data/graphql/server_query.py b/kdb-bot/src/bot_data/graphql/server_query.py new file mode 100644 index 00000000..f554736b --- /dev/null +++ b/kdb-bot/src/bot_data/graphql/server_query.py @@ -0,0 +1,20 @@ +from bot_data.abc.query_abc import QueryABC +from bot_data.graphql.query import Query +from bot_data.service.server_repository_service import ServerRepositoryService + + +class ServerQuery(QueryABC): + + def __init__( + self, + query: Query, + servers: ServerRepositoryService + ): + QueryABC.__init__(self, 'servers') + self._query = query + self._servers = servers + + query.set_field('servers', self.resolve_servers) + + async def resolve_servers(self): + return self._servers.get_servers() diff --git a/kdb-bot/src/bot_data/graphql/types/client_query_type.py b/kdb-bot/src/bot_data/graphql/types/client_query_type.py deleted file mode 100644 index da278f7f..00000000 --- a/kdb-bot/src/bot_data/graphql/types/client_query_type.py +++ /dev/null @@ -1,22 +0,0 @@ -from ariadne import gql - -from bot_data.abc.query_abc import QueryABC -from bot_data.model.client import Client - - -class ClientQueryType(QueryABC): - __schema__ = gql(""" - type Client { - id: ID - discord_id: String - sent_message_count: Int - received_message_count: Int - deleted_message_count: Int - received_command_count: Int - moved_users_count: Int - } - """) - - @classmethod - def resolve_id(cls, client: Client, *_): - return client.client_id diff --git a/kdb-bot/src/bot_data/graphql/types/level_query_type.py b/kdb-bot/src/bot_data/graphql/types/level_query_type.py deleted file mode 100644 index 4e9d6a5c..00000000 --- a/kdb-bot/src/bot_data/graphql/types/level_query_type.py +++ /dev/null @@ -1,21 +0,0 @@ -from ariadne import gql - -from bot_data.abc.query_abc import QueryABC -from bot_data.model.client import Client -from bot_data.model.level import Level - - -class LevelQueryType(QueryABC): - __schema__ = gql(""" - type Level { - id: ID - name: String - color: String - min_xp: Int - permissions: String - } - """) - - # @classmethod - # def resolve_id(cls, level: Level, *_): - # return level.id diff --git a/kdb-bot/src/bot_data/graphql/types/server_query_type.py b/kdb-bot/src/bot_data/graphql/types/server_query_type.py index cb5e7939..e11105bf 100644 --- a/kdb-bot/src/bot_data/graphql/types/server_query_type.py +++ b/kdb-bot/src/bot_data/graphql/types/server_query_type.py @@ -1,40 +1,21 @@ -from ariadne import gql +from ariadne import ObjectType from bot_data.abc.query_abc import QueryABC -from bot_data.graphql.types.client_query_type import ClientQueryType -from bot_data.graphql.types.level_query_type import LevelQueryType -from bot_data.graphql.types.user_query_type import UserQueryType from bot_data.model.server import Server class ServerQueryType(QueryABC): - __schema__ = gql(""" - type Server { - id: ID - discord_id: String - clients: [Client] - users: [User] - levels: [Level] - } - """) - __requires__ = [ClientQueryType, UserQueryType, LevelQueryType] - @classmethod - def resolve_id(cls, server: Server, *_): + def __init__(self): + QueryABC.__init__(self, 'Server') + + self.set_field('id', self.resolve_id) + self.set_field('discord_id', self.resolve_discord_id) + + @staticmethod + def resolve_id(server: Server, *_): return server.server_id - @classmethod - def resolve_discord_id(cls, server: Server, *_): + @staticmethod + def resolve_discord_id(server: Server, *_): return server.discord_server_id - - @classmethod - def resolve_clients(cls, server: Server, *_): - return cls._clients.where(lambda c: c.server.server_id == server.server_id) - - @classmethod - def resolve_users(cls, server: Server, *_): - return cls._users.where(lambda u: u.server.server_id == server.server_id) - - @classmethod - def resolve_levels(cls, server: Server, *_): - return cls._levels.where(lambda l: l.server.server_id == server.server_id) diff --git a/kdb-bot/src/bot_data/graphql/types/user_query_type.py b/kdb-bot/src/bot_data/graphql/types/user_query_type.py deleted file mode 100644 index fe526e07..00000000 --- a/kdb-bot/src/bot_data/graphql/types/user_query_type.py +++ /dev/null @@ -1,39 +0,0 @@ -from ariadne import gql - -from bot_data.abc.query_abc import QueryABC -from bot_data.graphql.types.level_query_type import LevelQueryType -from bot_data.model.user import User - - -class UserQueryType(QueryABC): - __schema__ = gql(""" - type User { - id: ID - discord_id: String - name: String - xp: Int - ontime: Int - level: Level - } - """) - __requires__ = [LevelQueryType] - - @classmethod - def resolve_id(cls, user: User, *_): - return user.user_id - - @classmethod - def resolve_name(cls, user: User, *_): - return cls._bot.get_user(user.discord_id).name - - @classmethod - def resolve_ontime(cls, user: User, *_): - return cls._user_joined_voice_channel.where(lambda ujvc: ujvc.user.user_id == user.user_id) \ - .where(lambda x: x.leaved_on is not None and x.joined_on is not None) \ - .sum(lambda join: round((join.leaved_on - join.joined_on).total_seconds() / 3600, 2)) - - @classmethod - def resolve_level(cls, user: User, *_): - levels = cls._levels.where(lambda l: l.server.server_id == user.server.server_id).order_by(lambda l: l.min_xp) - return levels.where(lambda l: user.xp >= l.min_xp).last_or_default() - \ No newline at end of file From dacb429d9b4e6662fa6ad8c5fdd42a1626fa2804 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 7 Jan 2023 12:39:31 +0100 Subject: [PATCH 05/36] Moved graphql stuff to own lib --- kdb-bot/cpl-workspace.json | 7 +-- kdb-bot/src/bot/bot.json | 1 + kdb-bot/src/bot/module_list.py | 2 + kdb-bot/src/bot_api/api.py | 2 +- .../bot_api/controller/grahpql_controller.py | 4 +- kdb-bot/src/bot_data/data_module.py | 12 ----- kdb-bot/src/bot_data/graphql/schema.py | 31 ------------- kdb-bot/src/bot_data/graphql/server_query.py | 20 --------- kdb-bot/src/bot_graphql/__init__.py | 1 + .../types => bot_graphql/abc}/__init__.py | 0 .../abc/query_abc.py | 0 kdb-bot/src/bot_graphql/bot-graphql.json | 44 +++++++++++++++++++ kdb-bot/src/bot_graphql/filter/__init__.py | 0 .../src/bot_graphql/filter/server_filter.py | 2 + kdb-bot/src/bot_graphql/graphql_module.py | 31 +++++++++++++ .../graphql_service.py | 2 +- .../graphql => bot_graphql}/model.gql | 0 kdb-bot/src/bot_graphql/queries/__init__.py | 0 .../queries/server_query.py} | 6 +-- .../graphql => bot_graphql}/query.py | 3 +- kdb-bot/src/bot_graphql/schema.py | 22 ++++++++++ 21 files changed, 111 insertions(+), 79 deletions(-) delete mode 100644 kdb-bot/src/bot_data/graphql/schema.py delete mode 100644 kdb-bot/src/bot_data/graphql/server_query.py create mode 100644 kdb-bot/src/bot_graphql/__init__.py rename kdb-bot/src/{bot_data/graphql/types => bot_graphql/abc}/__init__.py (100%) rename kdb-bot/src/{bot_data => bot_graphql}/abc/query_abc.py (100%) create mode 100644 kdb-bot/src/bot_graphql/bot-graphql.json create mode 100644 kdb-bot/src/bot_graphql/filter/__init__.py create mode 100644 kdb-bot/src/bot_graphql/filter/server_filter.py create mode 100644 kdb-bot/src/bot_graphql/graphql_module.py rename kdb-bot/src/{bot_data/graphql => bot_graphql}/graphql_service.py (69%) rename kdb-bot/src/{bot_data/graphql => bot_graphql}/model.gql (100%) create mode 100644 kdb-bot/src/bot_graphql/queries/__init__.py rename kdb-bot/src/{bot_data/graphql/types/server_query_type.py => bot_graphql/queries/server_query.py} (79%) rename kdb-bot/src/{bot_data/graphql => bot_graphql}/query.py (77%) create mode 100644 kdb-bot/src/bot_graphql/schema.py diff --git a/kdb-bot/cpl-workspace.json b/kdb-bot/cpl-workspace.json index 5c606705..32b7cdbe 100644 --- a/kdb-bot/cpl-workspace.json +++ b/kdb-bot/cpl-workspace.json @@ -6,6 +6,7 @@ "bot-api": "src/bot_api/bot-api.json", "bot-core": "src/bot_core/bot-core.json", "bot-data": "src/bot_data/bot-data.json", + "bot-graphql": "src/bot_graphql/bot-graphql.json", "auto-role": "src/modules/auto_role/auto-role.json", "base": "src/modules/base/base.json", "boot-log": "src/modules/boot_log/boot-log.json", @@ -21,22 +22,16 @@ "Scripts": { "sv": "cpl set-version $ARGS", "set-version": "cpl run set-version $ARGS --dev; echo '';", - "gv": "cpl get-version", "get-version": "export VERSION=$(cpl run get-version --dev); echo $VERSION;", - "pre-build": "cpl set-version $ARGS", "post-build": "cpl run post-build --dev", - "pre-prod": "cpl build", "prod": "export KDB_ENVIRONMENT=production; export KDB_NAME=KDB-Prod; cpl start;", - "pre-stage": "cpl build", "stage": "export KDB_ENVIRONMENT=staging; export KDB_NAME=KDB-Stage; cpl start;", - "pre-dev": "cpl build", "dev": "export KDB_ENVIRONMENT=development; export KDB_NAME=KDB-Dev; cpl start;", - "docker-build": "cpl build $ARGS; docker build -t kdb-bot/kdb-bot:$(cpl gv) .;", "dc-up": "docker-compose up -d", "dc-down": "docker-compose down", diff --git a/kdb-bot/src/bot/bot.json b/kdb-bot/src/bot/bot.json index f7b034a1..9245699a 100644 --- a/kdb-bot/src/bot/bot.json +++ b/kdb-bot/src/bot/bot.json @@ -57,6 +57,7 @@ "../bot_api/bot-api.json", "../bot_core/bot-core.json", "../bot_data/bot-data.json", + "../bot_graphql/bot-graphql.json", "../modules/auto_role/auto-role.json", "../modules/base/base.json", "../modules/boot_log/boot-log.json", diff --git a/kdb-bot/src/bot/module_list.py b/kdb-bot/src/bot/module_list.py index ba6be127..4ef433bb 100644 --- a/kdb-bot/src/bot/module_list.py +++ b/kdb-bot/src/bot/module_list.py @@ -4,6 +4,7 @@ from bot_api.api_module import ApiModule from bot_core.core_extension.core_extension_module import CoreExtensionModule from bot_core.core_module import CoreModule from bot_data.data_module import DataModule +from bot_graphql.graphql_module import GraphQLModule from modules.auto_role.auto_role_module import AutoRoleModule from modules.base.base_module import BaseModule from modules.boot_log.boot_log_module import BootLogModule @@ -22,6 +23,7 @@ class ModuleList: return List(type, [ CoreModule, # has to be first! DataModule, + GraphQLModule, PermissionModule, DatabaseModule, AutoRoleModule, diff --git a/kdb-bot/src/bot_api/api.py b/kdb-bot/src/bot_api/api.py index 700019c6..a0f2a8c1 100644 --- a/kdb-bot/src/bot_api/api.py +++ b/kdb-bot/src/bot_api/api.py @@ -21,7 +21,7 @@ from bot_api.exception.service_exception import ServiceException from bot_api.logging.api_logger import ApiLogger from bot_api.model.error_dto import ErrorDTO from bot_api.route.route import Route -from bot_data.graphql.graphql_service import GraphQLService +from bot_graphql.graphql_service import GraphQLService class Api(Flask): diff --git a/kdb-bot/src/bot_api/controller/grahpql_controller.py b/kdb-bot/src/bot_api/controller/grahpql_controller.py index b30f052f..9d51d363 100644 --- a/kdb-bot/src/bot_api/controller/grahpql_controller.py +++ b/kdb-bot/src/bot_api/controller/grahpql_controller.py @@ -1,14 +1,12 @@ from ariadne import graphql_sync from ariadne.constants import PLAYGROUND_HTML -from ariadne_graphql_modules import make_executable_schema from cpl_core.configuration import ConfigurationABC from cpl_core.environment import ApplicationEnvironmentABC from flask import request, jsonify from bot_api.logging.api_logger import ApiLogger from bot_api.route.route import Route -from bot_data.graphql.query import Query -from bot_data.graphql.schema import Schema +from bot_graphql.schema import Schema class GraphQLController: diff --git a/kdb-bot/src/bot_data/data_module.py b/kdb-bot/src/bot_data/data_module.py index 39b07a54..e32d8039 100644 --- a/kdb-bot/src/bot_data/data_module.py +++ b/kdb-bot/src/bot_data/data_module.py @@ -10,17 +10,11 @@ from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC from bot_data.abc.level_repository_abc import LevelRepositoryABC -from bot_data.abc.query_abc import QueryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.abc.statistic_repository_abc import StatisticRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_joined_voice_channel_abc import UserJoinedVoiceChannelRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC -from bot_data.graphql.graphql_service import GraphQLService -from bot_data.graphql.query import Query -from bot_data.graphql.schema import Schema -from bot_data.graphql.server_query import ServerQuery -from bot_data.graphql.types.server_query_type import ServerQueryType from bot_data.service.auth_user_repository_service import AuthUserRepositoryService from bot_data.service.auto_role_repository_service import AutoRoleRepositoryService from bot_data.service.client_repository_service import ClientRepositoryService @@ -54,10 +48,4 @@ class DataModule(ModuleABC): services.add_transient(LevelRepositoryABC, LevelRepositoryService) services.add_transient(StatisticRepositoryABC, StatisticRepositoryService) - services.add_singleton(Schema) - services.add_singleton(GraphQLService) - services.add_singleton(Query) - # services.add_transient(QueryABC, ServerQuery) - services.add_transient(QueryABC, ServerQueryType) - services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_data/graphql/schema.py b/kdb-bot/src/bot_data/graphql/schema.py deleted file mode 100644 index e9c7a72a..00000000 --- a/kdb-bot/src/bot_data/graphql/schema.py +++ /dev/null @@ -1,31 +0,0 @@ -from ariadne import make_executable_schema, gql -from graphql import GraphQLSchema - -from bot_data.graphql.query import Query -from bot_data.graphql.server_query import ServerQuery -from bot_data.graphql.types.server_query_type import ServerQueryType - - -class Schema: - - def __init__( - self, - query: Query, - server_query: ServerQuery, - server_query_type: ServerQueryType - ): - type_defs = gql(""" - type Query { - servers: [Server] - } - - type Server { - id: Int - discord_id: String - } - """) - self._schema = make_executable_schema(type_defs, query, server_query_type) - - @property - def schema(self) -> GraphQLSchema: - return self._schema diff --git a/kdb-bot/src/bot_data/graphql/server_query.py b/kdb-bot/src/bot_data/graphql/server_query.py deleted file mode 100644 index f554736b..00000000 --- a/kdb-bot/src/bot_data/graphql/server_query.py +++ /dev/null @@ -1,20 +0,0 @@ -from bot_data.abc.query_abc import QueryABC -from bot_data.graphql.query import Query -from bot_data.service.server_repository_service import ServerRepositoryService - - -class ServerQuery(QueryABC): - - def __init__( - self, - query: Query, - servers: ServerRepositoryService - ): - QueryABC.__init__(self, 'servers') - self._query = query - self._servers = servers - - query.set_field('servers', self.resolve_servers) - - async def resolve_servers(self): - return self._servers.get_servers() diff --git a/kdb-bot/src/bot_graphql/__init__.py b/kdb-bot/src/bot_graphql/__init__.py new file mode 100644 index 00000000..425ab6c1 --- /dev/null +++ b/kdb-bot/src/bot_graphql/__init__.py @@ -0,0 +1 @@ +# imports diff --git a/kdb-bot/src/bot_data/graphql/types/__init__.py b/kdb-bot/src/bot_graphql/abc/__init__.py similarity index 100% rename from kdb-bot/src/bot_data/graphql/types/__init__.py rename to kdb-bot/src/bot_graphql/abc/__init__.py diff --git a/kdb-bot/src/bot_data/abc/query_abc.py b/kdb-bot/src/bot_graphql/abc/query_abc.py similarity index 100% rename from kdb-bot/src/bot_data/abc/query_abc.py rename to kdb-bot/src/bot_graphql/abc/query_abc.py diff --git a/kdb-bot/src/bot_graphql/bot-graphql.json b/kdb-bot/src/bot_graphql/bot-graphql.json new file mode 100644 index 00000000..70ed81fd --- /dev/null +++ b/kdb-bot/src/bot_graphql/bot-graphql.json @@ -0,0 +1,44 @@ +{ + "ProjectSettings": { + "Name": "bot-data", + "Version": { + "Major": "0", + "Minor": "1", + "Micro": "0" + }, + "Author": "Sven Heidemann", + "AuthorEmail": "sven.heidemann@sh-edraft.de", + "Description": "Keksdose bot - graphql", + "LongDescription": "Discord bot for the Keksdose discord Server - graphql package", + "URL": "https://www.sh-edraft.de", + "CopyrightDate": "2023", + "CopyrightName": "sh-edraft.de", + "LicenseName": "MIT", + "LicenseDescription": "MIT, see LICENSE for more details.", + "Dependencies": [ + "cpl-core>=2022.12.1" + ], + "DevDependencies": [ + "cpl-cli>=2022.12.1" + ], + "PythonVersion": ">=3.10.4", + "PythonPath": {}, + "Classifiers": [] + }, + "BuildSettings": { + "ProjectType": "library", + "SourcePath": "", + "OutputPath": "../../dist", + "Main": "", + "EntryPoint": "", + "IncludePackageData": false, + "Included": [], + "Excluded": [ + "*/__pycache__", + "*/logs", + "*/tests" + ], + "PackageData": {}, + "ProjectReferences": [] + } +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/filter/__init__.py b/kdb-bot/src/bot_graphql/filter/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/kdb-bot/src/bot_graphql/filter/server_filter.py b/kdb-bot/src/bot_graphql/filter/server_filter.py new file mode 100644 index 00000000..3238500e --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/server_filter.py @@ -0,0 +1,2 @@ +class ServerFilter: + pass diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py new file mode 100644 index 00000000..59aa3ec4 --- /dev/null +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -0,0 +1,31 @@ +from cpl_core.configuration import ConfigurationABC +from cpl_core.dependency_injection import ServiceCollectionABC +from cpl_core.environment import ApplicationEnvironmentABC +from cpl_discord.service.discord_collection_abc import DiscordCollectionABC + +from bot_core.abc.module_abc import ModuleABC +from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum +from bot_data.service.seeder_service import SeederService +from bot_graphql.abc.query_abc import QueryABC +from bot_graphql.graphql_service import GraphQLService +from bot_graphql.queries.server_query import ServerQuery +from bot_graphql.query import Query +from bot_graphql.schema import Schema + + +class GraphQLModule(ModuleABC): + + def __init__(self, dc: DiscordCollectionABC): + ModuleABC.__init__(self, dc, FeatureFlagsEnum.data_module) + + def configure_configuration(self, config: ConfigurationABC, env: ApplicationEnvironmentABC): + pass + + def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): + + services.add_singleton(Schema) + services.add_singleton(GraphQLService) + services.add_singleton(Query) + services.add_transient(QueryABC, ServerQuery) + + services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_data/graphql/graphql_service.py b/kdb-bot/src/bot_graphql/graphql_service.py similarity index 69% rename from kdb-bot/src/bot_data/graphql/graphql_service.py rename to kdb-bot/src/bot_graphql/graphql_service.py index 34e1d5e8..fc4a4b9b 100644 --- a/kdb-bot/src/bot_data/graphql/graphql_service.py +++ b/kdb-bot/src/bot_graphql/graphql_service.py @@ -1,4 +1,4 @@ -from bot_data.abc.query_abc import QueryABC +from bot_graphql.abc.query_abc import QueryABC class GraphQLService: diff --git a/kdb-bot/src/bot_data/graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql similarity index 100% rename from kdb-bot/src/bot_data/graphql/model.gql rename to kdb-bot/src/bot_graphql/model.gql diff --git a/kdb-bot/src/bot_graphql/queries/__init__.py b/kdb-bot/src/bot_graphql/queries/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/kdb-bot/src/bot_data/graphql/types/server_query_type.py b/kdb-bot/src/bot_graphql/queries/server_query.py similarity index 79% rename from kdb-bot/src/bot_data/graphql/types/server_query_type.py rename to kdb-bot/src/bot_graphql/queries/server_query.py index e11105bf..1b1aea4b 100644 --- a/kdb-bot/src/bot_data/graphql/types/server_query_type.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -1,10 +1,8 @@ -from ariadne import ObjectType - -from bot_data.abc.query_abc import QueryABC +from bot_graphql.abc.query_abc import QueryABC from bot_data.model.server import Server -class ServerQueryType(QueryABC): +class ServerQuery(QueryABC): def __init__(self): QueryABC.__init__(self, 'Server') diff --git a/kdb-bot/src/bot_data/graphql/query.py b/kdb-bot/src/bot_graphql/query.py similarity index 77% rename from kdb-bot/src/bot_data/graphql/query.py rename to kdb-bot/src/bot_graphql/query.py index b839fce9..cacaf8ad 100644 --- a/kdb-bot/src/bot_data/graphql/query.py +++ b/kdb-bot/src/bot_graphql/query.py @@ -1,5 +1,6 @@ from ariadne import QueryType +from bot_graphql.filter.server_filter import ServerFilter from bot_data.service.server_repository_service import ServerRepositoryService @@ -14,5 +15,5 @@ class Query(QueryType): self.set_field('servers', self.resolve_servers) - def resolve_servers(self, *_): + def resolve_servers(self, filter: ServerFilter, *_): return self._servers.get_servers() diff --git a/kdb-bot/src/bot_graphql/schema.py b/kdb-bot/src/bot_graphql/schema.py new file mode 100644 index 00000000..f918e9dc --- /dev/null +++ b/kdb-bot/src/bot_graphql/schema.py @@ -0,0 +1,22 @@ +import os + +from ariadne import make_executable_schema, load_schema_from_path +from graphql import GraphQLSchema + +from bot_graphql.abc.query_abc import QueryABC +from bot_graphql.query import Query + + +class Schema: + + def __init__( + self, + query: Query, + queries: list[QueryABC] + ): + type_defs = load_schema_from_path(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'model.gql')) + self._schema = make_executable_schema(type_defs, query, *queries) + + @property + def schema(self) -> GraphQLSchema: + return self._schema From 7a836a7f59b7c257c9c77a041c987a8be85b4ed4 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 7 Jan 2023 15:17:11 +0100 Subject: [PATCH 06/36] Added filtering --- kdb-bot/src/bot/bot.json | 3 +- .../bot_api/controller/grahpql_controller.py | 1 + kdb-bot/src/bot_graphql/abc/data_query_abc.py | 20 +++++++ kdb-bot/src/bot_graphql/abc/filter_abc.py | 57 +++++++++++++++++++ .../src/bot_graphql/filter/server_filter.py | 45 ++++++++++++++- kdb-bot/src/bot_graphql/model.gql | 50 +++++++++++++--- .../src/bot_graphql/queries/server_query.py | 20 +++++-- kdb-bot/src/bot_graphql/query.py | 15 ++++- 8 files changed, 193 insertions(+), 18 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/abc/data_query_abc.py create mode 100644 kdb-bot/src/bot_graphql/abc/filter_abc.py diff --git a/kdb-bot/src/bot/bot.json b/kdb-bot/src/bot/bot.json index 9245699a..4f702589 100644 --- a/kdb-bot/src/bot/bot.json +++ b/kdb-bot/src/bot/bot.json @@ -29,8 +29,7 @@ "eventlet==0.33.2", "requests-oauthlib==1.3.1", "icmplib==3.0.3", - "ariadne==0.17.0", - "ariadne-graphql-modules==0.7.0" + "ariadne==0.17.0" ], "DevDependencies": [ "cpl-cli==2022.12.0" diff --git a/kdb-bot/src/bot_api/controller/grahpql_controller.py b/kdb-bot/src/bot_api/controller/grahpql_controller.py index 9d51d363..9c102ccc 100644 --- a/kdb-bot/src/bot_api/controller/grahpql_controller.py +++ b/kdb-bot/src/bot_api/controller/grahpql_controller.py @@ -3,6 +3,7 @@ from ariadne.constants import PLAYGROUND_HTML from cpl_core.configuration import ConfigurationABC from cpl_core.environment import ApplicationEnvironmentABC from flask import request, jsonify +from graphql import MiddlewareManager from bot_api.logging.api_logger import ApiLogger from bot_api.route.route import Route diff --git a/kdb-bot/src/bot_graphql/abc/data_query_abc.py b/kdb-bot/src/bot_graphql/abc/data_query_abc.py new file mode 100644 index 00000000..59af3956 --- /dev/null +++ b/kdb-bot/src/bot_graphql/abc/data_query_abc.py @@ -0,0 +1,20 @@ +from cpl_core.database import TableABC + +from bot_graphql.abc.query_abc import QueryABC + + +class DataQueryABC(QueryABC): + + def __init__(self, name: str): + QueryABC.__init__(self, name) + + self.set_field('created_at', self.resolve_created_at) + self.set_field('modified_at', self.resolve_modified_at) + + @staticmethod + def resolve_created_at(entry: TableABC, *_): + return entry.created_at + + @staticmethod + def resolve_modified_at(entry: TableABC, *_): + return entry.modified_at diff --git a/kdb-bot/src/bot_graphql/abc/filter_abc.py b/kdb-bot/src/bot_graphql/abc/filter_abc.py new file mode 100644 index 00000000..827f45b3 --- /dev/null +++ b/kdb-bot/src/bot_graphql/abc/filter_abc.py @@ -0,0 +1,57 @@ +import functools +from abc import ABC +from inspect import signature, Parameter + + +class FilterABC(ABC): + + def __init__(self): + ABC.__init__(self) + + self._page_index = 0 + self._page_size = 10 + self._sort_direction = '' + self._sort_column = '' + + @property + def page_index(self) -> int: + return self._page_index + + @property + def page_size(self) -> int: + return self._page_size + + @property + def sort_direction(self) -> str: + return self._sort_direction + + @property + def sort_column(self) -> str: + return self._sort_column + + @staticmethod + def get_filter(f, values: dict): + sig = signature(f) + for param in sig.parameters.items(): + parameter = param[1] + if parameter.name == 'self' or parameter.annotation == Parameter.empty: + continue + + if issubclass(parameter.annotation, FilterABC): + filter = parameter.annotation() + filter.from_dict(values) + return filter + + @classmethod + def resolve_filter_annotation(cls, f=None): + if f is None: + return functools.partial(cls.filter) + + @functools.wraps(f) + def decorator(*args, **kwargs): + if 'filter' in kwargs: + kwargs['filter'] = cls.get_filter(f, kwargs['filter']) + + return f(*args, **kwargs) + + return decorator diff --git a/kdb-bot/src/bot_graphql/filter/server_filter.py b/kdb-bot/src/bot_graphql/filter/server_filter.py index 3238500e..26fcb8ea 100644 --- a/kdb-bot/src/bot_graphql/filter/server_filter.py +++ b/kdb-bot/src/bot_graphql/filter/server_filter.py @@ -1,2 +1,43 @@ -class ServerFilter: - pass +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_discord.container import Guild +from cpl_discord.service import DiscordBotServiceABC +from cpl_query.extension import List + +from bot_data.model.server import Server +from bot_graphql.abc.filter_abc import FilterABC + + +class ServerFilter(FilterABC): + + def __init__(self): + FilterABC.__init__(self) + + self._id = None + self._discord_id = None + self._name = None + + def from_dict(self, values: dict): + if 'id' in values: + self._id = values['id'] + + @ServiceProviderABC.inject + def filter(self, query: List[Server], bot: DiscordBotServiceABC) -> List[Server]: + result = List(Server) + + if self._id is not None: + query = query.where(lambda x: x.server_id == self._id) + + if self._discord_id is not None: + query = query.where(lambda x: x.discord_server_id == self._discord_id) + + if self._name is not None: + def where_guild(x: Guild): + guild = bot.get_guild(x.discord_server_id) + return guild is not None and (self._name.lower() == guild.name.lower() or self._name.lower() in guild.name.lower()) + + query = query.where(where_guild) + + skip = self.page_size * self.page_index + result = query.skip(skip).take(self.page_size) + + return result diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql index dba3d2af..94182271 100644 --- a/kdb-bot/src/bot_graphql/model.gql +++ b/kdb-bot/src/bot_graphql/model.gql @@ -1,17 +1,38 @@ type Query { - servers: [Server] + servers(filter: ServerFilter): [Server] + server_count: Int known_users: [User] } -type Server { +interface TableQuery { + created_at: String + modified_at: String +} + +input ServerFilter { + id: Int + discord_id: String + name: String + + page_index: Int + page_size: Int + sort_direction: String + sort_column: String +} + +type Server implements TableQuery { id: ID discord_id: String + name: String clients: [Client] members: [User] level: [Level] + + created_at: String + modified_at: String } -type Client { +type Client implements TableQuery { id: ID discord_id: String sent_message_count: Int @@ -21,9 +42,12 @@ type Client { moved_users_count: Int server: Server + + created_at: String + modified_at: String } -type User { +type User implements TableQuery { id: ID discord_id: String name: String @@ -35,25 +59,34 @@ type User { joined_voice_channel: [UserJoinedVoiceChannel] server: Server + + created_at: String + modified_at: String } -type UserJoinedServer { +type UserJoinedServer implements TableQuery { id: ID user: User server: Server joined_on: String leaved_on: String + + created_at: String + modified_at: String } -type UserJoinedVoiceChannel { +type UserJoinedVoiceChannel implements TableQuery { id: ID channel_id: String user: User joined_on: String leaved_on: String + + created_at: String + modified_at: String } -type Level { +type Level implements TableQuery { id: ID name: String color: String @@ -61,4 +94,7 @@ type Level { permissions: String server: Server + + created_at: String + modified_at: String } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/queries/server_query.py b/kdb-bot/src/bot_graphql/queries/server_query.py index 1b1aea4b..81a1a2ac 100644 --- a/kdb-bot/src/bot_graphql/queries/server_query.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -1,14 +1,22 @@ -from bot_graphql.abc.query_abc import QueryABC +from cpl_discord.service import DiscordBotServiceABC + from bot_data.model.server import Server +from bot_graphql.abc.data_query_abc import DataQueryABC -class ServerQuery(QueryABC): +class ServerQuery(DataQueryABC): - def __init__(self): - QueryABC.__init__(self, 'Server') + def __init__( + self, + bot: DiscordBotServiceABC + ): + DataQueryABC.__init__(self, 'Server') + + self._bot = bot self.set_field('id', self.resolve_id) self.set_field('discord_id', self.resolve_discord_id) + self.set_field('name', self.resolve_name) @staticmethod def resolve_id(server: Server, *_): @@ -17,3 +25,7 @@ class ServerQuery(QueryABC): @staticmethod def resolve_discord_id(server: Server, *_): return server.discord_server_id + + def resolve_name(self, server: Server, *_): + guild = self._bot.get_guild(server.discord_server_id) + return None if guild is None else guild.name diff --git a/kdb-bot/src/bot_graphql/query.py b/kdb-bot/src/bot_graphql/query.py index cacaf8ad..b080fa2a 100644 --- a/kdb-bot/src/bot_graphql/query.py +++ b/kdb-bot/src/bot_graphql/query.py @@ -1,7 +1,8 @@ from ariadne import QueryType -from bot_graphql.filter.server_filter import ServerFilter from bot_data.service.server_repository_service import ServerRepositoryService +from bot_graphql.abc.filter_abc import FilterABC +from bot_graphql.filter.server_filter import ServerFilter class Query(QueryType): @@ -14,6 +15,14 @@ class Query(QueryType): self._servers = servers self.set_field('servers', self.resolve_servers) + self.set_field('server_count', self.resolve_server_count) - def resolve_servers(self, filter: ServerFilter, *_): - return self._servers.get_servers() + @FilterABC.resolve_filter_annotation + def resolve_servers(self, *_, filter: ServerFilter = None): + if filter is not None: + return filter.filter(self._servers.get_servers()) + else: + return self._servers.get_servers() + + def resolve_server_count(self, *_): + return self._servers.get_servers().count() From ce85bb332a2eb3c4fab330588433ac42adf40d21 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 7 Jan 2023 15:53:44 +0100 Subject: [PATCH 07/36] Improved query & filtering --- kdb-bot/src/bot_graphql/abc/filter_abc.py | 31 ++++++++++----- .../src/bot_graphql/filter/level_filter.py | 37 ++++++++++++++++++ .../src/bot_graphql/filter/server_filter.py | 9 +---- kdb-bot/src/bot_graphql/graphql_module.py | 2 + kdb-bot/src/bot_graphql/model.gql | 9 ++++- .../src/bot_graphql/queries/level_query.py | 39 +++++++++++++++++++ .../src/bot_graphql/queries/server_query.py | 15 ++++++- 7 files changed, 122 insertions(+), 20 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/filter/level_filter.py create mode 100644 kdb-bot/src/bot_graphql/queries/level_query.py diff --git a/kdb-bot/src/bot_graphql/abc/filter_abc.py b/kdb-bot/src/bot_graphql/abc/filter_abc.py index 827f45b3..d2174a7d 100644 --- a/kdb-bot/src/bot_graphql/abc/filter_abc.py +++ b/kdb-bot/src/bot_graphql/abc/filter_abc.py @@ -1,6 +1,9 @@ import functools from abc import ABC from inspect import signature, Parameter +from typing import Optional + +from cpl_query.extension import List class FilterABC(ABC): @@ -8,33 +11,41 @@ class FilterABC(ABC): def __init__(self): ABC.__init__(self) - self._page_index = 0 - self._page_size = 10 - self._sort_direction = '' - self._sort_column = '' + self._page_index = None + self._page_size = None + self._sort_direction = None + self._sort_column = None @property - def page_index(self) -> int: + def page_index(self) -> Optional[int]: return self._page_index @property - def page_size(self) -> int: + def page_size(self) -> Optional[int]: return self._page_size @property - def sort_direction(self) -> str: + def sort_direction(self) -> Optional[str]: return self._sort_direction @property - def sort_column(self) -> str: + def sort_column(self) -> Optional[str]: return self._sort_column + def skip_and_take(self, query: List): + if self._page_size is not None and self.page_index is not None: + skip = self.page_size * self.page_index + result = query.skip(skip).take(self.page_size) + return result + + return query + @staticmethod def get_filter(f, values: dict): sig = signature(f) for param in sig.parameters.items(): parameter = param[1] - if parameter.name == 'self' or parameter.annotation == Parameter.empty: + if parameter.name == 'self' or parameter.name == 'cls' or parameter.annotation == Parameter.empty: continue if issubclass(parameter.annotation, FilterABC): @@ -45,7 +56,7 @@ class FilterABC(ABC): @classmethod def resolve_filter_annotation(cls, f=None): if f is None: - return functools.partial(cls.filter) + return functools.partial(cls.resolve_filter_annotation) @functools.wraps(f) def decorator(*args, **kwargs): diff --git a/kdb-bot/src/bot_graphql/filter/level_filter.py b/kdb-bot/src/bot_graphql/filter/level_filter.py new file mode 100644 index 00000000..5c0aad6d --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/level_filter.py @@ -0,0 +1,37 @@ +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_discord.container import Guild +from cpl_discord.service import DiscordBotServiceABC +from cpl_query.extension import List + +from bot_data.model.level import Level +from bot_data.model.server import Server +from bot_graphql.abc.filter_abc import FilterABC + + +class LevelFilter(FilterABC): + + def __init__(self): + FilterABC.__init__(self) + + self._id = None + self._name = None + # self._server_id = None + + def from_dict(self, values: dict): + if 'id' in values: + self._id = values['id'] + + def filter(self, query: List[Level]) -> List[Level]: + if self._id is not None: + query = query.where(lambda x: x.id == self._id) + + if self._name is not None: + query = query.where(lambda x: self._name.lower() == x.name.lower() or self._name.lower() in x.name.lower()) + + # if self._server_id is not None: + # query = query.where(lambda x: x.server.server_id == self._server_id) + + skip = self.page_size * self.page_index + result = query.skip(skip).take(self.page_size) + + return result diff --git a/kdb-bot/src/bot_graphql/filter/server_filter.py b/kdb-bot/src/bot_graphql/filter/server_filter.py index 26fcb8ea..afa0711a 100644 --- a/kdb-bot/src/bot_graphql/filter/server_filter.py +++ b/kdb-bot/src/bot_graphql/filter/server_filter.py @@ -18,12 +18,10 @@ class ServerFilter(FilterABC): def from_dict(self, values: dict): if 'id' in values: - self._id = values['id'] + self._id = int(values['id']) @ServiceProviderABC.inject def filter(self, query: List[Server], bot: DiscordBotServiceABC) -> List[Server]: - result = List(Server) - if self._id is not None: query = query.where(lambda x: x.server_id == self._id) @@ -37,7 +35,4 @@ class ServerFilter(FilterABC): query = query.where(where_guild) - skip = self.page_size * self.page_index - result = query.skip(skip).take(self.page_size) - - return result + return self.skip_and_take(query) diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 59aa3ec4..4678a4c1 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -8,6 +8,7 @@ from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_data.service.seeder_service import SeederService from bot_graphql.abc.query_abc import QueryABC from bot_graphql.graphql_service import GraphQLService +from bot_graphql.queries.level_query import LevelQuery from bot_graphql.queries.server_query import ServerQuery from bot_graphql.query import Query from bot_graphql.schema import Schema @@ -27,5 +28,6 @@ class GraphQLModule(ModuleABC): services.add_singleton(GraphQLService) services.add_singleton(Query) services.add_transient(QueryABC, ServerQuery) + services.add_transient(QueryABC, LevelQuery) services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql index 94182271..93843058 100644 --- a/kdb-bot/src/bot_graphql/model.gql +++ b/kdb-bot/src/bot_graphql/model.gql @@ -10,7 +10,7 @@ interface TableQuery { } input ServerFilter { - id: Int + id: ID discord_id: String name: String @@ -26,7 +26,7 @@ type Server implements TableQuery { name: String clients: [Client] members: [User] - level: [Level] + levels: [Level] created_at: String modified_at: String @@ -86,6 +86,11 @@ type UserJoinedVoiceChannel implements TableQuery { modified_at: String } +input LevelFilter { + id: ID + name: String +} + type Level implements TableQuery { id: ID name: String diff --git a/kdb-bot/src/bot_graphql/queries/level_query.py b/kdb-bot/src/bot_graphql/queries/level_query.py new file mode 100644 index 00000000..0f19aa81 --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/level_query.py @@ -0,0 +1,39 @@ +from bot_data.model.level import Level +from bot_graphql.abc.data_query_abc import DataQueryABC + + +class LevelQuery(DataQueryABC): + + def __init__(self): + DataQueryABC.__init__(self, 'Level') + + self.set_field('id', self.resolve_id) + self.set_field('name', self.resolve_name) + self.set_field('color', self.resolve_color) + self.set_field('min_xp', self.resolve_min_xp) + self.set_field('permissions', self.resolve_permissions) + self.set_field('server', self.resolve_server) + + @staticmethod + def resolve_id(level: Level, *_): + return level.id + + @staticmethod + def resolve_name(level: Level, *_): + return level.name + + @staticmethod + def resolve_color(level: Level, *_): + return level.color + + @staticmethod + def resolve_min_xp(level: Level, *_): + return level.min_xp + + @staticmethod + def resolve_permissions(level: Level, *_): + return level.permissions + + @staticmethod + def resolve_server(level: Level, *_): + return level.server diff --git a/kdb-bot/src/bot_graphql/queries/server_query.py b/kdb-bot/src/bot_graphql/queries/server_query.py index 81a1a2ac..2c54692c 100644 --- a/kdb-bot/src/bot_graphql/queries/server_query.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -1,22 +1,28 @@ from cpl_discord.service import DiscordBotServiceABC +from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.model.server import Server from bot_graphql.abc.data_query_abc import DataQueryABC +from bot_graphql.abc.filter_abc import FilterABC +from bot_graphql.filter.level_filter import LevelFilter class ServerQuery(DataQueryABC): def __init__( self, - bot: DiscordBotServiceABC + bot: DiscordBotServiceABC, + levels: LevelRepositoryABC, ): DataQueryABC.__init__(self, 'Server') self._bot = bot + self._levels = levels self.set_field('id', self.resolve_id) self.set_field('discord_id', self.resolve_discord_id) self.set_field('name', self.resolve_name) + self.set_field('levels', self.resolve_levels) @staticmethod def resolve_id(server: Server, *_): @@ -29,3 +35,10 @@ class ServerQuery(DataQueryABC): def resolve_name(self, server: Server, *_): guild = self._bot.get_guild(server.discord_server_id) return None if guild is None else guild.name + + @FilterABC.resolve_filter_annotation + def resolve_levels(self, server: Server, *_, filter: LevelFilter = None): + if filter is not None: + return filter.filter(self._levels.get_levels_by_server_id(server.server_id)) + + return self._levels.get_levels_by_server_id(server.server_id) From 44204f5b9414449664e554f50fa47a6ceb6e7034 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 7 Jan 2023 15:53:53 +0100 Subject: [PATCH 08/36] Improved query & filtering --- kdb-bot/src/bot/config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kdb-bot/src/bot/config b/kdb-bot/src/bot/config index 64a551bc..e6faabbd 160000 --- a/kdb-bot/src/bot/config +++ b/kdb-bot/src/bot/config @@ -1 +1 @@ -Subproject commit 64a551bcee7fbfb06ee724355b48baec1c8e8e3e +Subproject commit e6faabbd8b9fe0dbd00533ea1647e7094ea8b19e From df42acec26f80e4aecb5cd1fc7ca2c2384ac4a5a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sat, 7 Jan 2023 16:31:28 +0100 Subject: [PATCH 09/36] Added mutations without saving data --- kdb-bot/src/bot_graphql/graphql_module.py | 4 ++ kdb-bot/src/bot_graphql/model.gql | 28 +++++++++++--- kdb-bot/src/bot_graphql/mutation.py | 18 +++++++++ kdb-bot/src/bot_graphql/mutations/__init__.py | 0 .../bot_graphql/mutations/level_mutation.py | 37 +++++++++++++++++++ kdb-bot/src/bot_graphql/schema.py | 4 +- 6 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/mutation.py create mode 100644 kdb-bot/src/bot_graphql/mutations/__init__.py create mode 100644 kdb-bot/src/bot_graphql/mutations/level_mutation.py diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 4678a4c1..dba88df5 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -8,6 +8,8 @@ from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_data.service.seeder_service import SeederService from bot_graphql.abc.query_abc import QueryABC from bot_graphql.graphql_service import GraphQLService +from bot_graphql.mutation import Mutation +from bot_graphql.mutations.level_mutation import LevelMutation from bot_graphql.queries.level_query import LevelQuery from bot_graphql.queries.server_query import ServerQuery from bot_graphql.query import Query @@ -27,7 +29,9 @@ class GraphQLModule(ModuleABC): services.add_singleton(Schema) services.add_singleton(GraphQLService) services.add_singleton(Query) + services.add_singleton(Mutation) services.add_transient(QueryABC, ServerQuery) services.add_transient(QueryABC, LevelQuery) + services.add_transient(QueryABC, LevelMutation) services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql index 93843058..727b92c1 100644 --- a/kdb-bot/src/bot_graphql/model.gql +++ b/kdb-bot/src/bot_graphql/model.gql @@ -1,14 +1,18 @@ +interface TableQuery { + created_at: String + modified_at: String +} + +type Mutation { + level: LevelMutation +} + type Query { servers(filter: ServerFilter): [Server] server_count: Int known_users: [User] } -interface TableQuery { - created_at: String - modified_at: String -} - input ServerFilter { id: ID discord_id: String @@ -102,4 +106,18 @@ type Level implements TableQuery { created_at: String modified_at: String +} + +input LevelInput { + name: String! + color: String! + min_xp: Int! + permissions: String! + server_id: ID! +} + +type LevelMutation { + create_level(input: LevelInput!): Level + update_level(input: LevelInput!): Level + delete_level(id: ID): Level } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/mutation.py b/kdb-bot/src/bot_graphql/mutation.py new file mode 100644 index 00000000..52b19a9f --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutation.py @@ -0,0 +1,18 @@ +from ariadne import MutationType + +from bot_graphql.mutations.level_mutation import LevelMutation + + +class Mutation(MutationType): + + def __init__( + self, + level_mutation: LevelMutation + ): + MutationType.__init__(self) + + self._level_mutation = level_mutation + self.set_field('level', self.resolve_level) + + def resolve_level(self, *_): + return self._level_mutation diff --git a/kdb-bot/src/bot_graphql/mutations/__init__.py b/kdb-bot/src/bot_graphql/mutations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/kdb-bot/src/bot_graphql/mutations/level_mutation.py b/kdb-bot/src/bot_graphql/mutations/level_mutation.py new file mode 100644 index 00000000..b1917843 --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutations/level_mutation.py @@ -0,0 +1,37 @@ +from bot_data.abc.level_repository_abc import LevelRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.model.level import Level +from bot_graphql.abc.query_abc import QueryABC + + +class LevelMutation(QueryABC): + + def __init__( + self, + servers: ServerRepositoryABC, + levels: LevelRepositoryABC + ): + QueryABC.__init__(self, 'LevelMutation') + + self._servers = servers + self._levels = levels + + self.set_field('create_level', self.resolve_create_level) + self.set_field('update_level', self.resolve_create_level) + self.set_field('delete_level', self.resolve_create_level) + + def resolve_create_level(self, *_, input: dict): + level = Level( + input['name'], + input['color'], + int(input['min_xp']), + int(input['permissions']), + self._servers.get_server_by_id(input['server_id']) + ) + return level + + def resolve_update_level(self, *_, input): + return self._levels.get_level_by_id(input.id) + + def resolve_delete_level(self, *_, id: int): + return self._levels.get_level_by_id(id) diff --git a/kdb-bot/src/bot_graphql/schema.py b/kdb-bot/src/bot_graphql/schema.py index f918e9dc..da5e2948 100644 --- a/kdb-bot/src/bot_graphql/schema.py +++ b/kdb-bot/src/bot_graphql/schema.py @@ -4,6 +4,7 @@ from ariadne import make_executable_schema, load_schema_from_path from graphql import GraphQLSchema from bot_graphql.abc.query_abc import QueryABC +from bot_graphql.mutation import Mutation from bot_graphql.query import Query @@ -12,10 +13,11 @@ class Schema: def __init__( self, query: Query, + mutation: Mutation, queries: list[QueryABC] ): type_defs = load_schema_from_path(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'model.gql')) - self._schema = make_executable_schema(type_defs, query, *queries) + self._schema = make_executable_schema(type_defs, query, mutation, *queries) @property def schema(self) -> GraphQLSchema: From 95b9eea2363edcf40099f5723d554679f19f981f Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 02:28:28 +0100 Subject: [PATCH 10/36] Added graphql from prototype #162 --- kdb-bot/src/bot/module_list.py | 1 - kdb-bot/src/bot_api/api.py | 78 +++++++++---------- .../bot_api/controller/grahpql_controller.py | 22 +++--- .../service/client_repository_service.py | 62 ++++++++------- .../service/server_repository_service.py | 30 ++----- kdb-bot/src/bot_graphql/abc/data_query_abc.py | 5 +- kdb-bot/src/bot_graphql/abc/filter_abc.py | 15 ++-- .../src/bot_graphql/filter/level_filter.py | 5 +- .../src/bot_graphql/filter/server_filter.py | 10 ++- kdb-bot/src/bot_graphql/graphql_module.py | 1 - kdb-bot/src/bot_graphql/graphql_service.py | 1 - kdb-bot/src/bot_graphql/mutation.py | 8 +- .../bot_graphql/mutations/level_mutation.py | 25 +++--- .../src/bot_graphql/queries/level_query.py | 15 ++-- .../src/bot_graphql/queries/server_query.py | 17 ++-- kdb-bot/src/bot_graphql/query.py | 10 +-- kdb-bot/src/bot_graphql/schema.py | 10 +-- 17 files changed, 134 insertions(+), 181 deletions(-) diff --git a/kdb-bot/src/bot/module_list.py b/kdb-bot/src/bot/module_list.py index 5a223d0d..b5388245 100644 --- a/kdb-bot/src/bot/module_list.py +++ b/kdb-bot/src/bot/module_list.py @@ -16,7 +16,6 @@ from modules.technician.technician_module import TechnicianModule class ModuleList: - @staticmethod def get_modules(): # core modules (modules out of modules folder) should be loaded first! diff --git a/kdb-bot/src/bot_api/api.py b/kdb-bot/src/bot_api/api.py index be71eb79..023fd54b 100644 --- a/kdb-bot/src/bot_api/api.py +++ b/kdb-bot/src/bot_api/api.py @@ -1,4 +1,3 @@ -import re import sys import textwrap import uuid @@ -16,29 +15,25 @@ from werkzeug.exceptions import NotFound from bot_api.configuration.api_settings import ApiSettings from bot_api.configuration.authentication_settings import AuthenticationSettings -from bot_api.configuration.frontend_settings import FrontendSettings from bot_api.exception.service_error_code_enum import ServiceErrorCode from bot_api.exception.service_exception import ServiceException from bot_api.logging.api_logger import ApiLogger from bot_api.model.error_dto import ErrorDTO from bot_api.route.route import Route -from bot_graphql.graphql_service import GraphQLService class Api(Flask): - def __init__( - self, - logger: ApiLogger, - services: ServiceProviderABC, - api_settings: ApiSettings, - frontend_settings: FrontendSettings, - auth_settings: AuthenticationSettings, - graphql: GraphQLService, - *args, **kwargs + self, + logger: ApiLogger, + services: ServiceProviderABC, + api_settings: ApiSettings, + auth_settings: AuthenticationSettings, + *args, + **kwargs, ): if not args: - kwargs.setdefault('import_name', __name__) + kwargs.setdefault("import_name", __name__) Flask.__init__(self, *args, **kwargs) @@ -58,17 +53,21 @@ class Api(Flask): self.register_error_handler(exc_class, self.handle_exception) # websockets - self._socketio = SocketIO(self, cors_allowed_origins='*', path='/api/socket.io') - self._socketio.on_event('connect', self.on_connect) - self._socketio.on_event('disconnect', self.on_disconnect) + self._socketio = SocketIO(self, cors_allowed_origins="*", path="/api/socket.io") + self._socketio.on_event("connect", self.on_connect) + self._socketio.on_event("disconnect", self.on_disconnect) self._requests = {} @staticmethod def _get_methods_from_registered_route() -> Union[list[str], str]: - methods = ['Unknown'] - if request.path in Route.registered_routes and len(Route.registered_routes[request.path]) >= 1 and 'methods' in Route.registered_routes[request.path][1]: - methods = Route.registered_routes[request.path][1]['methods'] + methods = ["Unknown"] + if ( + request.path in Route.registered_routes + and len(Route.registered_routes[request.path]) >= 1 + and "methods" in Route.registered_routes[request.path][1] + ): + methods = Route.registered_routes[request.path][1]["methods"] if len(methods) == 1: return methods[0] @@ -79,7 +78,7 @@ class Api(Flask): route = f[0] kwargs = f[1] cls = None - qual_name_split = route.__qualname__.split('.') + qual_name_split = route.__qualname__.split(".") if len(qual_name_split) > 0: cls_type = vars(sys.modules[route.__module__])[qual_name_split[0]] cls = self._services.get_service(cls_type) @@ -89,7 +88,7 @@ class Api(Flask): self.route(path, **kwargs)(partial_f) def handle_exception(self, e: Exception): - self._logger.error(__name__, f'Caught error', e) + self._logger.error(__name__, f"Caught error", e) if isinstance(e, ServiceException): ex: ServiceException = e @@ -102,7 +101,7 @@ class Api(Flask): return jsonify(error.to_dict()), 404 else: tracking_id = uuid.uuid4() - user_message = f'Tracking Id: {tracking_id}' + user_message = f"Tracking Id: {tracking_id}" self._logger.error(__name__, user_message, e) error = ErrorDTO(None, user_message) return jsonify(error.to_dict()), 400 @@ -112,47 +111,48 @@ class Api(Flask): self._requests[request] = request_id method = request.access_control_request_method - self._logger.info(__name__, f'Received {request_id} @ {self._get_methods_from_registered_route() if method is None else method} {request.url} from {request.remote_addr}') + self._logger.info( + __name__, + f"Received {request_id} @ {self._get_methods_from_registered_route() if method is None else method} {request.url} from {request.remote_addr}", + ) - headers = str(request.headers).replace('\n', '\n\t\t') + headers = str(request.headers).replace("\n", "\n\t\t") data = request.get_data() - data = '' if len(data) == 0 else str(data.decode(encoding="utf-8")) + data = "" if len(data) == 0 else str(data.decode(encoding="utf-8")) - text = textwrap.dedent(f'Request: {request_id}:\n\tHeader:\n\t\t{headers}\n\tUser-Agent: {request.user_agent.string}\n\tBody: {data}') + text = textwrap.dedent( + f"Request: {request_id}:\n\tHeader:\n\t\t{headers}\n\tUser-Agent: {request.user_agent.string}\n\tBody: {data}" + ) self._logger.trace(__name__, text) def after_request_hook(self, response: Response): method = request.access_control_request_method - request_id = f'{self._get_methods_from_registered_route() if method is None else method} {request.url} from {request.remote_addr}' + request_id = f"{self._get_methods_from_registered_route() if method is None else method} {request.url} from {request.remote_addr}" if request in self._requests: request_id = self._requests[request] - self._logger.info(__name__, f'Answered {request_id}') + self._logger.info(__name__, f"Answered {request_id}") - headers = str(request.headers).replace('\n', '\n\t\t') + headers = str(request.headers).replace("\n", "\n\t\t") data = request.get_data() - data = '' if len(data) == 0 else str(data.decode(encoding="utf-8")) + data = "" if len(data) == 0 else str(data.decode(encoding="utf-8")) - text = textwrap.dedent(f'Request: {request_id}:\n\tHeader:\n\t\t{headers}\n\tResponse: {data}') + text = textwrap.dedent(f"Request: {request_id}:\n\tHeader:\n\t\t{headers}\n\tResponse: {data}") self._logger.trace(__name__, text) return response def start(self): - self._logger.info(__name__, f'Starting API {self._api_settings.host}:{self._api_settings.port}') + self._logger.info(__name__, f"Starting API {self._api_settings.host}:{self._api_settings.port}") self._register_routes() self.secret_key = CredentialManager.decrypt(self._auth_settings.secret_key) # from waitress import serve # https://docs.pylonsproject.org/projects/waitress/en/stable/arguments.html # serve(self, host=self._apt_settings.host, port=self._apt_settings.port, threads=10, connection_limit=1000, channel_timeout=10) - wsgi.server( - eventlet.listen((self._api_settings.host, self._api_settings.port)), - self, - log_output=False - ) + wsgi.server(eventlet.listen((self._api_settings.host, self._api_settings.port)), self, log_output=False) def on_connect(self): - self._logger.info(__name__, f'Client connected') + self._logger.info(__name__, f"Client connected") def on_disconnect(self): - self._logger.info(__name__, f'Client disconnected') + self._logger.info(__name__, f"Client disconnected") diff --git a/kdb-bot/src/bot_api/controller/grahpql_controller.py b/kdb-bot/src/bot_api/controller/grahpql_controller.py index 9c102ccc..f25fd7b7 100644 --- a/kdb-bot/src/bot_api/controller/grahpql_controller.py +++ b/kdb-bot/src/bot_api/controller/grahpql_controller.py @@ -11,34 +11,30 @@ from bot_graphql.schema import Schema class GraphQLController: - BasePath = f'/api/graphql' + BasePath = f"/api/graphql" def __init__( - self, - config: ConfigurationABC, - env: ApplicationEnvironmentABC, - logger: ApiLogger, - schema: Schema, + self, + config: ConfigurationABC, + env: ApplicationEnvironmentABC, + logger: ApiLogger, + schema: Schema, ): self._config = config self._env = env self._logger = logger self._schema = schema - @Route.get(f'{BasePath}/playground') + @Route.get(f"{BasePath}/playground") async def playground(self): return PLAYGROUND_HTML, 200 - @Route.post(f'{BasePath}') + @Route.post(f"{BasePath}") async def graphql(self): data = request.get_json() # Note: Passing the request to the context is optional. # In Flask, the current request is always accessible as flask.request - success, result = graphql_sync( - self._schema.schema, - data, - context_value=request - ) + success, result = graphql_sync(self._schema.schema, data, context_value=request) return jsonify(result), 200 if success else 400 diff --git a/kdb-bot/src/bot_data/service/client_repository_service.py b/kdb-bot/src/bot_data/service/client_repository_service.py index 4a062943..aca04447 100644 --- a/kdb-bot/src/bot_data/service/client_repository_service.py +++ b/kdb-bot/src/bot_data/service/client_repository_service.py @@ -28,19 +28,21 @@ class ClientRepositoryService(ClientRepositoryABC): self._logger.trace(__name__, f"Send SQL command: {Client.get_select_all_string()}") results = self._context.select(Client.get_select_all_string()) for result in results: - self._logger.trace(__name__, f'Get client with id {result[0]}') - clients.append(Client( - result[1], - result[2], - result[3], - result[4], - result[5], - result[6], - self._servers.get_server_by_id(result[7]), - result[8], - result[9], - id=result[0] - )) + self._logger.trace(__name__, f"Get client with id {result[0]}") + clients.append( + Client( + result[1], + result[2], + result[3], + result[4], + result[5], + result[6], + self._servers.get_server_by_id(result[7]), + result[8], + result[9], + id=result[0], + ) + ) return clients @@ -57,7 +59,7 @@ class ClientRepositoryService(ClientRepositoryABC): self._servers.get_server_by_id(result[7]), result[8], result[9], - id=result[0] + id=result[0], ) def get_client_by_discord_id(self, discord_id: int) -> Client: @@ -76,7 +78,7 @@ class ClientRepositoryService(ClientRepositoryABC): self._servers.get_server_by_id(result[7]), result[8], result[9], - id=result[0] + id=result[0], ) def find_client_by_discord_id(self, discord_id: int) -> Optional[Client]: @@ -87,9 +89,9 @@ class ClientRepositoryService(ClientRepositoryABC): result = self._context.select(Client.get_select_by_discord_id_string(discord_id)) if result is None or len(result) == 0: return None - + result = result[0] - + return Client( result[1], result[2], @@ -100,7 +102,7 @@ class ClientRepositoryService(ClientRepositoryABC): self._servers.get_server_by_id(result[7]), result[8], result[9], - id=result[0] + id=result[0], ) def find_client_by_server_id(self, discord_id: int) -> Optional[Client]: @@ -111,9 +113,9 @@ class ClientRepositoryService(ClientRepositoryABC): result = self._context.select(Client.get_select_by_server_id_string(discord_id)) if result is None or len(result) == 0: return None - + result = result[0] - + return Client( result[1], result[2], @@ -124,7 +126,7 @@ class ClientRepositoryService(ClientRepositoryABC): self._servers.get_server_by_id(result[7]), result[8], result[9], - id=result[0] + id=result[0], ) def find_client_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> Optional[Client]: @@ -135,9 +137,9 @@ class ClientRepositoryService(ClientRepositoryABC): result = self._context.select(Client.get_select_by_discord_id_and_server_id_string(discord_id, server_id)) if result is None or len(result) == 0: return None - + result = result[0] - + return Client( result[1], result[2], @@ -148,7 +150,7 @@ class ClientRepositoryService(ClientRepositoryABC): self._servers.get_server_by_id(result[7]), result[8], result[9], - id=result[0] + id=result[0], ) def add_client(self, client: Client): @@ -166,14 +168,14 @@ class ClientRepositoryService(ClientRepositoryABC): def _get_client_and_server(self, id: int, server_id: int) -> Client: server = self._servers.find_server_by_discord_id(server_id) if server is None: - self._logger.warn(__name__, f'Cannot find server by id {server_id}') - raise Exception('Value not found') - + self._logger.warn(__name__, f"Cannot find server by id {server_id}") + raise Exception("Value not found") + client = self.find_client_by_discord_id_and_server_id(id, server.server_id) if client is None: - self._logger.warn(__name__, f'Cannot find client by ids {id}@{server.server_id}') - raise Exception('Value not found') - + self._logger.warn(__name__, f"Cannot find client by ids {id}@{server.server_id}") + raise Exception("Value not found") + return client def append_sent_message_count(self, client_id: int, server_id: int, value: int): diff --git a/kdb-bot/src/bot_data/service/server_repository_service.py b/kdb-bot/src/bot_data/service/server_repository_service.py index 67396bc7..3291ceaa 100644 --- a/kdb-bot/src/bot_data/service/server_repository_service.py +++ b/kdb-bot/src/bot_data/service/server_repository_service.py @@ -22,12 +22,7 @@ class ServerRepositoryService(ServerRepositoryABC): self._logger.trace(__name__, f"Send SQL command: {Server.get_select_all_string()}") results = self._context.select(Server.get_select_all_string()) for result in results: - servers.append(Server( - result[1], - result[2], - result[3], - id=result[0] - )) + servers.append(Server(result[1], result[2], result[3], id=result[0])) return servers @@ -59,12 +54,7 @@ class ServerRepositoryService(ServerRepositoryABC): def get_server_by_id(self, server_id: int) -> Server: self._logger.trace(__name__, f"Send SQL command: {Server.get_select_by_id_string(server_id)}") result = self._context.select(Server.get_select_by_id_string(server_id))[0] - return Server( - result[1], - result[2], - result[3], - id=result[0] - ) + return Server(result[1], result[2], result[3], id=result[0]) def get_server_by_discord_id(self, discord_id: int) -> Server: self._logger.trace( @@ -72,12 +62,7 @@ class ServerRepositoryService(ServerRepositoryABC): f"Send SQL command: {Server.get_select_by_discord_id_string(discord_id)}", ) result = self._context.select(Server.get_select_by_discord_id_string(discord_id))[0] - return Server( - result[1], - result[2], - result[3], - id=result[0] - ) + return Server(result[1], result[2], result[3], id=result[0]) def find_server_by_discord_id(self, discord_id: int) -> Optional[Server]: self._logger.trace( @@ -87,15 +72,10 @@ class ServerRepositoryService(ServerRepositoryABC): result = self._context.select(Server.get_select_by_discord_id_string(discord_id)) if result is None or len(result) == 0: return None - + result = result[0] - return Server( - result[1], - result[2], - result[3], - id=result[0] - ) + return Server(result[1], result[2], result[3], id=result[0]) def add_server(self, server: Server): self._logger.trace(__name__, f"Send SQL command: {server.insert_string}") diff --git a/kdb-bot/src/bot_graphql/abc/data_query_abc.py b/kdb-bot/src/bot_graphql/abc/data_query_abc.py index 59af3956..9ab28106 100644 --- a/kdb-bot/src/bot_graphql/abc/data_query_abc.py +++ b/kdb-bot/src/bot_graphql/abc/data_query_abc.py @@ -4,12 +4,11 @@ from bot_graphql.abc.query_abc import QueryABC class DataQueryABC(QueryABC): - def __init__(self, name: str): QueryABC.__init__(self, name) - self.set_field('created_at', self.resolve_created_at) - self.set_field('modified_at', self.resolve_modified_at) + self.set_field("created_at", self.resolve_created_at) + self.set_field("modified_at", self.resolve_modified_at) @staticmethod def resolve_created_at(entry: TableABC, *_): diff --git a/kdb-bot/src/bot_graphql/abc/filter_abc.py b/kdb-bot/src/bot_graphql/abc/filter_abc.py index d2174a7d..47f8c189 100644 --- a/kdb-bot/src/bot_graphql/abc/filter_abc.py +++ b/kdb-bot/src/bot_graphql/abc/filter_abc.py @@ -7,7 +7,6 @@ from cpl_query.extension import List class FilterABC(ABC): - def __init__(self): ABC.__init__(self) @@ -15,19 +14,19 @@ class FilterABC(ABC): self._page_size = None self._sort_direction = None self._sort_column = None - + @property def page_index(self) -> Optional[int]: return self._page_index - + @property def page_size(self) -> Optional[int]: return self._page_size - + @property def sort_direction(self) -> Optional[str]: return self._sort_direction - + @property def sort_column(self) -> Optional[str]: return self._sort_column @@ -45,7 +44,7 @@ class FilterABC(ABC): sig = signature(f) for param in sig.parameters.items(): parameter = param[1] - if parameter.name == 'self' or parameter.name == 'cls' or parameter.annotation == Parameter.empty: + if parameter.name == "self" or parameter.name == "cls" or parameter.annotation == Parameter.empty: continue if issubclass(parameter.annotation, FilterABC): @@ -60,8 +59,8 @@ class FilterABC(ABC): @functools.wraps(f) def decorator(*args, **kwargs): - if 'filter' in kwargs: - kwargs['filter'] = cls.get_filter(f, kwargs['filter']) + if "filter" in kwargs: + kwargs["filter"] = cls.get_filter(f, kwargs["filter"]) return f(*args, **kwargs) diff --git a/kdb-bot/src/bot_graphql/filter/level_filter.py b/kdb-bot/src/bot_graphql/filter/level_filter.py index 5c0aad6d..e12f40cb 100644 --- a/kdb-bot/src/bot_graphql/filter/level_filter.py +++ b/kdb-bot/src/bot_graphql/filter/level_filter.py @@ -9,7 +9,6 @@ from bot_graphql.abc.filter_abc import FilterABC class LevelFilter(FilterABC): - def __init__(self): FilterABC.__init__(self) @@ -18,8 +17,8 @@ class LevelFilter(FilterABC): # self._server_id = None def from_dict(self, values: dict): - if 'id' in values: - self._id = values['id'] + if "id" in values: + self._id = values["id"] def filter(self, query: List[Level]) -> List[Level]: if self._id is not None: diff --git a/kdb-bot/src/bot_graphql/filter/server_filter.py b/kdb-bot/src/bot_graphql/filter/server_filter.py index afa0711a..148fe096 100644 --- a/kdb-bot/src/bot_graphql/filter/server_filter.py +++ b/kdb-bot/src/bot_graphql/filter/server_filter.py @@ -8,7 +8,6 @@ from bot_graphql.abc.filter_abc import FilterABC class ServerFilter(FilterABC): - def __init__(self): FilterABC.__init__(self) @@ -17,8 +16,8 @@ class ServerFilter(FilterABC): self._name = None def from_dict(self, values: dict): - if 'id' in values: - self._id = int(values['id']) + if "id" in values: + self._id = int(values["id"]) @ServiceProviderABC.inject def filter(self, query: List[Server], bot: DiscordBotServiceABC) -> List[Server]: @@ -29,9 +28,12 @@ class ServerFilter(FilterABC): query = query.where(lambda x: x.discord_server_id == self._discord_id) if self._name is not None: + def where_guild(x: Guild): guild = bot.get_guild(x.discord_server_id) - return guild is not None and (self._name.lower() == guild.name.lower() or self._name.lower() in guild.name.lower()) + return guild is not None and ( + self._name.lower() == guild.name.lower() or self._name.lower() in guild.name.lower() + ) query = query.where(where_guild) diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index dba88df5..a06ad96e 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -17,7 +17,6 @@ from bot_graphql.schema import Schema class GraphQLModule(ModuleABC): - def __init__(self, dc: DiscordCollectionABC): ModuleABC.__init__(self, dc, FeatureFlagsEnum.data_module) diff --git a/kdb-bot/src/bot_graphql/graphql_service.py b/kdb-bot/src/bot_graphql/graphql_service.py index fc4a4b9b..699b57c6 100644 --- a/kdb-bot/src/bot_graphql/graphql_service.py +++ b/kdb-bot/src/bot_graphql/graphql_service.py @@ -2,6 +2,5 @@ from bot_graphql.abc.query_abc import QueryABC class GraphQLService: - def __init__(self, queries: list[QueryABC]): self._queries = queries diff --git a/kdb-bot/src/bot_graphql/mutation.py b/kdb-bot/src/bot_graphql/mutation.py index 52b19a9f..079dd104 100644 --- a/kdb-bot/src/bot_graphql/mutation.py +++ b/kdb-bot/src/bot_graphql/mutation.py @@ -4,15 +4,11 @@ from bot_graphql.mutations.level_mutation import LevelMutation class Mutation(MutationType): - - def __init__( - self, - level_mutation: LevelMutation - ): + def __init__(self, level_mutation: LevelMutation): MutationType.__init__(self) self._level_mutation = level_mutation - self.set_field('level', self.resolve_level) + self.set_field("level", self.resolve_level) def resolve_level(self, *_): return self._level_mutation diff --git a/kdb-bot/src/bot_graphql/mutations/level_mutation.py b/kdb-bot/src/bot_graphql/mutations/level_mutation.py index b1917843..5ede5953 100644 --- a/kdb-bot/src/bot_graphql/mutations/level_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/level_mutation.py @@ -5,28 +5,23 @@ from bot_graphql.abc.query_abc import QueryABC class LevelMutation(QueryABC): - - def __init__( - self, - servers: ServerRepositoryABC, - levels: LevelRepositoryABC - ): - QueryABC.__init__(self, 'LevelMutation') + def __init__(self, servers: ServerRepositoryABC, levels: LevelRepositoryABC): + QueryABC.__init__(self, "LevelMutation") self._servers = servers self._levels = levels - self.set_field('create_level', self.resolve_create_level) - self.set_field('update_level', self.resolve_create_level) - self.set_field('delete_level', self.resolve_create_level) + self.set_field("create_level", self.resolve_create_level) + self.set_field("update_level", self.resolve_create_level) + self.set_field("delete_level", self.resolve_create_level) def resolve_create_level(self, *_, input: dict): level = Level( - input['name'], - input['color'], - int(input['min_xp']), - int(input['permissions']), - self._servers.get_server_by_id(input['server_id']) + input["name"], + input["color"], + int(input["min_xp"]), + int(input["permissions"]), + self._servers.get_server_by_id(input["server_id"]), ) return level diff --git a/kdb-bot/src/bot_graphql/queries/level_query.py b/kdb-bot/src/bot_graphql/queries/level_query.py index 0f19aa81..bf492895 100644 --- a/kdb-bot/src/bot_graphql/queries/level_query.py +++ b/kdb-bot/src/bot_graphql/queries/level_query.py @@ -3,16 +3,15 @@ from bot_graphql.abc.data_query_abc import DataQueryABC class LevelQuery(DataQueryABC): - def __init__(self): - DataQueryABC.__init__(self, 'Level') + DataQueryABC.__init__(self, "Level") - self.set_field('id', self.resolve_id) - self.set_field('name', self.resolve_name) - self.set_field('color', self.resolve_color) - self.set_field('min_xp', self.resolve_min_xp) - self.set_field('permissions', self.resolve_permissions) - self.set_field('server', self.resolve_server) + self.set_field("id", self.resolve_id) + self.set_field("name", self.resolve_name) + self.set_field("color", self.resolve_color) + self.set_field("min_xp", self.resolve_min_xp) + self.set_field("permissions", self.resolve_permissions) + self.set_field("server", self.resolve_server) @staticmethod def resolve_id(level: Level, *_): diff --git a/kdb-bot/src/bot_graphql/queries/server_query.py b/kdb-bot/src/bot_graphql/queries/server_query.py index 2c54692c..92b308e8 100644 --- a/kdb-bot/src/bot_graphql/queries/server_query.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -8,21 +8,20 @@ from bot_graphql.filter.level_filter import LevelFilter class ServerQuery(DataQueryABC): - def __init__( - self, - bot: DiscordBotServiceABC, - levels: LevelRepositoryABC, + self, + bot: DiscordBotServiceABC, + levels: LevelRepositoryABC, ): - DataQueryABC.__init__(self, 'Server') + DataQueryABC.__init__(self, "Server") self._bot = bot self._levels = levels - self.set_field('id', self.resolve_id) - self.set_field('discord_id', self.resolve_discord_id) - self.set_field('name', self.resolve_name) - self.set_field('levels', self.resolve_levels) + self.set_field("id", self.resolve_id) + self.set_field("discord_id", self.resolve_discord_id) + self.set_field("name", self.resolve_name) + self.set_field("levels", self.resolve_levels) @staticmethod def resolve_id(server: Server, *_): diff --git a/kdb-bot/src/bot_graphql/query.py b/kdb-bot/src/bot_graphql/query.py index b080fa2a..8e234f14 100644 --- a/kdb-bot/src/bot_graphql/query.py +++ b/kdb-bot/src/bot_graphql/query.py @@ -6,16 +6,12 @@ from bot_graphql.filter.server_filter import ServerFilter class Query(QueryType): - - def __init__( - self, - servers: ServerRepositoryService - ): + def __init__(self, servers: ServerRepositoryService): QueryType.__init__(self) self._servers = servers - self.set_field('servers', self.resolve_servers) - self.set_field('server_count', self.resolve_server_count) + self.set_field("servers", self.resolve_servers) + self.set_field("server_count", self.resolve_server_count) @FilterABC.resolve_filter_annotation def resolve_servers(self, *_, filter: ServerFilter = None): diff --git a/kdb-bot/src/bot_graphql/schema.py b/kdb-bot/src/bot_graphql/schema.py index da5e2948..9e1b7c5e 100644 --- a/kdb-bot/src/bot_graphql/schema.py +++ b/kdb-bot/src/bot_graphql/schema.py @@ -9,14 +9,8 @@ from bot_graphql.query import Query class Schema: - - def __init__( - self, - query: Query, - mutation: Mutation, - queries: list[QueryABC] - ): - type_defs = load_schema_from_path(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'model.gql')) + def __init__(self, query: Query, mutation: Mutation, queries: list[QueryABC]): + type_defs = load_schema_from_path(os.path.join(os.path.dirname(os.path.realpath(__file__)), "model.gql")) self._schema = make_executable_schema(type_defs, query, mutation, *queries) @property From efb772094b8c80c15e2ef05216c6e3a5da1926ac Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 02:35:39 +0100 Subject: [PATCH 11/36] Updated cpl-core #162 --- kdb-bot/src/bot/bot.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kdb-bot/src/bot/bot.json b/kdb-bot/src/bot/bot.json index 23e3c349..4dedb3dc 100644 --- a/kdb-bot/src/bot/bot.json +++ b/kdb-bot/src/bot/bot.json @@ -16,7 +16,7 @@ "LicenseName": "MIT", "LicenseDescription": "MIT, see LICENSE for more details.", "Dependencies": [ - "cpl-core==2022.12.1.post2", + "cpl-core==2022.12.1.post3", "cpl-translation==2022.12.1", "cpl-query==2022.12.2.post1", "cpl-discord==2022.12.1.post2", @@ -29,7 +29,7 @@ "eventlet==0.33.2", "requests-oauthlib==1.3.1", "icmplib==3.0.3", - "ariadne==0.17.0" + "ariadne==0.17.1" ], "DevDependencies": [ "cpl-cli==2022.12.1.post2" From 552e686aeb67687474a0e37c9a81c715601ea55d Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 02:37:35 +0100 Subject: [PATCH 12/36] Updated model #162 --- kdb-bot/src/bot_graphql/filter/level_filter.py | 4 ---- kdb-bot/src/bot_graphql/model.gql | 5 +++++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/kdb-bot/src/bot_graphql/filter/level_filter.py b/kdb-bot/src/bot_graphql/filter/level_filter.py index e12f40cb..314b9112 100644 --- a/kdb-bot/src/bot_graphql/filter/level_filter.py +++ b/kdb-bot/src/bot_graphql/filter/level_filter.py @@ -1,10 +1,6 @@ -from cpl_core.dependency_injection import ServiceProviderABC -from cpl_discord.container import Guild -from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List from bot_data.model.level import Level -from bot_data.model.server import Server from bot_graphql.abc.filter_abc import FilterABC diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql index 727b92c1..e30efb9b 100644 --- a/kdb-bot/src/bot_graphql/model.gql +++ b/kdb-bot/src/bot_graphql/model.gql @@ -93,6 +93,11 @@ type UserJoinedVoiceChannel implements TableQuery { input LevelFilter { id: ID name: String + + page_index: Int + page_size: Int + sort_direction: String + sort_column: String } type Level implements TableQuery { From b8484185e984fe01e2ca06707ba800ca72590862 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 09:51:31 +0100 Subject: [PATCH 13/36] Improved level stuff #162 --- .../src/bot_graphql/filter/level_filter.py | 8 ++-- .../bot_graphql/mutations/level_mutation.py | 40 +++++++++++++++++-- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/kdb-bot/src/bot_graphql/filter/level_filter.py b/kdb-bot/src/bot_graphql/filter/level_filter.py index 314b9112..34126cfe 100644 --- a/kdb-bot/src/bot_graphql/filter/level_filter.py +++ b/kdb-bot/src/bot_graphql/filter/level_filter.py @@ -16,6 +16,9 @@ class LevelFilter(FilterABC): if "id" in values: self._id = values["id"] + if "name" in values: + self._id = values["name"] + def filter(self, query: List[Level]) -> List[Level]: if self._id is not None: query = query.where(lambda x: x.id == self._id) @@ -26,7 +29,4 @@ class LevelFilter(FilterABC): # if self._server_id is not None: # query = query.where(lambda x: x.server.server_id == self._server_id) - skip = self.page_size * self.page_index - result = query.skip(skip).take(self.page_size) - - return result + return self.skip_and_take(query) diff --git a/kdb-bot/src/bot_graphql/mutations/level_mutation.py b/kdb-bot/src/bot_graphql/mutations/level_mutation.py index 5ede5953..083207cc 100644 --- a/kdb-bot/src/bot_graphql/mutations/level_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/level_mutation.py @@ -1,3 +1,5 @@ +from cpl_core.database.context import DatabaseContextABC + from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.model.level import Level @@ -5,11 +7,17 @@ from bot_graphql.abc.query_abc import QueryABC class LevelMutation(QueryABC): - def __init__(self, servers: ServerRepositoryABC, levels: LevelRepositoryABC): + def __init__( + self, + servers: ServerRepositoryABC, + levels: LevelRepositoryABC, + db: DatabaseContextABC, + ): QueryABC.__init__(self, "LevelMutation") self._servers = servers self._levels = levels + self._db = db self.set_field("create_level", self.resolve_create_level) self.set_field("update_level", self.resolve_create_level) @@ -23,10 +31,34 @@ class LevelMutation(QueryABC): int(input["permissions"]), self._servers.get_server_by_id(input["server_id"]), ) - return level + self._levels.add_level(level) + self._db.save_changes() + + def get_new_server(l: Level): + return ( + l.name == level.name + and l.color == level.color + and l.min_xp == level.min_xp + and l.permissions == level.permissions + ) + + return self._levels.get_levels_by_server_id(level.server.server_id).where(get_new_server) def resolve_update_level(self, *_, input): - return self._levels.get_level_by_id(input.id) + level = self._levels.get_level_by_id(input.id) + level.name = input["name"] if "name" in input else level.name + level.color = input["color"] if "color" in input else level.color + level.min_xp = input["min_xp"] if "min_xp" in input else level.min_xp + level.permissions = input["permissions"] if "permissions" in input else level.permissions + + self._levels.update_level(level) + self._db.save_changes() + + level = self._levels.get_level_by_id(input.id) + return level def resolve_delete_level(self, *_, id: int): - return self._levels.get_level_by_id(id) + level = self._levels.get_level_by_id(id) + self._levels.delete_level(level) + self._db.save_changes() + return level From b9e66bba9d0d83e4adb52bbf3bce280571690e2f Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 10:36:00 +0100 Subject: [PATCH 14/36] Added more queries #162 --- kdb-bot/.cpl/schematic_query.py | 34 ++++++++++ kdb-bot/src/bot/bot.json | 2 +- .../src/bot_data/abc/client_repository_abc.py | 5 ++ .../src/bot_data/abc/user_repository_abc.py | 4 ++ kdb-bot/src/bot_data/model/user.py | 10 +++ .../service/client_repository_service.py | 22 ++++++ .../service/user_repository_service.py | 19 ++++++ kdb-bot/src/bot_graphql/graphql_module.py | 9 ++- .../src/bot_graphql/queries/client_query.py | 59 ++++++++++++++++ .../src/bot_graphql/queries/server_query.py | 14 ++++ .../queries/user_joined_server_query.py | 33 +++++++++ .../user_joined_voice_channel_query.py | 38 +++++++++++ kdb-bot/src/bot_graphql/queries/user_query.py | 67 +++++++++++++++++++ 13 files changed, 314 insertions(+), 2 deletions(-) create mode 100644 kdb-bot/.cpl/schematic_query.py create mode 100644 kdb-bot/src/bot_graphql/queries/client_query.py create mode 100644 kdb-bot/src/bot_graphql/queries/user_joined_server_query.py create mode 100644 kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py create mode 100644 kdb-bot/src/bot_graphql/queries/user_query.py diff --git a/kdb-bot/.cpl/schematic_query.py b/kdb-bot/.cpl/schematic_query.py new file mode 100644 index 00000000..e34a3b6e --- /dev/null +++ b/kdb-bot/.cpl/schematic_query.py @@ -0,0 +1,34 @@ +from cpl_cli.abc.generate_schematic_abc import GenerateSchematicABC + + +class query(GenerateSchematicABC): + def __init__(self, *args: str): + GenerateSchematicABC.__init__(self, *args) + + def get_code(self) -> str: + import textwrap + + code = textwrap.dedent( + """\ + from bot_graphql.abc.data_query_abc import DataQueryABC + + + class $ClassName(DataQueryABC): + def __init__(self): + DataQueryABC.__init__(self, "Name") + + self.set_field("id", self.resolve_id) + + @staticmethod + def resolve_id(x, *_): + return x.id + """ + ) + return self.build_code_str( + code, + ClassName=self._class_name, + ) + + @classmethod + def register(cls): + GenerateSchematicABC.register(cls, "query", []) diff --git a/kdb-bot/src/bot/bot.json b/kdb-bot/src/bot/bot.json index 4dedb3dc..2245c518 100644 --- a/kdb-bot/src/bot/bot.json +++ b/kdb-bot/src/bot/bot.json @@ -32,7 +32,7 @@ "ariadne==0.17.1" ], "DevDependencies": [ - "cpl-cli==2022.12.1.post2" + "cpl-cli==2022.12.1.post3" ], "PythonVersion": ">=3.10.4", "PythonPath": {}, diff --git a/kdb-bot/src/bot_data/abc/client_repository_abc.py b/kdb-bot/src/bot_data/abc/client_repository_abc.py index 97d34e13..3b9f9132 100644 --- a/kdb-bot/src/bot_data/abc/client_repository_abc.py +++ b/kdb-bot/src/bot_data/abc/client_repository_abc.py @@ -2,6 +2,7 @@ from abc import ABC, abstractmethod from typing import Optional from cpl_query.extension import List + from bot_data.model.client import Client @@ -22,6 +23,10 @@ class ClientRepositoryABC(ABC): def get_client_by_discord_id(self, discord_id: int) -> Client: pass + @abstractmethod + def get_clients_by_server_id(self, server_id: int) -> List[Client]: + pass + @abstractmethod def find_client_by_discord_id(self, discord_id: int) -> Optional[Client]: pass diff --git a/kdb-bot/src/bot_data/abc/user_repository_abc.py b/kdb-bot/src/bot_data/abc/user_repository_abc.py index c40f20e5..0e3ced21 100644 --- a/kdb-bot/src/bot_data/abc/user_repository_abc.py +++ b/kdb-bot/src/bot_data/abc/user_repository_abc.py @@ -27,6 +27,10 @@ class UserRepositoryABC(ABC): def get_users_by_discord_id(self, discord_id: int) -> List[User]: pass + @abstractmethod + def get_users_by_server_id(self, server_id: int) -> List[User]: + pass + @abstractmethod def get_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> User: pass diff --git a/kdb-bot/src/bot_data/model/user.py b/kdb-bot/src/bot_data/model/user.py index 1592e471..ab97ecd1 100644 --- a/kdb-bot/src/bot_data/model/user.py +++ b/kdb-bot/src/bot_data/model/user.py @@ -1,5 +1,6 @@ from datetime import datetime from typing import Optional + from cpl_core.database import TableABC from bot_data.model.server import Server @@ -62,6 +63,15 @@ class User(TableABC): """ ) + @staticmethod + def get_select_by_server_id_string(server_id: int) -> str: + return str( + f""" + SELECT * FROM `Users` + WHERE `ServerId` = {server_id}; + """ + ) + @staticmethod def get_select_by_discord_id_string(id: int) -> str: return str( diff --git a/kdb-bot/src/bot_data/service/client_repository_service.py b/kdb-bot/src/bot_data/service/client_repository_service.py index aca04447..51d3b6cf 100644 --- a/kdb-bot/src/bot_data/service/client_repository_service.py +++ b/kdb-bot/src/bot_data/service/client_repository_service.py @@ -62,6 +62,28 @@ class ClientRepositoryService(ClientRepositoryABC): id=result[0], ) + def get_clients_by_server_id(self, server_id: int) -> List[Client]: + clients = List(Client) + self._logger.trace(__name__, f"Send SQL command: {Client.get_select_by_server_id_string(server_id)}") + results = self._context.select(Client.get_select_by_server_id_string(server_id)) + for result in results: + clients.append( + Client( + result[1], + result[2], + result[3], + result[4], + result[5], + result[6], + self._servers.get_server_by_id(result[7]), + result[8], + result[9], + id=result[0], + ) + ) + + return clients + def get_client_by_discord_id(self, discord_id: int) -> Client: self._logger.trace( __name__, diff --git a/kdb-bot/src/bot_data/service/user_repository_service.py b/kdb-bot/src/bot_data/service/user_repository_service.py index 00ee4a64..9d44c0ba 100644 --- a/kdb-bot/src/bot_data/service/user_repository_service.py +++ b/kdb-bot/src/bot_data/service/user_repository_service.py @@ -85,6 +85,25 @@ class UserRepositoryService(UserRepositoryABC): return users + def get_users_by_server_id(self, server_id: int) -> List[User]: + users = List(User) + self._logger.trace( + __name__, + f"Send SQL command: {User.get_select_by_server_id_string(server_id)}", + ) + results = self._context.select(User.get_select_by_server_id_string(server_id)) + for result in results: + users.append( + User( + result[1], + result[2], + self._servers.get_server_by_id(result[3]), + id=result[0], + ) + ) + + return users + def get_user_by_discord_id_and_server_id(self, discord_id: int, server_id: int) -> User: self._logger.trace( __name__, diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index a06ad96e..37ca29fd 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -10,8 +10,12 @@ from bot_graphql.abc.query_abc import QueryABC from bot_graphql.graphql_service import GraphQLService from bot_graphql.mutation import Mutation from bot_graphql.mutations.level_mutation import LevelMutation +from bot_graphql.queries.client_query import ClientQuery from bot_graphql.queries.level_query import LevelQuery from bot_graphql.queries.server_query import ServerQuery +from bot_graphql.queries.user_joined_server_query import UserJoinedServerQuery +from bot_graphql.queries.user_joined_voice_channel_query import UserJoinedVoiceChannelQuery +from bot_graphql.queries.user_query import UserQuery from bot_graphql.query import Query from bot_graphql.schema import Schema @@ -24,7 +28,6 @@ class GraphQLModule(ModuleABC): pass def configure_services(self, services: ServiceCollectionABC, env: ApplicationEnvironmentABC): - services.add_singleton(Schema) services.add_singleton(GraphQLService) services.add_singleton(Query) @@ -32,5 +35,9 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, ServerQuery) services.add_transient(QueryABC, LevelQuery) services.add_transient(QueryABC, LevelMutation) + services.add_transient(QueryABC, ClientQuery) + services.add_transient(QueryABC, UserQuery) + services.add_transient(QueryABC, UserJoinedServerQuery) + services.add_transient(QueryABC, UserJoinedVoiceChannelQuery) services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_graphql/queries/client_query.py b/kdb-bot/src/bot_graphql/queries/client_query.py new file mode 100644 index 00000000..6460dc54 --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/client_query.py @@ -0,0 +1,59 @@ +from cpl_discord.service import DiscordBotServiceABC + +from bot_data.model.client import Client +from bot_graphql.abc.data_query_abc import DataQueryABC + + +class ClientQuery(DataQueryABC): + def __init__( + self, + bot: DiscordBotServiceABC, + ): + DataQueryABC.__init__(self, "Client") + + self._bot = bot + + self.set_field("id", self.resolve_id) + self.set_field("discord_id", self.resolve_discord_id) + self.set_field("name", self.resolve_name) + self.set_field("sent_message_count", self.resolve_sent_message_count) + self.set_field("received_message_count", self.resolve_received_message_count) + self.set_field("deleted_message_count", self.resolve_deleted_message_count) + self.set_field("received_command_count", self.resolve_received_command_count) + self.set_field("moved_users_count", self.resolve_moved_users_count) + self.set_field("server", self.resolve_server) + + @staticmethod + def resolve_id(client: Client, *_): + return client.client_id + + @staticmethod + def resolve_discord_id(client: Client, *_): + return client.discord_id + + def resolve_name(self, client: Client, *_): + return self._bot.user.name + + @staticmethod + def resolve_sent_message_count(client: Client, *_): + return client.sent_message_count + + @staticmethod + def resolve_received_message_count(client: Client, *_): + return client.received_command_count + + @staticmethod + def resolve_deleted_message_count(client: Client, *_): + return client.deleted_message_count + + @staticmethod + def resolve_received_command_count(client: Client, *_): + return client.received_command_count + + @staticmethod + def resolve_moved_users_count(client: Client, *_): + return client.moved_users_count + + @staticmethod + def resolve_server(client: Client, *_): + return client.server diff --git a/kdb-bot/src/bot_graphql/queries/server_query.py b/kdb-bot/src/bot_graphql/queries/server_query.py index 92b308e8..bfcfebbc 100644 --- a/kdb-bot/src/bot_graphql/queries/server_query.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -1,6 +1,8 @@ from cpl_discord.service import DiscordBotServiceABC +from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.level_repository_abc import LevelRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.model.server import Server from bot_graphql.abc.data_query_abc import DataQueryABC from bot_graphql.abc.filter_abc import FilterABC @@ -11,16 +13,22 @@ class ServerQuery(DataQueryABC): def __init__( self, bot: DiscordBotServiceABC, + clients: ClientRepositoryABC, + users: UserRepositoryABC, levels: LevelRepositoryABC, ): DataQueryABC.__init__(self, "Server") self._bot = bot + self._clients = clients + self._users = users self._levels = levels self.set_field("id", self.resolve_id) self.set_field("discord_id", self.resolve_discord_id) self.set_field("name", self.resolve_name) + self.set_field("clients", self.resolve_clients) + self.set_field("members", self.resolve_members) self.set_field("levels", self.resolve_levels) @staticmethod @@ -35,6 +43,12 @@ class ServerQuery(DataQueryABC): guild = self._bot.get_guild(server.discord_server_id) return None if guild is None else guild.name + def resolve_clients(self, server: Server, *_): + return self._clients.get_clients_by_server_id(server.server_id) + + def resolve_members(self, server: Server, *_): + return self._users.get_users_by_server_id(server.server_id) + @FilterABC.resolve_filter_annotation def resolve_levels(self, server: Server, *_, filter: LevelFilter = None): if filter is not None: diff --git a/kdb-bot/src/bot_graphql/queries/user_joined_server_query.py b/kdb-bot/src/bot_graphql/queries/user_joined_server_query.py new file mode 100644 index 00000000..ef9ebda2 --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/user_joined_server_query.py @@ -0,0 +1,33 @@ +from bot_data.model.user_joined_server import UserJoinedServer +from bot_graphql.abc.data_query_abc import DataQueryABC + + +class UserJoinedServerQuery(DataQueryABC): + def __init__(self): + DataQueryABC.__init__(self, "UserJoinedServer") + + self.set_field("id", self.resolve_id) + self.set_field("user", self.resolve_user) + self.set_field("server", self.resolve_server) + self.set_field("joined_on", self.resolve_joined_on) + self.set_field("leaved_on", self.resolve_leaved_on) + + @staticmethod + def resolve_id(x: UserJoinedServer, *_): + return x.join_id + + @staticmethod + def resolve_user(x: UserJoinedServer, *_): + return x.user + + @staticmethod + def resolve_server(x: UserJoinedServer, *_): + return x.user.server + + @staticmethod + def resolve_joined_on(x: UserJoinedServer, *_): + return x.joined_on + + @staticmethod + def resolve_leaved_on(x: UserJoinedServer, *_): + return x.leaved_on diff --git a/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py b/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py new file mode 100644 index 00000000..f4d18a32 --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py @@ -0,0 +1,38 @@ +from bot_data.model.user_joined_voice_channel import UserJoinedVoiceChannel +from bot_graphql.abc.data_query_abc import DataQueryABC + + +class UserJoinedVoiceChannelQuery(DataQueryABC): + def __init__(self): + DataQueryABC.__init__(self, "UserJoinedVoiceChannel") + + self.set_field("id", self.resolve_id) + self.set_field("channel_id", self.resolve_channel_id) + self.set_field("user", self.resolve_user) + self.set_field("server", self.resolve_server) + self.set_field("joined_on", self.resolve_joined_on) + self.set_field("leaved_on", self.resolve_leaved_on) + + @staticmethod + def resolve_id(x: UserJoinedVoiceChannel, *_): + return x.join_id + + @staticmethod + def resolve_channel_id(x: UserJoinedVoiceChannel, *_): + return x.dc_channel_id + + @staticmethod + def resolve_user(x: UserJoinedVoiceChannel, *_): + return x.user + + @staticmethod + def resolve_server(x: UserJoinedVoiceChannel, *_): + return x.user.server + + @staticmethod + def resolve_joined_on(x: UserJoinedVoiceChannel, *_): + return x.joined_on + + @staticmethod + def resolve_leaved_on(x: UserJoinedVoiceChannel, *_): + return x.leaved_on diff --git a/kdb-bot/src/bot_graphql/queries/user_query.py b/kdb-bot/src/bot_graphql/queries/user_query.py new file mode 100644 index 00000000..0e10d7da --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/user_query.py @@ -0,0 +1,67 @@ +from cpl_discord.service import DiscordBotServiceABC + +from bot_core.abc.client_utils_abc import ClientUtilsABC +from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC +from bot_data.abc.user_joined_voice_channel_repository_abc import UserJoinedVoiceChannelRepositoryABC +from bot_data.model.user import User +from bot_graphql.abc.data_query_abc import DataQueryABC +from modules.level.service.level_service import LevelService + + +class UserQuery(DataQueryABC): + def __init__( + self, + bot: DiscordBotServiceABC, + levels: LevelService, + client_utils: ClientUtilsABC, + ujs: UserJoinedServerRepositoryABC, + ujvs: UserJoinedVoiceChannelRepositoryABC, + ): + DataQueryABC.__init__(self, "User") + + self._bot = bot + self._levels = levels + self._client_utils = client_utils + self._ujs = ujs + self._ujvs = ujvs + + self.set_field("id", self.resolve_id) + self.set_field("discord_id", self.resolve_discord_id) + self.set_field("name", self.resolve_name) + self.set_field("xp", self.resolve_xp) + self.set_field("ontime", self.resolve_ontime) + self.set_field("level", self.resolve_level) + self.set_field("joined_servers", self.resolve_joined_servers) + self.set_field("joined_voice_channel", self.resolve_joined_voice_channel) + self.set_field("server", self.resolve_server) + + @staticmethod + def resolve_id(user: User, *_): + return user.user_id + + @staticmethod + def resolve_discord_id(user: User, *_): + return user.discord_id + + def resolve_name(self, user: User, *_): + return self._bot.get_guild(user.server.discord_server_id).get_member(user.discord_id).name + + @staticmethod + def resolve_xp(user: User, *_): + return user.xp + + def resolve_ontime(self, user: User, *_): + return self._client_utils.get_ontime_for_user(user) + + def resolve_level(self, user: User, *_): + return self._levels.get_level(user) + + def resolve_joined_servers(self, user: User, *_): + return self._ujs.get_user_joined_servers_by_user_id(user.user_id) + + def resolve_joined_voice_channel(self, user: User, *_): + return self._ujvs.get_user_joined_voice_channels_by_user_id(user.user_id) + + @staticmethod + def resolve_server(user: User, *_): + return user.server From 5efb1da0b8591836b0a568c37a8df5c5a24b28d0 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 10:59:25 +0100 Subject: [PATCH 15/36] Added more queries #162 --- kdb-bot/src/bot_data/model/known_user.py | 14 +++--- kdb-bot/src/bot_graphql/filter/user_filter.py | 47 +++++++++++++++++++ kdb-bot/src/bot_graphql/model.gql | 28 ++++++++++- kdb-bot/src/bot_graphql/query.py | 19 +++++++- 4 files changed, 97 insertions(+), 11 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/filter/user_filter.py diff --git a/kdb-bot/src/bot_data/model/known_user.py b/kdb-bot/src/bot_data/model/known_user.py index be64d9d5..92aa5f4d 100644 --- a/kdb-bot/src/bot_data/model/known_user.py +++ b/kdb-bot/src/bot_data/model/known_user.py @@ -1,17 +1,15 @@ from datetime import datetime -from typing import Optional -from cpl_core.database import TableABC -from bot_data.model.server import Server +from cpl_core.database import TableABC class KnownUser(TableABC): def __init__( - self, - dc_id: int, - created_at: datetime = None, - modified_at: datetime = None, - id=0, + self, + dc_id: int, + created_at: datetime = None, + modified_at: datetime = None, + id=0, ): self._known_user_id = id self._discord_id = dc_id diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py new file mode 100644 index 00000000..bb3ddd02 --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -0,0 +1,47 @@ +from cpl_query.extension import List + +from bot_data.model.user import User +from bot_graphql.abc.filter_abc import FilterABC + + +class UserFilter(FilterABC): + def __init__( + self, + ): + FilterABC.__init__(self) + + self._id = None + self._discord_id = None + self._name = None + self._xp = None + self._ontime = None + self._level = None + + def from_dict(self, values: dict): + if 'id' in values: + self._id = values['id'] + if 'discord_id' in values: + self._discord_id = values['discord_id'] + if 'name' in values: + self._name = values['name'] + if 'xp' in values: + self._xp = values['xp'] + if 'ontime' in values: + self._ontime = values['ontime'] + if 'level' in values: + self._level = values['level'] + + def filter(self, query: List[User]) -> List[User]: + if self._id is not None: + query = query.where(lambda x: x.id == self._id) + + if self._discord_id is not None: + query = query.where(lambda x: x.discord_id == self._discord_id) + + if self._xp is not None: + query = query.where(lambda x: x.xp == self._xp) + + # if self._ontime is not None: + # query = query.where(lambda x: self._client_utils.get_ontime_for_user(x) == self._ontime) + + return self.skip_and_take(query) diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql index e30efb9b..fc6be69c 100644 --- a/kdb-bot/src/bot_graphql/model.gql +++ b/kdb-bot/src/bot_graphql/model.gql @@ -10,7 +10,17 @@ type Mutation { type Query { servers(filter: ServerFilter): [Server] server_count: Int - known_users: [User] + + known_users: [KnownUser] + known_user_count: Int +} + +type KnownUser implements TableQuery { + id: ID + discord_id: String + + created_at: String + modified_at: String } input ServerFilter { @@ -39,6 +49,7 @@ type Server implements TableQuery { type Client implements TableQuery { id: ID discord_id: String + name: String sent_message_count: Int received_message_count: Int deleted_message_count: Int @@ -51,6 +62,20 @@ type Client implements TableQuery { modified_at: String } +input UserFilter { + id: ID + discord_id: String + name: String + xp: Int + ontime: Int + level: LevelFilter + + page_index: Int + page_size: Int + sort_direction: String + sort_column: String +} + type User implements TableQuery { id: ID discord_id: String @@ -83,6 +108,7 @@ type UserJoinedVoiceChannel implements TableQuery { id: ID channel_id: String user: User + server: Server joined_on: String leaved_on: String diff --git a/kdb-bot/src/bot_graphql/query.py b/kdb-bot/src/bot_graphql/query.py index 8e234f14..63209c78 100644 --- a/kdb-bot/src/bot_graphql/query.py +++ b/kdb-bot/src/bot_graphql/query.py @@ -1,18 +1,27 @@ from ariadne import QueryType -from bot_data.service.server_repository_service import ServerRepositoryService +from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_graphql.abc.filter_abc import FilterABC from bot_graphql.filter.server_filter import ServerFilter class Query(QueryType): - def __init__(self, servers: ServerRepositoryService): + def __init__( + self, + servers: ServerRepositoryABC, + known_users: KnownUserRepositoryABC, + ): QueryType.__init__(self) self._servers = servers + self._known_users = known_users self.set_field("servers", self.resolve_servers) self.set_field("server_count", self.resolve_server_count) + self.set_field("known_users", self.resolve_servers) + self.set_field("known_users_count", self.resolve_server_count) + @FilterABC.resolve_filter_annotation def resolve_servers(self, *_, filter: ServerFilter = None): if filter is not None: @@ -22,3 +31,9 @@ class Query(QueryType): def resolve_server_count(self, *_): return self._servers.get_servers().count() + + def resolve_known_users(self, *_): + return self._known_users.get_users() + + def resolve_known_users_count(self, *_): + return self._known_users.get_users().count() From ebcf8764574d0dc378566c7359060b73b0a7ee0a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 10:59:54 +0100 Subject: [PATCH 16/36] Added more queries #162 --- kdb-bot/src/bot_data/model/known_user.py | 10 +++---- kdb-bot/src/bot_graphql/filter/user_filter.py | 26 +++++++++---------- kdb-bot/src/bot_graphql/query.py | 4 +-- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/kdb-bot/src/bot_data/model/known_user.py b/kdb-bot/src/bot_data/model/known_user.py index 92aa5f4d..3c708f9b 100644 --- a/kdb-bot/src/bot_data/model/known_user.py +++ b/kdb-bot/src/bot_data/model/known_user.py @@ -5,11 +5,11 @@ from cpl_core.database import TableABC class KnownUser(TableABC): def __init__( - self, - dc_id: int, - created_at: datetime = None, - modified_at: datetime = None, - id=0, + self, + dc_id: int, + created_at: datetime = None, + modified_at: datetime = None, + id=0, ): self._known_user_id = id self._discord_id = dc_id diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index bb3ddd02..674561ba 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -6,7 +6,7 @@ from bot_graphql.abc.filter_abc import FilterABC class UserFilter(FilterABC): def __init__( - self, + self, ): FilterABC.__init__(self) @@ -18,18 +18,18 @@ class UserFilter(FilterABC): self._level = None def from_dict(self, values: dict): - if 'id' in values: - self._id = values['id'] - if 'discord_id' in values: - self._discord_id = values['discord_id'] - if 'name' in values: - self._name = values['name'] - if 'xp' in values: - self._xp = values['xp'] - if 'ontime' in values: - self._ontime = values['ontime'] - if 'level' in values: - self._level = values['level'] + if "id" in values: + self._id = values["id"] + if "discord_id" in values: + self._discord_id = values["discord_id"] + if "name" in values: + self._name = values["name"] + if "xp" in values: + self._xp = values["xp"] + if "ontime" in values: + self._ontime = values["ontime"] + if "level" in values: + self._level = values["level"] def filter(self, query: List[User]) -> List[User]: if self._id is not None: diff --git a/kdb-bot/src/bot_graphql/query.py b/kdb-bot/src/bot_graphql/query.py index 63209c78..ccfcafb9 100644 --- a/kdb-bot/src/bot_graphql/query.py +++ b/kdb-bot/src/bot_graphql/query.py @@ -19,8 +19,8 @@ class Query(QueryType): self.set_field("servers", self.resolve_servers) self.set_field("server_count", self.resolve_server_count) - self.set_field("known_users", self.resolve_servers) - self.set_field("known_users_count", self.resolve_server_count) + self.set_field("known_users", self.resolve_known_users) + self.set_field("known_user_count", self.resolve_known_users_count) @FilterABC.resolve_filter_annotation def resolve_servers(self, *_, filter: ServerFilter = None): From eb7fce140ab3d07f9e3105e1f9f6fd497e0f86de Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 11:00:00 +0100 Subject: [PATCH 17/36] Added more queries #162 --- kdb-bot/src/bot_graphql/query.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/kdb-bot/src/bot_graphql/query.py b/kdb-bot/src/bot_graphql/query.py index ccfcafb9..00cf7df1 100644 --- a/kdb-bot/src/bot_graphql/query.py +++ b/kdb-bot/src/bot_graphql/query.py @@ -8,9 +8,9 @@ from bot_graphql.filter.server_filter import ServerFilter class Query(QueryType): def __init__( - self, - servers: ServerRepositoryABC, - known_users: KnownUserRepositoryABC, + self, + servers: ServerRepositoryABC, + known_users: KnownUserRepositoryABC, ): QueryType.__init__(self) self._servers = servers From 8256ebed719dbe1ef572201f37e85de2da7f4d16 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 16:24:08 +0100 Subject: [PATCH 18/36] Added auto role query #162 --- kdb-bot/src/bot_graphql/graphql_module.py | 4 ++ kdb-bot/src/bot_graphql/model.gql | 23 +++++++++++ .../bot_graphql/queries/auto_role_query.py | 40 +++++++++++++++++++ .../queries/auto_role_rule_query.py | 33 +++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 kdb-bot/src/bot_graphql/queries/auto_role_query.py create mode 100644 kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 37ca29fd..decdf095 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -10,6 +10,8 @@ from bot_graphql.abc.query_abc import QueryABC from bot_graphql.graphql_service import GraphQLService from bot_graphql.mutation import Mutation from bot_graphql.mutations.level_mutation import LevelMutation +from bot_graphql.queries.auto_role_query import AutoRoleQuery +from bot_graphql.queries.auto_role_rule_query import AutoRoleRuleQuery from bot_graphql.queries.client_query import ClientQuery from bot_graphql.queries.level_query import LevelQuery from bot_graphql.queries.server_query import ServerQuery @@ -39,5 +41,7 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, UserQuery) services.add_transient(QueryABC, UserJoinedServerQuery) services.add_transient(QueryABC, UserJoinedVoiceChannelQuery) + services.add_transient(QueryABC, AutoRoleQuery) + services.add_transient(QueryABC, AutoRoleRuleQuery) services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql index fc6be69c..77661c7f 100644 --- a/kdb-bot/src/bot_graphql/model.gql +++ b/kdb-bot/src/bot_graphql/model.gql @@ -62,6 +62,29 @@ type Client implements TableQuery { modified_at: String } +type AutoRole implements TableQuery { + id: ID + channel_id: String + message_id: String + + server: Server + rules: [AutoRoleRule] + + created_at: String + modified_at: String +} + +type AutoRoleRule implements TableQuery { + id: ID + emoji_name: String + role_id: String + + auto_role: AutoRole + + created_at: String + modified_at: String +} + input UserFilter { id: ID discord_id: String diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_query.py new file mode 100644 index 00000000..96615e6e --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/auto_role_query.py @@ -0,0 +1,40 @@ +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.model.auto_role import AutoRole +from bot_graphql.abc.data_query_abc import DataQueryABC + + +class AutoRoleQuery(DataQueryABC): + def __init__( + self, + auto_role_rules: AutoRoleRepositoryABC, + servers: ServerRepositoryABC, + ): + DataQueryABC.__init__(self, "AutoRole") + + self._auto_role_rules = auto_role_rules + self._servers = servers + + self.set_field("id", self.resolve_id) + self.set_field("channel_id", self.resolve_channel_id) + self.set_field("message_id", self.resolve_message_id) + self.set_field("server", self.resolve_server) + self.set_field("rules", self.resolve_rules) + + @staticmethod + def resolve_id(x: AutoRole, *_): + return x.auto_role_id + + @staticmethod + def resolve_channel_id(x: AutoRole, *_): + return x.discord_channel_id + + @staticmethod + def resolve_message_id(x: AutoRole, *_): + return x.discord_message_id + + def resolve_server(self, x: AutoRole, *_): + return self._servers.get_server_by_id(x.server_id) + + def resolve_rules(self, x: AutoRole, *_): + return self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id) diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py new file mode 100644 index 00000000..1cbd870c --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py @@ -0,0 +1,33 @@ +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.model.auto_role_rule import AutoRoleRule +from bot_graphql.abc.data_query_abc import DataQueryABC + + +class AutoRoleRuleQuery(DataQueryABC): + def __init__( + self, + auto_roles: AutoRoleRepositoryABC, + ): + DataQueryABC.__init__(self, "AutoRoleRule") + + self._auto_roles = auto_roles + + self.set_field("id", self.resolve_id) + self.set_field("emoji_name", self.resolve_emoji_name) + self.set_field("role_id", self.resolve_role_id) + self.set_field("auto_role", self.resolve_auto_role) + + @staticmethod + def resolve_id(x: AutoRoleRule, *_): + return x.auto_role_rule_id + + @staticmethod + def resolve_emoji_name(x: AutoRoleRule, *_): + return x.emoji_name + + @staticmethod + def resolve_role_id(x: AutoRoleRule, *_): + return x.role_id + + def resolve_auto_role(self, x: AutoRoleRule, *_): + return self._auto_roles.get_auto_role_by_id(x.auto_role_id) From 6a1ad1ec9f7c9a8475a9e638469a4f71eff9f6b4 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 17:02:09 +0100 Subject: [PATCH 19/36] Added filters #162 --- kdb-bot/src/bot_graphql/abc/filter_abc.py | 6 ++- .../bot_graphql/filter/auto_role_filter.py | 38 ++++++++++++++++ .../filter/auto_role_rule_filter.py | 45 +++++++++++++++++++ .../src/bot_graphql/filter/level_filter.py | 5 +-- kdb-bot/src/bot_graphql/graphql_module.py | 12 +++++ .../bot_graphql/queries/auto_role_query.py | 12 ++++- .../src/bot_graphql/queries/server_query.py | 19 +++++++- 7 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/filter/auto_role_filter.py create mode 100644 kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py diff --git a/kdb-bot/src/bot_graphql/abc/filter_abc.py b/kdb-bot/src/bot_graphql/abc/filter_abc.py index 47f8c189..8a067217 100644 --- a/kdb-bot/src/bot_graphql/abc/filter_abc.py +++ b/kdb-bot/src/bot_graphql/abc/filter_abc.py @@ -3,6 +3,7 @@ from abc import ABC from inspect import signature, Parameter from typing import Optional +from cpl_core.dependency_injection import ServiceProviderABC from cpl_query.extension import List @@ -40,7 +41,8 @@ class FilterABC(ABC): return query @staticmethod - def get_filter(f, values: dict): + @ServiceProviderABC.inject + def get_filter(f, values: dict, services: ServiceProviderABC): sig = signature(f) for param in sig.parameters.items(): parameter = param[1] @@ -48,7 +50,7 @@ class FilterABC(ABC): continue if issubclass(parameter.annotation, FilterABC): - filter = parameter.annotation() + filter = services.get_service(parameter.annotation) filter.from_dict(values) return filter diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_filter.py new file mode 100644 index 00000000..b49038e3 --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/auto_role_filter.py @@ -0,0 +1,38 @@ +from cpl_query.extension import List + +from bot_data.model.auto_role import AutoRole +from bot_graphql.abc.filter_abc import FilterABC + + +class AutoRoleFilter(FilterABC): + def __init__(self): + FilterABC.__init__(self) + + self._id = None + self._channel_id = None + self._message_id = None + + def from_dict(self, values: dict): + if "id" in values: + self._id = values["id"] + + if "channel_id" in values: + self._channel_id = values["channel_id"] + + if "message_id" in values: + self._message_id = values["message_id"] + + def filter(self, query: List[AutoRole]) -> List[AutoRole]: + if self._id is not None: + query = query.where(lambda x: x.id == self._id) + + if self._channel_id is not None: + query = query.where(lambda x: x.discord_channel_id == self._channel_id) + + if self._message_id is not None: + query = query.where(lambda x: x.discord_message_id == self._message_id) + + # if self._server_id is not None: + # query = query.where(lambda x: x.server.server_id == self._server_id) + + return self.skip_and_take(query) diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py new file mode 100644 index 00000000..f47cc939 --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py @@ -0,0 +1,45 @@ +from cpl_query.extension import List + +from bot_data.model.auto_role_rule import AutoRoleRule +from bot_graphql.abc.filter_abc import FilterABC + + +class AutoRoleRuleFilter(FilterABC): + def __init__(self): + FilterABC.__init__(self) + + self._id = None + self._emoji_name = None + self._role_id = None + self._auto_role_id = None + + def from_dict(self, values: dict): + if "id" in values: + self._id = values["id"] + + if "emoji_name" in values: + self._emoji_name = values["emoji_name"] + + if "role_id" in values: + self._role_id = values["role_id"] + + if "auto_role_id" in values: + self._auto_role_id = values["auto_role_id"] + + def filter(self, query: List[AutoRoleRule]) -> List[AutoRoleRule]: + if self._id is not None: + query = query.where(lambda x: x.auto_role_rule_id == self._id) + + if self._auto_role_id is not None: + query = query.where(lambda x: x.auto_role_id == self._auto_role_id) + + if self._emoji_name is not None: + query = query.where(lambda x: x.emoji_name == self._emoji_name) + + if self._role_id is not None: + query = query.where(lambda x: x.role_id == self._role_id) + + # if self._server_id is not None: + # query = query.where(lambda x: x.server.server_id == self._server_id) + + return self.skip_and_take(query) diff --git a/kdb-bot/src/bot_graphql/filter/level_filter.py b/kdb-bot/src/bot_graphql/filter/level_filter.py index 34126cfe..c85ac9a8 100644 --- a/kdb-bot/src/bot_graphql/filter/level_filter.py +++ b/kdb-bot/src/bot_graphql/filter/level_filter.py @@ -10,7 +10,7 @@ class LevelFilter(FilterABC): self._id = None self._name = None - # self._server_id = None + self._server_id = None def from_dict(self, values: dict): if "id" in values: @@ -26,7 +26,4 @@ class LevelFilter(FilterABC): if self._name is not None: query = query.where(lambda x: self._name.lower() == x.name.lower() or self._name.lower() in x.name.lower()) - # if self._server_id is not None: - # query = query.where(lambda x: x.server.server_id == self._server_id) - return self.skip_and_take(query) diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index decdf095..a84d6c00 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -6,7 +6,13 @@ from cpl_discord.service.discord_collection_abc import DiscordCollectionABC from bot_core.abc.module_abc import ModuleABC from bot_core.configuration.feature_flags_enum import FeatureFlagsEnum from bot_data.service.seeder_service import SeederService +from bot_graphql.abc.filter_abc import FilterABC from bot_graphql.abc.query_abc import QueryABC +from bot_graphql.filter.auto_role_filter import AutoRoleFilter +from bot_graphql.filter.auto_role_rule_filter import AutoRoleRuleFilter +from bot_graphql.filter.level_filter import LevelFilter +from bot_graphql.filter.server_filter import ServerFilter +from bot_graphql.filter.user_filter import UserFilter from bot_graphql.graphql_service import GraphQLService from bot_graphql.mutation import Mutation from bot_graphql.mutations.level_mutation import LevelMutation @@ -44,4 +50,10 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, AutoRoleQuery) services.add_transient(QueryABC, AutoRoleRuleQuery) + services.add_singleton(FilterABC, AutoRoleFilter) + services.add_singleton(FilterABC, AutoRoleRuleFilter) + services.add_singleton(FilterABC, ServerFilter) + services.add_singleton(FilterABC, UserFilter) + services.add_singleton(FilterABC, LevelFilter) + services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_query.py index 96615e6e..8a62dcca 100644 --- a/kdb-bot/src/bot_graphql/queries/auto_role_query.py +++ b/kdb-bot/src/bot_graphql/queries/auto_role_query.py @@ -2,6 +2,8 @@ from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.model.auto_role import AutoRole from bot_graphql.abc.data_query_abc import DataQueryABC +from bot_graphql.filter.auto_role_rule_filter import AutoRoleRuleFilter +from bot_graphql.filter.server_filter import ServerFilter class AutoRoleQuery(DataQueryABC): @@ -33,8 +35,14 @@ class AutoRoleQuery(DataQueryABC): def resolve_message_id(x: AutoRole, *_): return x.discord_message_id - def resolve_server(self, x: AutoRole, *_): + def resolve_server(self, x: AutoRole, *_, filter: ServerFilter): + if filter is not None: + return filter.filter(self._servers.get_server_by_id(x.server_id)) + return self._servers.get_server_by_id(x.server_id) - def resolve_rules(self, x: AutoRole, *_): + def resolve_rules(self, x: AutoRole, *_, filter: AutoRoleRuleFilter): + if filter is not None: + return filter.filter(self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id)) + return self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id) diff --git a/kdb-bot/src/bot_graphql/queries/server_query.py b/kdb-bot/src/bot_graphql/queries/server_query.py index bfcfebbc..e4c9dccf 100644 --- a/kdb-bot/src/bot_graphql/queries/server_query.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -1,18 +1,22 @@ from cpl_discord.service import DiscordBotServiceABC +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.model.server import Server from bot_graphql.abc.data_query_abc import DataQueryABC from bot_graphql.abc.filter_abc import FilterABC +from bot_graphql.filter.auto_role_filter import AutoRoleFilter from bot_graphql.filter.level_filter import LevelFilter +from bot_graphql.filter.user_filter import UserFilter class ServerQuery(DataQueryABC): def __init__( self, bot: DiscordBotServiceABC, + auto_roles: AutoRoleRepositoryABC, clients: ClientRepositoryABC, users: UserRepositoryABC, levels: LevelRepositoryABC, @@ -20,6 +24,8 @@ class ServerQuery(DataQueryABC): DataQueryABC.__init__(self, "Server") self._bot = bot + self._auto_roles = auto_roles + self._users = users self._clients = clients self._users = users self._levels = levels @@ -27,6 +33,7 @@ class ServerQuery(DataQueryABC): self.set_field("id", self.resolve_id) self.set_field("discord_id", self.resolve_discord_id) self.set_field("name", self.resolve_name) + self.set_field("auto_roles", self.resolve_auto_roles) self.set_field("clients", self.resolve_clients) self.set_field("members", self.resolve_members) self.set_field("levels", self.resolve_levels) @@ -43,10 +50,20 @@ class ServerQuery(DataQueryABC): guild = self._bot.get_guild(server.discord_server_id) return None if guild is None else guild.name + def resolve_auto_roles(self, server: Server, *_, filter: AutoRoleFilter): + if filter is not None: + return filter.filter(self._auto_roles.get_auto_roles_by_server_id(server.server_id)) + + return self._auto_roles.get_auto_roles_by_server_id(server.server_id) + def resolve_clients(self, server: Server, *_): return self._clients.get_clients_by_server_id(server.server_id) - def resolve_members(self, server: Server, *_): + @FilterABC.resolve_filter_annotation + def resolve_members(self, server: Server, *_, filter: UserFilter = None): + if filter is not None: + return filter.filter(self._users.get_users_by_server_id(server.server_id)) + return self._users.get_users_by_server_id(server.server_id) @FilterABC.resolve_filter_annotation From c75cc54d166e27e3a2d548f8a331b22cc1fbbebc Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Sun, 15 Jan 2023 17:47:27 +0100 Subject: [PATCH 20/36] Improved queries #162 --- kdb-bot/src/bot_graphql/graphql_module.py | 2 ++ kdb-bot/src/bot_graphql/model.gql | 7 ++++--- .../src/bot_graphql/queries/auto_role_query.py | 4 ++-- .../bot_graphql/queries/known_user_query.py | 18 ++++++++++++++++++ .../src/bot_graphql/queries/server_query.py | 2 +- kdb-bot/src/bot_graphql/queries/user_query.py | 6 ++++-- 6 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/queries/known_user_query.py diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index a84d6c00..0d419007 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -19,6 +19,7 @@ from bot_graphql.mutations.level_mutation import LevelMutation from bot_graphql.queries.auto_role_query import AutoRoleQuery from bot_graphql.queries.auto_role_rule_query import AutoRoleRuleQuery from bot_graphql.queries.client_query import ClientQuery +from bot_graphql.queries.known_user_query import KnownUserQuery from bot_graphql.queries.level_query import LevelQuery from bot_graphql.queries.server_query import ServerQuery from bot_graphql.queries.user_joined_server_query import UserJoinedServerQuery @@ -41,6 +42,7 @@ class GraphQLModule(ModuleABC): services.add_singleton(Query) services.add_singleton(Mutation) services.add_transient(QueryABC, ServerQuery) + services.add_transient(QueryABC, KnownUserQuery) services.add_transient(QueryABC, LevelQuery) services.add_transient(QueryABC, LevelMutation) services.add_transient(QueryABC, ClientQuery) diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql index 77661c7f..98fd3dc7 100644 --- a/kdb-bot/src/bot_graphql/model.gql +++ b/kdb-bot/src/bot_graphql/model.gql @@ -38,6 +38,7 @@ type Server implements TableQuery { id: ID discord_id: String name: String + auto_roles: [AutoRole] clients: [Client] members: [User] levels: [Level] @@ -90,7 +91,7 @@ input UserFilter { discord_id: String name: String xp: Int - ontime: Int + ontime: Float level: LevelFilter page_index: Int @@ -104,11 +105,11 @@ type User implements TableQuery { discord_id: String name: String xp: Int - ontime: Int + ontime: Float level: Level joined_servers: [UserJoinedServer] - joined_voice_channel: [UserJoinedVoiceChannel] + joined_voice_channels: [UserJoinedVoiceChannel] server: Server diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_query.py index 8a62dcca..f6ba53ef 100644 --- a/kdb-bot/src/bot_graphql/queries/auto_role_query.py +++ b/kdb-bot/src/bot_graphql/queries/auto_role_query.py @@ -35,13 +35,13 @@ class AutoRoleQuery(DataQueryABC): def resolve_message_id(x: AutoRole, *_): return x.discord_message_id - def resolve_server(self, x: AutoRole, *_, filter: ServerFilter): + def resolve_server(self, x: AutoRole, *_, filter: ServerFilter = None): if filter is not None: return filter.filter(self._servers.get_server_by_id(x.server_id)) return self._servers.get_server_by_id(x.server_id) - def resolve_rules(self, x: AutoRole, *_, filter: AutoRoleRuleFilter): + def resolve_rules(self, x: AutoRole, *_, filter: AutoRoleRuleFilter = None): if filter is not None: return filter.filter(self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id)) diff --git a/kdb-bot/src/bot_graphql/queries/known_user_query.py b/kdb-bot/src/bot_graphql/queries/known_user_query.py new file mode 100644 index 00000000..4de78ba7 --- /dev/null +++ b/kdb-bot/src/bot_graphql/queries/known_user_query.py @@ -0,0 +1,18 @@ +from bot_data.model.known_user import KnownUser +from bot_graphql.abc.data_query_abc import DataQueryABC + + +class KnownUserQuery(DataQueryABC): + def __init__(self): + DataQueryABC.__init__(self, "KnownUser") + + self.set_field("id", self.resolve_id) + self.set_field("discord_id", self.resolve_discord_id) + + @staticmethod + def resolve_id(x: KnownUser, *_): + return x.known_user_id + + @staticmethod + def resolve_discord_id(x: KnownUser, *_): + return x.discord_id diff --git a/kdb-bot/src/bot_graphql/queries/server_query.py b/kdb-bot/src/bot_graphql/queries/server_query.py index e4c9dccf..ece81023 100644 --- a/kdb-bot/src/bot_graphql/queries/server_query.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -50,7 +50,7 @@ class ServerQuery(DataQueryABC): guild = self._bot.get_guild(server.discord_server_id) return None if guild is None else guild.name - def resolve_auto_roles(self, server: Server, *_, filter: AutoRoleFilter): + def resolve_auto_roles(self, server: Server, *_, filter: AutoRoleFilter = None): if filter is not None: return filter.filter(self._auto_roles.get_auto_roles_by_server_id(server.server_id)) diff --git a/kdb-bot/src/bot_graphql/queries/user_query.py b/kdb-bot/src/bot_graphql/queries/user_query.py index 0e10d7da..43a6e6c9 100644 --- a/kdb-bot/src/bot_graphql/queries/user_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_query.py @@ -32,7 +32,7 @@ class UserQuery(DataQueryABC): self.set_field("ontime", self.resolve_ontime) self.set_field("level", self.resolve_level) self.set_field("joined_servers", self.resolve_joined_servers) - self.set_field("joined_voice_channel", self.resolve_joined_voice_channel) + self.set_field("joined_voice_channels", self.resolve_joined_voice_channel) self.set_field("server", self.resolve_server) @staticmethod @@ -44,7 +44,9 @@ class UserQuery(DataQueryABC): return user.discord_id def resolve_name(self, user: User, *_): - return self._bot.get_guild(user.server.discord_server_id).get_member(user.discord_id).name + guild = self._bot.get_guild(user.server.discord_server_id) + user = guild.get_member(user.discord_id) + return None if user is None else user.name @staticmethod def resolve_xp(user: User, *_): From 807827e30f4f893de14020eafab3afdb0d37b4de Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 16 Jan 2023 15:13:45 +0100 Subject: [PATCH 21/36] Improved queries & filtering #162 --- kdb-bot/src/bot/config | 2 +- .../bot_api/controller/grahpql_controller.py | 1 - .../bot_graphql/abc/discord_query_type_abc.py | 20 ++++++ kdb-bot/src/bot_graphql/abc/filter_abc.py | 19 +++++- kdb-bot/src/bot_graphql/abc/query_abc.py | 25 +++++++ kdb-bot/src/bot_graphql/abc/query_type_abc.py | 29 ++++++++ kdb-bot/src/bot_graphql/filter/user_filter.py | 23 ++++++- kdb-bot/src/bot_graphql/model.gql | 42 ++++++++++-- .../bot_graphql/queries/auto_role_query.py | 11 +--- .../src/bot_graphql/queries/server_query.py | 39 +++-------- kdb-bot/src/bot_graphql/queries/user_query.py | 14 ++-- kdb-bot/src/bot_graphql/query.py | 66 +++++++++++-------- 12 files changed, 207 insertions(+), 84 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/abc/discord_query_type_abc.py create mode 100644 kdb-bot/src/bot_graphql/abc/query_type_abc.py diff --git a/kdb-bot/src/bot/config b/kdb-bot/src/bot/config index 54b1b386..b0ae8762 160000 --- a/kdb-bot/src/bot/config +++ b/kdb-bot/src/bot/config @@ -1 +1 @@ -Subproject commit 54b1b3860cb570d29c8ba2590dd082a1fa744265 +Subproject commit b0ae87621bbe54fd9c5650071ec8c1c9ec32df48 diff --git a/kdb-bot/src/bot_api/controller/grahpql_controller.py b/kdb-bot/src/bot_api/controller/grahpql_controller.py index f25fd7b7..cc5e4c7c 100644 --- a/kdb-bot/src/bot_api/controller/grahpql_controller.py +++ b/kdb-bot/src/bot_api/controller/grahpql_controller.py @@ -3,7 +3,6 @@ from ariadne.constants import PLAYGROUND_HTML from cpl_core.configuration import ConfigurationABC from cpl_core.environment import ApplicationEnvironmentABC from flask import request, jsonify -from graphql import MiddlewareManager from bot_api.logging.api_logger import ApiLogger from bot_api.route.route import Route diff --git a/kdb-bot/src/bot_graphql/abc/discord_query_type_abc.py b/kdb-bot/src/bot_graphql/abc/discord_query_type_abc.py new file mode 100644 index 00000000..84b756a5 --- /dev/null +++ b/kdb-bot/src/bot_graphql/abc/discord_query_type_abc.py @@ -0,0 +1,20 @@ +from datetime import datetime + +from bot_graphql.abc.query_type_abc import QueryTypeABC + + +class DiscordQueryTypeABC(QueryTypeABC): + def __init__( + self, + id: str, + discord_id: str, + created_at: datetime = None, + modified_at: datetime = None, + ): + QueryTypeABC.__init__(self, id, created_at, modified_at) + + self._discord_id = discord_id + + @property + def discord_id(self) -> str: + return self._discord_id diff --git a/kdb-bot/src/bot_graphql/abc/filter_abc.py b/kdb-bot/src/bot_graphql/abc/filter_abc.py index 8a067217..a88f8bc4 100644 --- a/kdb-bot/src/bot_graphql/abc/filter_abc.py +++ b/kdb-bot/src/bot_graphql/abc/filter_abc.py @@ -1,5 +1,5 @@ import functools -from abc import ABC +from abc import ABC, abstractmethod from inspect import signature, Parameter from typing import Optional @@ -40,6 +40,14 @@ class FilterABC(ABC): return query + @abstractmethod + def from_dict(self, values: dict): + pass + + @abstractmethod + def filter(self, query: List, *args) -> List: + pass + @staticmethod @ServiceProviderABC.inject def get_filter(f, values: dict, services: ServiceProviderABC): @@ -52,7 +60,14 @@ class FilterABC(ABC): if issubclass(parameter.annotation, FilterABC): filter = services.get_service(parameter.annotation) filter.from_dict(values) - return filter + return filter @ staticmethod + + @staticmethod + @ServiceProviderABC.inject + def get_collection_filter(filter_type: type, values: dict, services: ServiceProviderABC): + filter: FilterABC = services.get_service(filter_type) + filter.from_dict(values) + return filter @classmethod def resolve_filter_annotation(cls, f=None): diff --git a/kdb-bot/src/bot_graphql/abc/query_abc.py b/kdb-bot/src/bot_graphql/abc/query_abc.py index 8685c163..d892ea68 100644 --- a/kdb-bot/src/bot_graphql/abc/query_abc.py +++ b/kdb-bot/src/bot_graphql/abc/query_abc.py @@ -1,5 +1,30 @@ +from typing import Callable + from ariadne import ObjectType +from cpl_query.extension import List + +from bot_graphql.abc.filter_abc import FilterABC class QueryABC(ObjectType): __abstract__ = True + + def __init__(self, name: str): + ObjectType.__init__(self, name) + + def _add_collection(self, name: str, get_collection: Callable, filter_type: type = None): + def wrapper(*args, **kwargs): + if filter_type is not None and "filter" in kwargs: + kwargs["filter"] = FilterABC.get_collection_filter(filter_type, kwargs["filter"]) + else: + kwargs["filter"] = None + + return self._resolve_collection(get_collection(*args), *args, **kwargs) + + self.set_field(f"{name}s", wrapper) + self.set_field(f"{name}_count", lambda *args: get_collection(*args).count()) + + def _resolve_collection(self, collection: List, *_, filter: FilterABC = None): + if filter is not None: + return filter.filter(collection) + return collection diff --git a/kdb-bot/src/bot_graphql/abc/query_type_abc.py b/kdb-bot/src/bot_graphql/abc/query_type_abc.py new file mode 100644 index 00000000..774e4cef --- /dev/null +++ b/kdb-bot/src/bot_graphql/abc/query_type_abc.py @@ -0,0 +1,29 @@ +from abc import ABC +from datetime import datetime +from typing import Optional + + +class QueryTypeABC(ABC): + def __init__( + self, + id: str, + created_at: datetime = None, + modified_at: datetime = None, + ): + ABC.__init__(self) + + self._id = id + self._created_at = created_at + self._modified_at = modified_at + + @property + def id(self) -> str: + return self._id + + @property + def created_at(self) -> Optional[datetime]: + return self._created_at + + @property + def modified_at(self) -> Optional[datetime]: + return self._modified_at diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index 674561ba..960241ff 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -1,15 +1,25 @@ +from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List +from bot_core.abc.client_utils_abc import ClientUtilsABC from bot_data.model.user import User from bot_graphql.abc.filter_abc import FilterABC +from modules.level.service.level_service import LevelService class UserFilter(FilterABC): def __init__( self, + bot: DiscordBotServiceABC, + client_utils: ClientUtilsABC, + levels: LevelService, ): FilterABC.__init__(self) + self._bot = bot + self._client_utils = client_utils + self._levels = levels + self._id = None self._discord_id = None self._name = None @@ -38,10 +48,19 @@ class UserFilter(FilterABC): if self._discord_id is not None: query = query.where(lambda x: x.discord_id == self._discord_id) + if self._name is not None: + query = query.where( + lambda x: self._bot.get_user(x.discord_id).name == self._name + or self._name in self._bot.get_user(x.discord_id).name + ) + if self._xp is not None: query = query.where(lambda x: x.xp == self._xp) - # if self._ontime is not None: - # query = query.where(lambda x: self._client_utils.get_ontime_for_user(x) == self._ontime) + if self._ontime is not None: + query = query.where(lambda x: self._client_utils.get_ontime_for_user(x) == self._ontime) + + if self._level is not None: + query = query.where(lambda x: self._levels.get_level(x) == self._level) return self.skip_and_take(query) diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql index 98fd3dc7..32d8b6b1 100644 --- a/kdb-bot/src/bot_graphql/model.gql +++ b/kdb-bot/src/bot_graphql/model.gql @@ -8,11 +8,32 @@ type Mutation { } type Query { - servers(filter: ServerFilter): [Server] - server_count: Int + auto_roles: [AutoRole] + auto_role_count: Int + + auto_role_rules: [AutoRole] + auto_role_rule_count: Int + + clients: [Client] + client_count: Int known_users: [KnownUser] known_user_count: Int + + levels: [Level] + level_count: Int + + servers(filter: ServerFilter): [Server] + server_count: Int + + user_joined_servers: [User] + user_joined_server_count: Int + + user_joined_voice_channels: [User] + user_joined_voice_channel_count: Int + + users(filter: UserFilter): [User] + user_count: Int } type KnownUser implements TableQuery { @@ -38,10 +59,18 @@ type Server implements TableQuery { id: ID discord_id: String name: String + auto_roles: [AutoRole] + auto_role_count: Int + clients: [Client] - members: [User] + client_count: Int + + users(filter: UserFilter): [User] + user_count: Int + levels: [Level] + level_count: Int created_at: String modified_at: String @@ -69,7 +98,9 @@ type AutoRole implements TableQuery { message_id: String server: Server - rules: [AutoRoleRule] + + auto_role_rules: [AutoRoleRule] + auto_role_rule_count: Int created_at: String modified_at: String @@ -109,7 +140,10 @@ type User implements TableQuery { level: Level joined_servers: [UserJoinedServer] + joined_server_count: Int + joined_voice_channels: [UserJoinedVoiceChannel] + joined_voice_channel_count: Int server: Server diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_query.py index f6ba53ef..4b75fe89 100644 --- a/kdb-bot/src/bot_graphql/queries/auto_role_query.py +++ b/kdb-bot/src/bot_graphql/queries/auto_role_query.py @@ -2,7 +2,6 @@ from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.model.auto_role import AutoRole from bot_graphql.abc.data_query_abc import DataQueryABC -from bot_graphql.filter.auto_role_rule_filter import AutoRoleRuleFilter from bot_graphql.filter.server_filter import ServerFilter @@ -21,7 +20,9 @@ class AutoRoleQuery(DataQueryABC): self.set_field("channel_id", self.resolve_channel_id) self.set_field("message_id", self.resolve_message_id) self.set_field("server", self.resolve_server) - self.set_field("rules", self.resolve_rules) + self._add_collection( + "auto_role_rule", lambda x, *_: self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id) + ) @staticmethod def resolve_id(x: AutoRole, *_): @@ -40,9 +41,3 @@ class AutoRoleQuery(DataQueryABC): return filter.filter(self._servers.get_server_by_id(x.server_id)) return self._servers.get_server_by_id(x.server_id) - - def resolve_rules(self, x: AutoRole, *_, filter: AutoRoleRuleFilter = None): - if filter is not None: - return filter.filter(self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id)) - - return self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id) diff --git a/kdb-bot/src/bot_graphql/queries/server_query.py b/kdb-bot/src/bot_graphql/queries/server_query.py index ece81023..a1ba09b1 100644 --- a/kdb-bot/src/bot_graphql/queries/server_query.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -6,9 +6,6 @@ from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.model.server import Server from bot_graphql.abc.data_query_abc import DataQueryABC -from bot_graphql.abc.filter_abc import FilterABC -from bot_graphql.filter.auto_role_filter import AutoRoleFilter -from bot_graphql.filter.level_filter import LevelFilter from bot_graphql.filter.user_filter import UserFilter @@ -33,10 +30,15 @@ class ServerQuery(DataQueryABC): self.set_field("id", self.resolve_id) self.set_field("discord_id", self.resolve_discord_id) self.set_field("name", self.resolve_name) - self.set_field("auto_roles", self.resolve_auto_roles) - self.set_field("clients", self.resolve_clients) - self.set_field("members", self.resolve_members) - self.set_field("levels", self.resolve_levels) + + self._add_collection( + "auto_role", lambda server, *_: self._auto_roles.get_auto_roles_by_server_id(server.server_id) + ) + self._add_collection("client", lambda server, *_: self._clients.get_clients_by_server_id(server.server_id)) + self._add_collection( + "user", lambda server, *_: self._users.get_users_by_server_id(server.server_id), UserFilter + ) + self._add_collection("level", lambda server, *_: self._levels.get_levels_by_server_id(server.server_id)) @staticmethod def resolve_id(server: Server, *_): @@ -49,26 +51,3 @@ class ServerQuery(DataQueryABC): def resolve_name(self, server: Server, *_): guild = self._bot.get_guild(server.discord_server_id) return None if guild is None else guild.name - - def resolve_auto_roles(self, server: Server, *_, filter: AutoRoleFilter = None): - if filter is not None: - return filter.filter(self._auto_roles.get_auto_roles_by_server_id(server.server_id)) - - return self._auto_roles.get_auto_roles_by_server_id(server.server_id) - - def resolve_clients(self, server: Server, *_): - return self._clients.get_clients_by_server_id(server.server_id) - - @FilterABC.resolve_filter_annotation - def resolve_members(self, server: Server, *_, filter: UserFilter = None): - if filter is not None: - return filter.filter(self._users.get_users_by_server_id(server.server_id)) - - return self._users.get_users_by_server_id(server.server_id) - - @FilterABC.resolve_filter_annotation - def resolve_levels(self, server: Server, *_, filter: LevelFilter = None): - if filter is not None: - return filter.filter(self._levels.get_levels_by_server_id(server.server_id)) - - return self._levels.get_levels_by_server_id(server.server_id) diff --git a/kdb-bot/src/bot_graphql/queries/user_query.py b/kdb-bot/src/bot_graphql/queries/user_query.py index 43a6e6c9..a8081408 100644 --- a/kdb-bot/src/bot_graphql/queries/user_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_query.py @@ -31,8 +31,12 @@ class UserQuery(DataQueryABC): self.set_field("xp", self.resolve_xp) self.set_field("ontime", self.resolve_ontime) self.set_field("level", self.resolve_level) - self.set_field("joined_servers", self.resolve_joined_servers) - self.set_field("joined_voice_channels", self.resolve_joined_voice_channel) + self._add_collection( + "joined_server", lambda user, *_: self._ujs.get_user_joined_servers_by_user_id(user.user_id) + ) + self._add_collection( + "joined_voice_channel", lambda user, *_: self._ujvs.get_user_joined_voice_channels_by_user_id(user.user_id) + ) self.set_field("server", self.resolve_server) @staticmethod @@ -58,12 +62,6 @@ class UserQuery(DataQueryABC): def resolve_level(self, user: User, *_): return self._levels.get_level(user) - def resolve_joined_servers(self, user: User, *_): - return self._ujs.get_user_joined_servers_by_user_id(user.user_id) - - def resolve_joined_voice_channel(self, user: User, *_): - return self._ujvs.get_user_joined_voice_channels_by_user_id(user.user_id) - @staticmethod def resolve_server(user: User, *_): return user.server diff --git a/kdb-bot/src/bot_graphql/query.py b/kdb-bot/src/bot_graphql/query.py index 00cf7df1..2c4fa1b8 100644 --- a/kdb-bot/src/bot_graphql/query.py +++ b/kdb-bot/src/bot_graphql/query.py @@ -1,39 +1,49 @@ -from ariadne import QueryType - +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC +from bot_data.abc.level_repository_abc import LevelRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC -from bot_graphql.abc.filter_abc import FilterABC +from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC +from bot_data.abc.user_joined_voice_channel_repository_abc import UserJoinedVoiceChannelRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_graphql.abc.query_abc import QueryABC +from bot_graphql.filter.auto_role_filter import AutoRoleFilter +from bot_graphql.filter.auto_role_rule_filter import AutoRoleRuleFilter +from bot_graphql.filter.level_filter import LevelFilter from bot_graphql.filter.server_filter import ServerFilter +from bot_graphql.filter.user_filter import UserFilter -class Query(QueryType): +class Query(QueryABC): def __init__( self, - servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC, + clients: ClientRepositoryABC, known_users: KnownUserRepositoryABC, + levels: LevelRepositoryABC, + servers: ServerRepositoryABC, + user_joined_servers: UserJoinedServerRepositoryABC, + user_joined_voice_channel: UserJoinedVoiceChannelRepositoryABC, + users: UserRepositoryABC, ): - QueryType.__init__(self) - self._servers = servers + QueryABC.__init__(self, "Query") + self._auto_roles = auto_roles + self._clients = clients self._known_users = known_users + self._levels = levels + self._servers = servers + self._user_joined_servers = user_joined_servers + self._user_joined_voice_channels = user_joined_voice_channel + self._users = users - self.set_field("servers", self.resolve_servers) - self.set_field("server_count", self.resolve_server_count) - - self.set_field("known_users", self.resolve_known_users) - self.set_field("known_user_count", self.resolve_known_users_count) - - @FilterABC.resolve_filter_annotation - def resolve_servers(self, *_, filter: ServerFilter = None): - if filter is not None: - return filter.filter(self._servers.get_servers()) - else: - return self._servers.get_servers() - - def resolve_server_count(self, *_): - return self._servers.get_servers().count() - - def resolve_known_users(self, *_): - return self._known_users.get_users() - - def resolve_known_users_count(self, *_): - return self._known_users.get_users().count() + self._add_collection("auto_role", lambda *_: self._auto_roles.get_auto_roles(), AutoRoleFilter) + self._add_collection("auto_role_rule", lambda *_: self._auto_roles.get_auto_role_rules(), AutoRoleRuleFilter) + self._add_collection("client", lambda *_: self._clients.get_clients()) + self._add_collection("known_user", lambda *_: self._known_users.get_users()) + self._add_collection("level", lambda *_: self._levels.get_levels(), LevelFilter) + self._add_collection("server", lambda *_: self._servers.get_servers(), ServerFilter) + self._add_collection("user_joined_server", lambda *_: self._user_joined_servers.get_user_joined_servers()) + self._add_collection( + "user_joined_voice_channel", lambda *_: self._user_joined_voice_channels.get_user_joined_voice_channels() + ) + self._add_collection("user", lambda *_: self._users.get_users(), UserFilter) From 16066864ed9909c5e3c7d3f217f8c0c9b7353836 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Mon, 16 Jan 2023 19:18:23 +0100 Subject: [PATCH 22/36] Improved queries & filtering #162 --- .../bot_graphql/abc/discord_query_type_abc.py | 20 -- kdb-bot/src/bot_graphql/abc/filter_abc.py | 30 --- kdb-bot/src/bot_graphql/abc/query_abc.py | 22 +- kdb-bot/src/bot_graphql/abc/query_type_abc.py | 29 --- .../bot_graphql/filter/auto_role_filter.py | 2 +- .../filter/auto_role_rule_filter.py | 2 +- .../src/bot_graphql/filter/level_filter.py | 4 +- kdb-bot/src/bot_graphql/filter/page.py | 25 +++ .../src/bot_graphql/filter/server_filter.py | 8 +- kdb-bot/src/bot_graphql/filter/sort.py | 20 ++ kdb-bot/src/bot_graphql/filter/user_filter.py | 16 +- kdb-bot/src/bot_graphql/model.gql | 212 ------------------ kdb-bot/src/bot_graphql/model/auto_role.gql | 13 ++ .../src/bot_graphql/model/auto_role_rule.gql | 10 + kdb-bot/src/bot_graphql/model/base.gql | 14 ++ kdb-bot/src/bot_graphql/model/client.gql | 15 ++ kdb-bot/src/bot_graphql/model/known_user.gql | 7 + kdb-bot/src/bot_graphql/model/level.gql | 31 +++ kdb-bot/src/bot_graphql/model/mutation.gql | 3 + kdb-bot/src/bot_graphql/model/query.gql | 28 +++ kdb-bot/src/bot_graphql/model/server.gql | 26 +++ kdb-bot/src/bot_graphql/model/user.gql | 28 +++ .../bot_graphql/model/user_joined_server.gql | 10 + .../model/user_joined_voice_channel.gql | 11 + kdb-bot/src/bot_graphql/schema.py | 2 +- 25 files changed, 286 insertions(+), 302 deletions(-) delete mode 100644 kdb-bot/src/bot_graphql/abc/discord_query_type_abc.py delete mode 100644 kdb-bot/src/bot_graphql/abc/query_type_abc.py create mode 100644 kdb-bot/src/bot_graphql/filter/page.py create mode 100644 kdb-bot/src/bot_graphql/filter/sort.py delete mode 100644 kdb-bot/src/bot_graphql/model.gql create mode 100644 kdb-bot/src/bot_graphql/model/auto_role.gql create mode 100644 kdb-bot/src/bot_graphql/model/auto_role_rule.gql create mode 100644 kdb-bot/src/bot_graphql/model/base.gql create mode 100644 kdb-bot/src/bot_graphql/model/client.gql create mode 100644 kdb-bot/src/bot_graphql/model/known_user.gql create mode 100644 kdb-bot/src/bot_graphql/model/level.gql create mode 100644 kdb-bot/src/bot_graphql/model/mutation.gql create mode 100644 kdb-bot/src/bot_graphql/model/query.gql create mode 100644 kdb-bot/src/bot_graphql/model/server.gql create mode 100644 kdb-bot/src/bot_graphql/model/user.gql create mode 100644 kdb-bot/src/bot_graphql/model/user_joined_server.gql create mode 100644 kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql diff --git a/kdb-bot/src/bot_graphql/abc/discord_query_type_abc.py b/kdb-bot/src/bot_graphql/abc/discord_query_type_abc.py deleted file mode 100644 index 84b756a5..00000000 --- a/kdb-bot/src/bot_graphql/abc/discord_query_type_abc.py +++ /dev/null @@ -1,20 +0,0 @@ -from datetime import datetime - -from bot_graphql.abc.query_type_abc import QueryTypeABC - - -class DiscordQueryTypeABC(QueryTypeABC): - def __init__( - self, - id: str, - discord_id: str, - created_at: datetime = None, - modified_at: datetime = None, - ): - QueryTypeABC.__init__(self, id, created_at, modified_at) - - self._discord_id = discord_id - - @property - def discord_id(self) -> str: - return self._discord_id diff --git a/kdb-bot/src/bot_graphql/abc/filter_abc.py b/kdb-bot/src/bot_graphql/abc/filter_abc.py index a88f8bc4..e0d5ff8d 100644 --- a/kdb-bot/src/bot_graphql/abc/filter_abc.py +++ b/kdb-bot/src/bot_graphql/abc/filter_abc.py @@ -1,7 +1,6 @@ import functools from abc import ABC, abstractmethod from inspect import signature, Parameter -from typing import Optional from cpl_core.dependency_injection import ServiceProviderABC from cpl_query.extension import List @@ -11,35 +10,6 @@ class FilterABC(ABC): def __init__(self): ABC.__init__(self) - self._page_index = None - self._page_size = None - self._sort_direction = None - self._sort_column = None - - @property - def page_index(self) -> Optional[int]: - return self._page_index - - @property - def page_size(self) -> Optional[int]: - return self._page_size - - @property - def sort_direction(self) -> Optional[str]: - return self._sort_direction - - @property - def sort_column(self) -> Optional[str]: - return self._sort_column - - def skip_and_take(self, query: List): - if self._page_size is not None and self.page_index is not None: - skip = self.page_size * self.page_index - result = query.skip(skip).take(self.page_size) - return result - - return query - @abstractmethod def from_dict(self, values: dict): pass diff --git a/kdb-bot/src/bot_graphql/abc/query_abc.py b/kdb-bot/src/bot_graphql/abc/query_abc.py index d892ea68..4e094890 100644 --- a/kdb-bot/src/bot_graphql/abc/query_abc.py +++ b/kdb-bot/src/bot_graphql/abc/query_abc.py @@ -4,6 +4,8 @@ from ariadne import ObjectType from cpl_query.extension import List from bot_graphql.abc.filter_abc import FilterABC +from bot_graphql.filter.page import Page +from bot_graphql.filter.sort import Sort class QueryABC(ObjectType): @@ -19,12 +21,30 @@ class QueryABC(ObjectType): else: kwargs["filter"] = None + if "page" in kwargs: + page = Page() + page.from_dict(kwargs["page"]) + kwargs["page"] = page + + if "sort" in kwargs: + sort = Sort() + sort.from_dict(kwargs["sort"]) + kwargs["sort"] = sort + return self._resolve_collection(get_collection(*args), *args, **kwargs) self.set_field(f"{name}s", wrapper) self.set_field(f"{name}_count", lambda *args: get_collection(*args).count()) - def _resolve_collection(self, collection: List, *_, filter: FilterABC = None): + # @FilterABC.resolve_filter_annotation + def _resolve_collection(self, collection: List, *_, filter: FilterABC = None, page: Page = None, sort: Sort = None): if filter is not None: return filter.filter(collection) + + if page is not None: + return page.filter(collection) + + if sort is not None: + return sort.filter(collection) + return collection diff --git a/kdb-bot/src/bot_graphql/abc/query_type_abc.py b/kdb-bot/src/bot_graphql/abc/query_type_abc.py deleted file mode 100644 index 774e4cef..00000000 --- a/kdb-bot/src/bot_graphql/abc/query_type_abc.py +++ /dev/null @@ -1,29 +0,0 @@ -from abc import ABC -from datetime import datetime -from typing import Optional - - -class QueryTypeABC(ABC): - def __init__( - self, - id: str, - created_at: datetime = None, - modified_at: datetime = None, - ): - ABC.__init__(self) - - self._id = id - self._created_at = created_at - self._modified_at = modified_at - - @property - def id(self) -> str: - return self._id - - @property - def created_at(self) -> Optional[datetime]: - return self._created_at - - @property - def modified_at(self) -> Optional[datetime]: - return self._modified_at diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_filter.py index b49038e3..4e31cc73 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_filter.py @@ -35,4 +35,4 @@ class AutoRoleFilter(FilterABC): # if self._server_id is not None: # query = query.where(lambda x: x.server.server_id == self._server_id) - return self.skip_and_take(query) + return query diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py index f47cc939..da1905f6 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py @@ -42,4 +42,4 @@ class AutoRoleRuleFilter(FilterABC): # if self._server_id is not None: # query = query.where(lambda x: x.server.server_id == self._server_id) - return self.skip_and_take(query) + return query diff --git a/kdb-bot/src/bot_graphql/filter/level_filter.py b/kdb-bot/src/bot_graphql/filter/level_filter.py index c85ac9a8..c5cbbad8 100644 --- a/kdb-bot/src/bot_graphql/filter/level_filter.py +++ b/kdb-bot/src/bot_graphql/filter/level_filter.py @@ -17,7 +17,7 @@ class LevelFilter(FilterABC): self._id = values["id"] if "name" in values: - self._id = values["name"] + self._name = values["name"] def filter(self, query: List[Level]) -> List[Level]: if self._id is not None: @@ -26,4 +26,4 @@ class LevelFilter(FilterABC): if self._name is not None: query = query.where(lambda x: self._name.lower() == x.name.lower() or self._name.lower() in x.name.lower()) - return self.skip_and_take(query) + return query diff --git a/kdb-bot/src/bot_graphql/filter/page.py b/kdb-bot/src/bot_graphql/filter/page.py new file mode 100644 index 00000000..6b9e4f48 --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/page.py @@ -0,0 +1,25 @@ +from cpl_query.extension import List + +from bot_graphql.abc.filter_abc import FilterABC + + +class Page(FilterABC): + def __init__(self): + FilterABC.__init__(self) + self._page_index = None + self._page_size = None + + def from_dict(self, values: dict): + if "page_index" in values: + self._page_index = values["page_index"] + + if "page_size" in values: + self._page_size = values["page_size"] + + def filter(self, query: List, *args) -> List: + if self._page_size is not None and self._page_index is not None: + skip = self._page_size * self._page_index + result = query.skip(skip).take(self._page_size) + return result + + return query diff --git a/kdb-bot/src/bot_graphql/filter/server_filter.py b/kdb-bot/src/bot_graphql/filter/server_filter.py index 148fe096..6f31977f 100644 --- a/kdb-bot/src/bot_graphql/filter/server_filter.py +++ b/kdb-bot/src/bot_graphql/filter/server_filter.py @@ -19,6 +19,12 @@ class ServerFilter(FilterABC): if "id" in values: self._id = int(values["id"]) + if "discord_id" in values: + self._discord_id = values["discord_id"] + + if "name" in values: + self._name = values["name"] + @ServiceProviderABC.inject def filter(self, query: List[Server], bot: DiscordBotServiceABC) -> List[Server]: if self._id is not None: @@ -37,4 +43,4 @@ class ServerFilter(FilterABC): query = query.where(where_guild) - return self.skip_and_take(query) + return query diff --git a/kdb-bot/src/bot_graphql/filter/sort.py b/kdb-bot/src/bot_graphql/filter/sort.py new file mode 100644 index 00000000..cb431a39 --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/sort.py @@ -0,0 +1,20 @@ +from cpl_query.extension import List + +from bot_graphql.abc.filter_abc import FilterABC + + +class Sort(FilterABC): + def __init__(self): + FilterABC.__init__(self) + self._sort_direction = None + self._sort_column = None + + def from_dict(self, values: dict): + if "sort_direction" in values: + self._sort_direction = values["sort_direction"] + + if "sort_column" in values: + self._sort_column = values["sort_column"] + + def filter(self, query: List, *args) -> List: + return query diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index 960241ff..79f2a7dd 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -1,21 +1,27 @@ +from typing import Optional + +from cpl_core.dependency_injection import ServiceProviderABC from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List from bot_core.abc.client_utils_abc import ClientUtilsABC from bot_data.model.user import User from bot_graphql.abc.filter_abc import FilterABC +from bot_graphql.filter.level_filter import LevelFilter from modules.level.service.level_service import LevelService class UserFilter(FilterABC): def __init__( self, + services: ServiceProviderABC, bot: DiscordBotServiceABC, client_utils: ClientUtilsABC, levels: LevelService, ): FilterABC.__init__(self) + self._services = services self._bot = bot self._client_utils = client_utils self._levels = levels @@ -25,7 +31,7 @@ class UserFilter(FilterABC): self._name = None self._xp = None self._ontime = None - self._level = None + self._level: Optional[LevelFilter] = None def from_dict(self, values: dict): if "id" in values: @@ -39,7 +45,8 @@ class UserFilter(FilterABC): if "ontime" in values: self._ontime = values["ontime"] if "level" in values: - self._level = values["level"] + self._level: LevelFilter = self._services.get_service(LevelFilter) + self._level.from_dict(values["level"]) def filter(self, query: List[User]) -> List[User]: if self._id is not None: @@ -61,6 +68,7 @@ class UserFilter(FilterABC): query = query.where(lambda x: self._client_utils.get_ontime_for_user(x) == self._ontime) if self._level is not None: - query = query.where(lambda x: self._levels.get_level(x) == self._level) + levels = self._level.filter(query.select(lambda x: self._levels.get_level(x))).select(lambda x: x.id) + query = query.where(lambda x: self._levels.get_level(x).id in levels) - return self.skip_and_take(query) + return query diff --git a/kdb-bot/src/bot_graphql/model.gql b/kdb-bot/src/bot_graphql/model.gql deleted file mode 100644 index 32d8b6b1..00000000 --- a/kdb-bot/src/bot_graphql/model.gql +++ /dev/null @@ -1,212 +0,0 @@ -interface TableQuery { - created_at: String - modified_at: String -} - -type Mutation { - level: LevelMutation -} - -type Query { - auto_roles: [AutoRole] - auto_role_count: Int - - auto_role_rules: [AutoRole] - auto_role_rule_count: Int - - clients: [Client] - client_count: Int - - known_users: [KnownUser] - known_user_count: Int - - levels: [Level] - level_count: Int - - servers(filter: ServerFilter): [Server] - server_count: Int - - user_joined_servers: [User] - user_joined_server_count: Int - - user_joined_voice_channels: [User] - user_joined_voice_channel_count: Int - - users(filter: UserFilter): [User] - user_count: Int -} - -type KnownUser implements TableQuery { - id: ID - discord_id: String - - created_at: String - modified_at: String -} - -input ServerFilter { - id: ID - discord_id: String - name: String - - page_index: Int - page_size: Int - sort_direction: String - sort_column: String -} - -type Server implements TableQuery { - id: ID - discord_id: String - name: String - - auto_roles: [AutoRole] - auto_role_count: Int - - clients: [Client] - client_count: Int - - users(filter: UserFilter): [User] - user_count: Int - - levels: [Level] - level_count: Int - - created_at: String - modified_at: String -} - -type Client implements TableQuery { - id: ID - discord_id: String - name: String - sent_message_count: Int - received_message_count: Int - deleted_message_count: Int - received_command_count: Int - moved_users_count: Int - - server: Server - - created_at: String - modified_at: String -} - -type AutoRole implements TableQuery { - id: ID - channel_id: String - message_id: String - - server: Server - - auto_role_rules: [AutoRoleRule] - auto_role_rule_count: Int - - created_at: String - modified_at: String -} - -type AutoRoleRule implements TableQuery { - id: ID - emoji_name: String - role_id: String - - auto_role: AutoRole - - created_at: String - modified_at: String -} - -input UserFilter { - id: ID - discord_id: String - name: String - xp: Int - ontime: Float - level: LevelFilter - - page_index: Int - page_size: Int - sort_direction: String - sort_column: String -} - -type User implements TableQuery { - id: ID - discord_id: String - name: String - xp: Int - ontime: Float - level: Level - - joined_servers: [UserJoinedServer] - joined_server_count: Int - - joined_voice_channels: [UserJoinedVoiceChannel] - joined_voice_channel_count: Int - - server: Server - - created_at: String - modified_at: String -} - -type UserJoinedServer implements TableQuery { - id: ID - user: User - server: Server - joined_on: String - leaved_on: String - - created_at: String - modified_at: String -} - -type UserJoinedVoiceChannel implements TableQuery { - id: ID - channel_id: String - user: User - server: Server - joined_on: String - leaved_on: String - - created_at: String - modified_at: String -} - -input LevelFilter { - id: ID - name: String - - page_index: Int - page_size: Int - sort_direction: String - sort_column: String -} - -type Level implements TableQuery { - id: ID - name: String - color: String - min_xp: Int - permissions: String - - server: Server - - created_at: String - modified_at: String -} - -input LevelInput { - name: String! - color: String! - min_xp: Int! - permissions: String! - server_id: ID! -} - -type LevelMutation { - create_level(input: LevelInput!): Level - update_level(input: LevelInput!): Level - delete_level(id: ID): Level -} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/auto_role.gql b/kdb-bot/src/bot_graphql/model/auto_role.gql new file mode 100644 index 00000000..68fa0809 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/auto_role.gql @@ -0,0 +1,13 @@ +type AutoRole implements TableQuery { + id: ID + channel_id: String + message_id: String + + server: Server + + auto_role_rules: [AutoRoleRule] + auto_role_rule_count: Int + + created_at: String + modified_at: String +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql new file mode 100644 index 00000000..8939a312 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql @@ -0,0 +1,10 @@ +type AutoRoleRule implements TableQuery { + id: ID + emoji_name: String + role_id: String + + auto_role: AutoRole + + created_at: String + modified_at: String +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/base.gql b/kdb-bot/src/bot_graphql/model/base.gql new file mode 100644 index 00000000..b2babd53 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/base.gql @@ -0,0 +1,14 @@ +interface TableQuery { + created_at: String + modified_at: String +} + +input Page { + page_index: Int + page_size: Int +} + +input Sort { + sort_direction: String + sort_column: String +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/client.gql b/kdb-bot/src/bot_graphql/model/client.gql new file mode 100644 index 00000000..38043a80 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/client.gql @@ -0,0 +1,15 @@ +type Client implements TableQuery { + id: ID + discord_id: String + name: String + sent_message_count: Int + received_message_count: Int + deleted_message_count: Int + received_command_count: Int + moved_users_count: Int + + server: Server + + created_at: String + modified_at: String +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/known_user.gql b/kdb-bot/src/bot_graphql/model/known_user.gql new file mode 100644 index 00000000..fac127c0 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/known_user.gql @@ -0,0 +1,7 @@ +type KnownUser implements TableQuery { + id: ID + discord_id: String + + created_at: String + modified_at: String +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/level.gql b/kdb-bot/src/bot_graphql/model/level.gql new file mode 100644 index 00000000..20588aa6 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/level.gql @@ -0,0 +1,31 @@ +type Level implements TableQuery { + id: ID + name: String + color: String + min_xp: Int + permissions: String + + server: Server + + created_at: String + modified_at: String +} + +input LevelFilter { + id: ID + name: String +} + +type LevelMutation { + create_level(input: LevelInput!): Level + update_level(input: LevelInput!): Level + delete_level(id: ID): Level +} + +input LevelInput { + name: String! + color: String! + min_xp: Int! + permissions: String! + server_id: ID! +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/mutation.gql b/kdb-bot/src/bot_graphql/model/mutation.gql new file mode 100644 index 00000000..1f3a002a --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/mutation.gql @@ -0,0 +1,3 @@ +type Mutation { + level: LevelMutation +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/query.gql b/kdb-bot/src/bot_graphql/model/query.gql new file mode 100644 index 00000000..1e9ddab9 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/query.gql @@ -0,0 +1,28 @@ +type Query { + auto_roles: [AutoRole] + auto_role_count: Int + + auto_role_rules: [AutoRole] + auto_role_rule_count: Int + + clients: [Client] + client_count: Int + + known_users: [KnownUser] + known_user_count: Int + + levels(filter: LevelFilter, page: Page, sort: Sort): [Level] + level_count: Int + + servers(filter: ServerFilter, page: Page, sort: Sort): [Server] + server_count: Int + + user_joined_servers: [User] + user_joined_server_count: Int + + user_joined_voice_channels: [User] + user_joined_voice_channel_count: Int + + users(filter: UserFilter, page: Page, sort: Sort): [User] + user_count: Int +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/server.gql b/kdb-bot/src/bot_graphql/model/server.gql new file mode 100644 index 00000000..a5e8f7b2 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/server.gql @@ -0,0 +1,26 @@ +type Server implements TableQuery { + id: ID + discord_id: String + name: String + + auto_roles: [AutoRole] + auto_role_count: Int + + clients: [Client] + client_count: Int + + users(filter: UserFilter): [User] + user_count: Int + + levels: [Level] + level_count: Int + + created_at: String + modified_at: String +} + +input ServerFilter { + id: ID + discord_id: String + name: String +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/user.gql b/kdb-bot/src/bot_graphql/model/user.gql new file mode 100644 index 00000000..6ec8f992 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/user.gql @@ -0,0 +1,28 @@ +type User implements TableQuery { + id: ID + discord_id: String + name: String + xp: Int + ontime: Float + level: Level + + joined_servers: [UserJoinedServer] + joined_server_count: Int + + joined_voice_channels: [UserJoinedVoiceChannel] + joined_voice_channel_count: Int + + server: Server + + created_at: String + modified_at: String +} + +input UserFilter { + id: ID + discord_id: String + name: String + xp: Int + ontime: Float + level: LevelFilter +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/user_joined_server.gql b/kdb-bot/src/bot_graphql/model/user_joined_server.gql new file mode 100644 index 00000000..bd3d0350 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/user_joined_server.gql @@ -0,0 +1,10 @@ +type UserJoinedServer implements TableQuery { + id: ID + user: User + server: Server + joined_on: String + leaved_on: String + + created_at: String + modified_at: String +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql b/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql new file mode 100644 index 00000000..574489b2 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql @@ -0,0 +1,11 @@ +type UserJoinedVoiceChannel implements TableQuery { + id: ID + channel_id: String + user: User + server: Server + joined_on: String + leaved_on: String + + created_at: String + modified_at: String +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/schema.py b/kdb-bot/src/bot_graphql/schema.py index 9e1b7c5e..d2972a78 100644 --- a/kdb-bot/src/bot_graphql/schema.py +++ b/kdb-bot/src/bot_graphql/schema.py @@ -10,7 +10,7 @@ from bot_graphql.query import Query class Schema: def __init__(self, query: Query, mutation: Mutation, queries: list[QueryABC]): - type_defs = load_schema_from_path(os.path.join(os.path.dirname(os.path.realpath(__file__)), "model.gql")) + type_defs = load_schema_from_path(os.path.join(os.path.dirname(os.path.realpath(__file__)), "model/")) self._schema = make_executable_schema(type_defs, query, mutation, *queries) @property From dbfae8d8a46c8f39574b1e64d9f70eaeb7283702 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 7 Feb 2023 17:07:14 +0100 Subject: [PATCH 23/36] Added more gql filters #162 --- kdb-bot/src/bot_graphql/model/auto_role.gql | 7 +++++++ kdb-bot/src/bot_graphql/model/auto_role_rule.gql | 7 +++++++ kdb-bot/src/bot_graphql/model/client.gql | 12 ++++++++++++ kdb-bot/src/bot_graphql/model/query.gql | 10 +++++----- kdb-bot/src/bot_graphql/model/user_joined_server.gql | 8 ++++++++ .../bot_graphql/model/user_joined_voice_channel.gql | 9 +++++++++ 6 files changed, 48 insertions(+), 5 deletions(-) diff --git a/kdb-bot/src/bot_graphql/model/auto_role.gql b/kdb-bot/src/bot_graphql/model/auto_role.gql index 68fa0809..1099d6be 100644 --- a/kdb-bot/src/bot_graphql/model/auto_role.gql +++ b/kdb-bot/src/bot_graphql/model/auto_role.gql @@ -10,4 +10,11 @@ type AutoRole implements TableQuery { created_at: String modified_at: String +} + +input AutoRoleFilter { + id: ID + channel_id: String + message_id: String + server: ServerFilter } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql index 8939a312..27e06ad0 100644 --- a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql +++ b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql @@ -7,4 +7,11 @@ type AutoRoleRule implements TableQuery { created_at: String modified_at: String +} + +input AutoRoleRuleFilter { + id: ID + emoji_name: String + role_id: String + auto_role: AutoRoleFilter } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/client.gql b/kdb-bot/src/bot_graphql/model/client.gql index 38043a80..7330fc6f 100644 --- a/kdb-bot/src/bot_graphql/model/client.gql +++ b/kdb-bot/src/bot_graphql/model/client.gql @@ -12,4 +12,16 @@ type Client implements TableQuery { created_at: String modified_at: String +} + +input ClientFilter { + id: ID + discord_id: String + name: String + sent_message_count: Int + received_message_count: Int + deleted_message_count: Int + received_command_count: Int + moved_users_count: Int + server: ServerFilter } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/query.gql b/kdb-bot/src/bot_graphql/model/query.gql index 1e9ddab9..665c72e0 100644 --- a/kdb-bot/src/bot_graphql/model/query.gql +++ b/kdb-bot/src/bot_graphql/model/query.gql @@ -1,11 +1,11 @@ type Query { - auto_roles: [AutoRole] + auto_roles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole] auto_role_count: Int - auto_role_rules: [AutoRole] + auto_role_rules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRole] auto_role_rule_count: Int - clients: [Client] + clients(filter: ClientFilter, page: Page, sort: Sort): [Client] client_count: Int known_users: [KnownUser] @@ -17,10 +17,10 @@ type Query { servers(filter: ServerFilter, page: Page, sort: Sort): [Server] server_count: Int - user_joined_servers: [User] + user_joined_servers(filter: UserJoinedServerFilter, page: Page, sort: Sort): [User] user_joined_server_count: Int - user_joined_voice_channels: [User] + user_joined_voice_channels(filter: UserJoinedVoiceChannelFilter, page: Page, sort: Sort): [User] user_joined_voice_channel_count: Int users(filter: UserFilter, page: Page, sort: Sort): [User] diff --git a/kdb-bot/src/bot_graphql/model/user_joined_server.gql b/kdb-bot/src/bot_graphql/model/user_joined_server.gql index bd3d0350..5c145d2e 100644 --- a/kdb-bot/src/bot_graphql/model/user_joined_server.gql +++ b/kdb-bot/src/bot_graphql/model/user_joined_server.gql @@ -7,4 +7,12 @@ type UserJoinedServer implements TableQuery { created_at: String modified_at: String +} + +input UserJoinedServerFilter { + id: ID + user: UserFilter + server: ServerFilter + joined_on: String + leaved_on: String } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql b/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql index 574489b2..dd340eaf 100644 --- a/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql +++ b/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql @@ -8,4 +8,13 @@ type UserJoinedVoiceChannel implements TableQuery { created_at: String modified_at: String +} + +input UserJoinedVoiceChannelFilter { + id: ID + channel_id: String + user: UserFilter + server: ServerFilter + joined_on: String + leaved_on: String } \ No newline at end of file From e3c0a0dea3d24c8fada83a90deb8d6a483123ff8 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 7 Feb 2023 17:07:25 +0100 Subject: [PATCH 24/36] Reformatted files with black #162 --- kdb-bot/src/bot_api/exception/service_error_code_enum.py | 1 - kdb-bot/src/bot_data/db_context.py | 1 - kdb-bot/src/bot_data/model/auth_role_enum.py | 1 - kdb-bot/src/modules/stats/ui/add_statistic_form.py | 1 - kdb-bot/tools/post_build/service/remove_config.py | 1 - 5 files changed, 5 deletions(-) diff --git a/kdb-bot/src/bot_api/exception/service_error_code_enum.py b/kdb-bot/src/bot_api/exception/service_error_code_enum.py index 5848bf33..4bb3072f 100644 --- a/kdb-bot/src/bot_api/exception/service_error_code_enum.py +++ b/kdb-bot/src/bot_api/exception/service_error_code_enum.py @@ -4,7 +4,6 @@ from werkzeug.exceptions import Unauthorized class ServiceErrorCode(Enum): - Unknown = 0 InvalidDependencies = 1 diff --git a/kdb-bot/src/bot_data/db_context.py b/kdb-bot/src/bot_data/db_context.py index 6e208ed9..6e11f284 100644 --- a/kdb-bot/src/bot_data/db_context.py +++ b/kdb-bot/src/bot_data/db_context.py @@ -6,7 +6,6 @@ from bot_core.logging.database_logger import DatabaseLogger class DBContext(DatabaseContext): def __init__(self, logger: DatabaseLogger): - self._logger = logger DatabaseContext.__init__(self) diff --git a/kdb-bot/src/bot_data/model/auth_role_enum.py b/kdb-bot/src/bot_data/model/auth_role_enum.py index e1526136..8b93629b 100644 --- a/kdb-bot/src/bot_data/model/auth_role_enum.py +++ b/kdb-bot/src/bot_data/model/auth_role_enum.py @@ -2,6 +2,5 @@ from enum import Enum class AuthRoleEnum(Enum): - normal = 0 admin = 1 diff --git a/kdb-bot/src/modules/stats/ui/add_statistic_form.py b/kdb-bot/src/modules/stats/ui/add_statistic_form.py index 0688aef6..e07502e2 100644 --- a/kdb-bot/src/modules/stats/ui/add_statistic_form.py +++ b/kdb-bot/src/modules/stats/ui/add_statistic_form.py @@ -12,7 +12,6 @@ from bot_data.model.statistic import Statistic class AddStatisticForm(ui.Modal): - description = ui.TextInput(label="Beschreibung", required=True) code = ui.TextInput(label="Code", required=True, style=TextStyle.long) diff --git a/kdb-bot/tools/post_build/service/remove_config.py b/kdb-bot/tools/post_build/service/remove_config.py index ab8b9d47..60370937 100644 --- a/kdb-bot/tools/post_build/service/remove_config.py +++ b/kdb-bot/tools/post_build/service/remove_config.py @@ -16,7 +16,6 @@ class RemoveConfig: build: BuildSettings, post_build: PostBuildSettings, ): - self._env = env self._workspace = ws self._project = project From eb58c34c4d28572440a270086df16591905e05f1 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 7 Feb 2023 17:52:38 +0100 Subject: [PATCH 25/36] Added auto role filter #162 --- kdb-bot/src/bot_data/model/auto_role.py | 14 ++++--- .../service/auto_role_repository_service.py | 32 ++++++++++++---- kdb-bot/src/bot_graphql/abc/query_abc.py | 2 +- .../bot_graphql/filter/auto_role_filter.py | 37 ++++++++++++++++--- .../filter/auto_role_rule_filter.py | 6 +-- .../src/bot_graphql/filter/level_filter.py | 2 +- kdb-bot/src/bot_graphql/filter/page.py | 4 +- .../src/bot_graphql/filter/server_filter.py | 2 +- kdb-bot/src/bot_graphql/filter/user_filter.py | 8 ++-- kdb-bot/src/bot_graphql/model/auto_role.gql | 2 + .../bot_graphql/queries/auto_role_query.py | 15 ++++++-- .../src/bot_graphql/queries/server_query.py | 10 ++--- kdb-bot/src/bot_graphql/queries/user_query.py | 4 +- kdb-bot/src/bot_graphql/query.py | 18 ++++----- .../auto_role/command/auto_role_group.py | 9 ++--- 15 files changed, 109 insertions(+), 56 deletions(-) diff --git a/kdb-bot/src/bot_data/model/auto_role.py b/kdb-bot/src/bot_data/model/auto_role.py index dfef0155..5b2862c8 100644 --- a/kdb-bot/src/bot_data/model/auto_role.py +++ b/kdb-bot/src/bot_data/model/auto_role.py @@ -3,11 +3,13 @@ from typing import Optional from cpl_core.database import TableABC +from bot_data.model.server import Server + class AutoRole(TableABC): def __init__( self, - server_id: int, + server: Optional[Server], dc_channel_id: int, dc_message_id: int, created_at: datetime = None, @@ -15,7 +17,7 @@ class AutoRole(TableABC): id=0, ): self._auto_role_id = id - self._server_id = server_id + self._server = server self._discord_channel_id = dc_channel_id self._discord_message_id = dc_message_id @@ -28,8 +30,8 @@ class AutoRole(TableABC): return self._auto_role_id @property - def server_id(self) -> int: - return self._server_id + def server(self) -> Server: + return self._server @property def discord_channel_id(self) -> int: @@ -81,7 +83,7 @@ class AutoRole(TableABC): INSERT INTO `AutoRoles` ( `ServerId`, `DiscordChannelId`, `DiscordMessageId`, `CreatedAt`, `LastModifiedAt` ) VALUES ( - {self._server_id}, + {self._server.server_id}, {self._discord_channel_id}, {self._discord_message_id}, '{self._created_at}', @@ -95,7 +97,7 @@ class AutoRole(TableABC): return str( f""" UPDATE `AutoRoles` - SET `ServerId` = {self._server_id}, + SET `ServerId` = {self._server.server_id}, `DiscordChannelId` = {self._discord_channel_id}, `DiscordMessageId` = {self._discord_message_id}, `LastModifiedAt` = '{self._modified_at}' diff --git a/kdb-bot/src/bot_data/service/auto_role_repository_service.py b/kdb-bot/src/bot_data/service/auto_role_repository_service.py index b9d51e12..1e23916d 100644 --- a/kdb-bot/src/bot_data/service/auto_role_repository_service.py +++ b/kdb-bot/src/bot_data/service/auto_role_repository_service.py @@ -5,14 +5,16 @@ from cpl_query.extension import List from bot_core.logging.database_logger import DatabaseLogger from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.model.auto_role import AutoRole from bot_data.model.auto_role_rule import AutoRoleRule class AutoRoleRepositoryService(AutoRoleRepositoryABC): - def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC): + def __init__(self, logger: DatabaseLogger, db_context: DatabaseContextABC, servers: ServerRepositoryABC): self._logger = logger self._context = db_context + self._servers = servers AutoRoleRepositoryABC.__init__(self) @@ -21,14 +23,20 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): self._logger.trace(__name__, f"Send SQL command: {AutoRole.get_select_all_string()}") results = self._context.select(AutoRole.get_select_all_string()) for result in results: - auto_roles.append(AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0])) + auto_roles.append( + AutoRole( + self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0] + ) + ) return auto_roles def get_auto_role_by_id(self, id: int) -> AutoRole: self._logger.trace(__name__, f"Send SQL command: {AutoRole.get_select_by_id_string(id)}") result = self._context.select(AutoRole.get_select_by_id_string(id))[0] - return AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0]) + return AutoRole( + self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0] + ) def find_auto_role_by_id(self, id: int) -> Optional[AutoRole]: self._logger.trace(__name__, f"Send SQL command: {AutoRole.get_select_by_id_string(id)}") @@ -38,14 +46,20 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): result = result[0] - return AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0]) + return AutoRole( + self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0] + ) def get_auto_roles_by_server_id(self, id: int) -> List[AutoRole]: self._logger.trace(__name__, f"Send SQL command: {AutoRole.get_select_by_server_id_string(id)}") auto_roles = List(AutoRole) results = self._context.select(AutoRole.get_select_by_server_id_string(id)) for result in results: - auto_roles.append(AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0])) + auto_roles.append( + AutoRole( + self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0] + ) + ) return auto_roles @@ -55,7 +69,9 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): f"Send SQL command: {AutoRole.get_select_by_message_id_string(id)}", ) result = self._context.select(AutoRole.get_select_by_message_id_string(id))[0] - return AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0]) + return AutoRole( + self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0] + ) def find_auto_role_by_message_id(self, id: int) -> Optional[AutoRole]: self._logger.trace( @@ -68,7 +84,9 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): result = result[0] - return AutoRole(result[1], result[2], result[3], result[4], result[5], id=result[0]) + return AutoRole( + self._servers.get_server_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0] + ) def add_auto_role(self, auto_role: AutoRole): self._logger.trace(__name__, f"Send SQL command: {auto_role.insert_string}") diff --git a/kdb-bot/src/bot_graphql/abc/query_abc.py b/kdb-bot/src/bot_graphql/abc/query_abc.py index 4e094890..e52e927a 100644 --- a/kdb-bot/src/bot_graphql/abc/query_abc.py +++ b/kdb-bot/src/bot_graphql/abc/query_abc.py @@ -14,7 +14,7 @@ class QueryABC(ObjectType): def __init__(self, name: str): ObjectType.__init__(self, name) - def _add_collection(self, name: str, get_collection: Callable, filter_type: type = None): + def add_collection(self, name: str, get_collection: Callable, filter_type: type = None): def wrapper(*args, **kwargs): if filter_type is not None and "filter" in kwargs: kwargs["filter"] = FilterABC.get_collection_filter(filter_type, kwargs["filter"]) diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_filter.py index 4e31cc73..288aa68d 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_filter.py @@ -1,3 +1,4 @@ +from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List from bot_data.model.auto_role import AutoRole @@ -5,22 +6,39 @@ from bot_graphql.abc.filter_abc import FilterABC class AutoRoleFilter(FilterABC): - def __init__(self): + def __init__( + self, + bot: DiscordBotServiceABC, + ): FilterABC.__init__(self) + self._bot = bot + self._id = None self._channel_id = None + self._channel_name = None self._message_id = None + self._server = None def from_dict(self, values: dict): if "id" in values: - self._id = values["id"] + self._id = int(values["id"]) if "channel_id" in values: - self._channel_id = values["channel_id"] + self._channel_id = int(values["channel_id"]) + + if "channel_name" in values: + self._channel_name = values["channel_name"] if "message_id" in values: - self._message_id = values["message_id"] + self._message_id = int(values["message_id"]) + + if "server" in values: + from bot_graphql.filter.server_filter import ServerFilter + + server = ServerFilter() + server.from_dict(values["server"]) + self._server = server def filter(self, query: List[AutoRole]) -> List[AutoRole]: if self._id is not None: @@ -29,10 +47,17 @@ class AutoRoleFilter(FilterABC): if self._channel_id is not None: query = query.where(lambda x: x.discord_channel_id == self._channel_id) + if self._channel_name is not None and self._channel_id is not None: + query = query.where( + lambda x: self._bot.get_channel(x.discord_channel_id).name == self._channel_name + or self._channel_name in self._bot.get_channel(x.discord_channel_id).name + ) + if self._message_id is not None: query = query.where(lambda x: x.discord_message_id == self._message_id) - # if self._server_id is not None: - # query = query.where(lambda x: x.server.server_id == self._server_id) + if self._server is not None: + servers = self._server.filter(query.select(lambda x: x.server)).select(lambda x: x.server_id) + query = query.where(lambda x: x.server.server_id in servers) return query diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py index da1905f6..a1912fd7 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py @@ -15,16 +15,16 @@ class AutoRoleRuleFilter(FilterABC): def from_dict(self, values: dict): if "id" in values: - self._id = values["id"] + self._id = int(values["id"]) if "emoji_name" in values: self._emoji_name = values["emoji_name"] if "role_id" in values: - self._role_id = values["role_id"] + self._role_id = int(values["role_id"]) if "auto_role_id" in values: - self._auto_role_id = values["auto_role_id"] + self._auto_role_id = int(values["auto_role_id"]) def filter(self, query: List[AutoRoleRule]) -> List[AutoRoleRule]: if self._id is not None: diff --git a/kdb-bot/src/bot_graphql/filter/level_filter.py b/kdb-bot/src/bot_graphql/filter/level_filter.py index c5cbbad8..f4611107 100644 --- a/kdb-bot/src/bot_graphql/filter/level_filter.py +++ b/kdb-bot/src/bot_graphql/filter/level_filter.py @@ -14,7 +14,7 @@ class LevelFilter(FilterABC): def from_dict(self, values: dict): if "id" in values: - self._id = values["id"] + self._id = int(values["id"]) if "name" in values: self._name = values["name"] diff --git a/kdb-bot/src/bot_graphql/filter/page.py b/kdb-bot/src/bot_graphql/filter/page.py index 6b9e4f48..60978d31 100644 --- a/kdb-bot/src/bot_graphql/filter/page.py +++ b/kdb-bot/src/bot_graphql/filter/page.py @@ -11,10 +11,10 @@ class Page(FilterABC): def from_dict(self, values: dict): if "page_index" in values: - self._page_index = values["page_index"] + self._page_index = int(values["page_index"]) if "page_size" in values: - self._page_size = values["page_size"] + self._page_size = int(values["page_size"]) def filter(self, query: List, *args) -> List: if self._page_size is not None and self._page_index is not None: diff --git a/kdb-bot/src/bot_graphql/filter/server_filter.py b/kdb-bot/src/bot_graphql/filter/server_filter.py index 6f31977f..2ae039a0 100644 --- a/kdb-bot/src/bot_graphql/filter/server_filter.py +++ b/kdb-bot/src/bot_graphql/filter/server_filter.py @@ -20,7 +20,7 @@ class ServerFilter(FilterABC): self._id = int(values["id"]) if "discord_id" in values: - self._discord_id = values["discord_id"] + self._discord_id = int(values["discord_id"]) if "name" in values: self._name = values["name"] diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index 79f2a7dd..d92531df 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -35,15 +35,15 @@ class UserFilter(FilterABC): def from_dict(self, values: dict): if "id" in values: - self._id = values["id"] + self._id = int(values["id"]) if "discord_id" in values: - self._discord_id = values["discord_id"] + self._discord_id = int(values["discord_id"]) if "name" in values: self._name = values["name"] if "xp" in values: - self._xp = values["xp"] + self._xp = int(values["xp"]) if "ontime" in values: - self._ontime = values["ontime"] + self._ontime = int(values["ontime"]) if "level" in values: self._level: LevelFilter = self._services.get_service(LevelFilter) self._level.from_dict(values["level"]) diff --git a/kdb-bot/src/bot_graphql/model/auto_role.gql b/kdb-bot/src/bot_graphql/model/auto_role.gql index 1099d6be..47ee4e81 100644 --- a/kdb-bot/src/bot_graphql/model/auto_role.gql +++ b/kdb-bot/src/bot_graphql/model/auto_role.gql @@ -1,6 +1,7 @@ type AutoRole implements TableQuery { id: ID channel_id: String + channel_name: String message_id: String server: Server @@ -15,6 +16,7 @@ type AutoRole implements TableQuery { input AutoRoleFilter { id: ID channel_id: String + channel_name: String message_id: String server: ServerFilter } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_query.py index 4b75fe89..16ecfe65 100644 --- a/kdb-bot/src/bot_graphql/queries/auto_role_query.py +++ b/kdb-bot/src/bot_graphql/queries/auto_role_query.py @@ -1,3 +1,5 @@ +from cpl_discord.service import DiscordBotServiceABC + from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.server_repository_abc import ServerRepositoryABC from bot_data.model.auto_role import AutoRole @@ -8,19 +10,22 @@ from bot_graphql.filter.server_filter import ServerFilter class AutoRoleQuery(DataQueryABC): def __init__( self, + bot: DiscordBotServiceABC, auto_role_rules: AutoRoleRepositoryABC, servers: ServerRepositoryABC, ): DataQueryABC.__init__(self, "AutoRole") + self._bot = bot self._auto_role_rules = auto_role_rules self._servers = servers self.set_field("id", self.resolve_id) self.set_field("channel_id", self.resolve_channel_id) + self.set_field("channel_name", self.resolve_channel_name) self.set_field("message_id", self.resolve_message_id) self.set_field("server", self.resolve_server) - self._add_collection( + self.add_collection( "auto_role_rule", lambda x, *_: self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id) ) @@ -32,12 +37,16 @@ class AutoRoleQuery(DataQueryABC): def resolve_channel_id(x: AutoRole, *_): return x.discord_channel_id + def resolve_channel_name(self, x: AutoRole, *_): + channel = self._bot.get_channel(x.discord_channel_id) + return None if channel is None else channel.name + @staticmethod def resolve_message_id(x: AutoRole, *_): return x.discord_message_id def resolve_server(self, x: AutoRole, *_, filter: ServerFilter = None): if filter is not None: - return filter.filter(self._servers.get_server_by_id(x.server_id)) + return filter.filter(self._servers.get_server_by_id(x.server.server_id)) - return self._servers.get_server_by_id(x.server_id) + return self._servers.get_server_by_id(x.server.server_id) diff --git a/kdb-bot/src/bot_graphql/queries/server_query.py b/kdb-bot/src/bot_graphql/queries/server_query.py index a1ba09b1..ed817290 100644 --- a/kdb-bot/src/bot_graphql/queries/server_query.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -31,14 +31,12 @@ class ServerQuery(DataQueryABC): self.set_field("discord_id", self.resolve_discord_id) self.set_field("name", self.resolve_name) - self._add_collection( + self.add_collection( "auto_role", lambda server, *_: self._auto_roles.get_auto_roles_by_server_id(server.server_id) ) - self._add_collection("client", lambda server, *_: self._clients.get_clients_by_server_id(server.server_id)) - self._add_collection( - "user", lambda server, *_: self._users.get_users_by_server_id(server.server_id), UserFilter - ) - self._add_collection("level", lambda server, *_: self._levels.get_levels_by_server_id(server.server_id)) + self.add_collection("client", lambda server, *_: self._clients.get_clients_by_server_id(server.server_id)) + self.add_collection("user", lambda server, *_: self._users.get_users_by_server_id(server.server_id), UserFilter) + self.add_collection("level", lambda server, *_: self._levels.get_levels_by_server_id(server.server_id)) @staticmethod def resolve_id(server: Server, *_): diff --git a/kdb-bot/src/bot_graphql/queries/user_query.py b/kdb-bot/src/bot_graphql/queries/user_query.py index a8081408..17d8e04d 100644 --- a/kdb-bot/src/bot_graphql/queries/user_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_query.py @@ -31,10 +31,10 @@ class UserQuery(DataQueryABC): self.set_field("xp", self.resolve_xp) self.set_field("ontime", self.resolve_ontime) self.set_field("level", self.resolve_level) - self._add_collection( + self.add_collection( "joined_server", lambda user, *_: self._ujs.get_user_joined_servers_by_user_id(user.user_id) ) - self._add_collection( + self.add_collection( "joined_voice_channel", lambda user, *_: self._ujvs.get_user_joined_voice_channels_by_user_id(user.user_id) ) self.set_field("server", self.resolve_server) diff --git a/kdb-bot/src/bot_graphql/query.py b/kdb-bot/src/bot_graphql/query.py index 2c4fa1b8..687aafc4 100644 --- a/kdb-bot/src/bot_graphql/query.py +++ b/kdb-bot/src/bot_graphql/query.py @@ -36,14 +36,14 @@ class Query(QueryABC): self._user_joined_voice_channels = user_joined_voice_channel self._users = users - self._add_collection("auto_role", lambda *_: self._auto_roles.get_auto_roles(), AutoRoleFilter) - self._add_collection("auto_role_rule", lambda *_: self._auto_roles.get_auto_role_rules(), AutoRoleRuleFilter) - self._add_collection("client", lambda *_: self._clients.get_clients()) - self._add_collection("known_user", lambda *_: self._known_users.get_users()) - self._add_collection("level", lambda *_: self._levels.get_levels(), LevelFilter) - self._add_collection("server", lambda *_: self._servers.get_servers(), ServerFilter) - self._add_collection("user_joined_server", lambda *_: self._user_joined_servers.get_user_joined_servers()) - self._add_collection( + self.add_collection("auto_role", lambda *_: self._auto_roles.get_auto_roles(), AutoRoleFilter) + self.add_collection("auto_role_rule", lambda *_: self._auto_roles.get_auto_role_rules(), AutoRoleRuleFilter) + self.add_collection("client", lambda *_: self._clients.get_clients()) + self.add_collection("known_user", lambda *_: self._known_users.get_users()) + self.add_collection("level", lambda *_: self._levels.get_levels(), LevelFilter) + self.add_collection("server", lambda *_: self._servers.get_servers(), ServerFilter) + self.add_collection("user_joined_server", lambda *_: self._user_joined_servers.get_user_joined_servers()) + self.add_collection( "user_joined_voice_channel", lambda *_: self._user_joined_voice_channels.get_user_joined_voice_channels() ) - self._add_collection("user", lambda *_: self._users.get_users(), UserFilter) + self.add_collection("user", lambda *_: self._users.get_users(), UserFilter) diff --git a/kdb-bot/src/modules/auto_role/command/auto_role_group.py b/kdb-bot/src/modules/auto_role/command/auto_role_group.py index 947dc8bd..bde66962 100644 --- a/kdb-bot/src/modules/auto_role/command/auto_role_group.py +++ b/kdb-bot/src/modules/auto_role/command/auto_role_group.py @@ -1,9 +1,8 @@ -from typing import List as TList, Optional, Any +from typing import List as TList import discord from cpl_core.database.context import DatabaseContextABC from cpl_discord.command import DiscordCommandABC -from cpl_discord.container import TextChannel from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List from cpl_translation import TranslatePipe @@ -138,10 +137,10 @@ class AutoRoleGroup(DiscordCommandABC): self._logger.trace(__name__, f"Finished command auto-role add") return - server_id = self._servers.get_server_by_discord_id(ctx.guild.id).server_id - self._auto_roles.add_auto_role(AutoRole(server_id, int(channel.id), int(message_id))) + server = self._servers.get_server_by_discord_id(ctx.guild.id) + self._auto_roles.add_auto_role(AutoRole(server, int(channel.id), int(message_id))) self._db_context.save_changes() - self._logger.info(__name__, f"Saved auto-role for message {message_id} at server {server_id}") + self._logger.info(__name__, f"Saved auto-role for message {message_id} at server {server}") await self._message_service.send_ctx_msg( ctx, self._t.transform("modules.auto_role.add.success").format(message_id) ) From 3f7cfc47afe4df28449de5fa31ed8dee38f07c63 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 7 Feb 2023 18:53:28 +0100 Subject: [PATCH 26/36] Finished auto role filter #162 --- kdb-bot/src/bot_data/model/auto_role_rule.py | 15 ++++---- .../service/auto_role_repository_service.py | 16 +++++++-- .../bot_graphql/filter/auto_role_filter.py | 2 +- .../filter/auto_role_rule_filter.py | 35 ++++++++++++++----- kdb-bot/src/bot_graphql/model/auto_role.gql | 2 +- .../src/bot_graphql/model/auto_role_rule.gql | 2 ++ kdb-bot/src/bot_graphql/model/query.gql | 2 +- .../queries/auto_role_rule_query.py | 11 +++++- 8 files changed, 62 insertions(+), 23 deletions(-) diff --git a/kdb-bot/src/bot_data/model/auto_role_rule.py b/kdb-bot/src/bot_data/model/auto_role_rule.py index 01d45ebe..fc79f622 100644 --- a/kdb-bot/src/bot_data/model/auto_role_rule.py +++ b/kdb-bot/src/bot_data/model/auto_role_rule.py @@ -1,13 +1,14 @@ from datetime import datetime -from typing import Optional from cpl_core.database import TableABC +from bot_data.model.auto_role import AutoRole + class AutoRoleRule(TableABC): def __init__( self, - auto_role_id: int, + auto_role: AutoRole, discord_emoji_name: str, discord_role_id: int, created_at: datetime = None, @@ -15,7 +16,7 @@ class AutoRoleRule(TableABC): id=0, ): self._auto_role_rule_id = id - self._auto_role_id = auto_role_id + self._auto_role = auto_role self._discord_emoji_name = discord_emoji_name self._discord_role_id = discord_role_id @@ -28,8 +29,8 @@ class AutoRoleRule(TableABC): return self._auto_role_rule_id @property - def auto_role_id(self) -> int: - return self._auto_role_id + def auto_role(self) -> AutoRole: + return self._auto_role @property def emoji_name(self) -> str: @@ -72,7 +73,7 @@ class AutoRoleRule(TableABC): INSERT INTO `AutoRoleRules` ( `AutoRoleId`, `DiscordEmojiName`, `DiscordRoleId`, `CreatedAt`, `LastModifiedAt` ) VALUES ( - {self._auto_role_id}, + {self._auto_role}, '{self._discord_emoji_name}', {self._discord_role_id}, '{self._created_at}', @@ -86,7 +87,7 @@ class AutoRoleRule(TableABC): return str( f""" UPDATE `AutoRoleRules` - SET `AutoRoleId` = {self._auto_role_id}, + SET `AutoRoleId` = {self._auto_role}, `DiscordEmojiName` = {self._discord_emoji_name}, `DiscordRoleId` = {self._discord_role_id}, `LastModifiedAt` = '{self._modified_at}' diff --git a/kdb-bot/src/bot_data/service/auto_role_repository_service.py b/kdb-bot/src/bot_data/service/auto_role_repository_service.py index 1e23916d..d1599341 100644 --- a/kdb-bot/src/bot_data/service/auto_role_repository_service.py +++ b/kdb-bot/src/bot_data/service/auto_role_repository_service.py @@ -105,14 +105,20 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): self._logger.trace(__name__, f"Send SQL command: {AutoRoleRule.get_select_all_string()}") results = self._context.select(AutoRoleRule.get_select_all_string()) for result in results: - auto_role_rules.append(AutoRoleRule(result[1], result[2], result[3], result[4], result[5], id=result[0])) + auto_role_rules.append( + AutoRoleRule( + self.get_auto_role_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0] + ) + ) return auto_role_rules def get_auto_role_rule_by_id(self, id: int) -> AutoRoleRule: self._logger.trace(__name__, f"Send SQL command: {AutoRoleRule.get_select_by_id_string(id)}") result = self._context.select(AutoRoleRule.get_select_by_id_string(id))[0] - return AutoRoleRule(result[1], result[2], result[3], result[4], result[5], id=result[0]) + return AutoRoleRule( + self.get_auto_role_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0] + ) def get_auto_role_rules_by_auto_role_id(self, id: int) -> List[AutoRoleRule]: auto_role_rules = List(AutoRoleRule) @@ -122,7 +128,11 @@ class AutoRoleRepositoryService(AutoRoleRepositoryABC): ) results = self._context.select(AutoRoleRule.get_select_by_auto_role_id_string(id)) for result in results: - auto_role_rules.append(AutoRoleRule(result[1], result[2], result[3], result[4], result[5], id=result[0])) + auto_role_rules.append( + AutoRoleRule( + self.get_auto_role_by_id(result[1]), result[2], result[3], result[4], result[5], id=result[0] + ) + ) return auto_role_rules diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_filter.py index 288aa68d..8badeea9 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_filter.py @@ -42,7 +42,7 @@ class AutoRoleFilter(FilterABC): def filter(self, query: List[AutoRole]) -> List[AutoRole]: if self._id is not None: - query = query.where(lambda x: x.id == self._id) + query = query.where(lambda x: x.auto_role_id == self._id) if self._channel_id is not None: query = query.where(lambda x: x.discord_channel_id == self._channel_id) diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py index a1912fd7..65e54eb0 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py @@ -1,3 +1,4 @@ +from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List from bot_data.model.auto_role_rule import AutoRoleRule @@ -5,13 +6,15 @@ from bot_graphql.abc.filter_abc import FilterABC class AutoRoleRuleFilter(FilterABC): - def __init__(self): + def __init__(self, bot: DiscordBotServiceABC): FilterABC.__init__(self) + self._bot = bot self._id = None self._emoji_name = None self._role_id = None - self._auto_role_id = None + self._role_name = None + self._auto_role = None def from_dict(self, values: dict): if "id" in values: @@ -23,23 +26,37 @@ class AutoRoleRuleFilter(FilterABC): if "role_id" in values: self._role_id = int(values["role_id"]) - if "auto_role_id" in values: - self._auto_role_id = int(values["auto_role_id"]) + if "role_name" in values: + self._role_name = values["role_name"] + + if "auto_role" in values: + from bot_graphql.filter.auto_role_filter import AutoRoleFilter + + auto_role = AutoRoleFilter(self._bot) + auto_role.from_dict(values["auto_role"]) + self._auto_role = auto_role def filter(self, query: List[AutoRoleRule]) -> List[AutoRoleRule]: if self._id is not None: query = query.where(lambda x: x.auto_role_rule_id == self._id) - if self._auto_role_id is not None: - query = query.where(lambda x: x.auto_role_id == self._auto_role_id) - if self._emoji_name is not None: query = query.where(lambda x: x.emoji_name == self._emoji_name) if self._role_id is not None: query = query.where(lambda x: x.role_id == self._role_id) - # if self._server_id is not None: - # query = query.where(lambda x: x.server.server_id == self._server_id) + if self._role_name is not None: + + def get_role_name(x: AutoRoleRule): + guild = self._bot.get_guild(x.auto_role.server.discord_server_id) + name = guild.get_role(x.role_id).name + return name == self._role_name or self._role_name in name + + query = query.where(get_role_name) + + if self._auto_role is not None: + auto_roles = self._auto_role.filter(query.select(lambda x: x.auto_role)).select(lambda x: x.auto_role_id) + query = query.where(lambda x: x.auto_role.auto_role_id in auto_roles) return query diff --git a/kdb-bot/src/bot_graphql/model/auto_role.gql b/kdb-bot/src/bot_graphql/model/auto_role.gql index 47ee4e81..345f0df9 100644 --- a/kdb-bot/src/bot_graphql/model/auto_role.gql +++ b/kdb-bot/src/bot_graphql/model/auto_role.gql @@ -6,8 +6,8 @@ type AutoRole implements TableQuery { server: Server - auto_role_rules: [AutoRoleRule] auto_role_rule_count: Int + auto_role_rules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRoleRule] created_at: String modified_at: String diff --git a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql index 27e06ad0..34c9ccdd 100644 --- a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql +++ b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql @@ -2,6 +2,7 @@ type AutoRoleRule implements TableQuery { id: ID emoji_name: String role_id: String + role_name: String auto_role: AutoRole @@ -13,5 +14,6 @@ input AutoRoleRuleFilter { id: ID emoji_name: String role_id: String + role_name: String auto_role: AutoRoleFilter } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/query.gql b/kdb-bot/src/bot_graphql/model/query.gql index 665c72e0..02a63479 100644 --- a/kdb-bot/src/bot_graphql/model/query.gql +++ b/kdb-bot/src/bot_graphql/model/query.gql @@ -2,7 +2,7 @@ type Query { auto_roles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole] auto_role_count: Int - auto_role_rules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRole] + auto_role_rules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRoleRule] auto_role_rule_count: Int clients(filter: ClientFilter, page: Page, sort: Sort): [Client] diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py index 1cbd870c..943e4d8e 100644 --- a/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py +++ b/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py @@ -1,3 +1,5 @@ +from cpl_discord.service import DiscordBotServiceABC + from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.model.auto_role_rule import AutoRoleRule from bot_graphql.abc.data_query_abc import DataQueryABC @@ -6,15 +8,18 @@ from bot_graphql.abc.data_query_abc import DataQueryABC class AutoRoleRuleQuery(DataQueryABC): def __init__( self, + bot: DiscordBotServiceABC, auto_roles: AutoRoleRepositoryABC, ): DataQueryABC.__init__(self, "AutoRoleRule") + self._bot = bot self._auto_roles = auto_roles self.set_field("id", self.resolve_id) self.set_field("emoji_name", self.resolve_emoji_name) self.set_field("role_id", self.resolve_role_id) + self.set_field("role_name", self.resolve_role_name) self.set_field("auto_role", self.resolve_auto_role) @staticmethod @@ -29,5 +34,9 @@ class AutoRoleRuleQuery(DataQueryABC): def resolve_role_id(x: AutoRoleRule, *_): return x.role_id + def resolve_role_name(self, x: AutoRoleRule, *_): + guild = self._bot.get_guild(x.auto_role.server.discord_server_id) + return guild.get_role(x.role_id).name + def resolve_auto_role(self, x: AutoRoleRule, *_): - return self._auto_roles.get_auto_role_by_id(x.auto_role_id) + return self._auto_roles.get_auto_role_by_id(x.auto_role.auto_role_id) From 36fd3c73b9c5597a06746e5261b2209ea609d3ea Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 7 Feb 2023 18:58:09 +0100 Subject: [PATCH 27/36] Added client filter #162 --- .../src/bot_graphql/filter/client_filter.py | 47 +++++++++++++++++++ kdb-bot/src/bot_graphql/model/client.gql | 5 -- kdb-bot/src/bot_graphql/model/server.gql | 2 +- 3 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/filter/client_filter.py diff --git a/kdb-bot/src/bot_graphql/filter/client_filter.py b/kdb-bot/src/bot_graphql/filter/client_filter.py new file mode 100644 index 00000000..2d1fd20a --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/client_filter.py @@ -0,0 +1,47 @@ +from cpl_query.extension import List + +from bot_data.model.client import Client +from bot_graphql.abc.filter_abc import FilterABC + + +class ClientFilter(FilterABC): + def __init__(self): + FilterABC.__init__(self) + + self._id = None + self._discord_id = None + self._name = None + self._server = None + + def from_dict(self, values: dict): + if "id" in values: + self._id = int(values["id"]) + + if "discord_id" in values: + self._id = int(values["discord_id"]) + + if "name" in values: + self._name = values["name"] + + if "server" in values: + from bot_graphql.filter.server_filter import ServerFilter + + server = ServerFilter() + server.from_dict(values["server"]) + self._server = server + + def filter(self, query: List[Client]) -> List[Client]: + if self._id is not None: + query = query.where(lambda x: x.client_id == self._id) + + if self._discord_id is not None: + query = query.where(lambda x: x.client_id == self._discord_id) + + if self._name is not None: + query = query.where(lambda x: self._name.lower() == x.name.lower() or self._name.lower() in x.name.lower()) + + if self._server is not None: + servers = self._server.filter(query.select(lambda x: x.server)).select(lambda x: x.server_id) + query = query.where(lambda x: x.server.server_id in servers) + + return query diff --git a/kdb-bot/src/bot_graphql/model/client.gql b/kdb-bot/src/bot_graphql/model/client.gql index 7330fc6f..284ca0d6 100644 --- a/kdb-bot/src/bot_graphql/model/client.gql +++ b/kdb-bot/src/bot_graphql/model/client.gql @@ -18,10 +18,5 @@ input ClientFilter { id: ID discord_id: String name: String - sent_message_count: Int - received_message_count: Int - deleted_message_count: Int - received_command_count: Int - moved_users_count: Int server: ServerFilter } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/server.gql b/kdb-bot/src/bot_graphql/model/server.gql index a5e8f7b2..1f03360d 100644 --- a/kdb-bot/src/bot_graphql/model/server.gql +++ b/kdb-bot/src/bot_graphql/model/server.gql @@ -6,7 +6,7 @@ type Server implements TableQuery { auto_roles: [AutoRole] auto_role_count: Int - clients: [Client] + clients(filter: ClientFilter, page: Page, sort: Sort): [Client] client_count: Int users(filter: UserFilter): [User] From 12f8f669ed46531f979fe1ad6d0a9b232bc04f6b Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Tue, 7 Feb 2023 19:09:27 +0100 Subject: [PATCH 28/36] Improved filters #162 --- .../bot_graphql/filter/auto_role_rule_filter.py | 9 +++++---- kdb-bot/src/bot_graphql/filter/level_filter.py | 17 +++++++++++++++-- kdb-bot/src/bot_graphql/filter/user_filter.py | 11 +++++++++++ kdb-bot/src/bot_graphql/graphql_module.py | 4 +++- kdb-bot/src/bot_graphql/model/level.gql | 1 + kdb-bot/src/bot_graphql/model/server.gql | 6 +++--- kdb-bot/src/bot_graphql/model/user.gql | 1 + 7 files changed, 39 insertions(+), 10 deletions(-) diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py index 65e54eb0..eb2112e5 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py @@ -1,3 +1,4 @@ +from cpl_core.dependency_injection import ServiceProviderABC from cpl_discord.service import DiscordBotServiceABC from cpl_query.extension import List @@ -6,8 +7,9 @@ from bot_graphql.abc.filter_abc import FilterABC class AutoRoleRuleFilter(FilterABC): - def __init__(self, bot: DiscordBotServiceABC): + def __init__(self, services: ServiceProviderABC, bot: DiscordBotServiceABC): FilterABC.__init__(self) + self._services = services self._bot = bot self._id = None @@ -32,9 +34,8 @@ class AutoRoleRuleFilter(FilterABC): if "auto_role" in values: from bot_graphql.filter.auto_role_filter import AutoRoleFilter - auto_role = AutoRoleFilter(self._bot) - auto_role.from_dict(values["auto_role"]) - self._auto_role = auto_role + self._auto_role: AutoRoleFilter = self._services.get_service(AutoRoleFilter) + self._auto_role.from_dict(values["auto_role"]) def filter(self, query: List[AutoRoleRule]) -> List[AutoRoleRule]: if self._id is not None: diff --git a/kdb-bot/src/bot_graphql/filter/level_filter.py b/kdb-bot/src/bot_graphql/filter/level_filter.py index f4611107..c34ae63f 100644 --- a/kdb-bot/src/bot_graphql/filter/level_filter.py +++ b/kdb-bot/src/bot_graphql/filter/level_filter.py @@ -1,3 +1,4 @@ +from cpl_core.dependency_injection import ServiceProviderABC from cpl_query.extension import List from bot_data.model.level import Level @@ -5,12 +6,14 @@ from bot_graphql.abc.filter_abc import FilterABC class LevelFilter(FilterABC): - def __init__(self): + def __init__(self, services: ServiceProviderABC): FilterABC.__init__(self) + self._services = services + self._id = None self._name = None - self._server_id = None + self._server = None def from_dict(self, values: dict): if "id" in values: @@ -19,6 +22,12 @@ class LevelFilter(FilterABC): if "name" in values: self._name = values["name"] + if "server" in values: + from bot_graphql.filter.server_filter import ServerFilter + + self._server: ServerFilter = self._services.get_service(LevelFilter) + self._server.from_dict(values["server"]) + def filter(self, query: List[Level]) -> List[Level]: if self._id is not None: query = query.where(lambda x: x.id == self._id) @@ -26,4 +35,8 @@ class LevelFilter(FilterABC): if self._name is not None: query = query.where(lambda x: self._name.lower() == x.name.lower() or self._name.lower() in x.name.lower()) + if self._server is not None: + servers = self._server.filter(query.select(lambda x: x.server)).select(lambda x: x.server_id) + query = query.where(lambda x: x.server.server_id in servers) + return query diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index d92531df..8f772152 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -36,18 +36,29 @@ class UserFilter(FilterABC): def from_dict(self, values: dict): if "id" in values: self._id = int(values["id"]) + if "discord_id" in values: self._discord_id = int(values["discord_id"]) + if "name" in values: self._name = values["name"] + if "xp" in values: self._xp = int(values["xp"]) + if "ontime" in values: self._ontime = int(values["ontime"]) + if "level" in values: self._level: LevelFilter = self._services.get_service(LevelFilter) self._level.from_dict(values["level"]) + if "server" in values: + from bot_graphql.filter.server_filter import ServerFilter + + self._server: ServerFilter = self._services.get_service(LevelFilter) + self._server.from_dict(values["server"]) + def filter(self, query: List[User]) -> List[User]: if self._id is not None: query = query.where(lambda x: x.id == self._id) diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 0d419007..9552d6a7 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -10,6 +10,7 @@ from bot_graphql.abc.filter_abc import FilterABC from bot_graphql.abc.query_abc import QueryABC from bot_graphql.filter.auto_role_filter import AutoRoleFilter from bot_graphql.filter.auto_role_rule_filter import AutoRoleRuleFilter +from bot_graphql.filter.client_filter import ClientFilter from bot_graphql.filter.level_filter import LevelFilter from bot_graphql.filter.server_filter import ServerFilter from bot_graphql.filter.user_filter import UserFilter @@ -54,8 +55,9 @@ class GraphQLModule(ModuleABC): services.add_singleton(FilterABC, AutoRoleFilter) services.add_singleton(FilterABC, AutoRoleRuleFilter) + services.add_singleton(FilterABC, ClientFilter) + services.add_singleton(FilterABC, LevelFilter) services.add_singleton(FilterABC, ServerFilter) services.add_singleton(FilterABC, UserFilter) - services.add_singleton(FilterABC, LevelFilter) services.add_transient(SeederService) diff --git a/kdb-bot/src/bot_graphql/model/level.gql b/kdb-bot/src/bot_graphql/model/level.gql index 20588aa6..323ce8a5 100644 --- a/kdb-bot/src/bot_graphql/model/level.gql +++ b/kdb-bot/src/bot_graphql/model/level.gql @@ -14,6 +14,7 @@ type Level implements TableQuery { input LevelFilter { id: ID name: String + server: ServerFilter } type LevelMutation { diff --git a/kdb-bot/src/bot_graphql/model/server.gql b/kdb-bot/src/bot_graphql/model/server.gql index 1f03360d..f3543fd3 100644 --- a/kdb-bot/src/bot_graphql/model/server.gql +++ b/kdb-bot/src/bot_graphql/model/server.gql @@ -3,16 +3,16 @@ type Server implements TableQuery { discord_id: String name: String - auto_roles: [AutoRole] + auto_roles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole] auto_role_count: Int clients(filter: ClientFilter, page: Page, sort: Sort): [Client] client_count: Int - users(filter: UserFilter): [User] + users(filter: UserFilter, page: Page, sort: Sort): [User] user_count: Int - levels: [Level] + levels(filter: LevelFilter, page: Page, sort: Sort): [Level] level_count: Int created_at: String diff --git a/kdb-bot/src/bot_graphql/model/user.gql b/kdb-bot/src/bot_graphql/model/user.gql index 6ec8f992..cf075fa1 100644 --- a/kdb-bot/src/bot_graphql/model/user.gql +++ b/kdb-bot/src/bot_graphql/model/user.gql @@ -25,4 +25,5 @@ input UserFilter { xp: Int ontime: Float level: LevelFilter + server: ServerFilter } \ No newline at end of file From 93c60b9176d743e6c30b8cabcf726a5455533910 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Feb 2023 12:04:40 +0100 Subject: [PATCH 29/36] Improved filters #162 --- kdb-bot/src/bot_graphql/model/user.gql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kdb-bot/src/bot_graphql/model/user.gql b/kdb-bot/src/bot_graphql/model/user.gql index cf075fa1..01c0305d 100644 --- a/kdb-bot/src/bot_graphql/model/user.gql +++ b/kdb-bot/src/bot_graphql/model/user.gql @@ -6,10 +6,10 @@ type User implements TableQuery { ontime: Float level: Level - joined_servers: [UserJoinedServer] + joined_servers(filter: UserJoinedServerFilter, page: Page, sort: Sort): [UserJoinedServer] joined_server_count: Int - joined_voice_channels: [UserJoinedVoiceChannel] + joined_voice_channels(filter: UserJoinedVoiceChannelFilter, page: Page, sort: Sort): [UserJoinedVoiceChannel] joined_voice_channel_count: Int server: Server From fde318b85db86084ede7de7bd1f663bc2177176a Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Feb 2023 12:32:16 +0100 Subject: [PATCH 30/36] Improved filters & queries #162 --- .../filter/auto_role_rule_filter.py | 2 +- kdb-bot/src/bot_graphql/filter/user_filter.py | 6 +- .../filter/user_joined_server_filter.py | 55 ++++++++++++++ .../user_joined_voice_channel_filter.py | 74 +++++++++++++++++++ .../bot_graphql/model/user_joined_server.gql | 2 - .../model/user_joined_voice_channel.gql | 4 +- .../user_joined_voice_channel_query.py | 10 ++- 7 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/filter/user_joined_server_filter.py create mode 100644 kdb-bot/src/bot_graphql/filter/user_joined_voice_channel_filter.py diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py index eb2112e5..ebc89242 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py @@ -47,7 +47,7 @@ class AutoRoleRuleFilter(FilterABC): if self._role_id is not None: query = query.where(lambda x: x.role_id == self._role_id) - if self._role_name is not None: + if self._role_name is not None and self._role_id is not None: def get_role_name(x: AutoRoleRule): guild = self._bot.get_guild(x.auto_role.server.discord_server_id) diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index 8f772152..5c6c3958 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -56,7 +56,7 @@ class UserFilter(FilterABC): if "server" in values: from bot_graphql.filter.server_filter import ServerFilter - self._server: ServerFilter = self._services.get_service(LevelFilter) + self._server: ServerFilter = self._services.get_service(ServerFilter) self._server.from_dict(values["server"]) def filter(self, query: List[User]) -> List[User]: @@ -82,4 +82,8 @@ class UserFilter(FilterABC): levels = self._level.filter(query.select(lambda x: self._levels.get_level(x))).select(lambda x: x.id) query = query.where(lambda x: self._levels.get_level(x).id in levels) + if self._server is not None: + servers = self._server.filter(query.select(lambda x: x.server)).select(lambda x: x.server_id) + query = query.where(lambda x: x.server.server_id in servers) + return query diff --git a/kdb-bot/src/bot_graphql/filter/user_joined_server_filter.py b/kdb-bot/src/bot_graphql/filter/user_joined_server_filter.py new file mode 100644 index 00000000..cdad13db --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/user_joined_server_filter.py @@ -0,0 +1,55 @@ +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_discord.service import DiscordBotServiceABC +from cpl_query.extension import List + +from bot_data.model.user_joined_server import UserJoinedServer +from bot_graphql.abc.filter_abc import FilterABC + + +class UserJoinedServerFilter(FilterABC): + def __init__( + self, + services: ServiceProviderABC, + bot: DiscordBotServiceABC, + ): + FilterABC.__init__(self) + + self._services = services + self._bot = bot + + self._id = None + self._user = None + self._joined_on = None + self._leaved_on = None + + def from_dict(self, values: dict): + if "id" in values: + self._id = int(values["id"]) + + if "user" in values: + from bot_graphql.filter.user_filter import UserFilter + + self._user: UserFilter = self._services.get_service(UserFilter) + self._user.from_dict(values["user"]) + + if "joined_on" in values: + self._joined_on = values["joined_on"] + + if "leaved_on" in values: + self._leaved_on = values["leaved_on"] + + def filter(self, query: List[UserJoinedServer]) -> List[UserJoinedServer]: + if self._id is not None: + query = query.where(lambda x: x.id == self._id) + + if self._user is not None: + users = self._user.filter(query.select(lambda x: x.user)).select(lambda x: x.user_id) + query = query.where(lambda x: x.user.user_id in users) + + if self._joined_on is not None: + query = query.where(lambda x: x.joined_on == self._joined_on or self._joined_on in x.joined_on) + + if self._leaved_on is not None: + query = query.where(lambda x: x.leaved_on == self._leaved_on or self._leaved_on in x.leaved_on) + + return query diff --git a/kdb-bot/src/bot_graphql/filter/user_joined_voice_channel_filter.py b/kdb-bot/src/bot_graphql/filter/user_joined_voice_channel_filter.py new file mode 100644 index 00000000..13a204a7 --- /dev/null +++ b/kdb-bot/src/bot_graphql/filter/user_joined_voice_channel_filter.py @@ -0,0 +1,74 @@ +from cpl_core.dependency_injection import ServiceProviderABC +from cpl_discord.service import DiscordBotServiceABC +from cpl_query.extension import List + +from bot_data.model.user_joined_voice_channel import UserJoinedVoiceChannel +from bot_graphql.abc.filter_abc import FilterABC + + +class UserJoinedVoiceChannelFilter(FilterABC): + def __init__( + self, + services: ServiceProviderABC, + bot: DiscordBotServiceABC, + ): + FilterABC.__init__(self) + + self._services = services + self._bot = bot + + self._id = None + self._channel_id = None + self._channel_name = None + self._user = None + self._joined_on = None + self._leaved_on = None + + def from_dict(self, values: dict): + if "id" in values: + self._id = int(values["id"]) + + if "channel_id" in values: + self._channel_id = int(values["channel_id"]) + + if "channel_name" in values: + self._channel_name = values["channel_name"] + + if "user" in values: + from bot_graphql.filter.user_filter import UserFilter + + self._user: UserFilter = self._services.get_service(UserFilter) + self._user.from_dict(values["user"]) + + if "joined_on" in values: + self._joined_on = values["joined_on"] + + if "leaved_on" in values: + self._leaved_on = values["leaved_on"] + + def filter(self, query: List[UserJoinedVoiceChannel]) -> List[UserJoinedVoiceChannel]: + if self._id is not None: + query = query.where(lambda x: x.id == self._id) + + if self._channel_id is not None: + query = query.where(lambda x: x.dc_channel_id == self._channel_id) + + if self._channel_name is not None and self._channel_id is not None: + + def get_channel_name(x: UserJoinedVoiceChannel): + name = self._bot.get_channel(x.dc_channel_id).name + return name == self._channel_name or self._channel_name in name + + query = query.where(get_channel_name) + + if self._user is not None: + users = self._user.filter(query.select(lambda x: x.user)).select(lambda x: x.user_id) + query = query.where(lambda x: x.user.user_id in users) + + if self._joined_on is not None: + query = query.where(lambda x: x.joined_on == self._joined_on or self._joined_on in x.joined_on) + + if self._leaved_on is not None: + query = query.where(lambda x: x.leaved_on == self._leaved_on or self._leaved_on in x.leaved_on) + + return query diff --git a/kdb-bot/src/bot_graphql/model/user_joined_server.gql b/kdb-bot/src/bot_graphql/model/user_joined_server.gql index 5c145d2e..05cda1b5 100644 --- a/kdb-bot/src/bot_graphql/model/user_joined_server.gql +++ b/kdb-bot/src/bot_graphql/model/user_joined_server.gql @@ -1,7 +1,6 @@ type UserJoinedServer implements TableQuery { id: ID user: User - server: Server joined_on: String leaved_on: String @@ -12,7 +11,6 @@ type UserJoinedServer implements TableQuery { input UserJoinedServerFilter { id: ID user: UserFilter - server: ServerFilter joined_on: String leaved_on: String } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql b/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql index dd340eaf..51f90c53 100644 --- a/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql +++ b/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql @@ -1,8 +1,8 @@ type UserJoinedVoiceChannel implements TableQuery { id: ID channel_id: String + channel_name: String user: User - server: Server joined_on: String leaved_on: String @@ -13,8 +13,8 @@ type UserJoinedVoiceChannel implements TableQuery { input UserJoinedVoiceChannelFilter { id: ID channel_id: String + channel_name: String user: UserFilter - server: ServerFilter joined_on: String leaved_on: String } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py b/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py index f4d18a32..42270c90 100644 --- a/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py @@ -1,13 +1,18 @@ +from cpl_discord.service import DiscordBotServiceABC + from bot_data.model.user_joined_voice_channel import UserJoinedVoiceChannel from bot_graphql.abc.data_query_abc import DataQueryABC class UserJoinedVoiceChannelQuery(DataQueryABC): - def __init__(self): + def __init__(self, bot: DiscordBotServiceABC): DataQueryABC.__init__(self, "UserJoinedVoiceChannel") + self._bot = bot + self.set_field("id", self.resolve_id) self.set_field("channel_id", self.resolve_channel_id) + self.set_field("channel_name", self.resolve_channel_name) self.set_field("user", self.resolve_user) self.set_field("server", self.resolve_server) self.set_field("joined_on", self.resolve_joined_on) @@ -21,6 +26,9 @@ class UserJoinedVoiceChannelQuery(DataQueryABC): def resolve_channel_id(x: UserJoinedVoiceChannel, *_): return x.dc_channel_id + def resolve_channel_name(self, x: UserJoinedVoiceChannel, *_): + return self._bot.get_channel(x.dc_channel_id).name + @staticmethod def resolve_user(x: UserJoinedVoiceChannel, *_): return x.user From 23e238b7d587e5f7be2f50bbb44dda35021ade79 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Feb 2023 12:34:47 +0100 Subject: [PATCH 31/36] Removed server from queries where user set #162 --- kdb-bot/src/bot_graphql/queries/user_joined_server_query.py | 5 ----- .../bot_graphql/queries/user_joined_voice_channel_query.py | 5 ----- 2 files changed, 10 deletions(-) diff --git a/kdb-bot/src/bot_graphql/queries/user_joined_server_query.py b/kdb-bot/src/bot_graphql/queries/user_joined_server_query.py index ef9ebda2..775ecdfc 100644 --- a/kdb-bot/src/bot_graphql/queries/user_joined_server_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_joined_server_query.py @@ -8,7 +8,6 @@ class UserJoinedServerQuery(DataQueryABC): self.set_field("id", self.resolve_id) self.set_field("user", self.resolve_user) - self.set_field("server", self.resolve_server) self.set_field("joined_on", self.resolve_joined_on) self.set_field("leaved_on", self.resolve_leaved_on) @@ -20,10 +19,6 @@ class UserJoinedServerQuery(DataQueryABC): def resolve_user(x: UserJoinedServer, *_): return x.user - @staticmethod - def resolve_server(x: UserJoinedServer, *_): - return x.user.server - @staticmethod def resolve_joined_on(x: UserJoinedServer, *_): return x.joined_on diff --git a/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py b/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py index 42270c90..46790973 100644 --- a/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py @@ -14,7 +14,6 @@ class UserJoinedVoiceChannelQuery(DataQueryABC): self.set_field("channel_id", self.resolve_channel_id) self.set_field("channel_name", self.resolve_channel_name) self.set_field("user", self.resolve_user) - self.set_field("server", self.resolve_server) self.set_field("joined_on", self.resolve_joined_on) self.set_field("leaved_on", self.resolve_leaved_on) @@ -33,10 +32,6 @@ class UserJoinedVoiceChannelQuery(DataQueryABC): def resolve_user(x: UserJoinedVoiceChannel, *_): return x.user - @staticmethod - def resolve_server(x: UserJoinedVoiceChannel, *_): - return x.user.server - @staticmethod def resolve_joined_on(x: UserJoinedVoiceChannel, *_): return x.joined_on From af8c2dea60995fe5f0ea1f06690f6dab01814044 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Feb 2023 17:48:54 +0100 Subject: [PATCH 32/36] Added mutations #162 --- kdb-bot/src/bot_data/model/auto_role.py | 8 +++ kdb-bot/src/bot_data/model/auto_role_rule.py | 8 +++ kdb-bot/src/bot_graphql/model/auto_role.gql | 13 ++++ .../src/bot_graphql/model/auto_role_rule.gql | 13 ++++ kdb-bot/src/bot_graphql/model/level.gql | 1 + kdb-bot/src/bot_graphql/model/mutation.gql | 3 + kdb-bot/src/bot_graphql/model/user.gql | 9 +++ .../mutations/auto_role_mutation.py | 57 ++++++++++++++++++ .../mutations/auto_role_rule_mutation.py | 59 +++++++++++++++++++ .../bot_graphql/mutations/level_mutation.py | 14 ++--- .../bot_graphql/mutations/user_mutation.py | 31 ++++++++++ 11 files changed, 209 insertions(+), 7 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py create mode 100644 kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py create mode 100644 kdb-bot/src/bot_graphql/mutations/user_mutation.py diff --git a/kdb-bot/src/bot_data/model/auto_role.py b/kdb-bot/src/bot_data/model/auto_role.py index 5b2862c8..16287e6e 100644 --- a/kdb-bot/src/bot_data/model/auto_role.py +++ b/kdb-bot/src/bot_data/model/auto_role.py @@ -37,10 +37,18 @@ class AutoRole(TableABC): def discord_channel_id(self) -> int: return self._discord_channel_id + @discord_channel_id.setter + def discord_channel_id(self, value: int): + self._discord_channel_id = value + @property def discord_message_id(self) -> int: return self._discord_message_id + @discord_message_id.setter + def discord_message_id(self, value: int): + self._discord_message_id = value + @staticmethod def get_select_all_string() -> str: return str( diff --git a/kdb-bot/src/bot_data/model/auto_role_rule.py b/kdb-bot/src/bot_data/model/auto_role_rule.py index fc79f622..f7166637 100644 --- a/kdb-bot/src/bot_data/model/auto_role_rule.py +++ b/kdb-bot/src/bot_data/model/auto_role_rule.py @@ -36,10 +36,18 @@ class AutoRoleRule(TableABC): def emoji_name(self) -> str: return self._discord_emoji_name + @emoji_name.setter + def emoji_name(self, value: str): + self._discord_emoji_name = value + @property def role_id(self) -> int: return self._discord_role_id + @role_id.setter + def role_id(self, value: int): + self._discord_role_id = value + @staticmethod def get_select_all_string() -> str: return str( diff --git a/kdb-bot/src/bot_graphql/model/auto_role.gql b/kdb-bot/src/bot_graphql/model/auto_role.gql index 345f0df9..a45c925c 100644 --- a/kdb-bot/src/bot_graphql/model/auto_role.gql +++ b/kdb-bot/src/bot_graphql/model/auto_role.gql @@ -19,4 +19,17 @@ input AutoRoleFilter { channel_name: String message_id: String server: ServerFilter +} + +type AutoRoleMutation { + create_auto_role(input: AutoRoleInput!): AutoRole + update_auto_role(input: AutoRoleInput!): AutoRole + delete_auto_role(id: ID): AutoRole +} + +input AutoRoleInput { + id: ID! + channel_id: String! + message_id: String! + server_id: ID! } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql index 34c9ccdd..18460656 100644 --- a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql +++ b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql @@ -16,4 +16,17 @@ input AutoRoleRuleFilter { role_id: String role_name: String auto_role: AutoRoleFilter +} + +type AutoRoleRuleMutation { + create_auto_role_rule(input: AutoRoleRuleInput!): AutoRoleRule + update_auto_role_rule(input: AutoRoleRuleInput!): AutoRoleRule + delete_auto_role_rule(id: ID): AutoRoleRule +} + +input AutoRoleRuleInput { + id: ID! + emoji_name: String! + role_id: String! + auto_role_id: ID! } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/level.gql b/kdb-bot/src/bot_graphql/model/level.gql index 323ce8a5..cae1e3ab 100644 --- a/kdb-bot/src/bot_graphql/model/level.gql +++ b/kdb-bot/src/bot_graphql/model/level.gql @@ -24,6 +24,7 @@ type LevelMutation { } input LevelInput { + id: ID name: String! color: String! min_xp: Int! diff --git a/kdb-bot/src/bot_graphql/model/mutation.gql b/kdb-bot/src/bot_graphql/model/mutation.gql index 1f3a002a..1eee7819 100644 --- a/kdb-bot/src/bot_graphql/model/mutation.gql +++ b/kdb-bot/src/bot_graphql/model/mutation.gql @@ -1,3 +1,6 @@ type Mutation { + auto_role: AutoRoleMutation + auto_role_rule: AutoRoleRuleMutation level: LevelMutation + user: UserMutation } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/user.gql b/kdb-bot/src/bot_graphql/model/user.gql index 01c0305d..e175dab5 100644 --- a/kdb-bot/src/bot_graphql/model/user.gql +++ b/kdb-bot/src/bot_graphql/model/user.gql @@ -26,4 +26,13 @@ input UserFilter { ontime: Float level: LevelFilter server: ServerFilter +} + +type UserMutation { + update_user(input: UserInput!): User +} + +input UserInput { + id: ID! + xp: Int! } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py b/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py new file mode 100644 index 00000000..29f999b2 --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py @@ -0,0 +1,57 @@ +from cpl_core.database.context import DatabaseContextABC + +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.model.auto_role import AutoRole +from bot_graphql.abc.query_abc import QueryABC + + +class AutoRoleMutation(QueryABC): + def __init__( + self, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC, + db: DatabaseContextABC, + ): + QueryABC.__init__(self, "AutoRoleMutation") + + self._servers = servers + self._auto_roles = auto_roles + self._db = db + + self.set_field("create_auto_role", self.resolve_create_auto_role) + self.set_field("update_auto_role", self.resolve_update_auto_role) + self.set_field("delete_auto_role", self.resolve_delete_auto_role) + + def resolve_create_auto_role(self, *_, input: dict): + auto_role = AutoRole( + self._servers.get_server_by_id(input["server_id"]), input["channel_id"], input["message_id"] + ) + self._auto_roles.add_auto_role(auto_role) + self._db.save_changes() + + def get_new(x: AutoRole): + return ( + x.server.server_id == input["server_id"] + and x.discord_channel_id == input["channel_id"] + and x.discord_message_id == input["message_id"] + ) + + return self._auto_roles.get_auto_roles_by_server_id(auto_role.server.server_id).where(get_new) + + def resolve_update_auto_role(self, *_, input: dict): + auto_role = self._auto_roles.get_auto_role_by_id(input["id"]) + auto_role.discord_channel_id = input["channel_id"] if "channel_id" in input else auto_role.discord_channel_id + auto_role.discord_message_id = input["message_id"] if "message_id" in input else auto_role.discord_message_id + + self._auto_roles.update_auto_role(auto_role) + self._db.save_changes() + + auto_role = self._auto_roles.get_auto_role_by_id(input["id"]) + return auto_role + + def resolve_delete_auto_role(self, *_, id: int): + auto_role = self._auto_roles.get_auto_role_by_id(id) + self._auto_roles.delete_auto_role(auto_role) + self._db.save_changes() + return auto_role diff --git a/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py b/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py new file mode 100644 index 00000000..37052692 --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py @@ -0,0 +1,59 @@ +from cpl_core.database.context import DatabaseContextABC + +from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.model.auto_role_rule import AutoRoleRule +from bot_graphql.abc.query_abc import QueryABC + + +class AutoRoleRuleMutation(QueryABC): + def __init__( + self, + servers: ServerRepositoryABC, + auto_roles: AutoRoleRepositoryABC, + db: DatabaseContextABC, + ): + QueryABC.__init__(self, "AutoRoleRuleMutation") + + self._servers = servers + self._auto_roles = auto_roles + self._db = db + + self.set_field("create_auto_role_rule", self.resolve_create_auto_role_rule) + self.set_field("update_auto_role_rule", self.resolve_update_auto_role_rule) + self.set_field("delete_auto_role_rule", self.resolve_delete_auto_role_rule) + + def resolve_create_auto_role_rule(self, *_, input: dict): + auto_role_rule = AutoRoleRule( + self._auto_roles.get_auto_role_by_id(input["auto_role_id"]), input["emoji_name"], input["role_id"] + ) + self._auto_roles.add_auto_role_rule(auto_role_rule) + self._db.save_changes() + + def get_new(x: AutoRoleRule): + return ( + x.auto_role.auto_role_id == input["auto_role_id"] + and x.emoji_name == input["emoji_name"] + and x.role_id == input["role_id"] + ) + + return self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_rule.auto_role.auto_role_id).where( + get_new + ) + + def resolve_update_auto_role_rule(self, *_, input: dict): + auto_role_rule = self._auto_roles.get_auto_role_rule_by_id(input["id"]) + auto_role_rule.emoji_name = input["emoji_name"] if "emoji_name" in input else auto_role_rule.emoji_name + auto_role_rule.role_id = input["role_id"] if "role_id" in input else auto_role_rule.role_id + + self._auto_roles.update_auto_role_rule(auto_role_rule) + self._db.save_changes() + + auto_role_rule = self._auto_roles.get_auto_role_rule_by_id(input["id"]) + return auto_role_rule + + def resolve_delete_auto_role_rule(self, *_, id: int): + auto_role_rule = self._auto_roles.get_auto_role_rule_by_id(id) + self._auto_roles.delete_auto_role_rule(auto_role_rule) + self._db.save_changes() + return auto_role_rule diff --git a/kdb-bot/src/bot_graphql/mutations/level_mutation.py b/kdb-bot/src/bot_graphql/mutations/level_mutation.py index 083207cc..dd9681fc 100644 --- a/kdb-bot/src/bot_graphql/mutations/level_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/level_mutation.py @@ -20,8 +20,8 @@ class LevelMutation(QueryABC): self._db = db self.set_field("create_level", self.resolve_create_level) - self.set_field("update_level", self.resolve_create_level) - self.set_field("delete_level", self.resolve_create_level) + self.set_field("update_level", self.resolve_update_level) + self.set_field("delete_level", self.resolve_delete_level) def resolve_create_level(self, *_, input: dict): level = Level( @@ -34,7 +34,7 @@ class LevelMutation(QueryABC): self._levels.add_level(level) self._db.save_changes() - def get_new_server(l: Level): + def get_new_level(l: Level): return ( l.name == level.name and l.color == level.color @@ -42,10 +42,10 @@ class LevelMutation(QueryABC): and l.permissions == level.permissions ) - return self._levels.get_levels_by_server_id(level.server.server_id).where(get_new_server) + return self._levels.get_levels_by_server_id(level.server.server_id).where(get_new_level) - def resolve_update_level(self, *_, input): - level = self._levels.get_level_by_id(input.id) + def resolve_update_level(self, *_, input: dict): + level = self._levels.get_level_by_id(input["id"]) level.name = input["name"] if "name" in input else level.name level.color = input["color"] if "color" in input else level.color level.min_xp = input["min_xp"] if "min_xp" in input else level.min_xp @@ -54,7 +54,7 @@ class LevelMutation(QueryABC): self._levels.update_level(level) self._db.save_changes() - level = self._levels.get_level_by_id(input.id) + level = self._levels.get_level_by_id(input["id"]) return level def resolve_delete_level(self, *_, id: int): diff --git a/kdb-bot/src/bot_graphql/mutations/user_mutation.py b/kdb-bot/src/bot_graphql/mutations/user_mutation.py new file mode 100644 index 00000000..38ef276e --- /dev/null +++ b/kdb-bot/src/bot_graphql/mutations/user_mutation.py @@ -0,0 +1,31 @@ +from cpl_core.database.context import DatabaseContextABC + +from bot_data.abc.server_repository_abc import ServerRepositoryABC +from bot_data.abc.user_repository_abc import UserRepositoryABC +from bot_graphql.abc.query_abc import QueryABC + + +class UserMutation(QueryABC): + def __init__( + self, + servers: ServerRepositoryABC, + users: UserRepositoryABC, + db: DatabaseContextABC, + ): + QueryABC.__init__(self, "UserMutation") + + self._servers = servers + self._users = users + self._db = db + + self.set_field("update_user", self.resolve_update_user) + + def resolve_update_user(self, *_, input: dict): + user = self._users.get_user_by_id(input["id"]) + user.xp = input["xp"] if "xp" in input else user.xp + + self._users.update_user(user) + self._db.save_changes() + + user = self._users.get_user_by_id(input["id"]) + return user From 1dfe7edcba8de8625b96c2aab560dae7e1bdcb5c Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Feb 2023 17:50:43 +0100 Subject: [PATCH 33/36] Added services to di #162 --- kdb-bot/src/bot_graphql/graphql_module.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 9552d6a7..2ce333db 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -14,9 +14,14 @@ from bot_graphql.filter.client_filter import ClientFilter from bot_graphql.filter.level_filter import LevelFilter from bot_graphql.filter.server_filter import ServerFilter from bot_graphql.filter.user_filter import UserFilter +from bot_graphql.filter.user_joined_server_filter import UserJoinedServerFilter +from bot_graphql.filter.user_joined_voice_channel_filter import UserJoinedVoiceChannelFilter from bot_graphql.graphql_service import GraphQLService from bot_graphql.mutation import Mutation +from bot_graphql.mutations.auto_role_mutation import AutoRoleMutation +from bot_graphql.mutations.auto_role_rule_mutation import AutoRoleRuleMutation from bot_graphql.mutations.level_mutation import LevelMutation +from bot_graphql.mutations.user_mutation import UserMutation from bot_graphql.queries.auto_role_query import AutoRoleQuery from bot_graphql.queries.auto_role_rule_query import AutoRoleRuleQuery from bot_graphql.queries.client_query import ClientQuery @@ -42,16 +47,15 @@ class GraphQLModule(ModuleABC): services.add_singleton(GraphQLService) services.add_singleton(Query) services.add_singleton(Mutation) - services.add_transient(QueryABC, ServerQuery) + services.add_transient(QueryABC, AutoRoleQuery) + services.add_transient(QueryABC, AutoRoleRuleQuery) + services.add_transient(QueryABC, ClientQuery) services.add_transient(QueryABC, KnownUserQuery) services.add_transient(QueryABC, LevelQuery) - services.add_transient(QueryABC, LevelMutation) - services.add_transient(QueryABC, ClientQuery) + services.add_transient(QueryABC, ServerQuery) services.add_transient(QueryABC, UserQuery) services.add_transient(QueryABC, UserJoinedServerQuery) services.add_transient(QueryABC, UserJoinedVoiceChannelQuery) - services.add_transient(QueryABC, AutoRoleQuery) - services.add_transient(QueryABC, AutoRoleRuleQuery) services.add_singleton(FilterABC, AutoRoleFilter) services.add_singleton(FilterABC, AutoRoleRuleFilter) @@ -59,5 +63,12 @@ class GraphQLModule(ModuleABC): services.add_singleton(FilterABC, LevelFilter) services.add_singleton(FilterABC, ServerFilter) services.add_singleton(FilterABC, UserFilter) + services.add_singleton(FilterABC, UserJoinedServerFilter) + services.add_singleton(FilterABC, UserJoinedVoiceChannelFilter) + + services.add_transient(QueryABC, AutoRoleMutation) + services.add_transient(QueryABC, AutoRoleRuleMutation) + services.add_transient(QueryABC, LevelMutation) + services.add_transient(QueryABC, UserMutation) services.add_transient(SeederService) From 5d470be58395fe55b14954039c836856d5eda8a9 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Feb 2023 19:05:32 +0100 Subject: [PATCH 34/36] Changed from snake to camel case #162 --- .../abc/user_joined_server_repository_abc.py | 5 +++ .../src/bot_data/model/user_joined_server.py | 11 ++++-- .../user_joined_server_repository_service.py | 15 ++++++++ kdb-bot/src/bot_graphql/abc/query_abc.py | 2 +- .../bot_graphql/filter/auto_role_filter.py | 12 +++---- .../filter/auto_role_rule_filter.py | 14 ++++---- .../src/bot_graphql/filter/client_filter.py | 4 +-- kdb-bot/src/bot_graphql/filter/page.py | 8 ++--- .../src/bot_graphql/filter/server_filter.py | 4 +-- kdb-bot/src/bot_graphql/filter/sort.py | 8 ++--- kdb-bot/src/bot_graphql/filter/user_filter.py | 4 +-- .../filter/user_joined_server_filter.py | 8 ++--- .../user_joined_voice_channel_filter.py | 16 ++++----- kdb-bot/src/bot_graphql/graphql_module.py | 4 +++ kdb-bot/src/bot_graphql/model/autoRole.gql | 35 +++++++++++++++++++ .../src/bot_graphql/model/autoRoleRule.gql | 32 +++++++++++++++++ kdb-bot/src/bot_graphql/model/auto_role.gql | 35 ------------------- .../src/bot_graphql/model/auto_role_rule.gql | 32 ----------------- kdb-bot/src/bot_graphql/model/base.gql | 12 +++---- kdb-bot/src/bot_graphql/model/client.gql | 18 +++++----- kdb-bot/src/bot_graphql/model/knownUser.gql | 7 ++++ kdb-bot/src/bot_graphql/model/known_user.gql | 7 ---- kdb-bot/src/bot_graphql/model/level.gql | 16 ++++----- kdb-bot/src/bot_graphql/model/mutation.gql | 4 +-- kdb-bot/src/bot_graphql/model/query.gql | 28 +++++++-------- kdb-bot/src/bot_graphql/model/server.gql | 22 ++++++------ kdb-bot/src/bot_graphql/model/user.gql | 18 +++++----- ...joined_server.gql => userJoinedServer.gql} | 12 +++---- .../model/userJoinedVoiceChannel.gql | 20 +++++++++++ .../model/user_joined_voice_channel.gql | 20 ----------- kdb-bot/src/bot_graphql/mutation.py | 27 +++++++++++++- .../mutations/auto_role_mutation.py | 20 +++++------ .../mutations/auto_role_rule_mutation.py | 18 +++++----- .../bot_graphql/mutations/level_mutation.py | 12 +++---- .../bot_graphql/mutations/user_mutation.py | 2 +- .../bot_graphql/queries/auto_role_query.py | 8 ++--- .../queries/auto_role_rule_query.py | 8 ++--- .../src/bot_graphql/queries/client_query.py | 12 +++---- .../bot_graphql/queries/known_user_query.py | 2 +- .../src/bot_graphql/queries/level_query.py | 2 +- .../src/bot_graphql/queries/server_query.py | 15 +++++--- .../queries/user_joined_server_query.py | 4 +-- .../user_joined_voice_channel_query.py | 8 ++--- kdb-bot/src/bot_graphql/queries/user_query.py | 8 ++--- kdb-bot/src/bot_graphql/query.py | 10 +++--- 45 files changed, 323 insertions(+), 266 deletions(-) create mode 100644 kdb-bot/src/bot_graphql/model/autoRole.gql create mode 100644 kdb-bot/src/bot_graphql/model/autoRoleRule.gql delete mode 100644 kdb-bot/src/bot_graphql/model/auto_role.gql delete mode 100644 kdb-bot/src/bot_graphql/model/auto_role_rule.gql create mode 100644 kdb-bot/src/bot_graphql/model/knownUser.gql delete mode 100644 kdb-bot/src/bot_graphql/model/known_user.gql rename kdb-bot/src/bot_graphql/model/{user_joined_server.gql => userJoinedServer.gql} (51%) create mode 100644 kdb-bot/src/bot_graphql/model/userJoinedVoiceChannel.gql delete mode 100644 kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql diff --git a/kdb-bot/src/bot_data/abc/user_joined_server_repository_abc.py b/kdb-bot/src/bot_data/abc/user_joined_server_repository_abc.py index 2b8dcc5f..cbc3a718 100644 --- a/kdb-bot/src/bot_data/abc/user_joined_server_repository_abc.py +++ b/kdb-bot/src/bot_data/abc/user_joined_server_repository_abc.py @@ -2,6 +2,7 @@ from abc import ABC, abstractmethod from typing import Optional from cpl_query.extension import List + from bot_data.model.user_joined_server import UserJoinedServer @@ -18,6 +19,10 @@ class UserJoinedServerRepositoryABC(ABC): def get_user_joined_server_by_id(self, id: int) -> UserJoinedServer: pass + @abstractmethod + def get_user_joined_server_by_server_id(self, server_id: int) -> UserJoinedServer: + pass + @abstractmethod def get_user_joined_servers_by_user_id(self, user_id: int) -> list[UserJoinedServer]: pass diff --git a/kdb-bot/src/bot_data/model/user_joined_server.py b/kdb-bot/src/bot_data/model/user_joined_server.py index 7c0764d1..134e2f47 100644 --- a/kdb-bot/src/bot_data/model/user_joined_server.py +++ b/kdb-bot/src/bot_data/model/user_joined_server.py @@ -1,10 +1,8 @@ from datetime import datetime -from typing import Optional from cpl_core.database import TableABC from bot_data.model.user import User -from bot_data.model.server import Server class UserJoinedServer(TableABC): @@ -69,6 +67,15 @@ class UserJoinedServer(TableABC): """ ) + @staticmethod + def get_select_by_server_id_string(id: int) -> str: + return str( + f""" + SELECT * FROM `UserJoinedServers` + WHERE `ServerId` = {id}; + """ + ) + @staticmethod def get_select_by_user_id_string(id: int) -> str: return str( diff --git a/kdb-bot/src/bot_data/service/user_joined_server_repository_service.py b/kdb-bot/src/bot_data/service/user_joined_server_repository_service.py index 3c8a9d4b..abd0a17b 100644 --- a/kdb-bot/src/bot_data/service/user_joined_server_repository_service.py +++ b/kdb-bot/src/bot_data/service/user_joined_server_repository_service.py @@ -57,6 +57,21 @@ class UserJoinedServerRepositoryService(UserJoinedServerRepositoryABC): id=result[0], ) + def get_user_joined_server_by_server_id(self, server_id: int) -> UserJoinedServer: + self._logger.trace( + __name__, + f"Send SQL command: {UserJoinedServer.get(id)}", + ) + result = self._context.select(UserJoinedServer.get_select_by_id_string(id))[0] + return UserJoinedServer( + self._users.get_user_by_id(result[1]), + result[2], + result[3], + result[4], + result[5], + id=result[0], + ) + def get_user_joined_servers_by_user_id(self, user_id: int) -> List[UserJoinedServer]: joins = List(UserJoinedServer) self._logger.trace( diff --git a/kdb-bot/src/bot_graphql/abc/query_abc.py b/kdb-bot/src/bot_graphql/abc/query_abc.py index e52e927a..4e291ae4 100644 --- a/kdb-bot/src/bot_graphql/abc/query_abc.py +++ b/kdb-bot/src/bot_graphql/abc/query_abc.py @@ -34,7 +34,7 @@ class QueryABC(ObjectType): return self._resolve_collection(get_collection(*args), *args, **kwargs) self.set_field(f"{name}s", wrapper) - self.set_field(f"{name}_count", lambda *args: get_collection(*args).count()) + self.set_field(f"{name}Count", lambda *args: get_collection(*args).count()) # @FilterABC.resolve_filter_annotation def _resolve_collection(self, collection: List, *_, filter: FilterABC = None, page: Page = None, sort: Sort = None): diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_filter.py index 8badeea9..ca02a7d4 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_filter.py @@ -24,14 +24,14 @@ class AutoRoleFilter(FilterABC): if "id" in values: self._id = int(values["id"]) - if "channel_id" in values: - self._channel_id = int(values["channel_id"]) + if "channelId" in values: + self._channel_id = int(values["channelId"]) - if "channel_name" in values: - self._channel_name = values["channel_name"] + if "channelName" in values: + self._channel_name = values["channelName"] - if "message_id" in values: - self._message_id = int(values["message_id"]) + if "messageId" in values: + self._message_id = int(values["messageId"]) if "server" in values: from bot_graphql.filter.server_filter import ServerFilter diff --git a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py index ebc89242..44651957 100644 --- a/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py +++ b/kdb-bot/src/bot_graphql/filter/auto_role_rule_filter.py @@ -22,16 +22,16 @@ class AutoRoleRuleFilter(FilterABC): if "id" in values: self._id = int(values["id"]) - if "emoji_name" in values: - self._emoji_name = values["emoji_name"] + if "emojiName" in values: + self._emoji_name = values["emojiName"] - if "role_id" in values: - self._role_id = int(values["role_id"]) + if "roleId" in values: + self._role_id = int(values["roleId"]) - if "role_name" in values: - self._role_name = values["role_name"] + if "roleName" in values: + self._role_name = values["roleName"] - if "auto_role" in values: + if "autoRole" in values: from bot_graphql.filter.auto_role_filter import AutoRoleFilter self._auto_role: AutoRoleFilter = self._services.get_service(AutoRoleFilter) diff --git a/kdb-bot/src/bot_graphql/filter/client_filter.py b/kdb-bot/src/bot_graphql/filter/client_filter.py index 2d1fd20a..be81e742 100644 --- a/kdb-bot/src/bot_graphql/filter/client_filter.py +++ b/kdb-bot/src/bot_graphql/filter/client_filter.py @@ -17,8 +17,8 @@ class ClientFilter(FilterABC): if "id" in values: self._id = int(values["id"]) - if "discord_id" in values: - self._id = int(values["discord_id"]) + if "discordId" in values: + self._id = int(values["discordId"]) if "name" in values: self._name = values["name"] diff --git a/kdb-bot/src/bot_graphql/filter/page.py b/kdb-bot/src/bot_graphql/filter/page.py index 60978d31..b54080c9 100644 --- a/kdb-bot/src/bot_graphql/filter/page.py +++ b/kdb-bot/src/bot_graphql/filter/page.py @@ -10,11 +10,11 @@ class Page(FilterABC): self._page_size = None def from_dict(self, values: dict): - if "page_index" in values: - self._page_index = int(values["page_index"]) + if "pageIndex" in values: + self._page_index = int(values["pageIndex"]) - if "page_size" in values: - self._page_size = int(values["page_size"]) + if "pageSize" in values: + self._page_size = int(values["pageSize"]) def filter(self, query: List, *args) -> List: if self._page_size is not None and self._page_index is not None: diff --git a/kdb-bot/src/bot_graphql/filter/server_filter.py b/kdb-bot/src/bot_graphql/filter/server_filter.py index 2ae039a0..2964d1d7 100644 --- a/kdb-bot/src/bot_graphql/filter/server_filter.py +++ b/kdb-bot/src/bot_graphql/filter/server_filter.py @@ -19,8 +19,8 @@ class ServerFilter(FilterABC): if "id" in values: self._id = int(values["id"]) - if "discord_id" in values: - self._discord_id = int(values["discord_id"]) + if "discordId" in values: + self._discord_id = int(values["discordId"]) if "name" in values: self._name = values["name"] diff --git a/kdb-bot/src/bot_graphql/filter/sort.py b/kdb-bot/src/bot_graphql/filter/sort.py index cb431a39..f1c966dd 100644 --- a/kdb-bot/src/bot_graphql/filter/sort.py +++ b/kdb-bot/src/bot_graphql/filter/sort.py @@ -10,11 +10,11 @@ class Sort(FilterABC): self._sort_column = None def from_dict(self, values: dict): - if "sort_direction" in values: - self._sort_direction = values["sort_direction"] + if "sortDirection" in values: + self._sort_direction = values["sortDirection"] - if "sort_column" in values: - self._sort_column = values["sort_column"] + if "sortColumn" in values: + self._sort_column = values["sortColumn"] def filter(self, query: List, *args) -> List: return query diff --git a/kdb-bot/src/bot_graphql/filter/user_filter.py b/kdb-bot/src/bot_graphql/filter/user_filter.py index 5c6c3958..b0028674 100644 --- a/kdb-bot/src/bot_graphql/filter/user_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_filter.py @@ -37,8 +37,8 @@ class UserFilter(FilterABC): if "id" in values: self._id = int(values["id"]) - if "discord_id" in values: - self._discord_id = int(values["discord_id"]) + if "discordId" in values: + self._discord_id = int(values["discordId"]) if "name" in values: self._name = values["name"] diff --git a/kdb-bot/src/bot_graphql/filter/user_joined_server_filter.py b/kdb-bot/src/bot_graphql/filter/user_joined_server_filter.py index cdad13db..30af37fa 100644 --- a/kdb-bot/src/bot_graphql/filter/user_joined_server_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_joined_server_filter.py @@ -32,11 +32,11 @@ class UserJoinedServerFilter(FilterABC): self._user: UserFilter = self._services.get_service(UserFilter) self._user.from_dict(values["user"]) - if "joined_on" in values: - self._joined_on = values["joined_on"] + if "joinedOn" in values: + self._joined_on = values["joinedOn"] - if "leaved_on" in values: - self._leaved_on = values["leaved_on"] + if "leavedOn" in values: + self._leaved_on = values["leavedOn"] def filter(self, query: List[UserJoinedServer]) -> List[UserJoinedServer]: if self._id is not None: diff --git a/kdb-bot/src/bot_graphql/filter/user_joined_voice_channel_filter.py b/kdb-bot/src/bot_graphql/filter/user_joined_voice_channel_filter.py index 13a204a7..bdbddeab 100644 --- a/kdb-bot/src/bot_graphql/filter/user_joined_voice_channel_filter.py +++ b/kdb-bot/src/bot_graphql/filter/user_joined_voice_channel_filter.py @@ -28,11 +28,11 @@ class UserJoinedVoiceChannelFilter(FilterABC): if "id" in values: self._id = int(values["id"]) - if "channel_id" in values: - self._channel_id = int(values["channel_id"]) + if "channelId" in values: + self._channel_id = int(values["channelId"]) - if "channel_name" in values: - self._channel_name = values["channel_name"] + if "channelName" in values: + self._channel_name = values["channelName"] if "user" in values: from bot_graphql.filter.user_filter import UserFilter @@ -40,11 +40,11 @@ class UserJoinedVoiceChannelFilter(FilterABC): self._user: UserFilter = self._services.get_service(UserFilter) self._user.from_dict(values["user"]) - if "joined_on" in values: - self._joined_on = values["joined_on"] + if "joinedOn" in values: + self._joined_on = values["joinedOn"] - if "leaved_on" in values: - self._leaved_on = values["leaved_on"] + if "leavedOn" in values: + self._leaved_on = values["leavedOn"] def filter(self, query: List[UserJoinedVoiceChannel]) -> List[UserJoinedVoiceChannel]: if self._id is not None: diff --git a/kdb-bot/src/bot_graphql/graphql_module.py b/kdb-bot/src/bot_graphql/graphql_module.py index 2ce333db..43da66e0 100644 --- a/kdb-bot/src/bot_graphql/graphql_module.py +++ b/kdb-bot/src/bot_graphql/graphql_module.py @@ -47,6 +47,8 @@ class GraphQLModule(ModuleABC): services.add_singleton(GraphQLService) services.add_singleton(Query) services.add_singleton(Mutation) + + # queries services.add_transient(QueryABC, AutoRoleQuery) services.add_transient(QueryABC, AutoRoleRuleQuery) services.add_transient(QueryABC, ClientQuery) @@ -57,6 +59,7 @@ class GraphQLModule(ModuleABC): services.add_transient(QueryABC, UserJoinedServerQuery) services.add_transient(QueryABC, UserJoinedVoiceChannelQuery) + # filters services.add_singleton(FilterABC, AutoRoleFilter) services.add_singleton(FilterABC, AutoRoleRuleFilter) services.add_singleton(FilterABC, ClientFilter) @@ -66,6 +69,7 @@ class GraphQLModule(ModuleABC): services.add_singleton(FilterABC, UserJoinedServerFilter) services.add_singleton(FilterABC, UserJoinedVoiceChannelFilter) + # mutations services.add_transient(QueryABC, AutoRoleMutation) services.add_transient(QueryABC, AutoRoleRuleMutation) services.add_transient(QueryABC, LevelMutation) diff --git a/kdb-bot/src/bot_graphql/model/autoRole.gql b/kdb-bot/src/bot_graphql/model/autoRole.gql new file mode 100644 index 00000000..2680e264 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/autoRole.gql @@ -0,0 +1,35 @@ +type AutoRole implements TableQuery { + id: ID + channelId: String + channelName: String + messageId: String + + server: Server + + autoRoleRuleCount: Int + autoRoleRules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRoleRule] + + createdAt: String + modifiedAt: String +} + +input AutoRoleFilter { + id: ID + channelId: String + channelName: String + messageId: String + server: ServerFilter +} + +type AutoRoleMutation { + createAutoRole(input: AutoRoleInput!): AutoRole + updateAutoRole(input: AutoRoleInput!): AutoRole + deleteAutoRole(id: ID): AutoRole +} + +input AutoRoleInput { + id: ID! + channelId: String! + messageId: String! + serverId: ID! +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/autoRoleRule.gql b/kdb-bot/src/bot_graphql/model/autoRoleRule.gql new file mode 100644 index 00000000..31e87177 --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/autoRoleRule.gql @@ -0,0 +1,32 @@ +type AutoRoleRule implements TableQuery { + id: ID + emojiName: String + roleId: String + roleName: String + + autoRole: AutoRole + + createdAt: String + modifiedAt: String +} + +input AutoRoleRuleFilter { + id: ID + emojiName: String + roleId: String + roleName: String + autoRole: AutoRoleFilter +} + +type AutoRoleRuleMutation { + createAutoRoleRule(input: AutoRoleRuleInput!): AutoRoleRule + updateAutoRoleRule(input: AutoRoleRuleInput!): AutoRoleRule + deleteAutoRoleRule(id: ID): AutoRoleRule +} + +input AutoRoleRuleInput { + id: ID! + emojiName: String! + roleId: String! + autoRoleId: ID! +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/auto_role.gql b/kdb-bot/src/bot_graphql/model/auto_role.gql deleted file mode 100644 index a45c925c..00000000 --- a/kdb-bot/src/bot_graphql/model/auto_role.gql +++ /dev/null @@ -1,35 +0,0 @@ -type AutoRole implements TableQuery { - id: ID - channel_id: String - channel_name: String - message_id: String - - server: Server - - auto_role_rule_count: Int - auto_role_rules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRoleRule] - - created_at: String - modified_at: String -} - -input AutoRoleFilter { - id: ID - channel_id: String - channel_name: String - message_id: String - server: ServerFilter -} - -type AutoRoleMutation { - create_auto_role(input: AutoRoleInput!): AutoRole - update_auto_role(input: AutoRoleInput!): AutoRole - delete_auto_role(id: ID): AutoRole -} - -input AutoRoleInput { - id: ID! - channel_id: String! - message_id: String! - server_id: ID! -} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql b/kdb-bot/src/bot_graphql/model/auto_role_rule.gql deleted file mode 100644 index 18460656..00000000 --- a/kdb-bot/src/bot_graphql/model/auto_role_rule.gql +++ /dev/null @@ -1,32 +0,0 @@ -type AutoRoleRule implements TableQuery { - id: ID - emoji_name: String - role_id: String - role_name: String - - auto_role: AutoRole - - created_at: String - modified_at: String -} - -input AutoRoleRuleFilter { - id: ID - emoji_name: String - role_id: String - role_name: String - auto_role: AutoRoleFilter -} - -type AutoRoleRuleMutation { - create_auto_role_rule(input: AutoRoleRuleInput!): AutoRoleRule - update_auto_role_rule(input: AutoRoleRuleInput!): AutoRoleRule - delete_auto_role_rule(id: ID): AutoRoleRule -} - -input AutoRoleRuleInput { - id: ID! - emoji_name: String! - role_id: String! - auto_role_id: ID! -} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/base.gql b/kdb-bot/src/bot_graphql/model/base.gql index b2babd53..1ae03045 100644 --- a/kdb-bot/src/bot_graphql/model/base.gql +++ b/kdb-bot/src/bot_graphql/model/base.gql @@ -1,14 +1,14 @@ interface TableQuery { - created_at: String - modified_at: String + createdAt: String + modifiedAt: String } input Page { - page_index: Int - page_size: Int + pageIndex: Int + pageSize: Int } input Sort { - sort_direction: String - sort_column: String + sortDirection: String + sortColumn: String } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/client.gql b/kdb-bot/src/bot_graphql/model/client.gql index 284ca0d6..8afc7c73 100644 --- a/kdb-bot/src/bot_graphql/model/client.gql +++ b/kdb-bot/src/bot_graphql/model/client.gql @@ -1,22 +1,22 @@ type Client implements TableQuery { id: ID - discord_id: String + discordId: String name: String - sent_message_count: Int - received_message_count: Int - deleted_message_count: Int - received_command_count: Int - moved_users_count: Int + sentMessageCount: Int + receivedMessageCount: Int + deletedMessageCount: Int + receivedCommandCount: Int + movedUsersCount: Int server: Server - created_at: String - modified_at: String + createdAt: String + modifiedAt: String } input ClientFilter { id: ID - discord_id: String + discordId: String name: String server: ServerFilter } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/knownUser.gql b/kdb-bot/src/bot_graphql/model/knownUser.gql new file mode 100644 index 00000000..bb96cd3d --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/knownUser.gql @@ -0,0 +1,7 @@ +type KnownUser implements TableQuery { + id: ID + discordId: String + + createdAt: String + modifiedAt: String +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/known_user.gql b/kdb-bot/src/bot_graphql/model/known_user.gql deleted file mode 100644 index fac127c0..00000000 --- a/kdb-bot/src/bot_graphql/model/known_user.gql +++ /dev/null @@ -1,7 +0,0 @@ -type KnownUser implements TableQuery { - id: ID - discord_id: String - - created_at: String - modified_at: String -} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/level.gql b/kdb-bot/src/bot_graphql/model/level.gql index cae1e3ab..93081b7f 100644 --- a/kdb-bot/src/bot_graphql/model/level.gql +++ b/kdb-bot/src/bot_graphql/model/level.gql @@ -2,13 +2,13 @@ type Level implements TableQuery { id: ID name: String color: String - min_xp: Int + minXp: Int permissions: String server: Server - created_at: String - modified_at: String + createdAt: String + modifiedAt: String } input LevelFilter { @@ -18,16 +18,16 @@ input LevelFilter { } type LevelMutation { - create_level(input: LevelInput!): Level - update_level(input: LevelInput!): Level - delete_level(id: ID): Level + createLevel(input: LevelInput!): Level + updateLevel(input: LevelInput!): Level + deleteLevel(id: ID): Level } input LevelInput { id: ID name: String! color: String! - min_xp: Int! + minXp: Int! permissions: String! - server_id: ID! + serverId: ID! } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/mutation.gql b/kdb-bot/src/bot_graphql/model/mutation.gql index 1eee7819..5290dd1c 100644 --- a/kdb-bot/src/bot_graphql/model/mutation.gql +++ b/kdb-bot/src/bot_graphql/model/mutation.gql @@ -1,6 +1,6 @@ type Mutation { - auto_role: AutoRoleMutation - auto_role_rule: AutoRoleRuleMutation + autoRole: AutoRoleMutation + autoRoleRule: AutoRoleRuleMutation level: LevelMutation user: UserMutation } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/query.gql b/kdb-bot/src/bot_graphql/model/query.gql index 02a63479..c28c5c6d 100644 --- a/kdb-bot/src/bot_graphql/model/query.gql +++ b/kdb-bot/src/bot_graphql/model/query.gql @@ -1,28 +1,28 @@ type Query { - auto_roles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole] - auto_role_count: Int + autoRoles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole] + autoRole_count: Int - auto_role_rules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRoleRule] - auto_role_rule_count: Int + autoRoleRules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRoleRule] + autoRoleRuleCount: Int clients(filter: ClientFilter, page: Page, sort: Sort): [Client] - client_count: Int + clientCount: Int - known_users: [KnownUser] - known_user_count: Int + knownUsers: [KnownUser] + knownUserCount: Int levels(filter: LevelFilter, page: Page, sort: Sort): [Level] - level_count: Int + levelCount: Int servers(filter: ServerFilter, page: Page, sort: Sort): [Server] - server_count: Int + serverCount: Int - user_joined_servers(filter: UserJoinedServerFilter, page: Page, sort: Sort): [User] - user_joined_server_count: Int + userJoinedServers(filter: UserJoinedServerFilter, page: Page, sort: Sort): [User] + userJoinedServerCount: Int - user_joined_voice_channels(filter: UserJoinedVoiceChannelFilter, page: Page, sort: Sort): [User] - user_joined_voice_channel_count: Int + userJoinedVoiceChannels(filter: UserJoinedVoiceChannelFilter, page: Page, sort: Sort): [User] + userJoinedVoiceChannelCount: Int users(filter: UserFilter, page: Page, sort: Sort): [User] - user_count: Int + userCount: Int } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/server.gql b/kdb-bot/src/bot_graphql/model/server.gql index f3543fd3..659287f7 100644 --- a/kdb-bot/src/bot_graphql/model/server.gql +++ b/kdb-bot/src/bot_graphql/model/server.gql @@ -1,26 +1,26 @@ type Server implements TableQuery { id: ID - discord_id: String + discordId: String name: String - auto_roles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole] - auto_role_count: Int + autoRoles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole] + autoRoleCount: Int clients(filter: ClientFilter, page: Page, sort: Sort): [Client] - client_count: Int - - users(filter: UserFilter, page: Page, sort: Sort): [User] - user_count: Int + clientCount: Int levels(filter: LevelFilter, page: Page, sort: Sort): [Level] - level_count: Int + levelCount: Int - created_at: String - modified_at: String + users(filter: UserFilter, page: Page, sort: Sort): [User] + userCount: Int + + createdAt: String + modifiedAt: String } input ServerFilter { id: ID - discord_id: String + discordId: String name: String } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/user.gql b/kdb-bot/src/bot_graphql/model/user.gql index e175dab5..6d0ad93a 100644 --- a/kdb-bot/src/bot_graphql/model/user.gql +++ b/kdb-bot/src/bot_graphql/model/user.gql @@ -1,26 +1,26 @@ type User implements TableQuery { id: ID - discord_id: String + discordId: String name: String xp: Int ontime: Float level: Level - joined_servers(filter: UserJoinedServerFilter, page: Page, sort: Sort): [UserJoinedServer] - joined_server_count: Int + joinedServers(filter: UserJoinedServerFilter, page: Page, sort: Sort): [UserJoinedServer] + joinedServerCount: Int - joined_voice_channels(filter: UserJoinedVoiceChannelFilter, page: Page, sort: Sort): [UserJoinedVoiceChannel] - joined_voice_channel_count: Int + joinedVoiceChannels(filter: UserJoinedVoiceChannelFilter, page: Page, sort: Sort): [UserJoinedVoiceChannel] + joinedVoiceChannelCount: Int server: Server - created_at: String - modified_at: String + createdAt: String + modifiedAt: String } input UserFilter { id: ID - discord_id: String + discordId: String name: String xp: Int ontime: Float @@ -29,7 +29,7 @@ input UserFilter { } type UserMutation { - update_user(input: UserInput!): User + updateUser(input: UserInput!): User } input UserInput { diff --git a/kdb-bot/src/bot_graphql/model/user_joined_server.gql b/kdb-bot/src/bot_graphql/model/userJoinedServer.gql similarity index 51% rename from kdb-bot/src/bot_graphql/model/user_joined_server.gql rename to kdb-bot/src/bot_graphql/model/userJoinedServer.gql index 05cda1b5..a8cbb343 100644 --- a/kdb-bot/src/bot_graphql/model/user_joined_server.gql +++ b/kdb-bot/src/bot_graphql/model/userJoinedServer.gql @@ -1,16 +1,16 @@ type UserJoinedServer implements TableQuery { id: ID user: User - joined_on: String - leaved_on: String + joinedOn: String + leavedOn: String - created_at: String - modified_at: String + createdAt: String + modifiedAt: String } input UserJoinedServerFilter { id: ID user: UserFilter - joined_on: String - leaved_on: String + joinedOn: String + leavedOn: String } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/userJoinedVoiceChannel.gql b/kdb-bot/src/bot_graphql/model/userJoinedVoiceChannel.gql new file mode 100644 index 00000000..d49f8c9e --- /dev/null +++ b/kdb-bot/src/bot_graphql/model/userJoinedVoiceChannel.gql @@ -0,0 +1,20 @@ +type UserJoinedVoiceChannel implements TableQuery { + id: ID + channelId: String + channelName: String + user: User + joinedOn: String + leavedOn: String + + createdAt: String + modifiedAt: String +} + +input UserJoinedVoiceChannelFilter { + id: ID + channelId: String + channelName: String + user: UserFilter + joinedOn: String + leavedOn: String +} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql b/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql deleted file mode 100644 index 51f90c53..00000000 --- a/kdb-bot/src/bot_graphql/model/user_joined_voice_channel.gql +++ /dev/null @@ -1,20 +0,0 @@ -type UserJoinedVoiceChannel implements TableQuery { - id: ID - channel_id: String - channel_name: String - user: User - joined_on: String - leaved_on: String - - created_at: String - modified_at: String -} - -input UserJoinedVoiceChannelFilter { - id: ID - channel_id: String - channel_name: String - user: UserFilter - joined_on: String - leaved_on: String -} \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/mutation.py b/kdb-bot/src/bot_graphql/mutation.py index 079dd104..5b980c14 100644 --- a/kdb-bot/src/bot_graphql/mutation.py +++ b/kdb-bot/src/bot_graphql/mutation.py @@ -1,14 +1,39 @@ from ariadne import MutationType +from bot_graphql.mutations.auto_role_mutation import AutoRoleMutation +from bot_graphql.mutations.auto_role_rule_mutation import AutoRoleRuleMutation from bot_graphql.mutations.level_mutation import LevelMutation +from bot_graphql.mutations.user_mutation import UserMutation class Mutation(MutationType): - def __init__(self, level_mutation: LevelMutation): + def __init__( + self, + auto_role_mutation: AutoRoleMutation, + auto_role_rule_mutation: AutoRoleRuleMutation, + level_mutation: LevelMutation, + user_mutation: UserMutation, + ): MutationType.__init__(self) + self._auto_role_mutation = auto_role_mutation + self._auto_role_rule_mutation = auto_role_rule_mutation self._level_mutation = level_mutation + self._user_mutation = user_mutation + + self.set_field("autoRole", self.resolve_auto_role) + self.set_field("autoRoleRule", self.resolve_auto_role_rule) self.set_field("level", self.resolve_level) + self.set_field("user", self.resolve_user) + + def resolve_auto_role(self, *_): + return self._auto_role_mutation + + def resolve_auto_role_rule(self, *_): + return self._auto_role_rule_mutation def resolve_level(self, *_): return self._level_mutation + + def resolve_user(self, *_): + return self._user_mutation diff --git a/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py b/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py index 29f999b2..b5a0cc40 100644 --- a/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py @@ -19,30 +19,28 @@ class AutoRoleMutation(QueryABC): self._auto_roles = auto_roles self._db = db - self.set_field("create_auto_role", self.resolve_create_auto_role) - self.set_field("update_auto_role", self.resolve_update_auto_role) - self.set_field("delete_auto_role", self.resolve_delete_auto_role) + self.set_field("createAutoRole", self.resolve_create_auto_role) + self.set_field("updateAutoRole", self.resolve_update_auto_role) + self.set_field("deleteAutoRole", self.resolve_delete_auto_role) def resolve_create_auto_role(self, *_, input: dict): - auto_role = AutoRole( - self._servers.get_server_by_id(input["server_id"]), input["channel_id"], input["message_id"] - ) + auto_role = AutoRole(self._servers.get_server_by_id(input["serverId"]), input["channelId"], input["messageId"]) self._auto_roles.add_auto_role(auto_role) self._db.save_changes() def get_new(x: AutoRole): return ( - x.server.server_id == input["server_id"] - and x.discord_channel_id == input["channel_id"] - and x.discord_message_id == input["message_id"] + x.server.server_id == input["serverId"] + and x.discord_channel_id == input["channelId"] + and x.discord_message_id == input["messageId"] ) return self._auto_roles.get_auto_roles_by_server_id(auto_role.server.server_id).where(get_new) def resolve_update_auto_role(self, *_, input: dict): auto_role = self._auto_roles.get_auto_role_by_id(input["id"]) - auto_role.discord_channel_id = input["channel_id"] if "channel_id" in input else auto_role.discord_channel_id - auto_role.discord_message_id = input["message_id"] if "message_id" in input else auto_role.discord_message_id + auto_role.discord_channel_id = input["channelId"] if "channelId" in input else auto_role.discord_channel_id + auto_role.discord_message_id = input["messageId"] if "messageId" in input else auto_role.discord_message_id self._auto_roles.update_auto_role(auto_role) self._db.save_changes() diff --git a/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py b/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py index 37052692..89ed0669 100644 --- a/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py @@ -19,22 +19,22 @@ class AutoRoleRuleMutation(QueryABC): self._auto_roles = auto_roles self._db = db - self.set_field("create_auto_role_rule", self.resolve_create_auto_role_rule) - self.set_field("update_auto_role_rule", self.resolve_update_auto_role_rule) - self.set_field("delete_auto_role_rule", self.resolve_delete_auto_role_rule) + self.set_field("createAutoRoleRule", self.resolve_create_auto_role_rule) + self.set_field("updateAutoRoleRule", self.resolve_update_auto_role_rule) + self.set_field("deleteAutoRoleRule", self.resolve_delete_auto_role_rule) def resolve_create_auto_role_rule(self, *_, input: dict): auto_role_rule = AutoRoleRule( - self._auto_roles.get_auto_role_by_id(input["auto_role_id"]), input["emoji_name"], input["role_id"] + self._auto_roles.get_auto_role_by_id(input["autoRoleId"]), input["emojiName"], input["roleId"] ) self._auto_roles.add_auto_role_rule(auto_role_rule) self._db.save_changes() def get_new(x: AutoRoleRule): return ( - x.auto_role.auto_role_id == input["auto_role_id"] - and x.emoji_name == input["emoji_name"] - and x.role_id == input["role_id"] + x.auto_role.auto_role_id == input["autoRoleId"] + and x.emoji_name == input["emojiName"] + and x.role_id == input["roleId"] ) return self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_rule.auto_role.auto_role_id).where( @@ -43,8 +43,8 @@ class AutoRoleRuleMutation(QueryABC): def resolve_update_auto_role_rule(self, *_, input: dict): auto_role_rule = self._auto_roles.get_auto_role_rule_by_id(input["id"]) - auto_role_rule.emoji_name = input["emoji_name"] if "emoji_name" in input else auto_role_rule.emoji_name - auto_role_rule.role_id = input["role_id"] if "role_id" in input else auto_role_rule.role_id + auto_role_rule.emoji_name = input["emojiName"] if "emojiName" in input else auto_role_rule.emoji_name + auto_role_rule.role_id = input["roleId"] if "roleId" in input else auto_role_rule.role_id self._auto_roles.update_auto_role_rule(auto_role_rule) self._db.save_changes() diff --git a/kdb-bot/src/bot_graphql/mutations/level_mutation.py b/kdb-bot/src/bot_graphql/mutations/level_mutation.py index dd9681fc..15940a4f 100644 --- a/kdb-bot/src/bot_graphql/mutations/level_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/level_mutation.py @@ -19,17 +19,17 @@ class LevelMutation(QueryABC): self._levels = levels self._db = db - self.set_field("create_level", self.resolve_create_level) - self.set_field("update_level", self.resolve_update_level) - self.set_field("delete_level", self.resolve_delete_level) + self.set_field("createLevel", self.resolve_create_level) + self.set_field("updateLevel", self.resolve_update_level) + self.set_field("deleteLevel", self.resolve_delete_level) def resolve_create_level(self, *_, input: dict): level = Level( input["name"], input["color"], - int(input["min_xp"]), + int(input["minXp"]), int(input["permissions"]), - self._servers.get_server_by_id(input["server_id"]), + self._servers.get_server_by_id(input["serverId"]), ) self._levels.add_level(level) self._db.save_changes() @@ -48,7 +48,7 @@ class LevelMutation(QueryABC): level = self._levels.get_level_by_id(input["id"]) level.name = input["name"] if "name" in input else level.name level.color = input["color"] if "color" in input else level.color - level.min_xp = input["min_xp"] if "min_xp" in input else level.min_xp + level.min_xp = input["minXp"] if "minXp" in input else level.min_xp level.permissions = input["permissions"] if "permissions" in input else level.permissions self._levels.update_level(level) diff --git a/kdb-bot/src/bot_graphql/mutations/user_mutation.py b/kdb-bot/src/bot_graphql/mutations/user_mutation.py index 38ef276e..1e90cd83 100644 --- a/kdb-bot/src/bot_graphql/mutations/user_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/user_mutation.py @@ -18,7 +18,7 @@ class UserMutation(QueryABC): self._users = users self._db = db - self.set_field("update_user", self.resolve_update_user) + self.set_field("updateUser", self.resolve_update_user) def resolve_update_user(self, *_, input: dict): user = self._users.get_user_by_id(input["id"]) diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_query.py index 16ecfe65..53a2b97c 100644 --- a/kdb-bot/src/bot_graphql/queries/auto_role_query.py +++ b/kdb-bot/src/bot_graphql/queries/auto_role_query.py @@ -21,12 +21,12 @@ class AutoRoleQuery(DataQueryABC): self._servers = servers self.set_field("id", self.resolve_id) - self.set_field("channel_id", self.resolve_channel_id) - self.set_field("channel_name", self.resolve_channel_name) - self.set_field("message_id", self.resolve_message_id) + self.set_field("channelId", self.resolve_channel_id) + self.set_field("channelName", self.resolve_channel_name) + self.set_field("messageId", self.resolve_message_id) self.set_field("server", self.resolve_server) self.add_collection( - "auto_role_rule", lambda x, *_: self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id) + "autoRoleRule", lambda x, *_: self._auto_role_rules.get_auto_role_rules_by_auto_role_id(x.auto_role_id) ) @staticmethod diff --git a/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py b/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py index 943e4d8e..5103aec4 100644 --- a/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py +++ b/kdb-bot/src/bot_graphql/queries/auto_role_rule_query.py @@ -17,10 +17,10 @@ class AutoRoleRuleQuery(DataQueryABC): self._auto_roles = auto_roles self.set_field("id", self.resolve_id) - self.set_field("emoji_name", self.resolve_emoji_name) - self.set_field("role_id", self.resolve_role_id) - self.set_field("role_name", self.resolve_role_name) - self.set_field("auto_role", self.resolve_auto_role) + self.set_field("emojiName", self.resolve_emoji_name) + self.set_field("roleId", self.resolve_role_id) + self.set_field("roleName", self.resolve_role_name) + self.set_field("autoRole", self.resolve_auto_role) @staticmethod def resolve_id(x: AutoRoleRule, *_): diff --git a/kdb-bot/src/bot_graphql/queries/client_query.py b/kdb-bot/src/bot_graphql/queries/client_query.py index 6460dc54..824afa9b 100644 --- a/kdb-bot/src/bot_graphql/queries/client_query.py +++ b/kdb-bot/src/bot_graphql/queries/client_query.py @@ -14,13 +14,13 @@ class ClientQuery(DataQueryABC): self._bot = bot self.set_field("id", self.resolve_id) - self.set_field("discord_id", self.resolve_discord_id) + self.set_field("discordId", self.resolve_discord_id) self.set_field("name", self.resolve_name) - self.set_field("sent_message_count", self.resolve_sent_message_count) - self.set_field("received_message_count", self.resolve_received_message_count) - self.set_field("deleted_message_count", self.resolve_deleted_message_count) - self.set_field("received_command_count", self.resolve_received_command_count) - self.set_field("moved_users_count", self.resolve_moved_users_count) + self.set_field("sentMessageCount", self.resolve_sent_message_count) + self.set_field("receivedMessageCount", self.resolve_received_message_count) + self.set_field("deletedMessageCount", self.resolve_deleted_message_count) + self.set_field("receivedCommandCount", self.resolve_received_command_count) + self.set_field("movedUsersCount", self.resolve_moved_users_count) self.set_field("server", self.resolve_server) @staticmethod diff --git a/kdb-bot/src/bot_graphql/queries/known_user_query.py b/kdb-bot/src/bot_graphql/queries/known_user_query.py index 4de78ba7..243a6745 100644 --- a/kdb-bot/src/bot_graphql/queries/known_user_query.py +++ b/kdb-bot/src/bot_graphql/queries/known_user_query.py @@ -7,7 +7,7 @@ class KnownUserQuery(DataQueryABC): DataQueryABC.__init__(self, "KnownUser") self.set_field("id", self.resolve_id) - self.set_field("discord_id", self.resolve_discord_id) + self.set_field("discordId", self.resolve_discord_id) @staticmethod def resolve_id(x: KnownUser, *_): diff --git a/kdb-bot/src/bot_graphql/queries/level_query.py b/kdb-bot/src/bot_graphql/queries/level_query.py index bf492895..a0f22453 100644 --- a/kdb-bot/src/bot_graphql/queries/level_query.py +++ b/kdb-bot/src/bot_graphql/queries/level_query.py @@ -9,7 +9,7 @@ class LevelQuery(DataQueryABC): self.set_field("id", self.resolve_id) self.set_field("name", self.resolve_name) self.set_field("color", self.resolve_color) - self.set_field("min_xp", self.resolve_min_xp) + self.set_field("minXp", self.resolve_min_xp) self.set_field("permissions", self.resolve_permissions) self.set_field("server", self.resolve_server) diff --git a/kdb-bot/src/bot_graphql/queries/server_query.py b/kdb-bot/src/bot_graphql/queries/server_query.py index ed817290..88dfe324 100644 --- a/kdb-bot/src/bot_graphql/queries/server_query.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -3,6 +3,8 @@ from cpl_discord.service import DiscordBotServiceABC from bot_data.abc.auto_role_repository_abc import AutoRoleRepositoryABC from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.level_repository_abc import LevelRepositoryABC +from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC +from bot_data.abc.user_joined_voice_channel_repository_abc import UserJoinedVoiceChannelRepositoryABC from bot_data.abc.user_repository_abc import UserRepositoryABC from bot_data.model.server import Server from bot_graphql.abc.data_query_abc import DataQueryABC @@ -15,28 +17,31 @@ class ServerQuery(DataQueryABC): bot: DiscordBotServiceABC, auto_roles: AutoRoleRepositoryABC, clients: ClientRepositoryABC, - users: UserRepositoryABC, levels: LevelRepositoryABC, + users: UserRepositoryABC, + ujs: UserJoinedServerRepositoryABC, + ujvs: UserJoinedVoiceChannelRepositoryABC, ): DataQueryABC.__init__(self, "Server") self._bot = bot self._auto_roles = auto_roles - self._users = users self._clients = clients - self._users = users self._levels = levels + self._users = users + self._ujs = ujs + self._ujvs = ujvs self.set_field("id", self.resolve_id) - self.set_field("discord_id", self.resolve_discord_id) + self.set_field("discordId", self.resolve_discord_id) self.set_field("name", self.resolve_name) self.add_collection( "auto_role", lambda server, *_: self._auto_roles.get_auto_roles_by_server_id(server.server_id) ) self.add_collection("client", lambda server, *_: self._clients.get_clients_by_server_id(server.server_id)) - self.add_collection("user", lambda server, *_: self._users.get_users_by_server_id(server.server_id), UserFilter) self.add_collection("level", lambda server, *_: self._levels.get_levels_by_server_id(server.server_id)) + self.add_collection("user", lambda server, *_: self._users.get_users_by_server_id(server.server_id), UserFilter) @staticmethod def resolve_id(server: Server, *_): diff --git a/kdb-bot/src/bot_graphql/queries/user_joined_server_query.py b/kdb-bot/src/bot_graphql/queries/user_joined_server_query.py index 775ecdfc..db18781a 100644 --- a/kdb-bot/src/bot_graphql/queries/user_joined_server_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_joined_server_query.py @@ -8,8 +8,8 @@ class UserJoinedServerQuery(DataQueryABC): self.set_field("id", self.resolve_id) self.set_field("user", self.resolve_user) - self.set_field("joined_on", self.resolve_joined_on) - self.set_field("leaved_on", self.resolve_leaved_on) + self.set_field("joinedOn", self.resolve_joined_on) + self.set_field("leavedOn", self.resolve_leaved_on) @staticmethod def resolve_id(x: UserJoinedServer, *_): diff --git a/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py b/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py index 46790973..99bcc0c3 100644 --- a/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_joined_voice_channel_query.py @@ -11,11 +11,11 @@ class UserJoinedVoiceChannelQuery(DataQueryABC): self._bot = bot self.set_field("id", self.resolve_id) - self.set_field("channel_id", self.resolve_channel_id) - self.set_field("channel_name", self.resolve_channel_name) + self.set_field("channelId", self.resolve_channel_id) + self.set_field("channelName", self.resolve_channel_name) self.set_field("user", self.resolve_user) - self.set_field("joined_on", self.resolve_joined_on) - self.set_field("leaved_on", self.resolve_leaved_on) + self.set_field("joinedOn", self.resolve_joined_on) + self.set_field("leavedOn", self.resolve_leaved_on) @staticmethod def resolve_id(x: UserJoinedVoiceChannel, *_): diff --git a/kdb-bot/src/bot_graphql/queries/user_query.py b/kdb-bot/src/bot_graphql/queries/user_query.py index 17d8e04d..7bca0379 100644 --- a/kdb-bot/src/bot_graphql/queries/user_query.py +++ b/kdb-bot/src/bot_graphql/queries/user_query.py @@ -26,16 +26,14 @@ class UserQuery(DataQueryABC): self._ujvs = ujvs self.set_field("id", self.resolve_id) - self.set_field("discord_id", self.resolve_discord_id) + self.set_field("discordId", self.resolve_discord_id) self.set_field("name", self.resolve_name) self.set_field("xp", self.resolve_xp) self.set_field("ontime", self.resolve_ontime) self.set_field("level", self.resolve_level) + self.add_collection("joinedServer", lambda user, *_: self._ujs.get_user_joined_servers_by_user_id(user.user_id)) self.add_collection( - "joined_server", lambda user, *_: self._ujs.get_user_joined_servers_by_user_id(user.user_id) - ) - self.add_collection( - "joined_voice_channel", lambda user, *_: self._ujvs.get_user_joined_voice_channels_by_user_id(user.user_id) + "joinedVoiceChannel", lambda user, *_: self._ujvs.get_user_joined_voice_channels_by_user_id(user.user_id) ) self.set_field("server", self.resolve_server) diff --git a/kdb-bot/src/bot_graphql/query.py b/kdb-bot/src/bot_graphql/query.py index 687aafc4..e5fd5803 100644 --- a/kdb-bot/src/bot_graphql/query.py +++ b/kdb-bot/src/bot_graphql/query.py @@ -36,14 +36,14 @@ class Query(QueryABC): self._user_joined_voice_channels = user_joined_voice_channel self._users = users - self.add_collection("auto_role", lambda *_: self._auto_roles.get_auto_roles(), AutoRoleFilter) - self.add_collection("auto_role_rule", lambda *_: self._auto_roles.get_auto_role_rules(), AutoRoleRuleFilter) + self.add_collection("autoRole", lambda *_: self._auto_roles.get_auto_roles(), AutoRoleFilter) + self.add_collection("autoRoleRule", lambda *_: self._auto_roles.get_auto_role_rules(), AutoRoleRuleFilter) self.add_collection("client", lambda *_: self._clients.get_clients()) - self.add_collection("known_user", lambda *_: self._known_users.get_users()) + self.add_collection("knowUser", lambda *_: self._known_users.get_users()) self.add_collection("level", lambda *_: self._levels.get_levels(), LevelFilter) self.add_collection("server", lambda *_: self._servers.get_servers(), ServerFilter) - self.add_collection("user_joined_server", lambda *_: self._user_joined_servers.get_user_joined_servers()) + self.add_collection("userJoinedServer", lambda *_: self._user_joined_servers.get_user_joined_servers()) self.add_collection( - "user_joined_voice_channel", lambda *_: self._user_joined_voice_channels.get_user_joined_voice_channels() + "userJoinedVoiceChannel", lambda *_: self._user_joined_voice_channels.get_user_joined_voice_channels() ) self.add_collection("user", lambda *_: self._users.get_users(), UserFilter) From e9559dec08818ebfa3c551cae960ca4cda893ad4 Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Feb 2023 19:08:12 +0100 Subject: [PATCH 35/36] Fixed queries #162 --- kdb-bot/src/bot_graphql/abc/data_query_abc.py | 4 ++-- kdb-bot/src/bot_graphql/model/query.gql | 18 +++++++++--------- kdb-bot/src/bot_graphql/model/server.gql | 8 ++++---- .../src/bot_graphql/queries/server_query.py | 2 +- kdb-bot/src/bot_graphql/query.py | 2 +- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/kdb-bot/src/bot_graphql/abc/data_query_abc.py b/kdb-bot/src/bot_graphql/abc/data_query_abc.py index 9ab28106..7603a4d2 100644 --- a/kdb-bot/src/bot_graphql/abc/data_query_abc.py +++ b/kdb-bot/src/bot_graphql/abc/data_query_abc.py @@ -7,8 +7,8 @@ class DataQueryABC(QueryABC): def __init__(self, name: str): QueryABC.__init__(self, name) - self.set_field("created_at", self.resolve_created_at) - self.set_field("modified_at", self.resolve_modified_at) + self.set_field("createdAt", self.resolve_created_at) + self.set_field("modifiedAt", self.resolve_modified_at) @staticmethod def resolve_created_at(entry: TableABC, *_): diff --git a/kdb-bot/src/bot_graphql/model/query.gql b/kdb-bot/src/bot_graphql/model/query.gql index c28c5c6d..46f80381 100644 --- a/kdb-bot/src/bot_graphql/model/query.gql +++ b/kdb-bot/src/bot_graphql/model/query.gql @@ -1,28 +1,28 @@ type Query { + autoRoleCount: Int autoRoles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole] - autoRole_count: Int - autoRoleRules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRoleRule] autoRoleRuleCount: Int + autoRoleRules(filter: AutoRoleRuleFilter, page: Page, sort: Sort): [AutoRoleRule] - clients(filter: ClientFilter, page: Page, sort: Sort): [Client] clientCount: Int + clients(filter: ClientFilter, page: Page, sort: Sort): [Client] - knownUsers: [KnownUser] knownUserCount: Int + knownUsers: [KnownUser] - levels(filter: LevelFilter, page: Page, sort: Sort): [Level] levelCount: Int + levels(filter: LevelFilter, page: Page, sort: Sort): [Level] - servers(filter: ServerFilter, page: Page, sort: Sort): [Server] serverCount: Int + servers(filter: ServerFilter, page: Page, sort: Sort): [Server] - userJoinedServers(filter: UserJoinedServerFilter, page: Page, sort: Sort): [User] userJoinedServerCount: Int + userJoinedServers(filter: UserJoinedServerFilter, page: Page, sort: Sort): [User] - userJoinedVoiceChannels(filter: UserJoinedVoiceChannelFilter, page: Page, sort: Sort): [User] userJoinedVoiceChannelCount: Int + userJoinedVoiceChannels(filter: UserJoinedVoiceChannelFilter, page: Page, sort: Sort): [User] - users(filter: UserFilter, page: Page, sort: Sort): [User] userCount: Int + users(filter: UserFilter, page: Page, sort: Sort): [User] } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/server.gql b/kdb-bot/src/bot_graphql/model/server.gql index 659287f7..04047892 100644 --- a/kdb-bot/src/bot_graphql/model/server.gql +++ b/kdb-bot/src/bot_graphql/model/server.gql @@ -3,17 +3,17 @@ type Server implements TableQuery { discordId: String name: String - autoRoles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole] autoRoleCount: Int + autoRoles(filter: AutoRoleFilter, page: Page, sort: Sort): [AutoRole] - clients(filter: ClientFilter, page: Page, sort: Sort): [Client] clientCount: Int + clients(filter: ClientFilter, page: Page, sort: Sort): [Client] - levels(filter: LevelFilter, page: Page, sort: Sort): [Level] levelCount: Int + levels(filter: LevelFilter, page: Page, sort: Sort): [Level] - users(filter: UserFilter, page: Page, sort: Sort): [User] userCount: Int + users(filter: UserFilter, page: Page, sort: Sort): [User] createdAt: String modifiedAt: String diff --git a/kdb-bot/src/bot_graphql/queries/server_query.py b/kdb-bot/src/bot_graphql/queries/server_query.py index 88dfe324..33fff047 100644 --- a/kdb-bot/src/bot_graphql/queries/server_query.py +++ b/kdb-bot/src/bot_graphql/queries/server_query.py @@ -37,7 +37,7 @@ class ServerQuery(DataQueryABC): self.set_field("name", self.resolve_name) self.add_collection( - "auto_role", lambda server, *_: self._auto_roles.get_auto_roles_by_server_id(server.server_id) + "autoRole", lambda server, *_: self._auto_roles.get_auto_roles_by_server_id(server.server_id) ) self.add_collection("client", lambda server, *_: self._clients.get_clients_by_server_id(server.server_id)) self.add_collection("level", lambda server, *_: self._levels.get_levels_by_server_id(server.server_id)) diff --git a/kdb-bot/src/bot_graphql/query.py b/kdb-bot/src/bot_graphql/query.py index e5fd5803..9c574ebc 100644 --- a/kdb-bot/src/bot_graphql/query.py +++ b/kdb-bot/src/bot_graphql/query.py @@ -39,7 +39,7 @@ class Query(QueryABC): self.add_collection("autoRole", lambda *_: self._auto_roles.get_auto_roles(), AutoRoleFilter) self.add_collection("autoRoleRule", lambda *_: self._auto_roles.get_auto_role_rules(), AutoRoleRuleFilter) self.add_collection("client", lambda *_: self._clients.get_clients()) - self.add_collection("knowUser", lambda *_: self._known_users.get_users()) + self.add_collection("knownUser", lambda *_: self._known_users.get_users()) self.add_collection("level", lambda *_: self._levels.get_levels(), LevelFilter) self.add_collection("server", lambda *_: self._servers.get_servers(), ServerFilter) self.add_collection("userJoinedServer", lambda *_: self._user_joined_servers.get_user_joined_servers()) From b33c5dc9bfbd4d6094c0f6b7d8af4ee8d0545b0d Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 8 Feb 2023 19:24:07 +0100 Subject: [PATCH 36/36] Fixed mutations #162 --- kdb-bot/src/bot_graphql/model/autoRole.gql | 8 ++++---- kdb-bot/src/bot_graphql/model/autoRoleRule.gql | 8 ++++---- kdb-bot/src/bot_graphql/model/level.gql | 10 +++++----- kdb-bot/src/bot_graphql/model/user.gql | 4 ++-- .../src/bot_graphql/mutations/auto_role_mutation.py | 2 +- .../bot_graphql/mutations/auto_role_rule_mutation.py | 6 ++++-- kdb-bot/src/bot_graphql/mutations/level_mutation.py | 2 +- 7 files changed, 21 insertions(+), 19 deletions(-) diff --git a/kdb-bot/src/bot_graphql/model/autoRole.gql b/kdb-bot/src/bot_graphql/model/autoRole.gql index 2680e264..0b523583 100644 --- a/kdb-bot/src/bot_graphql/model/autoRole.gql +++ b/kdb-bot/src/bot_graphql/model/autoRole.gql @@ -28,8 +28,8 @@ type AutoRoleMutation { } input AutoRoleInput { - id: ID! - channelId: String! - messageId: String! - serverId: ID! + id: ID + channelId: String + messageId: String + serverId: ID } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/autoRoleRule.gql b/kdb-bot/src/bot_graphql/model/autoRoleRule.gql index 31e87177..2484c8bd 100644 --- a/kdb-bot/src/bot_graphql/model/autoRoleRule.gql +++ b/kdb-bot/src/bot_graphql/model/autoRoleRule.gql @@ -25,8 +25,8 @@ type AutoRoleRuleMutation { } input AutoRoleRuleInput { - id: ID! - emojiName: String! - roleId: String! - autoRoleId: ID! + id: ID + emojiName: String + roleId: String + autoRoleId: ID } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/level.gql b/kdb-bot/src/bot_graphql/model/level.gql index 93081b7f..2ef07540 100644 --- a/kdb-bot/src/bot_graphql/model/level.gql +++ b/kdb-bot/src/bot_graphql/model/level.gql @@ -25,9 +25,9 @@ type LevelMutation { input LevelInput { id: ID - name: String! - color: String! - minXp: Int! - permissions: String! - serverId: ID! + name: String + color: String + minXp: Int + permissions: String + serverId: ID } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/model/user.gql b/kdb-bot/src/bot_graphql/model/user.gql index 6d0ad93a..07f7acc4 100644 --- a/kdb-bot/src/bot_graphql/model/user.gql +++ b/kdb-bot/src/bot_graphql/model/user.gql @@ -33,6 +33,6 @@ type UserMutation { } input UserInput { - id: ID! - xp: Int! + id: ID + xp: Int } \ No newline at end of file diff --git a/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py b/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py index b5a0cc40..283b6105 100644 --- a/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/auto_role_mutation.py @@ -35,7 +35,7 @@ class AutoRoleMutation(QueryABC): and x.discord_message_id == input["messageId"] ) - return self._auto_roles.get_auto_roles_by_server_id(auto_role.server.server_id).where(get_new) + return self._auto_roles.get_auto_roles_by_server_id(auto_role.server.server_id).where(get_new).last() def resolve_update_auto_role(self, *_, input: dict): auto_role = self._auto_roles.get_auto_role_by_id(input["id"]) diff --git a/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py b/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py index 89ed0669..34935bbf 100644 --- a/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/auto_role_rule_mutation.py @@ -37,8 +37,10 @@ class AutoRoleRuleMutation(QueryABC): and x.role_id == input["roleId"] ) - return self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_rule.auto_role.auto_role_id).where( - get_new + return ( + self._auto_roles.get_auto_role_rules_by_auto_role_id(auto_role_rule.auto_role.auto_role_id) + .where(get_new) + .last() ) def resolve_update_auto_role_rule(self, *_, input: dict): diff --git a/kdb-bot/src/bot_graphql/mutations/level_mutation.py b/kdb-bot/src/bot_graphql/mutations/level_mutation.py index 15940a4f..0557c968 100644 --- a/kdb-bot/src/bot_graphql/mutations/level_mutation.py +++ b/kdb-bot/src/bot_graphql/mutations/level_mutation.py @@ -42,7 +42,7 @@ class LevelMutation(QueryABC): and l.permissions == level.permissions ) - return self._levels.get_levels_by_server_id(level.server.server_id).where(get_new_level) + return self._levels.get_levels_by_server_id(level.server.server_id).where(get_new_level).last() def resolve_update_level(self, *_, input: dict): level = self._levels.get_level_by_id(input["id"])