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), EMOTE("/me", "<message>", R.string.command_description_emote),
BAN_USER("/ban", "<user-id> [reason]", R.string.command_description_ban_user), BAN_USER("/ban", "<user-id> [reason]", R.string.command_description_ban_user),
UNBAN_USER("/unban", "<user-id> [reason]", R.string.command_description_unban_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), 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), 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), INVITE("/invite", "<user-id> [reason]", R.string.command_description_invite_user),
JOIN_ROOM("/join", "<room-alias> [reason]", R.string.command_description_join_room), JOIN_ROOM("/join", "<room-alias> [reason]", R.string.command_description_join_room),
PART("/part", "<room-alias> [reason]", R.string.command_description_part_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), CLEAR_SCALAR_TOKEN("/clear_scalar_token", "", R.string.command_description_clear_scalar_token),
SPOILER("/spoiler", "<message>", R.string.command_description_spoiler), SPOILER("/spoiler", "<message>", R.string.command_description_spoiler),
POLL("/poll", "Question | Option 1 | Option 2 ...", R.string.command_description_poll), 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), 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); DISCARD_SESSION("/discardsession", "", R.string.command_description_discard_session);
val length val length

View file

@ -102,7 +102,7 @@ object CommandParser {
ParsedCommand.SendRainbowEmote(message) ParsedCommand.SendRainbowEmote(message)
} }
Command.JOIN_ROOM.command -> { Command.JOIN_ROOM.command -> {
if (messageParts.size >= 2) { if (messageParts.size >= 2) {
val roomAlias = messageParts[1] val roomAlias = messageParts[1]
@ -120,7 +120,7 @@ object CommandParser {
ParsedCommand.ErrorSyntax(Command.JOIN_ROOM) ParsedCommand.ErrorSyntax(Command.JOIN_ROOM)
} }
} }
Command.PART.command -> { Command.PART.command -> {
if (messageParts.size >= 2) { if (messageParts.size >= 2) {
val roomAlias = messageParts[1] val roomAlias = messageParts[1]
@ -138,7 +138,16 @@ object CommandParser {
ParsedCommand.ErrorSyntax(Command.PART) ParsedCommand.ErrorSyntax(Command.PART)
} }
} }
Command.INVITE.command -> { 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) { if (messageParts.size >= 2) {
val userId = messageParts[1] val userId = messageParts[1]
@ -183,7 +192,7 @@ object CommandParser {
ParsedCommand.ErrorSyntax(Command.KICK_USER) ParsedCommand.ErrorSyntax(Command.KICK_USER)
} }
} }
Command.BAN_USER.command -> { Command.BAN_USER.command -> {
if (messageParts.size >= 2) { if (messageParts.size >= 2) {
val userId = messageParts[1] val userId = messageParts[1]
@ -201,7 +210,7 @@ object CommandParser {
ParsedCommand.ErrorSyntax(Command.BAN_USER) ParsedCommand.ErrorSyntax(Command.BAN_USER)
} }
} }
Command.UNBAN_USER.command -> { Command.UNBAN_USER.command -> {
if (messageParts.size >= 2) { if (messageParts.size >= 2) {
val userId = messageParts[1] val userId = messageParts[1]
@ -219,7 +228,33 @@ object CommandParser {
ParsedCommand.ErrorSyntax(Command.UNBAN_USER) ParsedCommand.ErrorSyntax(Command.UNBAN_USER)
} }
} }
Command.SET_USER_POWER_LEVEL.command -> { 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) { if (messageParts.size == 3) {
val userId = messageParts[1] val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) { if (MatrixPatterns.isUserId(userId)) {
@ -252,7 +287,7 @@ object CommandParser {
ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL) ParsedCommand.ErrorSyntax(Command.SET_USER_POWER_LEVEL)
} }
} }
Command.MARKDOWN.command -> { Command.MARKDOWN.command -> {
if (messageParts.size == 2) { if (messageParts.size == 2) {
when { when {
"on".equals(messageParts[1], true) -> ParsedCommand.SetMarkdown(true) "on".equals(messageParts[1], true) -> ParsedCommand.SetMarkdown(true)
@ -263,23 +298,28 @@ object CommandParser {
ParsedCommand.ErrorSyntax(Command.MARKDOWN) ParsedCommand.ErrorSyntax(Command.MARKDOWN)
} }
} }
Command.CLEAR_SCALAR_TOKEN.command -> { Command.CLEAR_SCALAR_TOKEN.command -> {
if (messageParts.size == 1) { if (messageParts.size == 1) {
ParsedCommand.ClearScalarToken ParsedCommand.ClearScalarToken
} else { } else {
ParsedCommand.ErrorSyntax(Command.CLEAR_SCALAR_TOKEN) ParsedCommand.ErrorSyntax(Command.CLEAR_SCALAR_TOKEN)
} }
} }
Command.SPOILER.command -> { Command.SPOILER.command -> {
val message = textMessage.substring(Command.SPOILER.command.length).trim() val message = textMessage.substring(Command.SPOILER.command.length).trim()
ParsedCommand.SendSpoiler(message) ParsedCommand.SendSpoiler(message)
} }
Command.SHRUG.command -> { Command.SHRUG.command -> {
val message = textMessage.substring(Command.SHRUG.command.length).trim() val message = textMessage.substring(Command.SHRUG.command.length).trim()
ParsedCommand.SendShrug(message) ParsedCommand.SendShrug(message)
} }
Command.POLL.command -> { 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 rawCommand = textMessage.substring(Command.POLL.command.length).trim()
val split = rawCommand.split("|").map { it.trim() } val split = rawCommand.split("|").map { it.trim() }
if (split.size > 2) { if (split.size > 2) {
@ -288,10 +328,23 @@ object CommandParser {
ParsedCommand.ErrorSyntax(Command.POLL) ParsedCommand.ErrorSyntax(Command.POLL)
} }
} }
Command.DISCARD_SESSION.command -> { Command.DISCARD_SESSION.command -> {
ParsedCommand.DiscardSession ParsedCommand.DiscardSession
} }
else -> { 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 // Unknown command
ParsedCommand.ErrorUnknownSlashCommand(slashCommand) ParsedCommand.ErrorUnknownSlashCommand(slashCommand)
} }

View file

@ -41,7 +41,10 @@ sealed class ParsedCommand {
class SendRainbowEmote(val message: CharSequence) : ParsedCommand() class SendRainbowEmote(val message: CharSequence) : ParsedCommand()
class BanUser(val userId: String, val reason: String?) : ParsedCommand() class BanUser(val userId: String, val reason: String?) : ParsedCommand()
class UnbanUser(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 SetUserPowerLevel(val userId: String, val powerLevel: Int?) : ParsedCommand()
class ChangeRoomName(val name: String) : ParsedCommand()
class Invite(val userId: String, val reason: String?) : ParsedCommand() class Invite(val userId: String, val reason: String?) : ParsedCommand()
class Invite3Pid(val threePid: ThreePid) : ParsedCommand() class Invite3Pid(val threePid: ThreePid) : ParsedCommand()
class JoinRoom(val roomAlias: String, val reason: String?) : ParsedCommand() class JoinRoom(val roomAlias: String, val reason: String?) : ParsedCommand()
@ -53,6 +56,8 @@ sealed class ParsedCommand {
object ClearScalarToken : ParsedCommand() object ClearScalarToken : ParsedCommand()
class SendSpoiler(val message: String) : ParsedCommand() class SendSpoiler(val message: String) : ParsedCommand()
class SendShrug(val message: CharSequence) : ParsedCommand() class SendShrug(val message: CharSequence) : ParsedCommand()
class SendLenny(val message: CharSequence) : ParsedCommand()
class SendPoll(val question: String, val options: List<String>) : ParsedCommand() class SendPoll(val question: String, val options: List<String>) : ParsedCommand()
object DiscardSession: 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.SendMessageResult -> renderSendMessageResult(it)
is RoomDetailViewEvents.ShowE2EErrorMessage -> displayE2eError(it.withHeldCode) is RoomDetailViewEvents.ShowE2EErrorMessage -> displayE2eError(it.withHeldCode)
RoomDetailViewEvents.DisplayPromptForIntegrationManager -> displayPromptForIntegrationManager() RoomDetailViewEvents.DisplayPromptForIntegrationManager -> displayPromptForIntegrationManager()
is RoomDetailViewEvents.OpenRoomMemberProfile -> openRoomMemberProfile(it.userId)
is RoomDetailViewEvents.OpenStickerPicker -> openStickerPicker(it) is RoomDetailViewEvents.OpenStickerPicker -> openStickerPicker(it)
is RoomDetailViewEvents.DisplayEnableIntegrationsWarning -> displayDisabledIntegrationDialog() is RoomDetailViewEvents.DisplayEnableIntegrationsWarning -> displayDisabledIntegrationDialog()
is RoomDetailViewEvents.OpenIntegrationManager -> openIntegrationManager() is RoomDetailViewEvents.OpenIntegrationManager -> openIntegrationManager()

View file

@ -64,14 +64,16 @@ sealed class RoomDetailViewEvents : VectorViewEvents {
abstract class SendMessageResult : RoomDetailViewEvents() abstract class SendMessageResult : RoomDetailViewEvents()
object DisplayPromptForIntegrationManager: RoomDetailViewEvents() object DisplayPromptForIntegrationManager : RoomDetailViewEvents()
object DisplayEnableIntegrationsWarning: RoomDetailViewEvents() object DisplayEnableIntegrationsWarning : RoomDetailViewEvents()
data class OpenStickerPicker(val widget: Widget): RoomDetailViewEvents() data class OpenRoomMemberProfile(val userId: String) : RoomDetailViewEvents()
object OpenIntegrationManager: RoomDetailViewEvents() data class OpenStickerPicker(val widget: Widget) : RoomDetailViewEvents()
object OpenActiveWidgetBottomSheet: RoomDetailViewEvents()
object OpenIntegrationManager : RoomDetailViewEvents()
object OpenActiveWidgetBottomSheet : RoomDetailViewEvents()
data class RequestNativeWidgetPermission(val widget: Widget, data class RequestNativeWidgetPermission(val widget: Widget,
val domain: String, val domain: String,
val grantedEvents: RoomDetailViewEvents) : RoomDetailViewEvents() val grantedEvents: RoomDetailViewEvents) : RoomDetailViewEvents()

View file

@ -571,6 +571,10 @@ class RoomDetailViewModel @AssistedInject constructor(
_viewEvents.post(RoomDetailViewEvents.MessageSent) _viewEvents.post(RoomDetailViewEvents.MessageSent)
popDraft() popDraft()
} }
is ParsedCommand.ChangeRoomName -> {
handleChangeRoomNameSlashCommand(slashCommandResult)
popDraft()
}
is ParsedCommand.Invite -> { is ParsedCommand.Invite -> {
handleInviteSlashCommand(slashCommandResult) handleInviteSlashCommand(slashCommandResult)
popDraft() 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)) if (slashCommandResult.enable) R.string.markdown_has_been_enabled else R.string.markdown_has_been_disabled))
popDraft() popDraft()
} }
is ParsedCommand.BanUser -> {
handleBanSlashCommand(slashCommandResult)
popDraft()
}
is ParsedCommand.UnbanUser -> { is ParsedCommand.UnbanUser -> {
handleUnbanSlashCommand(slashCommandResult) handleUnbanSlashCommand(slashCommandResult)
popDraft() popDraft()
} }
is ParsedCommand.BanUser -> { is ParsedCommand.IgnoreUser -> {
handleBanSlashCommand(slashCommandResult) handleIgnoreSlashCommand(slashCommandResult)
popDraft()
}
is ParsedCommand.UnignoreUser -> {
handleUnignoreSlashCommand(slashCommandResult)
popDraft() popDraft()
} }
is ParsedCommand.KickUser -> { is ParsedCommand.KickUser -> {
@ -641,14 +653,12 @@ class RoomDetailViewModel @AssistedInject constructor(
popDraft() popDraft()
} }
is ParsedCommand.SendShrug -> { is ParsedCommand.SendShrug -> {
val sequence = buildString { sendPrefixedMessage("¯\\_(ツ)_/¯", slashCommandResult.message)
append("¯\\_(ツ)_/¯") _viewEvents.post(RoomDetailViewEvents.SlashCommandHandled())
if (slashCommandResult.message.isNotEmpty()) { popDraft()
append(" ") }
append(slashCommandResult.message) is ParsedCommand.SendLenny -> {
} sendPrefixedMessage("( ͡° ͜ʖ ͡°)", slashCommandResult.message)
}
room.sendTextMessage(sequence)
_viewEvents.post(RoomDetailViewEvents.SlashCommandHandled()) _viewEvents.post(RoomDetailViewEvents.SlashCommandHandled())
popDraft() popDraft()
} }
@ -665,6 +675,11 @@ class RoomDetailViewModel @AssistedInject constructor(
handleChangeDisplayNameSlashCommand(slashCommandResult) handleChangeDisplayNameSlashCommand(slashCommandResult)
popDraft() popDraft()
} }
is ParsedCommand.ShowUser -> {
_viewEvents.post(RoomDetailViewEvents.SlashCommandHandled())
handleWhoisSlashCommand(slashCommandResult)
popDraft()
}
is ParsedCommand.DiscardSession -> { is ParsedCommand.DiscardSession -> {
if (room.isEncrypted()) { if (room.isEncrypted()) {
session.cryptoService().discardOutboundSession(room.roomId) 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) { private fun handleInviteSlashCommand(invite: ParsedCommand.Invite) {
launchSlashCommandFlow { launchSlashCommandFlow {
room.invite(invite.userId, invite.reason, it) 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) { private fun launchSlashCommandFlow(lambda: (MatrixCallback<Unit>) -> Unit) {
_viewEvents.post(RoomDetailViewEvents.SlashCommandHandled()) _viewEvents.post(RoomDetailViewEvents.SlashCommandHandled())
val matrixCallback = object : MatrixCallback<Unit> { val matrixCallback = object : MatrixCallback<Unit> {

View file

@ -1287,8 +1287,11 @@
<string name="command_description_emote">Displays action</string> <string name="command_description_emote">Displays action</string>
<string name="command_description_ban_user">Bans user with given id</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_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_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_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_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_join_room">Joins room with given alias</string>
<string name="command_description_part_room">Leave room</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_nick">Changes your display nickname</string>
<string name="command_description_markdown">On/Off markdown</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_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_enabled">Markdown has been enabled.</string>
<string name="markdown_has_been_disabled">Markdown has been disabled.</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="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_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_title">"Enable encryption"</string>
<string name="create_room_encryption_description">"Once enabled, encryption cannot be disabled."</string> <string name="create_room_encryption_description">"Once enabled, encryption cannot be disabled."</string>