2019-01-18 10:12:08 +00:00
|
|
|
/*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Copyright 2019 New Vector Ltd
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* 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
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* 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.
|
2019-01-18 10:12:08 +00:00
|
|
|
*/
|
|
|
|
|
2018-12-20 15:27:59 +00:00
|
|
|
package im.vector.matrix.android.internal.session.room.prune
|
2018-11-09 08:43:12 +00:00
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import androidx.work.Worker
|
|
|
|
import androidx.work.WorkerParameters
|
2018-11-09 13:06:23 +00:00
|
|
|
import com.squareup.moshi.JsonClass
|
2018-11-27 11:06:40 +00:00
|
|
|
import com.zhuinden.monarchy.Monarchy
|
2019-05-17 15:15:44 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.Event
|
2018-11-09 08:43:12 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.EventType
|
2019-05-17 15:15:44 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.UnsignedData
|
|
|
|
import im.vector.matrix.android.api.session.events.model.toModel
|
|
|
|
import im.vector.matrix.android.api.session.room.model.annotation.ReactionContent
|
2018-12-17 17:24:01 +00:00
|
|
|
import im.vector.matrix.android.internal.database.mapper.ContentMapper
|
2019-05-17 15:15:44 +00:00
|
|
|
import im.vector.matrix.android.internal.database.mapper.EventMapper
|
|
|
|
import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryEntity
|
2018-11-09 08:43:12 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.EventEntity
|
2019-05-17 15:15:44 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.ReactionAggregatedSummaryEntityFields
|
2018-11-09 08:43:12 +00:00
|
|
|
import im.vector.matrix.android.internal.database.query.where
|
2018-12-20 14:44:01 +00:00
|
|
|
import im.vector.matrix.android.internal.di.MatrixKoinComponent
|
2019-05-17 15:15:44 +00:00
|
|
|
import im.vector.matrix.android.internal.di.MoshiProvider
|
2018-11-09 13:06:23 +00:00
|
|
|
import im.vector.matrix.android.internal.util.WorkerParamsFactory
|
2019-01-14 15:18:39 +00:00
|
|
|
import im.vector.matrix.android.internal.util.tryTransactionSync
|
2018-11-09 08:43:12 +00:00
|
|
|
import io.realm.Realm
|
|
|
|
import org.koin.standalone.inject
|
2019-05-17 15:15:44 +00:00
|
|
|
import timber.log.Timber
|
2018-11-09 08:43:12 +00:00
|
|
|
|
2019-05-19 10:07:27 +00:00
|
|
|
//TODO should be a task instead of worker
|
2018-11-09 08:43:12 +00:00
|
|
|
internal class PruneEventWorker(context: Context,
|
|
|
|
workerParameters: WorkerParameters
|
2018-12-20 14:44:01 +00:00
|
|
|
) : Worker(context, workerParameters), MatrixKoinComponent {
|
2018-11-09 08:43:12 +00:00
|
|
|
|
2018-11-09 13:06:23 +00:00
|
|
|
@JsonClass(generateAdapter = true)
|
2019-04-04 17:55:58 +00:00
|
|
|
internal class Params(
|
2019-05-19 10:07:27 +00:00
|
|
|
val redactionEvents: List<Event>,
|
|
|
|
val userId: String
|
2018-11-09 13:06:23 +00:00
|
|
|
)
|
|
|
|
|
2018-11-27 11:06:40 +00:00
|
|
|
private val monarchy by inject<Monarchy>()
|
2018-11-09 08:43:12 +00:00
|
|
|
|
|
|
|
override fun doWork(): Result {
|
2018-11-09 13:06:23 +00:00
|
|
|
val params = WorkerParamsFactory.fromData<Params>(inputData)
|
2019-05-17 15:15:44 +00:00
|
|
|
?: return Result.failure()
|
2018-11-09 13:06:23 +00:00
|
|
|
|
2019-01-14 15:18:39 +00:00
|
|
|
val result = monarchy.tryTransactionSync { realm ->
|
2019-05-17 15:15:44 +00:00
|
|
|
params.redactionEvents.forEach { event ->
|
2019-05-19 10:07:27 +00:00
|
|
|
pruneEvent(realm, event, params.userId)
|
2018-11-09 08:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-17 15:15:44 +00:00
|
|
|
return result.fold({
|
|
|
|
Result.retry()
|
|
|
|
}, {
|
|
|
|
Result.success()
|
|
|
|
})
|
2018-11-09 08:43:12 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 10:07:27 +00:00
|
|
|
private fun pruneEvent(realm: Realm, redactionEvent: Event, userId: String) {
|
2019-05-17 15:15:44 +00:00
|
|
|
if (redactionEvent.redacts.isNullOrBlank()) {
|
2018-11-09 08:43:12 +00:00
|
|
|
return
|
|
|
|
}
|
2019-04-04 17:55:58 +00:00
|
|
|
|
2019-05-17 15:15:44 +00:00
|
|
|
val eventToPrune = EventEntity.where(realm, eventId = redactionEvent.redacts).findFirst()
|
|
|
|
?: return
|
2018-11-09 08:43:12 +00:00
|
|
|
|
|
|
|
val allowedKeys = computeAllowedKeys(eventToPrune.type)
|
2018-12-17 17:24:01 +00:00
|
|
|
if (allowedKeys.isNotEmpty()) {
|
|
|
|
val prunedContent = ContentMapper.map(eventToPrune.content)?.filterKeys { key -> allowedKeys.contains(key) }
|
|
|
|
eventToPrune.content = ContentMapper.map(prunedContent)
|
2019-05-17 15:15:44 +00:00
|
|
|
} else {
|
|
|
|
when (eventToPrune.type) {
|
|
|
|
EventType.MESSAGE -> {
|
|
|
|
Timber.d("REDACTION for message ${eventToPrune.eventId}")
|
|
|
|
val unsignedData = EventMapper.map(eventToPrune).unsignedData
|
|
|
|
?: UnsignedData(null, null)
|
|
|
|
val modified = unsignedData.copy(redactedEvent = redactionEvent)
|
|
|
|
eventToPrune.content = ContentMapper.map(emptyMap())
|
|
|
|
eventToPrune.unsignedData = MoshiProvider.providesMoshi().adapter(UnsignedData::class.java).toJson(modified)
|
2019-05-19 10:07:27 +00:00
|
|
|
|
2019-05-17 15:15:44 +00:00
|
|
|
}
|
|
|
|
EventType.REACTION -> {
|
|
|
|
Timber.d("REDACTION of reaction ${eventToPrune.eventId}")
|
|
|
|
//delete a reaction, need to update the annotation summary if any
|
|
|
|
val reactionContent: ReactionContent = EventMapper.map(eventToPrune).content.toModel()
|
|
|
|
?: return
|
|
|
|
val eventThatWasReacted = reactionContent.relatesTo?.eventId ?: return
|
|
|
|
|
|
|
|
val reactionkey = reactionContent.relatesTo.key
|
|
|
|
Timber.d("REMOVE reaction for key $reactionkey")
|
|
|
|
val summary = EventAnnotationsSummaryEntity.where(realm, eventThatWasReacted).findFirst()
|
|
|
|
if (summary != null) {
|
|
|
|
summary.reactionsSummary.where()
|
|
|
|
.equalTo(ReactionAggregatedSummaryEntityFields.KEY, reactionkey)
|
|
|
|
.findFirst()?.let { summary ->
|
|
|
|
Timber.d("Find summary for key with ${summary.sourceEvents.size} known reactions (count:${summary.count})")
|
|
|
|
Timber.d("Known reactions ${summary.sourceEvents.joinToString(",")}")
|
|
|
|
if (summary.sourceEvents.contains(eventToPrune.eventId)) {
|
|
|
|
Timber.d("REMOVE reaction for key $reactionkey")
|
|
|
|
summary.sourceEvents.remove(eventToPrune.eventId)
|
|
|
|
Timber.d("Known reactions after ${summary.sourceEvents.joinToString(",")}")
|
|
|
|
summary.count = summary.count - 1
|
2019-05-19 10:07:27 +00:00
|
|
|
if (eventToPrune.sender == userId) {
|
|
|
|
//Was it a redact on my reaction?
|
|
|
|
summary.addedByMe = false
|
|
|
|
}
|
2019-05-17 15:15:44 +00:00
|
|
|
if (summary.count == 0) {
|
|
|
|
//delete!
|
|
|
|
summary.deleteFromRealm()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Timber.e("## Cannot remove summary from count, corresponding reaction ${eventToPrune.eventId} is not known")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Timber.e("## Cannot find summary for key $reactionkey")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-12-17 17:24:01 +00:00
|
|
|
}
|
2018-11-09 08:43:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 17:24:01 +00:00
|
|
|
private fun computeAllowedKeys(type: String): List<String> {
|
2018-11-09 08:43:12 +00:00
|
|
|
// Add filtered content, allowed keys in content depends on the event type
|
2018-12-17 17:24:01 +00:00
|
|
|
return when (type) {
|
2019-05-17 15:15:44 +00:00
|
|
|
EventType.STATE_ROOM_MEMBER -> listOf("membership")
|
|
|
|
EventType.STATE_ROOM_CREATE -> listOf("creator")
|
|
|
|
EventType.STATE_ROOM_JOIN_RULES -> listOf("join_rule")
|
2018-11-09 08:43:12 +00:00
|
|
|
EventType.STATE_ROOM_POWER_LEVELS -> listOf("users",
|
2019-05-17 15:15:44 +00:00
|
|
|
"users_default",
|
|
|
|
"events",
|
|
|
|
"events_default",
|
|
|
|
"state_default",
|
|
|
|
"ban",
|
|
|
|
"kick",
|
|
|
|
"redact",
|
|
|
|
"invite")
|
|
|
|
EventType.STATE_ROOM_ALIASES -> listOf("aliases")
|
|
|
|
EventType.STATE_CANONICAL_ALIAS -> listOf("alias")
|
|
|
|
EventType.FEEDBACK -> listOf("type", "target_event_id")
|
|
|
|
else -> emptyList()
|
2018-11-09 08:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|