Add reason to slash commands

This commit is contained in:
Benoit Marty 2019-12-04 20:11:19 +01:00
parent 03d51281a2
commit 57354cbd69
21 changed files with 105 additions and 67 deletions

View File

@ -57,8 +57,8 @@ class RxRoom(private val room: Room) {
room.loadRoomMembersIfNeeded(MatrixCallbackSingle(it)).toSingle(it)
}
fun joinRoom(viaServers: List<String> = emptyList()): Single<Unit> = Single.create {
room.join(viaServers, MatrixCallbackSingle(it)).toSingle(it)
fun joinRoom(reason: String?, viaServers: List<String> = emptyList()): Single<Unit> = Single.create {
room.join(reason, viaServers, MatrixCallbackSingle(it)).toSingle(it)
}
fun liveEventReadReceipts(eventId: String): Observable<List<ReadReceipt>> {

View File

@ -76,8 +76,8 @@ class RxSession(private val session: Session) {
session.searchUsersDirectory(search, limit, excludedUserIds, MatrixCallbackSingle(it)).toSingle(it)
}
fun joinRoom(roomId: String, viaServers: List<String> = emptyList()): Single<Unit> = Single.create {
session.joinRoom(roomId, viaServers, MatrixCallbackSingle(it)).toSingle(it)
fun joinRoom(roomId: String, reason: String?, viaServers: List<String> = emptyList()): Single<Unit> = Single.create {
session.joinRoom(roomId, reason, viaServers, MatrixCallbackSingle(it)).toSingle(it)
}
}

View File

@ -35,7 +35,7 @@ interface RoomDirectoryService {
/**
* Join a room by id
*/
fun joinRoom(roomId: String, callback: MatrixCallback<Unit>): Cancelable
fun joinRoom(roomId: String, reason: String?, callback: MatrixCallback<Unit>): Cancelable
/**
* Fetches the overall metadata about protocols supported by the homeserver.

View File

@ -35,9 +35,11 @@ interface RoomService {
/**
* Join a room by id
* @param roomId the roomId of the room to join
* @param reason optional reason for joining the room
* @param viaServers the servers to attempt to join the room through. One of the servers must be participating in the room.
*/
fun joinRoom(roomId: String,
reason: String?,
viaServers: List<String> = emptyList(),
callback: MatrixCallback<Unit>): Cancelable

View File

@ -52,16 +52,16 @@ interface MembershipService {
/**
* Invite a user in the room
*/
fun invite(userId: String, callback: MatrixCallback<Unit>): Cancelable
fun invite(userId: String, reason: String?, callback: MatrixCallback<Unit>): Cancelable
/**
* Join the room, or accept an invitation.
*/
fun join(viaServers: List<String> = emptyList(), callback: MatrixCallback<Unit>): Cancelable
fun join(reason: String?, viaServers: List<String> = emptyList(), callback: MatrixCallback<Unit>): Cancelable
/**
* Leave the room, or reject an invitation.
*/
fun leave(callback: MatrixCallback<Unit>): Cancelable
fun leave(reason: String?, callback: MatrixCallback<Unit>): Cancelable
}

View File

@ -44,9 +44,9 @@ internal class DefaultRoomDirectoryService @Inject constructor(private val getPu
.executeBy(taskExecutor)
}
override fun joinRoom(roomId: String, callback: MatrixCallback<Unit>): Cancelable {
override fun joinRoom(roomId: String, reason: String?, callback: MatrixCallback<Unit>): Cancelable {
return joinRoomTask
.configureWith(JoinRoomTask.Params(roomId)) {
.configureWith(JoinRoomTask.Params(roomId, reason)) {
this.callback = callback
}
.executeBy(taskExecutor)

View File

@ -96,9 +96,9 @@ internal class DefaultRoomService @Inject constructor(private val monarchy: Mona
.executeBy(taskExecutor)
}
override fun joinRoom(roomId: String, viaServers: List<String>, callback: MatrixCallback<Unit>): Cancelable {
override fun joinRoom(roomId: String, reason: String?, viaServers: List<String>, callback: MatrixCallback<Unit>): Cancelable {
return joinRoomTask
.configureWith(JoinRoomTask.Params(roomId, viaServers)) {
.configureWith(JoinRoomTask.Params(roomId, reason, viaServers)) {
this.callback = callback
}
.executeBy(taskExecutor)

View File

@ -217,7 +217,7 @@ internal interface RoomAPI {
@POST(NetworkConstants.URI_API_PREFIX_PATH_R0 + "rooms/{roomId}/join")
fun join(@Path("roomId") roomId: String,
@Query("server_name") viaServers: List<String>,
@Body params: Map<String, String>): Call<Unit>
@Body params: Map<String, String?>): Call<Unit>
/**
* Leave the given room.
@ -227,7 +227,7 @@ internal interface RoomAPI {
*/
@POST(NetworkConstants.URI_API_PREFIX_PATH_R0 + "rooms/{roomId}/leave")
fun leave(@Path("roomId") roomId: String,
@Body params: Map<String, String>): Call<Unit>
@Body params: Map<String, String?>): Call<Unit>
/**
* Strips all information out of an event which isn't critical to the integrity of the server-side representation of the room.

View File

@ -83,8 +83,8 @@ internal class DefaultMembershipService @AssistedInject constructor(@Assisted pr
return result
}
override fun invite(userId: String, callback: MatrixCallback<Unit>): Cancelable {
val params = InviteTask.Params(roomId, userId)
override fun invite(userId: String, reason: String?, callback: MatrixCallback<Unit>): Cancelable {
val params = InviteTask.Params(roomId, userId, reason)
return inviteTask
.configureWith(params) {
this.callback = callback
@ -92,8 +92,8 @@ internal class DefaultMembershipService @AssistedInject constructor(@Assisted pr
.executeBy(taskExecutor)
}
override fun join(viaServers: List<String>, callback: MatrixCallback<Unit>): Cancelable {
val params = JoinRoomTask.Params(roomId, viaServers)
override fun join(reason: String?, viaServers: List<String>, callback: MatrixCallback<Unit>): Cancelable {
val params = JoinRoomTask.Params(roomId, reason, viaServers)
return joinTask
.configureWith(params) {
this.callback = callback
@ -101,8 +101,8 @@ internal class DefaultMembershipService @AssistedInject constructor(@Assisted pr
.executeBy(taskExecutor)
}
override fun leave(callback: MatrixCallback<Unit>): Cancelable {
val params = LeaveRoomTask.Params(roomId)
override fun leave(reason: String?, callback: MatrixCallback<Unit>): Cancelable {
val params = LeaveRoomTask.Params(roomId, reason)
return leaveRoomTask
.configureWith(params) {
this.callback = callback

View File

@ -21,5 +21,6 @@ import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class InviteBody(
@Json(name = "user_id") val userId: String
@Json(name = "user_id") val userId: String,
@Json(name = "reason") val reason: String?
)

View File

@ -24,7 +24,8 @@ import javax.inject.Inject
internal interface InviteTask : Task<InviteTask.Params, Unit> {
data class Params(
val roomId: String,
val userId: String
val userId: String,
val reason: String?
)
}
@ -32,7 +33,7 @@ internal class DefaultInviteTask @Inject constructor(private val roomAPI: RoomAP
override suspend fun execute(params: InviteTask.Params) {
return executeRequest {
val body = InviteBody(params.userId)
val body = InviteBody(params.userId, params.reason)
apiCall = roomAPI.invite(params.roomId, body)
}
}

View File

@ -32,6 +32,7 @@ import javax.inject.Inject
internal interface JoinRoomTask : Task<JoinRoomTask.Params, Unit> {
data class Params(
val roomId: String,
val reason: String?,
val viaServers: List<String> = emptyList()
)
}
@ -43,7 +44,7 @@ internal class DefaultJoinRoomTask @Inject constructor(private val roomAPI: Room
override suspend fun execute(params: JoinRoomTask.Params) {
executeRequest<Unit> {
apiCall = roomAPI.join(params.roomId, params.viaServers, HashMap())
apiCall = roomAPI.join(params.roomId, params.viaServers, mapOf("reason" to params.reason))
}
val roomId = params.roomId
// Wait for room to come back from the sync (but it can maybe be in the DB is the sync response is received before)

View File

@ -23,7 +23,8 @@ import javax.inject.Inject
internal interface LeaveRoomTask : Task<LeaveRoomTask.Params, Unit> {
data class Params(
val roomId: String
val roomId: String,
val reason: String?
)
}
@ -31,7 +32,7 @@ internal class DefaultLeaveRoomTask @Inject constructor(private val roomAPI: Roo
override suspend fun execute(params: LeaveRoomTask.Params) {
return executeRequest {
apiCall = roomAPI.leave(params.roomId, HashMap())
apiCall = roomAPI.leave(params.roomId, mapOf("reason" to params.reason))
}
}
}

View File

@ -27,12 +27,12 @@ import im.vector.riotx.R
enum class Command(val command: String, val parameters: String, @StringRes val description: Int) {
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>", R.string.command_description_unban_user),
UNBAN_USER("/unban", "<user-id> [reason]", R.string.command_description_unban_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),
INVITE("/invite", "<user-id>", R.string.command_description_invite_user),
JOIN_ROOM("/join", "<room-alias>", R.string.command_description_join_room),
PART("/part", "<room-alias>", R.string.command_description_part_room),
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),
TOPIC("/topic", "<topic>", R.string.command_description_topic),
KICK_USER("/kick", "<user-id> [reason]", R.string.command_description_kick_user),
CHANGE_DISPLAY_NAME("/nick", "<display-name>", R.string.command_description_nick),

View File

@ -81,29 +81,52 @@ object CommandParser {
ParsedCommand.SendEmote(message)
}
Command.JOIN_ROOM.command -> {
val roomAlias = textMessage.substring(Command.JOIN_ROOM.command.length).trim()
if (messageParts.size >= 2) {
val roomAlias = messageParts[1]
if (roomAlias.isNotEmpty()) {
ParsedCommand.JoinRoom(roomAlias)
if (roomAlias.isNotEmpty()) {
ParsedCommand.JoinRoom(
roomAlias,
textMessage.substring(Command.JOIN_ROOM.command.length + 1 + roomAlias.length)
.trim()
.takeIf { it.isNotBlank() }
)
} else {
ParsedCommand.ErrorSyntax(Command.JOIN_ROOM)
}
} else {
ParsedCommand.ErrorSyntax(Command.JOIN_ROOM)
}
}
Command.PART.command -> {
val roomAlias = textMessage.substring(Command.PART.command.length).trim()
if (messageParts.size >= 2) {
val roomAlias = messageParts[1]
if (roomAlias.isNotEmpty()) {
ParsedCommand.PartRoom(roomAlias)
if (roomAlias.isNotEmpty()) {
ParsedCommand.PartRoom(
roomAlias,
textMessage.substring(Command.PART.command.length + 1 + roomAlias.length)
.trim()
.takeIf { it.isNotBlank() }
)
} else {
ParsedCommand.ErrorSyntax(Command.PART)
}
} else {
ParsedCommand.ErrorSyntax(Command.PART)
}
}
Command.INVITE.command -> {
if (messageParts.size == 2) {
if (messageParts.size >= 2) {
val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
ParsedCommand.Invite(userId)
ParsedCommand.Invite(
userId,
textMessage.substring(Command.INVITE.command.length + 1 + userId.length)
.trim()
.takeIf { it.isNotBlank() }
)
} else {
ParsedCommand.ErrorSyntax(Command.INVITE)
}
@ -114,12 +137,14 @@ object CommandParser {
Command.KICK_USER.command -> {
if (messageParts.size >= 2) {
val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
val reason = textMessage.substring(Command.KICK_USER.command.length
+ 1
+ userId.length).trim()
ParsedCommand.KickUser(userId, reason)
if (MatrixPatterns.isUserId(userId)) {
ParsedCommand.KickUser(
userId,
textMessage.substring(Command.KICK_USER.command.length + 1 + userId.length)
.trim()
.takeIf { it.isNotBlank() }
)
} else {
ParsedCommand.ErrorSyntax(Command.KICK_USER)
}
@ -130,12 +155,14 @@ object CommandParser {
Command.BAN_USER.command -> {
if (messageParts.size >= 2) {
val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
val reason = textMessage.substring(Command.BAN_USER.command.length
+ 1
+ userId.length).trim()
ParsedCommand.BanUser(userId, reason)
if (MatrixPatterns.isUserId(userId)) {
ParsedCommand.BanUser(
userId,
textMessage.substring(Command.BAN_USER.command.length + 1 + userId.length)
.trim()
.takeIf { it.isNotBlank() }
)
} else {
ParsedCommand.ErrorSyntax(Command.BAN_USER)
}
@ -144,11 +171,16 @@ object CommandParser {
}
}
Command.UNBAN_USER.command -> {
if (messageParts.size == 2) {
if (messageParts.size >= 2) {
val userId = messageParts[1]
if (MatrixPatterns.isUserId(userId)) {
ParsedCommand.UnbanUser(userId)
ParsedCommand.UnbanUser(
userId,
textMessage.substring(Command.UNBAN_USER.command.length + 1 + userId.length)
.trim()
.takeIf { it.isNotBlank() }
)
} else {
ParsedCommand.ErrorSyntax(Command.UNBAN_USER)
}

View File

@ -34,14 +34,14 @@ sealed class ParsedCommand {
// Valid commands:
class SendEmote(val message: CharSequence) : ParsedCommand()
class BanUser(val userId: String, val reason: String) : ParsedCommand()
class UnbanUser(val userId: String) : ParsedCommand()
class BanUser(val userId: String, val reason: String?) : ParsedCommand()
class UnbanUser(val userId: String, val reason: String?) : ParsedCommand()
class SetUserPowerLevel(val userId: String, val powerLevel: Int) : ParsedCommand()
class Invite(val userId: String) : ParsedCommand()
class JoinRoom(val roomAlias: String) : ParsedCommand()
class PartRoom(val roomAlias: String) : ParsedCommand()
class Invite(val userId: String, val reason: String?) : ParsedCommand()
class JoinRoom(val roomAlias: String, val reason: String?) : ParsedCommand()
class PartRoom(val roomAlias: String, val reason: String?) : ParsedCommand()
class ChangeTopic(val topic: String) : ParsedCommand()
class KickUser(val userId: String, val reason: String) : ParsedCommand()
class KickUser(val userId: String, val reason: String?) : ParsedCommand()
class ChangeDisplayName(val displayName: String) : ParsedCommand()
class SetMarkdown(val enable: Boolean) : ParsedCommand()
object ClearScalarToken : ParsedCommand()

View File

@ -266,7 +266,7 @@ class RoomDetailViewModel @AssistedInject constructor(@Assisted initialState: Ro
}
}
session.rx()
.joinRoom(roomId, viaServer)
.joinRoom(roomId, null, viaServer)
.map { roomId }
.execute {
copy(tombstoneEventHandling = it)
@ -487,7 +487,7 @@ class RoomDetailViewModel @AssistedInject constructor(@Assisted initialState: Ro
private fun handleInviteSlashCommand(invite: ParsedCommand.Invite) {
_sendMessageResultLiveData.postLiveEvent(SendMessageResult.SlashCommandHandled())
room.invite(invite.userId, object : MatrixCallback<Unit> {
room.invite(invite.userId, invite.reason, object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {
_sendMessageResultLiveData.postLiveEvent(SendMessageResult.SlashCommandResultOk)
}
@ -553,11 +553,11 @@ class RoomDetailViewModel @AssistedInject constructor(@Assisted initialState: Ro
}
private fun handleRejectInvite() {
room.leave(object : MatrixCallback<Unit> {})
room.leave(null, object : MatrixCallback<Unit> {})
}
private fun handleAcceptInvite() {
room.join(callback = object : MatrixCallback<Unit> {})
room.join(null, callback = object : MatrixCallback<Unit> {})
}
private fun handleEditAction(action: RoomDetailAction.EnterEditMode) {

View File

@ -123,7 +123,7 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState,
)
}
session.getRoom(roomId)?.join(emptyList(), object : MatrixCallback<Unit> {
session.getRoom(roomId)?.join(null, emptyList(), object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {
// We do not update the joiningRoomsIds here, because, the room is not joined yet regarding the sync data.
// Instead, we wait for the room to be joined
@ -158,7 +158,7 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState,
)
}
session.getRoom(roomId)?.leave(object : MatrixCallback<Unit> {
session.getRoom(roomId)?.leave(null, object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {
// We do not update the rejectingRoomsIds here, because, the room is not rejected yet regarding the sync data.
// Instead, we wait for the room to be rejected
@ -197,7 +197,7 @@ class RoomListViewModel @Inject constructor(initialState: RoomListViewState,
}
private fun handleLeaveRoom(action: RoomListAction.LeaveRoom) {
session.getRoom(action.roomId)?.leave(object : MatrixCallback<Unit> {
session.getRoom(action.roomId)?.leave(null, object : MatrixCallback<Unit> {
override fun onFailure(failure: Throwable) {
_viewEvents.post(RoomListViewEvents.Failure(failure))
}

View File

@ -74,14 +74,14 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
private fun handleJoinRoom(roomId: String) {
activeSessionHolder.getSafeActiveSession()?.let { session ->
session.getRoom(roomId)
?.join(emptyList(), object : MatrixCallback<Unit> {})
?.join(null, emptyList(), object : MatrixCallback<Unit> {})
}
}
private fun handleRejectRoom(roomId: String) {
activeSessionHolder.getSafeActiveSession()?.let { session ->
session.getRoom(roomId)
?.leave(object : MatrixCallback<Unit> {})
?.leave(null, object : MatrixCallback<Unit> {})
}
}

View File

@ -214,7 +214,7 @@ class RoomDirectoryViewModel @AssistedInject constructor(@Assisted initialState:
)
}
session.joinRoom(action.roomId, emptyList(), object : MatrixCallback<Unit> {
session.joinRoom(action.roomId, null, emptyList(), object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {
// We do not update the joiningRoomsIds here, because, the room is not joined yet regarding the sync data.
// Instead, we wait for the room to be joined

View File

@ -97,7 +97,7 @@ class RoomPreviewViewModel @AssistedInject constructor(@Assisted initialState: R
)
}
session.joinRoom(state.roomId, emptyList(), object : MatrixCallback<Unit> {
session.joinRoom(state.roomId, null, emptyList(), object : MatrixCallback<Unit> {
override fun onSuccess(data: Unit) {
// We do not update the joiningRoomsIds here, because, the room is not joined yet regarding the sync data.
// Instead, we wait for the room to be joined