Skip to content

Commit ae03d00

Browse files
committed
Draft MSC3912
1 parent 5b22a12 commit ae03d00

File tree

12 files changed

+77
-13
lines changed

12 files changed

+77
-13
lines changed

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/homeserver/HomeServerCapabilities.kt

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ data class HomeServerCapabilities(
7575
* True if the home server supports remote toggle of Pusher for a given device.
7676
*/
7777
val canRemotelyTogglePushNotificationsOfDevices: Boolean = false,
78+
79+
/**
80+
* True if the home server supports event redaction with relations.
81+
*/
82+
var canRedactEventWithRelations: Boolean = false,
7883
) {
7984

8085
enum class RoomCapabilitySupport {

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/version/Versions.kt

+12
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ private const val FEATURE_QR_CODE_LOGIN = "org.matrix.msc3882"
5858
private const val FEATURE_THREADS_MSC3771 = "org.matrix.msc3771"
5959
private const val FEATURE_THREADS_MSC3773 = "org.matrix.msc3773"
6060
private const val FEATURE_REMOTE_TOGGLE_PUSH_NOTIFICATIONS_MSC3881 = "org.matrix.msc3881"
61+
private const val FEATURE_EVENT_REDACTION_WITH_RELATIONS = "org.matrix.msc3912"
62+
private const val FEATURE_EVENT_REDACTION_WITH_RELATIONS_STABLE = "org.matrix.msc3912.stable"
6163

6264
/**
6365
* Return true if the SDK supports this homeserver version.
@@ -153,3 +155,13 @@ private fun Versions.getMaxVersion(): HomeServerVersion {
153155
internal fun Versions.doesServerSupportRemoteToggleOfPushNotifications(): Boolean {
154156
return unstableFeatures?.get(FEATURE_REMOTE_TOGGLE_PUSH_NOTIFICATIONS_MSC3881).orFalse()
155157
}
158+
159+
/**
160+
* Indicate if the server supports MSC3912: https://github.com/matrix-org/matrix-spec-proposals/pull/3912.
161+
*
162+
* @return true if event redaction with relations is supported
163+
*/
164+
internal fun Versions.doesServerSupportRedactEventWithRelations(): Boolean {
165+
return unstableFeatures?.get(FEATURE_EVENT_REDACTION_WITH_RELATIONS).orFalse() ||
166+
unstableFeatures?.get(FEATURE_EVENT_REDACTION_WITH_RELATIONS_STABLE).orFalse()
167+
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/tasks/RedactEventTask.kt

+14-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*/
1616
package org.matrix.android.sdk.internal.crypto.tasks
1717

18+
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilitiesService
1819
import org.matrix.android.sdk.internal.network.GlobalErrorReceiver
1920
import org.matrix.android.sdk.internal.network.executeRequest
21+
import org.matrix.android.sdk.internal.session.room.EventRedactBody
2022
import org.matrix.android.sdk.internal.session.room.RoomAPI
2123
import org.matrix.android.sdk.internal.task.Task
2224
import javax.inject.Inject
@@ -26,22 +28,31 @@ internal interface RedactEventTask : Task<RedactEventTask.Params, String> {
2628
val txID: String,
2729
val roomId: String,
2830
val eventId: String,
29-
val reason: String?
31+
val reason: String?,
32+
val withRelations: List<String>?
3033
)
3134
}
3235

3336
internal class DefaultRedactEventTask @Inject constructor(
3437
private val roomAPI: RoomAPI,
35-
private val globalErrorReceiver: GlobalErrorReceiver
38+
private val globalErrorReceiver: GlobalErrorReceiver,
39+
private val homeServerCapabilitiesService: HomeServerCapabilitiesService,
3640
) : RedactEventTask {
3741

3842
override suspend fun execute(params: RedactEventTask.Params): String {
43+
val withRelations = if (homeServerCapabilitiesService.getHomeServerCapabilities().canRedactEventWithRelations &&
44+
!params.withRelations.isNullOrEmpty()) {
45+
params.withRelations
46+
} else {
47+
null
48+
}
49+
3950
val response = executeRequest(globalErrorReceiver) {
4051
roomAPI.redactEvent(
4152
txId = params.txID,
4253
roomId = params.roomId,
4354
eventId = params.eventId,
44-
reason = if (params.reason == null) emptyMap() else mapOf("reason" to params.reason)
55+
body = EventRedactBody(params.reason, withRelations)
4556
)
4657
}
4758
return response.eventId

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/mapper/HomeServerCapabilitiesMapper.kt

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ internal object HomeServerCapabilitiesMapper {
4747
canLoginWithQrCode = entity.canLoginWithQrCode,
4848
canUseThreadReadReceiptsAndNotifications = entity.canUseThreadReadReceiptsAndNotifications,
4949
canRemotelyTogglePushNotificationsOfDevices = entity.canRemotelyTogglePushNotificationsOfDevices,
50+
canRedactEventWithRelations = entity.canRedactEventWithRelations,
5051
)
5152
}
5253

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/model/HomeServerCapabilitiesEntity.kt

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ internal open class HomeServerCapabilitiesEntity(
3434
var canLoginWithQrCode: Boolean = false,
3535
var canUseThreadReadReceiptsAndNotifications: Boolean = false,
3636
var canRemotelyTogglePushNotificationsOfDevices: Boolean = false,
37+
var canRedactEventWithRelations: Boolean = false,
3738
) : RealmObject() {
3839

3940
companion object

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/homeserver/GetHomeServerCapabilitiesTask.kt

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities
2525
import org.matrix.android.sdk.internal.auth.version.Versions
2626
import org.matrix.android.sdk.internal.auth.version.doesServerSupportLogoutDevices
2727
import org.matrix.android.sdk.internal.auth.version.doesServerSupportQrCodeLogin
28+
import org.matrix.android.sdk.internal.auth.version.doesServerSupportRedactEventWithRelations
2829
import org.matrix.android.sdk.internal.auth.version.doesServerSupportRemoteToggleOfPushNotifications
2930
import org.matrix.android.sdk.internal.auth.version.doesServerSupportThreadUnreadNotifications
3031
import org.matrix.android.sdk.internal.auth.version.doesServerSupportThreads
@@ -154,6 +155,8 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
154155
getVersionResult.doesServerSupportQrCodeLogin()
155156
homeServerCapabilitiesEntity.canRemotelyTogglePushNotificationsOfDevices =
156157
getVersionResult.doesServerSupportRemoteToggleOfPushNotifications()
158+
homeServerCapabilitiesEntity.canRedactEventWithRelations =
159+
getVersionResult.doesServerSupportRedactEventWithRelations()
157160
}
158161

159162
if (getWellknownResult != null && getWellknownResult is WellknownResult.Prompt) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2021 The Matrix.org Foundation C.I.C.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.matrix.android.sdk.internal.session.room
18+
19+
import com.squareup.moshi.Json
20+
import com.squareup.moshi.JsonClass
21+
22+
@JsonClass(generateAdapter = true)
23+
internal data class EventRedactBody(
24+
@Json(name = "reason")
25+
val reason: String? = null,
26+
27+
@Json(name = "with_relations")
28+
val withRelations: List<String>? = null
29+
)

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/RoomAPI.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,14 @@ internal interface RoomAPI {
350350
* @param txId the transaction Id
351351
* @param roomId the room id
352352
* @param eventId the event to delete
353-
* @param reason json containing reason key {"reason": "Indecent material"}
353+
* @param body body containing reason key {"reason": "Indecent material"} and with_relations
354354
*/
355355
@PUT(NetworkConstants.URI_API_PREFIX_PATH_R0 + "rooms/{roomId}/redact/{eventId}/{txnId}")
356356
suspend fun redactEvent(
357357
@Path("txnId") txId: String,
358358
@Path("roomId") roomId: String,
359359
@Path("eventId") eventId: String,
360-
@Body reason: Map<String, String>
360+
@Body body: EventRedactBody
361361
): SendResponse
362362

363363
/**

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/queue/EventSenderProcessor.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ internal interface EventSenderProcessor : SessionLifecycleObserver {
2626

2727
fun postEvent(event: Event, encrypt: Boolean): Cancelable
2828

29-
fun postRedaction(redactionLocalEcho: Event, reason: String?): Cancelable
29+
fun postRedaction(redactionLocalEcho: Event, reason: String?, withRelations: List<String>?): Cancelable
3030

31-
fun postRedaction(redactionLocalEchoId: String, eventToRedactId: String, roomId: String, reason: String?): Cancelable
31+
fun postRedaction(redactionLocalEchoId: String, eventToRedactId: String, roomId: String, reason: String?, withRelations: List<String>?): Cancelable
3232

3333
fun postTask(task: QueuedTask): Cancelable
3434

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/queue/EventSenderProcessorCoroutine.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ internal class EventSenderProcessorCoroutine @Inject constructor(
101101
return postTask(task)
102102
}
103103

104-
override fun postRedaction(redactionLocalEcho: Event, reason: String?): Cancelable {
105-
return postRedaction(redactionLocalEcho.eventId!!, redactionLocalEcho.redacts!!, redactionLocalEcho.roomId!!, reason)
104+
override fun postRedaction(redactionLocalEcho: Event, reason: String?, withRelations: List<String>?): Cancelable {
105+
return postRedaction(redactionLocalEcho.eventId!!, redactionLocalEcho.redacts!!, redactionLocalEcho.roomId!!, reason, withRelations)
106106
}
107107

108-
override fun postRedaction(redactionLocalEchoId: String, eventToRedactId: String, roomId: String, reason: String?): Cancelable {
109-
val task = queuedTaskFactory.createRedactTask(redactionLocalEchoId, eventToRedactId, roomId, reason)
108+
override fun postRedaction(redactionLocalEchoId: String, eventToRedactId: String, roomId: String, reason: String?, withRelations: List<String>?): Cancelable {
109+
val task = queuedTaskFactory.createRedactTask(redactionLocalEchoId, eventToRedactId, roomId, reason, withRelations)
110110
return postTask(task)
111111
}
112112

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/queue/QueuedTaskFactory.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ internal class QueuedTaskFactory @Inject constructor(
4343
)
4444
}
4545

46-
fun createRedactTask(redactionLocalEcho: String, eventId: String, roomId: String, reason: String?): QueuedTask {
46+
fun createRedactTask(redactionLocalEcho: String, eventId: String, roomId: String, reason: String?, withRelations: List<String>?): QueuedTask {
4747
return RedactQueuedTask(
4848
redactionLocalEchoId = redactionLocalEcho,
4949
toRedactEventId = eventId,
5050
roomId = roomId,
5151
reason = reason,
52+
withRelations = withRelations,
5253
redactEventTask = redactEventTask,
5354
localEchoRepository = localEchoRepository,
5455
cancelSendTracker = cancelSendTracker

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/queue/RedactQueuedTask.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ internal class RedactQueuedTask(
2626
val redactionLocalEchoId: String,
2727
private val roomId: String,
2828
private val reason: String?,
29+
private val withRelations: List<String>?,
2930
private val redactEventTask: RedactEventTask,
3031
private val localEchoRepository: LocalEchoRepository,
3132
private val cancelSendTracker: CancelSendTracker
3233
) : QueuedTask(queueIdentifier = roomId, taskIdentifier = redactionLocalEchoId) {
3334

3435
override suspend fun doExecute() {
35-
redactEventTask.execute(RedactEventTask.Params(redactionLocalEchoId, roomId, toRedactEventId, reason))
36+
redactEventTask.execute(RedactEventTask.Params(redactionLocalEchoId, roomId, toRedactEventId, reason, withRelations))
3637
}
3738

3839
override fun onTaskFailed() {

0 commit comments

Comments
 (0)