diff --git a/kdb-bot/src/bot_graphql/abc/query_abc.py b/kdb-bot/src/bot_graphql/abc/query_abc.py index 09cc3414..b9e9f444 100644 --- a/kdb-bot/src/bot_graphql/abc/query_abc.py +++ b/kdb-bot/src/bot_graphql/abc/query_abc.py @@ -50,15 +50,15 @@ class QueryABC(ObjectType): sort.from_dict(kwargs["sort"]) kwargs["sort"] = sort - collection = get_collection(*args) + collection: List = get_collection(*args) user = Route.get_user() if user == "system" or user.auth_role == AuthRoleEnum.admin: return self._resolve_collection(collection, *args, **kwargs) - for x in collection: + for x in collection.to_list(): if not self._can_user_see_element(user, x): - return List() + collection.remove(x) return self._resolve_collection(collection, *args, **kwargs) diff --git a/kdb-bot/src/modules/database/database_on_ready_event.py b/kdb-bot/src/modules/database/database_on_ready_event.py index 08aa8177..72f35442 100644 --- a/kdb-bot/src/modules/database/database_on_ready_event.py +++ b/kdb-bot/src/modules/database/database_on_ready_event.py @@ -11,6 +11,7 @@ from bot_core.logging.database_logger import DatabaseLogger from bot_core.pipes.date_time_offset_pipe import DateTimeOffsetPipe from bot_data.abc.client_repository_abc import ClientRepositoryABC from bot_data.abc.known_user_repository_abc import KnownUserRepositoryABC +from bot_data.abc.user_joined_game_server_repository_abc import UserJoinedGameServerRepositoryABC from bot_data.abc.user_joined_server_repository_abc import UserJoinedServerRepositoryABC from bot_data.abc.user_joined_voice_channel_repository_abc import ( UserJoinedVoiceChannelRepositoryABC, @@ -41,6 +42,7 @@ class DatabaseOnReadyEvent(OnReadyABC): known_users: KnownUserRepositoryABC, user_joins: UserJoinedServerRepositoryABC, user_joins_vc: UserJoinedVoiceChannelRepositoryABC, + user_joined_gs: UserJoinedGameServerRepositoryABC, dtp: DateTimeOffsetPipe, ): self._config = config @@ -55,6 +57,7 @@ class DatabaseOnReadyEvent(OnReadyABC): self._known_users = known_users self._user_joins = user_joins self._user_joins_vc = user_joins_vc + self._user_joined_gs = user_joined_gs self._dtp = dtp OnReadyABC.__init__(self) @@ -327,6 +330,48 @@ class DatabaseOnReadyEvent(OnReadyABC): except Exception as e: self._logger.error(__name__, f"Cannot get UserJoinedVoiceChannel", e) + def _check_user_joined_gs(self): + self._logger.debug(__name__, f"Start checking UserJoinedVoiceChannel table") + for guild in self._bot.guilds: + guild: discord.Guild = guild + + server = self._servers.find_server_by_discord_id(guild.id) + if server is None: + self._logger.fatal(__name__, f"Server not found in database: {guild.id}") + + try: + for member in guild.members: + if member.bot: + self._logger.trace(__name__, f"User {member.id} is ignored, because its a bot") + continue + + user = self._users.find_user_by_discord_id_and_server_id(member.id, server.server_id) + if user is None: + self._logger.fatal(__name__, f"User not found in database: {member.id}") + + joins = self._user_joined_gs.find_active_user_joined_game_servers_by_user_id(user.user_id) + if joins is None or len(joins) == 0: + continue + + for join in joins: + self._logger.warn( + __name__, + f"Active UserJoinedGameServer found in database: {guild.id}:{member.id}@{join.joined_on}", + ) + join.leaved_on = datetime.now() + settings: BaseServerSettings = self._config.get_configuration(f"BaseServerSettings_{guild.id}") + + if ( + (join.leaved_on - join.joined_on).total_seconds() / 60 / 60 + ) > settings.max_voice_state_hours: + join.leaved_on = join.joined_on + timedelta(hours=settings.max_voice_state_hours) + + self._user_joined_gs.update_user_joined_game_server(join) + # todo: maybe add XP + self._db_context.save_changes() + except Exception as e: + self._logger.error(__name__, f"Cannot get UserJoinedVoiceChannel", e) + async def on_ready(self): self._logger.debug(__name__, f"Module {type(self)} started") @@ -338,6 +383,7 @@ class DatabaseOnReadyEvent(OnReadyABC): self._check_users() self._check_user_joins() self._check_user_joins_vc() + self._check_user_joined_gs() self._validate_init_time() self._logger.trace(__name__, f"Module {type(self)} stopped")