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