Commit f1b43573 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Request to run commands if message starts with "/"

parent 0d49577b
...@@ -24,6 +24,7 @@ import chat.rocket.common.model.roomTypeOf ...@@ -24,6 +24,7 @@ import chat.rocket.common.model.roomTypeOf
import chat.rocket.common.util.ifNull import chat.rocket.common.util.ifNull
import chat.rocket.core.internal.realtime.State import chat.rocket.core.internal.realtime.State
import chat.rocket.core.internal.rest.* import chat.rocket.core.internal.rest.*
import chat.rocket.core.model.Command
import chat.rocket.core.model.Message import chat.rocket.core.model.Message
import chat.rocket.core.model.Value import chat.rocket.core.model.Value
import kotlinx.coroutines.experimental.CommonPool import kotlinx.coroutines.experimental.CommonPool
...@@ -504,6 +505,35 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView, ...@@ -504,6 +505,35 @@ class ChatRoomPresenter @Inject constructor(private val view: ChatRoomView,
} }
} }
fun runCommand(text: String, roomId: String) {
launchUI(strategy) {
try {
if (text.length == 1) {
// we have just the slash, post it anyway
sendMessage(roomId, text, null)
} else {
val command = text.split(" ")
val name = command[0].substring(1)
var params: String = ""
command.forEachIndexed { index, param ->
if (index > 0) {
params += "$param "
}
}
val result = client.runCommand(Command(name, params), roomId)
if (!result) {
// failed, command is not valid so post it
sendMessage(roomId, text, null)
}
}
} catch (ex: RocketChatException) {
Timber.e(ex)
// command is not valid, post it
sendMessage(roomId, text, null)
}
}
}
private fun updateMessage(streamedMessage: Message) { private fun updateMessage(streamedMessage: Message) {
launchUI(strategy) { launchUI(strategy) {
val viewModelStreamedMessage = mapper.map(streamedMessage) val viewModelStreamedMessage = mapper.map(streamedMessage)
......
...@@ -216,7 +216,11 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -216,7 +216,11 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
override fun sendMessage(text: String) { override fun sendMessage(text: String) {
if (!text.isBlank()) { if (!text.isBlank()) {
presenter.sendMessage(chatRoomId, text, editingMessageId) if (!text.startsWith("/")) {
presenter.sendMessage(chatRoomId, text, editingMessageId)
} else {
presenter.runCommand(text, chatRoomId)
}
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment