Added commands from element web

This commit is contained in:
Constantin Wartenburger 2020-10-10 16:36:04 +02:00
parent 5fa281dd3a
commit c0cf534845
No known key found for this signature in database
GPG key ID: 7439D96D8E1DB894
7 changed files with 149 additions and 30 deletions

View file

@ -28,8 +28,11 @@ enum class Command(val command: String, val parameters: String, @StringRes val d
EMOTE("/me", "<message>", R.string.command_description_emote),
BAN_USER("/ban", "<user-id> [reason]", R.string.command_description_ban_user),
UNBAN_USER("/unban", "<user-id> [reason]", R.string.command_description_unban_user),
IGNORE_USER("/ignore", "<user-id> [reason]", R.string.command_description_ignore_user),
UNIGNORE_USER("/unignore", "<user-id>", R.string.command_description_unignore_user),
SET_USER_POWER_LEVEL("/op", "<user-id> [<power-level>]", R.string.command_description_op_user),
RESET_USER_POWER_LEVEL("/deop", "<user-id>", R.string.command_description_deop_user),
ROOM_NAME("/roomname", "<user-id> [reason]", R.string.command_description_room_name),
INVITE("/invite", "<user-id> [reason]", R.string.command_description_invite_user),
JOIN_ROOM("/join", "<room-alias> [reason]", R.string.command_description_join_room),
PART("/part", "<room-alias> [reason]", R.string.command_description_part_room),
@ -42,8 +45,10 @@ enum class Command(val command: String, val parameters: String, @StringRes val d
CLEAR_SCALAR_TOKEN("/clear_scalar_token", "", R.string.command_description_clear_scalar_token),
SPOILER("/spoiler", "<message>", R.string.command_description_spoiler),
POLL("/poll", "Question | Option 1 | Option 2 ...", R.string.command_description_poll),
SHRUG("/shrug", "<message>", R.string.command_description_shrug),
SHRUG("/shrug", "[<message>]", R.string.command_description_shrug),
LENNY("/lenny", "[<message>]", R.string.command_description_lenny),
PLAIN("/plain", "<message>", R.string.command_description_plain),
WHOIS("/whois", "<user-id>", R.string.command_description_whois),
DISCARD_SESSION("/discardsession", "", R.string.command_description_discard_session);
val length

View file

@ -138,6 +138,15 @@ object CommandParser {
ParsedCommand.ErrorSyntax(Command.PART)
}
}
Command.ROOM_NAME.command -> {
val newRoomName = textMessage.substring(Command.ROOM_NAME.command.length).trim()
if (newRoomName.isNotEmpty()) {
ParsedCommand.ChangeRoomName(newRoomName)
} else {
ParsedCommand.ErrorSyntax(Command.ROOM_NAME)
}
}
Command.INVITE.command -> {
if (messageParts.size >= 2) {
val userId = messageParts[1]
@ -219,6 +228,32 @@ object CommandParser {
ParsedCommand.ErrorSyntax(Command.UNBAN_USER)
}
}
Command.IGNORE_USER.command -> {
if (messageParts.size == 2) {
val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
ParsedCommand.IgnoreUser(userId)
} else {
ParsedCommand.ErrorSyntax(Command.IGNORE_USER)
}
} else {
ParsedCommand.ErrorSyntax(Command.IGNORE_USER)
}
}
Command.UNIGNORE_USER.command -> {
if (messageParts.size == 2) {
val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
ParsedCommand.UnignoreUser(userId)
} else {
ParsedCommand.ErrorSyntax(Command.UNIGNORE_USER)
}
} else {
ParsedCommand.ErrorSyntax(Command.UNIGNORE_USER)
}
}
Command.SET_USER_POWER_LEVEL.command -> {
if (messageParts.size == 3) {
val userId = messageParts[1]
@ -279,6 +314,11 @@ object CommandParser {
ParsedCommand.SendShrug(message)
}
Command.LENNY.command -> {
val message = textMessage.substring(Command.LENNY.command.length).trim()
ParsedCommand.SendLenny(message)
}
Command.POLL.command -> {
val rawCommand = textMessage.substring(Command.POLL.command.length).trim()
val split = rawCommand.split("|").map { it.trim() }
@ -291,6 +331,19 @@ object CommandParser {
Command.DISCARD_SESSION.command -> {
ParsedCommand.DiscardSession
}
Command.WHOIS.command -> {
if (messageParts.size == 2) {
val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
ParsedCommand.ShowUser(userId)
} else {
ParsedCommand.ErrorSyntax(Command.WHOIS)
}
} else {
ParsedCommand.ErrorSyntax(Command.WHOIS)
}
}
else -> {
// Unknown command
ParsedCommand.ErrorUnknownSlashCommand(slashCommand)

View file

@ -41,7 +41,10 @@ sealed class ParsedCommand {
class SendRainbowEmote(val message: CharSequence) : ParsedCommand()
class BanUser(val userId: String, val reason: String?) : ParsedCommand()
class UnbanUser(val userId: String, val reason: String?) : ParsedCommand()
class IgnoreUser(val userId: String) : ParsedCommand()
class UnignoreUser(val userId: String) : ParsedCommand()
class SetUserPowerLevel(val userId: String, val powerLevel: Int?) : ParsedCommand()
class ChangeRoomName(val name: String) : 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()
@ -53,6 +56,8 @@ sealed class ParsedCommand {
object ClearScalarToken : ParsedCommand()
class SendSpoiler(val message: String) : ParsedCommand()
class SendShrug(val message: CharSequence) : ParsedCommand()
class SendLenny(val message: CharSequence) : ParsedCommand()
class SendPoll(val question: String, val options: List<String>) : ParsedCommand()
object DiscardSession : ParsedCommand()
class ShowUser(val userId: String) : ParsedCommand()
}

View file

@ -359,6 +359,7 @@ class RoomDetailFragment @Inject constructor(
is RoomDetailViewEvents.SendMessageResult -> renderSendMessageResult(it)
is RoomDetailViewEvents.ShowE2EErrorMessage -> displayE2eError(it.withHeldCode)
RoomDetailViewEvents.DisplayPromptForIntegrationManager -> displayPromptForIntegrationManager()
is RoomDetailViewEvents.OpenRoomMemberProfile -> openRoomMemberProfile(it.userId)
is RoomDetailViewEvents.OpenStickerPicker -> openStickerPicker(it)
is RoomDetailViewEvents.DisplayEnableIntegrationsWarning -> displayDisabledIntegrationDialog()
is RoomDetailViewEvents.OpenIntegrationManager -> openIntegrationManager()

View file

@ -68,6 +68,8 @@ sealed class RoomDetailViewEvents : VectorViewEvents {
object DisplayEnableIntegrationsWarning : RoomDetailViewEvents()
data class OpenRoomMemberProfile(val userId: String) : RoomDetailViewEvents()
data class OpenStickerPicker(val widget: Widget) : RoomDetailViewEvents()
object OpenIntegrationManager : RoomDetailViewEvents()

View file

@ -571,6 +571,10 @@ class RoomDetailViewModel @AssistedInject constructor(
_viewEvents.post(RoomDetailViewEvents.MessageSent)
popDraft()
}
is ParsedCommand.ChangeRoomName -> {
handleChangeRoomNameSlashCommand(slashCommandResult)
popDraft()
}
is ParsedCommand.Invite -> {
handleInviteSlashCommand(slashCommandResult)
popDraft()
@ -593,12 +597,20 @@ class RoomDetailViewModel @AssistedInject constructor(
if (slashCommandResult.enable) R.string.markdown_has_been_enabled else R.string.markdown_has_been_disabled))
popDraft()
}
is ParsedCommand.BanUser -> {
handleBanSlashCommand(slashCommandResult)
popDraft()
}
is ParsedCommand.UnbanUser -> {
handleUnbanSlashCommand(slashCommandResult)
popDraft()
}
is ParsedCommand.BanUser -> {
handleBanSlashCommand(slashCommandResult)
is ParsedCommand.IgnoreUser -> {
handleIgnoreSlashCommand(slashCommandResult)
popDraft()
}
is ParsedCommand.UnignoreUser -> {
handleUnignoreSlashCommand(slashCommandResult)
popDraft()
}
is ParsedCommand.KickUser -> {
@ -641,14 +653,12 @@ class RoomDetailViewModel @AssistedInject constructor(
popDraft()
}
is ParsedCommand.SendShrug -> {
val sequence = buildString {
append("¯\\_(ツ)_/¯")
if (slashCommandResult.message.isNotEmpty()) {
append(" ")
append(slashCommandResult.message)
sendPrefixedMessage("¯\\_(ツ)_/¯", slashCommandResult.message)
_viewEvents.post(RoomDetailViewEvents.SlashCommandHandled())
popDraft()
}
}
room.sendTextMessage(sequence)
is ParsedCommand.SendLenny -> {
sendPrefixedMessage("( ͡° ͜ʖ ͡°)", slashCommandResult.message)
_viewEvents.post(RoomDetailViewEvents.SlashCommandHandled())
popDraft()
}
@ -665,6 +675,11 @@ class RoomDetailViewModel @AssistedInject constructor(
handleChangeDisplayNameSlashCommand(slashCommandResult)
popDraft()
}
is ParsedCommand.ShowUser -> {
_viewEvents.post(RoomDetailViewEvents.SlashCommandHandled())
handleWhoisSlashCommand(slashCommandResult)
popDraft()
}
is ParsedCommand.DiscardSession -> {
if (room.isEncrypted()) {
session.cryptoService().discardOutboundSession(room.roomId)
@ -786,6 +801,12 @@ class RoomDetailViewModel @AssistedInject constructor(
}
}
private fun handleChangeRoomNameSlashCommand(changeRoomName: ParsedCommand.ChangeRoomName) {
launchSlashCommandFlow {
room.updateName(changeRoomName.name, it)
}
}
private fun handleInviteSlashCommand(invite: ParsedCommand.Invite) {
launchSlashCommandFlow {
room.invite(invite.userId, invite.reason, it)
@ -833,6 +854,33 @@ class RoomDetailViewModel @AssistedInject constructor(
}
}
private fun handleIgnoreSlashCommand(ignore: ParsedCommand.IgnoreUser) {
launchSlashCommandFlow {
session.ignoreUserIds(listOf(ignore.userId), it)
}
}
private fun handleUnignoreSlashCommand(unignore: ParsedCommand.UnignoreUser) {
launchSlashCommandFlow {
session.unIgnoreUserIds(listOf(unignore.userId), it)
}
}
private fun handleWhoisSlashCommand(whois: ParsedCommand.ShowUser) {
_viewEvents.post(RoomDetailViewEvents.OpenRoomMemberProfile(whois.userId))
}
private fun sendPrefixedMessage(prefix: String, message: CharSequence) {
val sequence = buildString {
append(prefix)
if (message.isNotEmpty()) {
append(" ")
append(message)
}
}
room.sendTextMessage(sequence)
}
private fun launchSlashCommandFlow(lambda: (MatrixCallback<Unit>) -> Unit) {
_viewEvents.post(RoomDetailViewEvents.SlashCommandHandled())
val matrixCallback = object : MatrixCallback<Unit> {

View file

@ -1287,8 +1287,11 @@
<string name="command_description_emote">Displays action</string>
<string name="command_description_ban_user">Bans user with given id</string>
<string name="command_description_unban_user">Unbans user with given id</string>
<string name="command_description_ignore_user">Ignores a user, hiding their messages from you</string>
<string name="command_description_unignore_user">Stops ignoring a user, showing their messages going forward</string>
<string name="command_description_op_user">Define the power level of a user</string>
<string name="command_description_deop_user">Deops user with given id</string>
<string name="command_description_room_name">Sets the room name</string>
<string name="command_description_invite_user">Invites user with given id to current room</string>
<string name="command_description_join_room">Joins room with given alias</string>
<string name="command_description_part_room">Leave room</string>
@ -1297,6 +1300,7 @@
<string name="command_description_nick">Changes your display nickname</string>
<string name="command_description_markdown">On/Off markdown</string>
<string name="command_description_clear_scalar_token">To fix Matrix Apps management</string>
<string name="command_description_whois">Displays information about a user</string>
<string name="markdown_has_been_enabled">Markdown has been enabled.</string>
<string name="markdown_has_been_disabled">Markdown has been disabled.</string>
@ -2063,6 +2067,7 @@
<string name="settings_developer_mode_fail_fast_summary">Element may crash more often when an unexpected error occurs</string>
<string name="command_description_shrug">Prepends ¯\\_(ツ)_/¯ to a plain-text message</string>
<string name="command_description_lenny">Prepends ( ͡° ͜ʖ ͡°) to a plain-text message</string>
<string name="create_room_encryption_title">"Enable encryption"</string>
<string name="create_room_encryption_description">"Once enabled, encryption cannot be disabled."</string>