diff --git a/lib/events.dart b/lib/events.dart index 86226cae6..b633de8ba 100644 --- a/lib/events.dart +++ b/lib/events.dart @@ -36,3 +36,4 @@ export 'package:mineral/src/domains/events/contracts/server/server_roles_update_ export 'package:mineral/src/domains/events/contracts/server/server_stickers_update_event.dart'; export 'package:mineral/src/domains/events/contracts/server/server_text_select_event.dart'; export 'package:mineral/src/domains/events/contracts/server/server_update_event.dart'; +export 'package:mineral/src/domains/events/event.dart'; diff --git a/lib/services.dart b/lib/services.dart index 6c80381d7..cb2b288fb 100644 --- a/lib/services.dart +++ b/lib/services.dart @@ -10,6 +10,7 @@ export 'package:mineral/src/infrastructure/services/http/http_interceptor.dart'; export 'package:mineral/src/infrastructure/services/http/request.dart'; export 'package:mineral/src/infrastructure/services/http/response.dart'; export 'package:mineral/src/infrastructure/services/http/type/response_code.dart'; +export 'package:mineral/src/infrastructure/services/logger/log_level.dart'; export 'package:mineral/src/infrastructure/services/logger/logger.dart'; export 'package:mineral/src/infrastructure/services/placeholder/env_placeholder.dart'; export 'package:mineral/src/infrastructure/services/placeholder/placeholder.dart'; diff --git a/lib/src/domains/events/contracts/private/private_user_select_event.dart b/lib/src/domains/events/contracts/private/private_user_select_event.dart index bb230a7da..95bd79526 100644 --- a/lib/src/domains/events/contracts/private/private_user_select_event.dart +++ b/lib/src/domains/events/contracts/private/private_user_select_event.dart @@ -15,5 +15,5 @@ abstract class PrivateUserSelectEvent implements ListenableEvent { @override String? customId; - FutureOr handle(PrivateSelectContext ctx, List roles); + FutureOr handle(PrivateSelectContext ctx, List users); } diff --git a/lib/src/domains/events/event.dart b/lib/src/domains/events/event.dart index 37d567da4..287fbb568 100644 --- a/lib/src/domains/events/event.dart +++ b/lib/src/domains/events/event.dart @@ -45,53 +45,56 @@ import 'package:mineral/src/domains/events/contracts/server/server_update_event. interface class EventType {} enum Event implements EnhancedEnum, EventType { - ready(ReadyEvent), - serverCreate(ServerCreateEvent), - serverUpdate(ServerUpdateEvent), - serverDelete(ServerDeleteEvent), - serverMessageCreate(ServerMessageCreateEvent), - serverChannelCreate(ServerChannelCreateEvent), - serverChannelUpdate(ServerChannelUpdateEvent), - serverChannelDelete(ServerChannelDeleteEvent), - serverChannelPinsUpdate(ServerChannelPinsUpdateEvent), - privateChannelPinsUpdate(PrivateChannelPinsUpdateEvent), - serverMemberAdd(ServerMemberAddEvent), - serverMemberRemove(ServerMemberRemoveEvent), - serverBanAdd(ServerBanAddEvent), - serverBanRemove(ServerBanRemoveEvent), - serverMemberUpdate(ServerMemberUpdateEvent), - serverPresenceUpdate(ServerPresenceUpdateEvent), - serverEmojisUpdate(ServerEmojisUpdateEvent), - serverStickersUpdate(ServerStickersUpdateEvent), - serverRoleCreate(ServerRolesCreateEvent), - serverRoleUpdate(ServerRolesUpdateEvent), - serverRoleDelete(ServerRolesDeleteEvent), - serverButtonClick(ServerButtonClickEvent), - serverDialogSubmit(ServerDialogSubmitEvent), - serverChannelSelect(ServerChannelSelectEvent), - serverRoleSelect(ServerRoleSelectEvent), - serverMemberSelect(ServerMemberSelectEvent), - serverTextSelect(ServerTextSelectEvent), - serverThreadCreate(ServerThreadCreateEvent), - serverThreadUpdate(ServerThreadUpdateEvent), - serverThreadDelete(ServerThreadDeleteEvent), - serverThreadMemberUpdate(ServerThreadMemberUpdateEvent), - serverThreadMemberAdd(ServerThreadMemberAddEvent), - serverThreadMemberRemove(ServerThreadMemberRemoveEvent), - serverThreadListSync(ServerThreadListSyncEvent), + ready(ReadyEvent, ['Bot bot']), + serverCreate(ServerCreateEvent, ['Server server']), + serverUpdate(ServerUpdateEvent, ['Server before', 'Server after']), + serverDelete(ServerDeleteEvent, ['Server? server']), + serverMessageCreate(ServerMessageCreateEvent, ['ServerMessage message']), + serverChannelCreate(ServerChannelCreateEvent, ['ServerChannel channel']), + serverChannelUpdate(ServerChannelUpdateEvent, ['ServerChannel before', 'ServerChannel after']), + serverChannelDelete(ServerChannelDeleteEvent, ['ServerChannel? channel']), + serverChannelPinsUpdate(ServerChannelPinsUpdateEvent, ['ServerChannel channel']), + privateChannelPinsUpdate(PrivateChannelPinsUpdateEvent, ['PrivateChannel channel']), + serverMemberAdd(ServerMemberAddEvent, ['Member member', 'Server server']), + serverMemberRemove(ServerMemberRemoveEvent, ['Member member']), + serverBanAdd(ServerBanAddEvent, ['ServerBan ban']), + serverBanRemove(ServerBanRemoveEvent, ['ServerBan ban']), + serverMemberUpdate( + ServerMemberUpdateEvent, ['ServerMember? before', 'Member after', 'Server server']), + serverPresenceUpdate(ServerPresenceUpdateEvent, ['Member member', 'Server server', 'Presence presence']), + serverEmojisUpdate(ServerEmojisUpdateEvent, ['EmojiManager emojisManager', 'Server server']), + serverStickersUpdate(ServerStickersUpdateEvent, ['StickerManager stickerManager', 'Server server']), + serverRoleCreate(ServerRolesCreateEvent, ['Role role', 'Server server']), + serverRoleUpdate(ServerRolesUpdateEvent, ['Role before', 'Role after', 'Server server']), + serverRoleDelete(ServerRolesDeleteEvent, ['Role? role', 'Server server']), + serverButtonClick(ServerButtonClickEvent, ['ServerButtonContext ctx']), + serverDialogSubmit(ServerDialogSubmitEvent, ['ServerDialogContext ctx']), + serverChannelSelect(ServerChannelSelectEvent, ['ServerSelectContext ctx', 'List channels']), + serverRoleSelect(ServerRoleSelectEvent, ['ServerSelectContext ctx', 'List roles']), + serverMemberSelect(ServerMemberSelectEvent, ['ServerSelectContext ctx', 'List members']), + serverTextSelect(ServerTextSelectEvent, ['ServerSelectContext ctx', 'List values']), + serverThreadCreate(ServerThreadCreateEvent, ['ThreadChannel channel', 'Server server']), + serverThreadUpdate(ServerThreadUpdateEvent, ['ThreadChannel before', 'ThreadChannel after', 'Server server']), + serverThreadDelete(ServerThreadDeleteEvent, ['ThreadChannel thread', 'Server server']), + serverThreadMemberUpdate(ServerThreadMemberUpdateEvent, ['ThreadChannel thread', 'Server server', 'Member member']), + serverThreadMemberAdd(ServerThreadMemberAddEvent, ['ThreadChannel thread', 'Server server', 'Member member']), + serverThreadMemberRemove(ServerThreadMemberRemoveEvent, ['ThreadChannel thread', 'Server server', 'Member member']), + serverThreadListSync(ServerThreadListSyncEvent, ['List threads', 'Server server']), // private - privateMessageCreate(PrivateMessageCreateEvent), - privateChannelCreate(PrivateChannelCreateEvent), - privateChannelUpdate(PrivateChannelUpdateEvent), - privateChannelDelete(PrivateChannelDeleteEvent), - privateButtonClick(PrivateButtonClickEvent), - privateDialogSubmit(PrivateDialogSubmitEvent), - privateUserSelect(PrivateUserSelectEvent), - privateTextSelect(PrivateTextSelectEvent); + privateMessageCreate(PrivateMessageCreateEvent, ['PrivateMessage message']), + privateChannelCreate(PrivateChannelCreateEvent, ['PrivateChannel channel']), + privateChannelUpdate(PrivateChannelUpdateEvent, ['PrivateChannel before', 'PrivateChannel after']), + privateChannelDelete(PrivateChannelDeleteEvent, ['PrivateChannel? channel']), + privateButtonClick(PrivateButtonClickEvent, ['PrivateButtonContext ctx']), + privateDialogSubmit(PrivateDialogSubmitEvent, ['PrivateDialogContext ctx']), + privateUserSelect(PrivateUserSelectEvent, ['PrivateSelectContext ctx', 'List users']), + privateTextSelect(PrivateTextSelectEvent, ['PrivateSelectContext ctx', 'List values']),; @override final Type value; - const Event(this.value); + final List parameters; + + const Event(this.value, this.parameters); } diff --git a/lib/src/domains/providers/provider.dart b/lib/src/domains/providers/provider.dart index 1f2318c04..cf311b803 100644 --- a/lib/src/domains/providers/provider.dart +++ b/lib/src/domains/providers/provider.dart @@ -1,6 +1,14 @@ import 'dart:async'; -abstract class ProviderContract { +abstract interface class ProviderContract { + FutureOr ready(); + FutureOr dispose(); +} + +abstract class Provider implements ProviderContract { + @override FutureOr ready() {} + + @override FutureOr dispose() {} } diff --git a/lib/src/infrastructure/commons/file.dart b/lib/src/infrastructure/commons/file.dart index a423d932a..ee5f520f3 100644 --- a/lib/src/infrastructure/commons/file.dart +++ b/lib/src/infrastructure/commons/file.dart @@ -48,3 +48,22 @@ extension JsonFile on File { return constructor != null ? constructor(map) : map; } } + +extension YamlWriter on Map { + void writeAsYaml({required StringBuffer buffer, required List payload, int spacing = 2}) { + for (final entry in payload) { + final spaces = ' ' * spacing; + + if (entry.value is String) { + buffer.writeln('$spaces${entry.key}: ${entry.value}'); + } else if (entry.value is Map) { + buffer.writeln('$spaces${entry.key}:'); + + writeAsYaml( + buffer: buffer, + payload: (entry.value as dynamic).entries.toList(), + spacing: spacing + 2); + } + } + } +} diff --git a/lib/src/infrastructure/services/logger/log_level.dart b/lib/src/infrastructure/services/logger/log_level.dart new file mode 100644 index 000000000..66eb75be9 --- /dev/null +++ b/lib/src/infrastructure/services/logger/log_level.dart @@ -0,0 +1,17 @@ +import 'package:logging/logging.dart' as logging; +import 'package:mineral/api.dart'; + +enum LogLevel implements EnhancedEnum { + trace('TRACE', logging.Level.FINEST), + fatal('FATAL', logging.Level.SHOUT), + error('ERROR', logging.Level.SEVERE), + warn('WARN', logging.Level.WARNING), + info('INFO', logging.Level.INFO); + + final String name; + + @override + final logging.Level value; + + const LogLevel(this.name, this.value); +} diff --git a/lib/src/infrastructure/services/logger/logger.dart b/lib/src/infrastructure/services/logger/logger.dart index c02c65fb0..511aaa4ae 100644 --- a/lib/src/infrastructure/services/logger/logger.dart +++ b/lib/src/infrastructure/services/logger/logger.dart @@ -5,6 +5,7 @@ import 'package:logging/logging.dart' as logging; import 'package:mansion/mansion.dart'; import 'package:mineral/src/infrastructure/internals/environment/app_env.dart'; import 'package:mineral/src/infrastructure/internals/environment/environment.dart'; +import 'package:mineral/src/infrastructure/services/logger/log_level.dart'; abstract interface class LoggerContract { void trace(Object message); @@ -20,6 +21,7 @@ abstract interface class LoggerContract { final class Logger implements LoggerContract { static Color get primaryColor => Color.fromRGB(140, 169, 238); + static Color get mutedColor => Color.brightBlack; final EnvContract _env; @@ -29,26 +31,18 @@ final class Logger implements LoggerContract { final level = _env.get(AppEnv.logLevel); final dartEnv = _env.get(AppEnv.dartEnv); - const logLevels = { - 'TRACE': logging.Level.FINEST, - 'FATAL': logging.Level.SHOUT, - 'ERROR': logging.Level.SEVERE, - 'WARN': logging.Level.WARNING, - 'INFO': logging.Level.INFO, - }; - - final bool logLevel = logLevels.keys.contains(level.toUpperCase()); + final bool logLevel = LogLevel.values.map((level) => level.name).contains(level.toUpperCase()); if (!logLevel) { throw Exception( - 'Invalid LOG_LEVEL environment variable, please include in ${logLevels.keys.map((e) => e.toLowerCase())}'); + 'Invalid LOG_LEVEL environment variable, please include in ${LogLevel.values.map((level) => level.name.toLowerCase())}'); } - logging.Logger.root.level = logLevels[level.toUpperCase()]; + logging.Logger.root.level = + LogLevel.values.firstWhere((element) => element.name == level.toUpperCase()).value; logging.Logger.root.onRecord.listen((record) { final time = '[${DateFormat.Hms().format(record.time)}]'; - List makeMessage( - String messageType, Color messageColor, List message) { + List makeMessage(String messageType, Color messageColor, List message) { return [ SetStyles(Style.foreground(Color.brightBlack)), Print(time), @@ -63,18 +57,13 @@ final class Logger implements LoggerContract { } final message = switch (record.level) { - logging.Level.FINEST => makeMessage('trace', Color.white, [ - SetStyles(Style.foreground(Color.brightBlack)), - Print(record.message) - ]), - logging.Level.SHOUT => - makeMessage('fatal', Color.brightRed, [Print(record.message)]), - logging.Level.SEVERE => - makeMessage('error', Color.red, [Print(record.message)]), - logging.Level.WARNING => - makeMessage('warn', Color.yellow, [Print(record.message)]), - logging.Level.INFO => makeMessage( - 'info', Color.fromRGB(140, 169, 238), [Print(record.message)]), + logging.Level.FINEST => makeMessage('trace', Color.white, + [SetStyles(Style.foreground(Color.brightBlack)), Print(record.message)]), + logging.Level.SHOUT => makeMessage('fatal', Color.brightRed, [Print(record.message)]), + logging.Level.SEVERE => makeMessage('error', Color.red, [Print(record.message)]), + logging.Level.WARNING => makeMessage('warn', Color.yellow, [Print(record.message)]), + logging.Level.INFO => + makeMessage('info', Color.fromRGB(140, 169, 238), [Print(record.message)]), _ => makeMessage('unknown', Color.blue, [Print(record.message)]), };