Skip to content

Commit

Permalink
Upate nyxx_commands and improve jellyfin admin check handling (#34)
Browse files Browse the repository at this point in the history
* Update nyxx_commands; Implement jellyfin admin check

* Refactor switch case on error handling
  • Loading branch information
l7ssha authored Oct 19, 2024
1 parent b9c4b82 commit 1317b45
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
14 changes: 12 additions & 2 deletions bin/running_on_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,18 @@ void main() async {
return;
}

if (error is UncaughtException && error.exception is JellyfinConfigNotFoundException) {
error.context.respond(MessageBuilder(content: error.message));
if (error is UncaughtException) {
final context = error.context;

switch (error.exception) {
case JellyfinConfigNotFoundException(:final message):
error.context.respond(MessageBuilder(content: message));
break;
case JellyfinAdminUserRequired _:
context.respond(MessageBuilder(content: "This command can use only logged jellyfin users with administrator privileges."), level: ResponseLevel.private);
break;
case _: print(error.exception);
}
}
});

Expand Down
15 changes: 9 additions & 6 deletions lib/src/commands/jellyfin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ Future<AuthenticatedJellyfinClient> getJellyfinClient(JellyfinConfigUser? config
return Injector.appInstance.get<JellyfinModuleV2>().createJellyfinClientAuthenticated(config);
}

Future<void> ensureAdminJellyfinUser(AuthenticatedJellyfinClient jellyfinClient) async {
final currentUser = await jellyfinClient.getCurrentUser();

if (!(currentUser.policy?.isAdministrator ?? false)) {
throw JellyfinAdminUserRequired();
}
}

final jellyfin = ChatGroup("jellyfin", "Jellyfin Testing Commands", checks: [
jellyfinFeatureEnabledCheck,
], children: [
Expand All @@ -58,13 +66,8 @@ final jellyfin = ChatGroup("jellyfin", "Jellyfin Testing Commands", checks: [
[@Description('Inform user about invitation') User? user,
@Description("Instance to use. Default selected if not provided") JellyfinConfigUser? config]) async {
final jellyfinClient = await getJellyfinClient(config, context);
final currentUser = await jellyfinClient.getCurrentUser();

if (!(currentUser.policy?.isAdministrator ?? false)) {
return context.respond(
MessageBuilder(content: "This command can use only logged jellyfin users with administrator privileges."),
level: ResponseLevel.private);
}
await ensureAdminJellyfinUser(jellyfinClient);

final wizarrClient = await Injector.appInstance.get<JellyfinModuleV2>().fetchGetWizarrClientWithFallback(
originalConfig: jellyfinClient.configUser.config!, parentId: context.guild?.id ?? context.user.id);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/modules/jellyfin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ class JellyfinConfigNotFoundException implements Exception {
String toString() => "JellyfinConfigNotFoundException: $message";
}

class JellyfinAdminUserRequired implements Exception {
@override
String toString() => "JellyfinAdminUserRequired: You don't have admin permissions on jellyfin instance.";
}

class JellyfinModuleV2 implements RequiresInitialization {
final JellyfinConfigRepository _jellyfinConfigRepository = Injector.appInstance.get();
final NyxxGateway _client = Injector.appInstance.get();
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ issue_tracker: https://github.com/nyxx-discord/running_on_dart/issues
publish_to: none

environment:
sdk: '^3.5.0'
sdk: ^3.5.0

dependencies:
http: ^1.0.0
Expand All @@ -18,7 +18,7 @@ dependencies:
migent:
git: https://github.com/nyxx-discord/migent.git
nyxx: ^6.3.0
nyxx_commands: ^6.0.0
nyxx_commands: ^6.0.3
nyxx_extensions: ^4.1.0
postgres: ^3.4.0
fuzzy: ^0.5.1
Expand Down

0 comments on commit 1317b45

Please sign in to comment.