Improved filters & queries #162

This commit is contained in:
Sven Heidemann 2023-02-08 12:32:16 +01:00
parent 93c60b9176
commit fde318b85d
7 changed files with 146 additions and 7 deletions

View File

@ -47,7 +47,7 @@ class AutoRoleRuleFilter(FilterABC):
if self._role_id is not None: if self._role_id is not None:
query = query.where(lambda x: x.role_id == self._role_id) 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): def get_role_name(x: AutoRoleRule):
guild = self._bot.get_guild(x.auto_role.server.discord_server_id) guild = self._bot.get_guild(x.auto_role.server.discord_server_id)

View File

@ -56,7 +56,7 @@ class UserFilter(FilterABC):
if "server" in values: if "server" in values:
from bot_graphql.filter.server_filter import ServerFilter 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"]) self._server.from_dict(values["server"])
def filter(self, query: List[User]) -> List[User]: 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) 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) 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 return query

View File

@ -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

View File

@ -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

View File

@ -1,7 +1,6 @@
type UserJoinedServer implements TableQuery { type UserJoinedServer implements TableQuery {
id: ID id: ID
user: User user: User
server: Server
joined_on: String joined_on: String
leaved_on: String leaved_on: String
@ -12,7 +11,6 @@ type UserJoinedServer implements TableQuery {
input UserJoinedServerFilter { input UserJoinedServerFilter {
id: ID id: ID
user: UserFilter user: UserFilter
server: ServerFilter
joined_on: String joined_on: String
leaved_on: String leaved_on: String
} }

View File

@ -1,8 +1,8 @@
type UserJoinedVoiceChannel implements TableQuery { type UserJoinedVoiceChannel implements TableQuery {
id: ID id: ID
channel_id: String channel_id: String
channel_name: String
user: User user: User
server: Server
joined_on: String joined_on: String
leaved_on: String leaved_on: String
@ -13,8 +13,8 @@ type UserJoinedVoiceChannel implements TableQuery {
input UserJoinedVoiceChannelFilter { input UserJoinedVoiceChannelFilter {
id: ID id: ID
channel_id: String channel_id: String
channel_name: String
user: UserFilter user: UserFilter
server: ServerFilter
joined_on: String joined_on: String
leaved_on: String leaved_on: String
} }

View File

@ -1,13 +1,18 @@
from cpl_discord.service import DiscordBotServiceABC
from bot_data.model.user_joined_voice_channel import UserJoinedVoiceChannel from bot_data.model.user_joined_voice_channel import UserJoinedVoiceChannel
from bot_graphql.abc.data_query_abc import DataQueryABC from bot_graphql.abc.data_query_abc import DataQueryABC
class UserJoinedVoiceChannelQuery(DataQueryABC): class UserJoinedVoiceChannelQuery(DataQueryABC):
def __init__(self): def __init__(self, bot: DiscordBotServiceABC):
DataQueryABC.__init__(self, "UserJoinedVoiceChannel") DataQueryABC.__init__(self, "UserJoinedVoiceChannel")
self._bot = bot
self.set_field("id", self.resolve_id) self.set_field("id", self.resolve_id)
self.set_field("channel_id", self.resolve_channel_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("user", self.resolve_user)
self.set_field("server", self.resolve_server) self.set_field("server", self.resolve_server)
self.set_field("joined_on", self.resolve_joined_on) self.set_field("joined_on", self.resolve_joined_on)
@ -21,6 +26,9 @@ class UserJoinedVoiceChannelQuery(DataQueryABC):
def resolve_channel_id(x: UserJoinedVoiceChannel, *_): def resolve_channel_id(x: UserJoinedVoiceChannel, *_):
return x.dc_channel_id return x.dc_channel_id
def resolve_channel_name(self, x: UserJoinedVoiceChannel, *_):
return self._bot.get_channel(x.dc_channel_id).name
@staticmethod @staticmethod
def resolve_user(x: UserJoinedVoiceChannel, *_): def resolve_user(x: UserJoinedVoiceChannel, *_):
return x.user return x.user