add internal gomuks command support
This commit is contained in:
parent
3fc33a5ac9
commit
8650c44266
1 changed files with 168 additions and 79 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import "package:collection/collection.dart";
|
||||
import "package:fast_immutable_collections/fast_immutable_collections.dart";
|
||||
import "package:flutter_riverpod/flutter_riverpod.dart";
|
||||
import "package:nexus/controllers/gomuks_commands.dart";
|
||||
import "package:nexus/controllers/rooms.dart";
|
||||
import "package:nexus/helpers/extensions/get_localpart.dart";
|
||||
import "package:nexus/models/bot_command.dart";
|
||||
|
|
@ -10,56 +11,66 @@ import "package:nexus/models/content/membership.dart";
|
|||
import "package:nexus/models/event.dart";
|
||||
|
||||
class BotCommandsController(final String roomId)
|
||||
extends Notifier<IList<BotCommand>> {
|
||||
extends AsyncNotifier<IList<BotCommand>> {
|
||||
@override
|
||||
IList<BotCommand> build() => ref.watch(
|
||||
RoomsController.provider.select((rooms) {
|
||||
final room = rooms[roomId];
|
||||
if (room == null) return const .empty();
|
||||
Future<IList<BotCommand>> build() async {
|
||||
final gomuksCommands = await ref.watch(
|
||||
GomuksCommandsController.provider.future,
|
||||
);
|
||||
|
||||
Content? membership(String userId) =>
|
||||
switch (room.state[EventType.membership.type]?[userId]) {
|
||||
final rowId? => room.events[rowId]?.content,
|
||||
null => null,
|
||||
};
|
||||
final botCommands = ref.watch(
|
||||
RoomsController.provider.select((rooms) {
|
||||
final room = rooms[roomId];
|
||||
if (room == null) return const IList<BotCommand>.empty();
|
||||
|
||||
return (room.state[EventType.botCommandDescription.type]?.values ??
|
||||
const [])
|
||||
.map(
|
||||
(rowId) => switch (room.events[rowId]) {
|
||||
Event(
|
||||
redactedBy: null,
|
||||
:final sender,
|
||||
content: final BotCommandDescriptionContent description,
|
||||
)
|
||||
when description.isValid &&
|
||||
!BotCommand.builtIn.contains(
|
||||
description.command.split(" ").first,
|
||||
) =>
|
||||
switch (membership(sender)) {
|
||||
MembershipContent(
|
||||
status: .join,
|
||||
:final displayName,
|
||||
:final avatarUrl,
|
||||
) =>
|
||||
BotCommand(
|
||||
sender: sender,
|
||||
botName: displayName ?? sender.localpart,
|
||||
botAvatar: avatarUrl,
|
||||
description: description,
|
||||
),
|
||||
_ => null,
|
||||
},
|
||||
_ => null,
|
||||
},
|
||||
)
|
||||
.nonNulls
|
||||
.sortedBy((command) => command.description.command)
|
||||
.toIList();
|
||||
}),
|
||||
);
|
||||
Content? membership(String userId) =>
|
||||
switch (room.state[EventType.membership.type]?[userId]) {
|
||||
final rowId? => room.events[rowId]?.content,
|
||||
null => null,
|
||||
};
|
||||
|
||||
static final provider = NotifierProvider.autoDispose
|
||||
return (room.state[EventType.botCommandDescription.type]?.values ??
|
||||
const [])
|
||||
.map(
|
||||
(rowId) => switch (room.events[rowId]) {
|
||||
Event(
|
||||
redactedBy: null,
|
||||
:final sender,
|
||||
content: final BotCommandDescriptionContent description,
|
||||
)
|
||||
when description.isValid &&
|
||||
!BotCommand.builtIn.contains(
|
||||
description.command.split(" ").first,
|
||||
) =>
|
||||
switch (membership(sender)) {
|
||||
MembershipContent(
|
||||
status: .join,
|
||||
:final displayName,
|
||||
:final avatarUrl,
|
||||
) =>
|
||||
BotCommand(
|
||||
sender: sender,
|
||||
botName: displayName ?? sender.localpart,
|
||||
botAvatar: avatarUrl,
|
||||
description: description,
|
||||
),
|
||||
_ => null,
|
||||
},
|
||||
_ => null,
|
||||
},
|
||||
)
|
||||
.nonNulls
|
||||
.toIList();
|
||||
}),
|
||||
);
|
||||
|
||||
return gomuksCommands
|
||||
.addAll(botCommands)
|
||||
.sortedBy((command) => command.description.command)
|
||||
.toIList();
|
||||
}
|
||||
|
||||
static final provider = AsyncNotifierProvider.autoDispose
|
||||
.family<BotCommandsController, IList<BotCommand>, String>(
|
||||
BotCommandsController.new,
|
||||
);
|
||||
|
|
|
|||
Loading…
Reference in a new issue