Skip to content

Commit

Permalink
chore: use stdout for default command output
Browse files Browse the repository at this point in the history
Signed-off-by: The one with the braid <[email protected]>
  • Loading branch information
TheOneWithTheBraid committed Oct 14, 2024
1 parent 9217165 commit b0fab79
Showing 1 changed file with 83 additions and 15 deletions.
98 changes: 83 additions & 15 deletions lib/src/utils/commands_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,27 @@ extension CommandsClientExtension on Client {
throw CommandException('You must enter a valid mxid when using /dm');
}

return await args.client.startDirectChat(
final roomId = await args.client.startDirectChat(
mxid,
enableEncryption: !parts.any((part) => part == '--no-encryption'),
);
stdout?.write(DefaultCommandOutput(
rooms: [roomId],
users: [mxid],
).toString());
return null;
});
addCommand('create', (args, stdout) async {
final groupName = args.msg.replaceFirst('--no-encryption', '').trim();

final parts = args.msg.split(' ');

return await args.client.createGroupChat(
final roomId = await args.client.createGroupChat(
groupName: groupName,
enableEncryption: !parts.any((part) => part == '--no-encryption'),
);
stdout?.write(DefaultCommandOutput(rooms: [roomId]).toString());
return null;
});
addCommand('plain', (args, stdout) async {
final room = args.room;
Expand Down Expand Up @@ -202,7 +209,8 @@ extension CommandsClientExtension on Client {
return await room.sendReaction(inReplyTo.eventId, reaction);
});
addCommand('join', (args, stdout) async {
await args.client.joinRoom(args.msg);
final roomId = await args.client.joinRoom(args.msg);
stdout?.write(DefaultCommandOutput(rooms: [roomId]).toString());
return null;
});
addCommand('leave', (args, stdout) async {
Expand All @@ -219,16 +227,16 @@ extension CommandsClientExtension on Client {
throw RoomCommandException();
}
final parts = args.msg.split(' ');
print(parts);
if (parts.isEmpty || !parts.first.isValidMatrixId) {
throw CommandException('You must enter a valid mxid when using /op');
}
int? pl;
if (parts.length >= 2) {
pl = int.tryParse(parts[1]);
if (pl == null)
if (pl == null) {
throw CommandException(
'Invalid power level ${parts[1]} when using /op');
}
}
final mxid = parts.first;
return await room.setPower(mxid, pl ?? 50);
Expand All @@ -244,6 +252,7 @@ extension CommandsClientExtension on Client {
throw CommandException('You must enter a valid mxid when using /kick');
}
await room.kick(mxid);
stdout?.write(DefaultCommandOutput(users: [mxid]).toString());
return null;
});
addCommand('ban', (args, stdout) async {
Expand All @@ -257,6 +266,7 @@ extension CommandsClientExtension on Client {
throw CommandException('You must enter a valid mxid when using /ban');
}
await room.ban(mxid);
stdout?.write(DefaultCommandOutput(users: [mxid]).toString());
return null;
});
addCommand('unban', (args, stdout) async {
Expand All @@ -270,6 +280,7 @@ extension CommandsClientExtension on Client {
throw CommandException('You must enter a valid mxid when using /unban');
}
await room.unban(mxid);
stdout?.write(DefaultCommandOutput(users: [mxid]).toString());
return null;
});
addCommand('invite', (args, stdout) async {
Expand All @@ -285,6 +296,7 @@ extension CommandsClientExtension on Client {
'You must enter a valid mxid when using /invite');
}
await room.invite(mxid);
stdout?.write(DefaultCommandOutput(users: [mxid]).toString());
return null;
});
addCommand('myroomnick', (args, stdout) async {
Expand Down Expand Up @@ -355,7 +367,8 @@ extension CommandsClientExtension on Client {
throw CommandException('User $mxid is not in this room');
}
await room.addToDirectChat(mxid);
return;
stdout?.write(DefaultCommandOutput(users: [mxid]).toString());
return null;
});
addCommand('markasgroup', (args, stdout) async {
final room = args.room;
Expand Down Expand Up @@ -422,6 +435,7 @@ extension CommandsClientExtension on Client {
throw CommandException('Please provide a User ID');
}
await ignoreUser(mxid);
stdout?.write(DefaultCommandOutput(users: [mxid]).toString());
return null;
});
addCommand('unignore', (args, stdout) async {
Expand All @@ -430,6 +444,7 @@ extension CommandsClientExtension on Client {
throw CommandException('Please provide a User ID');
}
await unignoreUser(mxid);
stdout?.write(DefaultCommandOutput(users: [mxid]).toString());
return null;
});
}
Expand All @@ -445,15 +460,16 @@ class CommandArgs {
String? threadRootEventId;
String? threadLastEventId;

CommandArgs(
{required this.msg,
this.editEventId,
this.inReplyTo,
required this.client,
this.room,
this.txid,
this.threadRootEventId,
this.threadLastEventId});
CommandArgs({
required this.msg,
this.editEventId,
this.inReplyTo,
required this.client,
this.room,
this.txid,
this.threadRootEventId,
this.threadLastEventId,
});
}

class CommandException implements Exception {
Expand All @@ -470,3 +486,55 @@ class CommandException implements Exception {
class RoomCommandException extends CommandException {
const RoomCommandException() : super('This command must run on a room');
}

/// Helper class for normalized command output
///
/// This class can be used to provide a default, processable output of commands
/// containing some generic data.
class DefaultCommandOutput {
static const format = 'com.famedly.default_command_output';
final List<String>? rooms;
final List<String>? events;
final List<String>? users;
final List<String>? messages;
final Map<String, Object?>? custom;

const DefaultCommandOutput({
this.rooms,
this.events,
this.users,
this.messages,
this.custom,
});

static DefaultCommandOutput? fromStdout(String stdout) {
final Object? json = jsonDecode(stdout);
if (json is! Map<String, Object?>) {
return null;
}
if (json['format'] != format) return null;
return DefaultCommandOutput(
rooms: json['rooms'] as List<String>?,
events: json['events'] as List<String>?,
users: json['users'] as List<String>?,
messages: json['messages'] as List<String>?,
custom: json['custom'] as Map<String, Object?>?,
);
}

Map<String, Object?> toJson() {
return {
'format': format,
if (rooms != null) 'rooms': rooms,
if (events != null) 'events': events,
if (users != null) 'users': users,
if (messages != null) 'messages': messages,
...?custom,
};
}

@override
String toString() {
return jsonEncode(toJson());
}
}

0 comments on commit b0fab79

Please sign in to comment.