Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MSC3912 implementation: Redaction of related events #8532

Merged
merged 2 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/8481.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update MSC3912 implementation: Redaction of related events
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ data class HomeServerCapabilities(
val canRemotelyTogglePushNotificationsOfDevices: Boolean = false,

/**
* True if the home server supports event redaction with relations.
* True if the home server supports redaction of related events.
*/
var canRedactEventWithRelations: Boolean = false,
var canRedactRelatedEvents: Boolean = false,

/**
* External account management url for use with MSC3824 delegated OIDC, provided in Wellknown.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ interface SendService {
* Redact (delete) the given event.
* @param event the event to redact
* @param reason optional reason string
* @param withRelations the list of relation types to redact with this event
* @param withRelTypes the list of relation types to redact with this event
* @param additionalContent additional content to put in the event content
*/
fun redactEvent(event: Event, reason: String?, withRelations: List<String>? = null, additionalContent: Content? = null): Cancelable
fun redactEvent(event: Event, reason: String?, withRelTypes: List<String>? = null, additionalContent: Content? = null): Cancelable

/**
* Schedule this message to be resent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ private const val FEATURE_QR_CODE_LOGIN = "org.matrix.msc3882"
private const val FEATURE_THREADS_MSC3771 = "org.matrix.msc3771"
private const val FEATURE_THREADS_MSC3773 = "org.matrix.msc3773"
private const val FEATURE_REMOTE_TOGGLE_PUSH_NOTIFICATIONS_MSC3881 = "org.matrix.msc3881"
private const val FEATURE_EVENT_REDACTION_WITH_RELATIONS = "org.matrix.msc3912"
private const val FEATURE_EVENT_REDACTION_WITH_RELATIONS_STABLE = "org.matrix.msc3912.stable"
private const val FEATURE_REDACTION_OF_RELATED_EVENT = "org.matrix.msc3912"

/**
* Return true if the SDK supports this homeserver version.
Expand Down Expand Up @@ -162,9 +161,8 @@ internal fun Versions.doesServerSupportRemoteToggleOfPushNotifications(): Boolea
/**
* Indicate if the server supports MSC3912: https://github.com/matrix-org/matrix-spec-proposals/pull/3912.
*
* @return true if event redaction with relations is supported
* @return true if redaction of related events is supported
*/
internal fun Versions.doesServerSupportRedactEventWithRelations(): Boolean {
return unstableFeatures?.get(FEATURE_EVENT_REDACTION_WITH_RELATIONS).orFalse() ||
unstableFeatures?.get(FEATURE_EVENT_REDACTION_WITH_RELATIONS_STABLE).orFalse()
internal fun Versions.doesServerSupportRedactionOfRelatedEvents(): Boolean {
return unstableFeatures?.get(FEATURE_REDACTION_OF_RELATED_EVENT).orFalse()
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal interface RedactEventTask : Task<RedactEventTask.Params, String> {
val roomId: String,
val eventId: String,
val reason: String?,
val withRelations: List<String>?,
val withRelTypes: List<String>?,
)
}

Expand All @@ -41,9 +41,9 @@ internal class DefaultRedactEventTask @Inject constructor(
) : RedactEventTask {

override suspend fun execute(params: RedactEventTask.Params): String {
val withRelations = if (homeServerCapabilitiesDataSource.getHomeServerCapabilities()?.canRedactEventWithRelations.orFalse() &&
!params.withRelations.isNullOrEmpty()) {
params.withRelations
val withRelTypes = if (homeServerCapabilitiesDataSource.getHomeServerCapabilities()?.canRedactRelatedEvents.orFalse() &&
!params.withRelTypes.isNullOrEmpty()) {
params.withRelTypes
} else {
null
}
Expand All @@ -55,7 +55,7 @@ internal class DefaultRedactEventTask @Inject constructor(
eventId = params.eventId,
body = EventRedactBody(
reason = params.reason,
withRelations = withRelations,
unstableWithRelTypes = withRelTypes,
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal object HomeServerCapabilitiesMapper {
canLoginWithQrCode = entity.canLoginWithQrCode,
canUseThreadReadReceiptsAndNotifications = entity.canUseThreadReadReceiptsAndNotifications,
canRemotelyTogglePushNotificationsOfDevices = entity.canRemotelyTogglePushNotificationsOfDevices,
canRedactEventWithRelations = entity.canRedactEventWithRelations,
canRedactRelatedEvents = entity.canRedactEventWithRelations,
externalAccountManagementUrl = entity.externalAccountManagementUrl,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilities
import org.matrix.android.sdk.internal.auth.version.Versions
import org.matrix.android.sdk.internal.auth.version.doesServerSupportLogoutDevices
import org.matrix.android.sdk.internal.auth.version.doesServerSupportQrCodeLogin
import org.matrix.android.sdk.internal.auth.version.doesServerSupportRedactEventWithRelations
import org.matrix.android.sdk.internal.auth.version.doesServerSupportRedactionOfRelatedEvents
import org.matrix.android.sdk.internal.auth.version.doesServerSupportRemoteToggleOfPushNotifications
import org.matrix.android.sdk.internal.auth.version.doesServerSupportThreadUnreadNotifications
import org.matrix.android.sdk.internal.auth.version.doesServerSupportThreads
Expand Down Expand Up @@ -154,7 +154,7 @@ internal class DefaultGetHomeServerCapabilitiesTask @Inject constructor(
homeServerCapabilitiesEntity.canRemotelyTogglePushNotificationsOfDevices =
getVersionResult.doesServerSupportRemoteToggleOfPushNotifications()
homeServerCapabilitiesEntity.canRedactEventWithRelations =
getVersionResult.doesServerSupportRedactEventWithRelations()
getVersionResult.doesServerSupportRedactionOfRelatedEvents()
}

if (getWellknownResult != null && getWellknownResult is WellknownResult.Prompt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ internal class DefaultSendService @AssistedInject constructor(
.let { sendEvent(it) }
}

override fun redactEvent(event: Event, reason: String?, withRelations: List<String>?, additionalContent: Content?): Cancelable {
override fun redactEvent(event: Event, reason: String?, withRelTypes: List<String>?, additionalContent: Content?): Cancelable {
// TODO manage media/attachements?
val redactionEcho = localEchoEventFactory.createRedactEvent(roomId, event.eventId!!, reason, withRelations, additionalContent)
val redactionEcho = localEchoEventFactory.createRedactEvent(roomId, event.eventId!!, reason, withRelTypes, additionalContent)
.also { createLocalEcho(it) }
return eventSenderProcessor.postRedaction(redactionEcho, reason, withRelations)
return eventSenderProcessor.postRedaction(redactionEcho, reason, withRelTypes)
}

override fun resendTextMessage(localEcho: TimelineEvent): Cancelable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,12 +812,12 @@ internal class LocalEchoEventFactory @Inject constructor(
}
}
*/
fun createRedactEvent(roomId: String, eventId: String, reason: String?, withRelations: List<String>? = null, additionalContent: Content? = null): Event {
fun createRedactEvent(roomId: String, eventId: String, reason: String?, withRelTypes: List<String>? = null, additionalContent: Content? = null): Event {
val localId = LocalEcho.createLocalEchoId()
val content = if (reason != null || withRelations != null) {
val content = if (reason != null || withRelTypes != null) {
EventRedactBody(
reason = reason,
withRelations = withRelations,
unstableWithRelTypes = withRelTypes,
).toContent().plus(additionalContent.orEmpty())
} else {
additionalContent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal class RedactEventWorker(context: Context, params: WorkerParameters, ses
val roomId: String,
val eventId: String,
val reason: String?,
val withRelations: List<String>? = null,
val withRelTypes: List<String>? = null,
override val lastFailureMessage: String? = null
) : SessionWorkerParams

Expand All @@ -63,7 +63,7 @@ internal class RedactEventWorker(context: Context, params: WorkerParameters, ses
roomId = params.roomId,
eventId = params.eventId,
reason = params.reason,
withRelations = params.withRelations,
withRelTypes = params.withRelTypes,
)
)
}.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ internal data class EventRedactBody(
val reason: String? = null,

@Json(name = "org.matrix.msc3912.with_relations")
val withRelations: List<String>? = null,
)
val unstableWithRelTypes: List<String>? = null,

@Json(name = "with_rel_types")
val withRelTypes: List<String>? = null,
) {
fun getBestWithRelTypes() = withRelTypes ?: unstableWithRelTypes
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ internal interface EventSenderProcessor : SessionLifecycleObserver {

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

fun postRedaction(redactionLocalEcho: Event, reason: String?, withRelations: List<String>? = null): Cancelable
fun postRedaction(redactionLocalEcho: Event, reason: String?, withRelTypes: List<String>? = null): Cancelable

fun postRedaction(redactionLocalEchoId: String, eventToRedactId: String, roomId: String, reason: String?, withRelations: List<String>? = null): Cancelable
fun postRedaction(redactionLocalEchoId: String, eventToRedactId: String, roomId: String, reason: String?, withRelTypes: List<String>? = null): Cancelable

fun postTask(task: QueuedTask): Cancelable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,18 @@ internal class EventSenderProcessorCoroutine @Inject constructor(
return postTask(task)
}

override fun postRedaction(redactionLocalEcho: Event, reason: String?, withRelations: List<String>?): Cancelable {
return postRedaction(redactionLocalEcho.eventId!!, redactionLocalEcho.redacts!!, redactionLocalEcho.roomId!!, reason, withRelations)
override fun postRedaction(redactionLocalEcho: Event, reason: String?, withRelTypes: List<String>?): Cancelable {
return postRedaction(redactionLocalEcho.eventId!!, redactionLocalEcho.redacts!!, redactionLocalEcho.roomId!!, reason, withRelTypes)
}

override fun postRedaction(
redactionLocalEchoId: String,
eventToRedactId: String,
roomId: String,
reason: String?,
withRelations: List<String>?
withRelTypes: List<String>?
): Cancelable {
val task = queuedTaskFactory.createRedactTask(redactionLocalEchoId, eventToRedactId, roomId, reason, withRelations)
val task = queuedTaskFactory.createRedactTask(redactionLocalEchoId, eventToRedactId, roomId, reason, withRelTypes)
return postTask(task)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ internal class QueueMemento @Inject constructor(
eventId = it.redacts,
roomId = it.roomId,
reason = body?.reason,
withRelations = body?.withRelations,
withRelTypes = body?.getBestWithRelTypes(),
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ internal class QueuedTaskFactory @Inject constructor(
)
}

fun createRedactTask(redactionLocalEcho: String, eventId: String, roomId: String, reason: String?, withRelations: List<String>? = null): QueuedTask {
fun createRedactTask(redactionLocalEcho: String, eventId: String, roomId: String, reason: String?, withRelTypes: List<String>? = null): QueuedTask {
return RedactQueuedTask(
redactionLocalEchoId = redactionLocalEcho,
toRedactEventId = eventId,
roomId = roomId,
reason = reason,
withRelations = withRelations,
withRelTypes = withRelTypes,
redactEventTask = redactEventTask,
localEchoRepository = localEchoRepository,
cancelSendTracker = cancelSendTracker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ internal class RedactQueuedTask(
val redactionLocalEchoId: String,
private val roomId: String,
private val reason: String?,
private val withRelations: List<String>?,
private val withRelTypes: List<String>?,
private val redactEventTask: RedactEventTask,
private val localEchoRepository: LocalEchoRepository,
private val cancelSendTracker: CancelSendTracker
) : QueuedTask(queueIdentifier = roomId, taskIdentifier = redactionLocalEchoId) {

override suspend fun doExecute() {
redactEventTask.execute(RedactEventTask.Params(redactionLocalEchoId, roomId, toRedactEventId, reason, withRelations))
redactEventTask.execute(RedactEventTask.Params(redactionLocalEchoId, roomId, toRedactEventId, reason, withRelTypes))
}

override fun onTaskFailed() {
Expand Down