Implement /part command, with or without parameter

This commit is contained in:
Benoit Marty 2021-10-19 11:55:09 +02:00
parent f5eaf2f05f
commit fe2ba28441
5 changed files with 23 additions and 19 deletions

1
changelog.d/2909.feature Normal file
View file

@ -0,0 +1 @@
Implement /part command, with or without parameter

View file

@ -35,7 +35,7 @@ enum class Command(val command: String, val parameters: String, @StringRes val d
ROOM_NAME("/roomname", "<name>", R.string.command_description_room_name, false),
INVITE("/invite", "<user-id> [reason]", R.string.command_description_invite_user, false),
JOIN_ROOM("/join", "<room-address> [reason]", R.string.command_description_join_room, false),
PART("/part", "<room-address> [reason]", R.string.command_description_part_room, false),
PART("/part", "[<room-address>]", R.string.command_description_part_room, false),
TOPIC("/topic", "<topic>", R.string.command_description_topic, false),
KICK_USER("/kick", "<user-id> [reason]", R.string.command_description_kick_user, false),
CHANGE_DISPLAY_NAME("/nick", "<display-name>", R.string.command_description_nick, false),

View file

@ -158,21 +158,10 @@ object CommandParser {
}
}
Command.PART.command -> {
if (messageParts.size >= 2) {
val roomAlias = messageParts[1]
if (roomAlias.isNotEmpty()) {
ParsedCommand.PartRoom(
roomAlias,
textMessage.substring(Command.PART.length + roomAlias.length)
.trim()
.takeIf { it.isNotBlank() }
)
} else {
ParsedCommand.ErrorSyntax(Command.PART)
}
} else {
ParsedCommand.ErrorSyntax(Command.PART)
when (messageParts.size) {
1 -> ParsedCommand.PartRoom(null)
2 -> ParsedCommand.PartRoom(messageParts[1])
else -> ParsedCommand.ErrorSyntax(Command.PART)
}
}
Command.ROOM_NAME.command -> {

View file

@ -49,7 +49,7 @@ sealed class ParsedCommand {
class Invite(val userId: String, val reason: String?) : ParsedCommand()
class Invite3Pid(val threePid: ThreePid) : ParsedCommand()
class JoinRoom(val roomAlias: String, val reason: String?) : ParsedCommand()
class PartRoom(val roomAlias: String, val reason: String?) : ParsedCommand()
class PartRoom(val roomAlias: String?) : ParsedCommand()
class ChangeTopic(val topic: String) : ParsedCommand()
class KickUser(val userId: String, val reason: String?) : ParsedCommand()
class ChangeDisplayName(val displayName: String) : ParsedCommand()

View file

@ -225,8 +225,8 @@ class TextComposerViewModel @AssistedInject constructor(
popDraft()
}
is ParsedCommand.PartRoom -> {
// TODO
_viewEvents.post(TextComposerViewEvents.SlashCommandNotImplemented)
handlePartSlashCommand(slashCommandResult)
popDraft()
}
is ParsedCommand.SendEmote -> {
room.sendTextMessage(slashCommandResult.message, msgType = MessageType.MSGTYPE_EMOTE, autoMarkdown = action.autoMarkdown)
@ -578,6 +578,20 @@ class TextComposerViewModel @AssistedInject constructor(
}
}
private fun handlePartSlashCommand(command: ParsedCommand.PartRoom) {
launchSlashCommandFlowSuspendable {
if (command.roomAlias == null) {
// Leave the current room
room
} else {
session.getRoomSummary(roomIdOrAlias = command.roomAlias)
?.roomId
?.let { session.getRoom(it) }
}
?.leave(reason = null)
}
}
private fun handleKickSlashCommand(kick: ParsedCommand.KickUser) {
launchSlashCommandFlowSuspendable {
room.kick(kick.userId, kick.reason)