From b7ff546f6608390f1d1fd5fe88615afa1c029a08 Mon Sep 17 00:00:00 2001
From: Benoit Marty <benoitm@matrix.org>
Date: Sun, 1 Mar 2020 09:59:00 +0100
Subject: [PATCH] Add support for `/plain` command (#12)

---
 CHANGES.md                                               | 2 +-
 .../java/im/vector/riotx/features/command/Command.kt     | 1 +
 .../im/vector/riotx/features/command/CommandParser.kt    | 9 +++++++++
 .../im/vector/riotx/features/command/ParsedCommand.kt    | 1 +
 .../features/home/room/detail/RoomDetailViewModel.kt     | 6 ++++++
 vector/src/main/res/values/strings_riotX.xml             | 2 +-
 6 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 4f8838deb6..b2ad0766c4 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -5,7 +5,7 @@ Features ✨:
  -
 
 Improvements 🙌:
- -
+ - Add support for `/plain` command (#12)
 
 Bugfix 🐛:
  - Fix crash on attachment preview screen (#1088)
diff --git a/vector/src/main/java/im/vector/riotx/features/command/Command.kt b/vector/src/main/java/im/vector/riotx/features/command/Command.kt
index f39f1cb7cd..72f686c2c8 100644
--- a/vector/src/main/java/im/vector/riotx/features/command/Command.kt
+++ b/vector/src/main/java/im/vector/riotx/features/command/Command.kt
@@ -43,6 +43,7 @@ enum class Command(val command: String, val parameters: String, @StringRes val d
     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),
+    PLAIN("/plain", "<message>", R.string.command_description_plain),
     // TODO temporary command
     VERIFY_USER("/verify", "<user-id>", R.string.command_description_verify);
 
diff --git a/vector/src/main/java/im/vector/riotx/features/command/CommandParser.kt b/vector/src/main/java/im/vector/riotx/features/command/CommandParser.kt
index abc047e273..875fe92610 100644
--- a/vector/src/main/java/im/vector/riotx/features/command/CommandParser.kt
+++ b/vector/src/main/java/im/vector/riotx/features/command/CommandParser.kt
@@ -57,6 +57,15 @@ object CommandParser {
             }
 
             return when (val slashCommand = messageParts.first()) {
+                Command.PLAIN.command                  -> {
+                    val text = textMessage.substring(Command.PLAIN.command.length).trim()
+
+                    if (text.isNotEmpty()) {
+                        ParsedCommand.SendPlainText(text)
+                    } else {
+                        ParsedCommand.ErrorSyntax(Command.PLAIN)
+                    }
+                }
                 Command.CHANGE_DISPLAY_NAME.command    -> {
                     val newDisplayName = textMessage.substring(Command.CHANGE_DISPLAY_NAME.command.length).trim()
 
diff --git a/vector/src/main/java/im/vector/riotx/features/command/ParsedCommand.kt b/vector/src/main/java/im/vector/riotx/features/command/ParsedCommand.kt
index d823429ac9..e4fee27ee6 100644
--- a/vector/src/main/java/im/vector/riotx/features/command/ParsedCommand.kt
+++ b/vector/src/main/java/im/vector/riotx/features/command/ParsedCommand.kt
@@ -33,6 +33,7 @@ sealed class ParsedCommand {
 
     // Valid commands:
 
+    class SendPlainText(val message: CharSequence) : ParsedCommand()
     class SendEmote(val message: CharSequence) : ParsedCommand()
     class SendRainbow(val message: CharSequence) : ParsedCommand()
     class SendRainbowEmote(val message: CharSequence) : ParsedCommand()
diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailViewModel.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailViewModel.kt
index 8a231fb25d..1fcae90e95 100644
--- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailViewModel.kt
+++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailViewModel.kt
@@ -340,6 +340,12 @@ class RoomDetailViewModel @AssistedInject constructor(@Assisted initialState: Ro
                         is ParsedCommand.ErrorUnknownSlashCommand -> {
                             _viewEvents.post(RoomDetailViewEvents.SlashCommandUnknown(slashCommandResult.slashCommand))
                         }
+                        is ParsedCommand.SendPlainText            -> {
+                            // Send the text message to the room, without markdown
+                            room.sendTextMessage(slashCommandResult.message, autoMarkdown = false)
+                            _viewEvents.post(RoomDetailViewEvents.MessageSent)
+                            popDraft()
+                        }
                         is ParsedCommand.Invite                   -> {
                             handleInviteSlashCommand(slashCommandResult)
                             popDraft()
diff --git a/vector/src/main/res/values/strings_riotX.xml b/vector/src/main/res/values/strings_riotX.xml
index 00bf65e121..69d821c0ca 100644
--- a/vector/src/main/res/values/strings_riotX.xml
+++ b/vector/src/main/res/values/strings_riotX.xml
@@ -26,7 +26,7 @@
 
 
     <!-- BEGIN Strings added by Others -->
-
+    <string name="command_description_plain">Sends a message as plain text, without interpreting it as markdown</string>
     <!-- END Strings added by Others -->
 
 </resources>