2019-01-18 10:12:08 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* * Copyright 2019 New Vector Ltd
|
|
|
|
* *
|
|
|
|
* * 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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
|
2018-11-09 08:43:12 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.Event
|
|
|
|
import im.vector.matrix.android.api.session.events.model.EventType
|
2018-12-17 17:24:01 +00:00
|
|
|
import im.vector.matrix.android.internal.database.mapper.ContentMapper
|
2018-11-09 08:43:12 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.EventEntity
|
|
|
|
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
|
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
|
|
|
|
|
|
|
|
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)
|
|
|
|
internal data class Params(
|
2019-01-14 15:18:39 +00:00
|
|
|
val redactionEvents: List<Event>
|
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)
|
2018-12-17 17:24:01 +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 ->
|
|
|
|
params.redactionEvents.forEach { event ->
|
|
|
|
pruneEvent(realm, event)
|
2018-11-09 08:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-13 10:00:50 +00:00
|
|
|
return result.fold({ Result.retry() }, { Result.success() })
|
2018-11-09 08:43:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun pruneEvent(realm: Realm, redactionEvent: Event?) {
|
|
|
|
if (redactionEvent == null || redactionEvent.redacts.isNullOrEmpty()) {
|
|
|
|
return
|
|
|
|
}
|
2018-12-17 17:24:01 +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)
|
|
|
|
}
|
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) {
|
|
|
|
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",
|
2018-12-17 17:24:01 +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")
|
2019-01-18 15:26:17 +00:00
|
|
|
else -> emptyList()
|
2018-11-09 08:43:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|