diff --git a/lib/src/init.dart b/lib/src/init.dart index 201db41..46d900f 100644 --- a/lib/src/init.dart +++ b/lib/src/init.dart @@ -1,5 +1,6 @@ import 'package:injector/injector.dart'; import 'package:nyxx/nyxx.dart'; +import 'package:running_on_dart/running_on_dart.dart'; import 'package:running_on_dart/src/modules/bot_start_duration.dart'; import 'package:running_on_dart/src/modules/docs.dart'; import 'package:running_on_dart/src/modules/emoji_react_module.dart'; @@ -55,7 +56,8 @@ Future setupContainer(NyxxGateway client) async { ..registerSingleton(() => MentionsMonitoringModule()) ..registerSingleton(() => KavitaModule()) ..registerSingleton(() => EmojiReactModule()) - ..registerSingleton(() => BotInfoService()); + ..registerSingleton(() => BotInfoService()) + ..registerDependency(() => client.options.plugins.whereType().first); await Injector.appInstance.get().init(); await Injector.appInstance.get().init(); diff --git a/lib/src/web_app/api_server.dart b/lib/src/web_app/api_server.dart index b42a108..407ebbe 100644 --- a/lib/src/web_app/api_server.dart +++ b/lib/src/web_app/api_server.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:io'; import 'package:injector/injector.dart'; import 'package:nyxx/nyxx.dart'; @@ -8,6 +9,7 @@ import 'package:running_on_dart/running_on_dart.dart'; import 'package:running_on_dart/src/modules/tag.dart'; import 'package:running_on_dart/src/repository/feature_settings.dart'; import 'package:running_on_dart/src/web_app/mustache.dart'; +import 'package:running_on_dart/src/web_app/session_manager_plugin.dart'; import 'package:running_on_dart/src/web_app/utils.dart'; import 'package:running_on_dart/src/services/bot_info.dart'; import 'package:shelf_cors_headers/shelf_cors_headers.dart'; @@ -25,7 +27,19 @@ final clientSecret = getEnv('DISCORD_CLIENT_SECRET'); final clientRedirectUri = getEnv('DISCORD_REDIRECT_URI'); class WebServer { - final Logger _logger = Logger('WebServer'); + final Logger _logger = Logger('ROD.WebServer'); + + Future _handleSessions(shelf.Request request) async { + if (!isAdminFromSession(request)) { + return shelf.Response.forbidden(null); + } + + final sessions = jsonDecode(await File(sessionsFile).readAsString()) as Map; + + return MustacheResponse(name: "sessions.html", parameters: { + 'sessions': sessions.values.toList(), + }); + } Future _handleGuilds(shelf.Request request) async { if (!isAdminFromSession(request)) { @@ -43,7 +57,9 @@ class WebServer { .fold(0, (previous, channel) => previous + channel.messages.cache.length); final enabledFeatures = - await Injector.appInstance.get().fetchSettingsForGuild(entry.id); + (await Injector.appInstance.get().fetchSettingsForGuild(entry.id)) + .map((s) => s.setting.name) + .join(", "); final tagsCount = tagModule.getGuildTags(entry.id).length; @@ -56,7 +72,7 @@ class WebServer { 'cached_channels': guildChannels.length, 'cached_messages': guildCachedMessages, 'cached_roles': entry.roles.cache.length, - 'enabled_features': enabledFeatures.map((s) => s.setting.name).join(", "), + 'enabled_features': enabledFeatures.isNotEmpty ? enabledFeatures : "None enabled", 'tags_count': tagsCount, }; }); @@ -110,6 +126,7 @@ class WebServer { Future _setupRouter() async { return shelf_router.Router() ..get("/", _sessionAware(_processMustache(_handleIndex))) + ..get('/sessions', _sessionAware(_processMustache(_handleSessions))) ..get("/guilds", _sessionAware(_processMustache(_handleGuilds))) ..get("/redirect", _sessionAware(_handleRedirect)) ..get("/logout", _sessionAware(_handleLogOut)); diff --git a/lib/src/web_app/session_manager_plugin.dart b/lib/src/web_app/session_manager_plugin.dart index f2da484..ed5bddb 100644 --- a/lib/src/web_app/session_manager_plugin.dart +++ b/lib/src/web_app/session_manager_plugin.dart @@ -4,34 +4,34 @@ import 'dart:io'; import 'package:nyxx/nyxx.dart'; import 'package:shelf_session/session_middleware.dart'; -class SessionManagerPlugin extends NyxxPlugin { - static const _sessionsFile = '/sessions/sessions.json'; +const sessionsFile = '/sessions/sessions.json'; - void _saveSessions() async { +class SessionManagerPlugin extends NyxxPlugin { + Future triggerSaveSessions() async { await saveSessions((sessionData) async { logger.info("Saving session file"); - await File(_sessionsFile).writeAsString(sessionData); + await File(sessionsFile).writeAsString(sessionData); }); } @override FutureOr afterConnect(NyxxGateway client) async { await restoreSessions(() async { - final file = File(_sessionsFile); + final file = File(sessionsFile); if (await file.exists()) { logger.info("Loading session file."); - return File(_sessionsFile).readAsString(); + return File(sessionsFile).readAsString(); } logger.info("Session file missing. Returning default"); return '{}'; }); - Timer.periodic(Duration(minutes: 15), (timer) => _saveSessions()); + Timer.periodic(Duration(minutes: 15), (timer) => triggerSaveSessions()); } @override FutureOr afterClose() async { - _saveSessions(); + triggerSaveSessions(); } } diff --git a/lib/src/web_app/utils.dart b/lib/src/web_app/utils.dart index 0e5960d..fabb233 100644 --- a/lib/src/web_app/utils.dart +++ b/lib/src/web_app/utils.dart @@ -1,5 +1,6 @@ import 'dart:convert'; +import 'package:injector/injector.dart'; import 'package:nyxx/nyxx.dart'; import 'package:running_on_dart/running_on_dart.dart'; import 'package:shelf/shelf.dart' as shelf; @@ -33,6 +34,8 @@ void initSession(shelf.Request request, Map userDataJson) { session.data['is_admin'] = adminIds.contains(Snowflake.parse(userId)); session.expires = DateTime.now().add(Duration(days: 3)); + + Injector.appInstance.get().triggerSaveSessions(); } void deleteSession(shelf.Request request) { diff --git a/templates/component/navigation.html b/templates/component/navigation.html index 2ab9a4f..c55cf39 100644 --- a/templates/component/navigation.html +++ b/templates/component/navigation.html @@ -10,9 +10,8 @@ Home {{#is_admin}} - + + {{/is_admin}}