Skip to content

Commit

Permalink
Merge pull request #4158 from vector-im/feature/bma/new_commands
Browse files Browse the repository at this point in the history
Handle new commands
  • Loading branch information
bmarty authored Oct 5, 2021
2 parents 9fd1cc9 + 90781db commit 7ebdd78
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 56 deletions.
1 change: 1 addition & 0 deletions changelog.d/4158.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle 8 new slash commands: `/ignore`, `/unignore`, `/roomname`, `/myroomnick`, `/roomavatar`, `/myroomavatar`, `/lenny`, `/whois`.
1 change: 1 addition & 0 deletions changelog.d/4158.removal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Create extension `String.isMxcUrl()`
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.matrix.android.sdk.api

/**
* This class contains pattern to match Matrix Url, aka mxc urls
*/
object MatrixUrls {
const val MATRIX_CONTENT_URI_SCHEME = "mxc://"

fun String.isMxcUrl() = startsWith(MATRIX_CONTENT_URI_SCHEME)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@

package org.matrix.android.sdk.internal.session.content

import org.matrix.android.sdk.api.MatrixUrls
import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
import org.matrix.android.sdk.api.session.content.ContentUrlResolver
import org.matrix.android.sdk.internal.network.NetworkConstants
import org.matrix.android.sdk.internal.util.ensureTrailingSlash
import javax.inject.Inject

private const val MATRIX_CONTENT_URI_SCHEME = "mxc://"

internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectionConfig: HomeServerConnectionConfig) : ContentUrlResolver {

private val baseUrl = homeServerConnectionConfig.homeServerUriBase.toString().ensureTrailingSlash()
Expand All @@ -33,7 +33,7 @@ internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectio
override fun resolveFullSize(contentUrl: String?): String? {
return contentUrl
// do not allow non-mxc content URLs
?.takeIf { it.isValidMatrixContentUrl() }
?.takeIf { it.isMxcUrl() }
?.let {
resolve(
contentUrl = it,
Expand All @@ -45,7 +45,7 @@ internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectio
override fun resolveThumbnail(contentUrl: String?, width: Int, height: Int, method: ContentUrlResolver.ThumbnailMethod): String? {
return contentUrl
// do not allow non-mxc content URLs
?.takeIf { it.isValidMatrixContentUrl() }
?.takeIf { it.isMxcUrl() }
?.let {
resolve(
contentUrl = it,
Expand All @@ -58,7 +58,7 @@ internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectio
private fun resolve(contentUrl: String,
prefix: String,
params: String = ""): String? {
var serverAndMediaId = contentUrl.removePrefix(MATRIX_CONTENT_URI_SCHEME)
var serverAndMediaId = contentUrl.removePrefix(MatrixUrls.MATRIX_CONTENT_URI_SCHEME)
val fragmentOffset = serverAndMediaId.indexOf("#")
var fragment = ""
if (fragmentOffset >= 0) {
Expand All @@ -68,8 +68,4 @@ internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectio

return baseUrl + prefix + serverAndMediaId + params + fragment
}

private fun String.isValidMatrixContentUrl(): Boolean {
return startsWith(MATRIX_CONTENT_URI_SCHEME)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl
import org.matrix.android.sdk.api.session.content.ContentAttachmentData
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.events.model.isAttachmentMessage
Expand Down Expand Up @@ -130,7 +131,7 @@ internal class DefaultSendService @AssistedInject constructor(
val messageContent = clearContent?.toModel<MessageContent>() as? MessageWithAttachmentContent ?: return NoOpCancellable

val url = messageContent.getFileUrl() ?: return NoOpCancellable
if (url.startsWith("mxc://")) {
if (url.isMxcUrl()) {
// We need to resend only the message as the attachment is ok
localEchoRepository.updateSendState(localEcho.eventId, roomId, SendState.UNSENT)
return sendEvent(localEcho.root)
Expand Down
10 changes: 9 additions & 1 deletion vector/src/main/java/im/vector/app/features/command/Command.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,34 @@ enum class Command(val command: String, val parameters: String, @StringRes val d
EMOTE("/me", "<message>", R.string.command_description_emote, false),
BAN_USER("/ban", "<user-id> [reason]", R.string.command_description_ban_user, false),
UNBAN_USER("/unban", "<user-id> [reason]", R.string.command_description_unban_user, false),
IGNORE_USER("/ignore", "<user-id> [reason]", R.string.command_description_ignore_user, false),
UNIGNORE_USER("/unignore", "<user-id>", R.string.command_description_unignore_user, false),
SET_USER_POWER_LEVEL("/op", "<user-id> [<power-level>]", R.string.command_description_op_user, false),
RESET_USER_POWER_LEVEL("/deop", "<user-id>", R.string.command_description_deop_user, false),
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-alias> [reason]", R.string.command_description_join_room, false),
PART("/part", "<room-alias> [reason]", 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),
CHANGE_DISPLAY_NAME_FOR_ROOM("/myroomnick", "<display-name>", R.string.command_description_nick_for_room, false),
ROOM_AVATAR("/roomavatar", "<mxc_url>", R.string.command_description_room_avatar, true /* Since user has to know the mxc url */),
CHANGE_AVATAR_FOR_ROOM("/myroomavatar", "<mxc_url>", R.string.command_description_avatar_for_room, true /* Since user has to know the mxc url */),
MARKDOWN("/markdown", "<on|off>", R.string.command_description_markdown, false),
RAINBOW("/rainbow", "<message>", R.string.command_description_rainbow, false),
RAINBOW_EMOTE("/rainbowme", "<message>", R.string.command_description_rainbow_emote, false),
CLEAR_SCALAR_TOKEN("/clear_scalar_token", "", R.string.command_description_clear_scalar_token, false),
SPOILER("/spoiler", "<message>", R.string.command_description_spoiler, false),
POLL("/poll", "Question | Option 1 | Option 2 ...", R.string.command_description_poll, false),
SHRUG("/shrug", "<message>", R.string.command_description_shrug, false),
LENNY("/lenny", "<message>", R.string.command_description_lenny, false),
PLAIN("/plain", "<message>", R.string.command_description_plain, false),
WHOIS("/whois", "<user-id>", R.string.command_description_whois, false),
DISCARD_SESSION("/discardsession", "", R.string.command_description_discard_session, false),
CONFETTI("/confetti", "<message>", R.string.command_confetti, false),
SNOWFALL("/snowfall", "<message>", R.string.command_snow, false),
CREATE_SPACE("/createspace", "<name> <invitee>*", R.string.command_description_create_space, true),
CREATE_SPACE("/createspace", "<name> <invitee>*", R.string.command_description_create_space, true),
ADD_TO_SPACE("/addToSpace", "spaceId", R.string.command_description_add_to_space, true),
JOIN_SPACE("/joinSpace", "spaceId", R.string.command_description_join_space, true),
LEAVE_ROOM("/leave", "<roomId?>", R.string.command_description_leave_room, true),
Expand Down
Loading

0 comments on commit 7ebdd78

Please sign in to comment.