GraphQL Part1 (gql endpoint only) #162 #192

Merged
edraft merged 38 commits from #162 into 1.0.0 2023-02-11 10:30:42 +01:00
7 changed files with 146 additions and 7 deletions
Showing only changes of commit fde318b85d - Show all commits

View File

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

View File

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

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 {
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
}

View File

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

View File

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