Merge pull request #626 from vector-im/feature/myUserIdInject

Inject userId where possible
This commit is contained in:
Benoit Marty 2019-10-21 16:01:48 +02:00 committed by GitHub
commit 723a007c39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 44 additions and 47 deletions

View file

@ -50,21 +50,19 @@ interface RelationService {
/**
* Sends a reaction (emoji) to the targetedEvent.
* @param reaction the reaction (preferably emoji)
* @param targetEventId the id of the event being reacted
* @param reaction the reaction (preferably emoji)
*/
fun sendReaction(reaction: String,
targetEventId: String): Cancelable
fun sendReaction(targetEventId: String,
reaction: String): Cancelable
/**
* Undo a reaction (emoji) to the targetedEvent.
* @param reaction the reaction (preferably emoji)
* @param targetEventId the id of the event being reacted
* @param myUserId used to know if a reaction event was made by the user
* @param reaction the reaction (preferably emoji)
*/
fun undoReaction(reaction: String,
targetEventId: String,
myUserId: String) // : Cancelable
fun undoReaction(targetEventId: String,
reaction: String): Cancelable
/**
* Edit a text message body. Limited to "m.text" contentType
@ -92,7 +90,7 @@ interface RelationService {
compatibilityBodyText: String = "* $newBodyText"): Cancelable
/**
* Get's the edit history of the given event
* Get the edit history of the given event
*/
fun fetchEditHistory(eventId: String, callback: MatrixCallback<List<Event>>)

View file

@ -50,6 +50,7 @@ import im.vector.matrix.android.internal.crypto.model.OlmInboundGroupSessionWrap
import im.vector.matrix.android.internal.crypto.store.IMXCryptoStore
import im.vector.matrix.android.internal.crypto.store.db.model.KeysBackupDataEntity
import im.vector.matrix.android.internal.di.MoshiProvider
import im.vector.matrix.android.internal.di.UserId
import im.vector.matrix.android.internal.extensions.foldToCallback
import im.vector.matrix.android.internal.session.SessionScope
import im.vector.matrix.android.internal.task.Task
@ -77,6 +78,7 @@ import kotlin.random.Random
@SessionScope
internal class KeysBackup @Inject constructor(
@UserId private val userId: String,
private val credentials: Credentials,
private val cryptoStore: IMXCryptoStore,
private val olmDevice: MXOlmDevice,
@ -375,8 +377,6 @@ internal class KeysBackup @Inject constructor(
*/
@WorkerThread
private fun getKeysBackupTrustBg(keysBackupVersion: KeysVersionResult): KeysBackupVersionTrust {
val myUserId = credentials.userId
val keysBackupVersionTrust = KeysBackupVersionTrust()
val authData = keysBackupVersion.getAuthDataAsMegolmBackupAuthData()
@ -388,7 +388,7 @@ internal class KeysBackup @Inject constructor(
return keysBackupVersionTrust
}
val mySigs = authData.signatures?.get(myUserId)
val mySigs = authData.signatures?.get(userId)
if (mySigs.isNullOrEmpty()) {
Timber.v("getKeysBackupTrust: Ignoring key backup because it lacks any signatures from this user")
return keysBackupVersionTrust
@ -403,7 +403,7 @@ internal class KeysBackup @Inject constructor(
}
if (deviceId != null) {
val device = cryptoStore.getUserDevice(deviceId, myUserId)
val device = cryptoStore.getUserDevice(deviceId, userId)
var isSignatureValid = false
if (device == null) {
@ -450,10 +450,8 @@ internal class KeysBackup @Inject constructor(
} else {
GlobalScope.launch(coroutineDispatchers.main) {
val updateKeysBackupVersionBody = withContext(coroutineDispatchers.crypto) {
val myUserId = credentials.userId
// Get current signatures, or create an empty set
val myUserSignatures = authData.signatures?.get(myUserId)?.toMutableMap()
val myUserSignatures = authData.signatures?.get(userId)?.toMutableMap()
?: HashMap()
if (trust) {
@ -462,7 +460,7 @@ internal class KeysBackup @Inject constructor(
val deviceSignatures = objectSigner.signObject(canonicalJson)
deviceSignatures[myUserId]?.forEach { entry ->
deviceSignatures[userId]?.forEach { entry ->
myUserSignatures[entry.key] = entry.value
}
} else {
@ -478,7 +476,7 @@ internal class KeysBackup @Inject constructor(
val newMegolmBackupAuthData = authData.copy()
val newSignatures = newMegolmBackupAuthData.signatures!!.toMutableMap()
newSignatures[myUserId] = myUserSignatures
newSignatures[userId] = myUserSignatures
newMegolmBackupAuthData.signatures = newSignatures
@ -1411,5 +1409,5 @@ internal class KeysBackup @Inject constructor(
* DEBUG INFO
* ========================================================================================== */
override fun toString() = "KeysBackup for ${credentials.userId}"
override fun toString() = "KeysBackup for $userId"
}

View file

@ -65,7 +65,7 @@ internal class DefaultRelationService @AssistedInject constructor(@Assisted priv
fun create(roomId: String): RelationService
}
override fun sendReaction(reaction: String, targetEventId: String): Cancelable {
override fun sendReaction(targetEventId: String, reaction: String): Cancelable {
val event = eventFactory.createReactionEvent(roomId, targetEventId, reaction)
.also {
saveLocalEcho(it)
@ -75,13 +75,13 @@ internal class DefaultRelationService @AssistedInject constructor(@Assisted priv
return CancelableWork(context, sendRelationWork.id)
}
override fun undoReaction(reaction: String, targetEventId: String, myUserId: String)/*: Cancelable*/ {
override fun undoReaction(targetEventId: String, reaction: String): Cancelable {
val params = FindReactionEventForUndoTask.Params(
roomId,
targetEventId,
reaction,
myUserId
reaction
)
// TODO We should avoid using MatrixCallback internally
val callback = object : MatrixCallback<FindReactionEventForUndoTask.Result> {
override fun onSuccess(data: FindReactionEventForUndoTask.Result) {
if (data.redactEventId == null) {
@ -89,7 +89,6 @@ internal class DefaultRelationService @AssistedInject constructor(@Assisted priv
// TODO?
}
data.redactEventId?.let { toRedact ->
val redactEvent = eventFactory.createRedactEvent(roomId, toRedact, null).also {
saveLocalEcho(it)
}
@ -99,7 +98,7 @@ internal class DefaultRelationService @AssistedInject constructor(@Assisted priv
}
}
}
findReactionEventForUndoTask
return findReactionEventForUndoTask
.configureWith(params) {
this.retryCount = Int.MAX_VALUE
this.callback = callback

View file

@ -20,6 +20,7 @@ import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryE
import im.vector.matrix.android.internal.database.model.EventEntity
import im.vector.matrix.android.internal.database.model.ReactionAggregatedSummaryEntityFields
import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.di.UserId
import im.vector.matrix.android.internal.task.Task
import io.realm.Realm
import javax.inject.Inject
@ -29,8 +30,7 @@ internal interface FindReactionEventForUndoTask : Task<FindReactionEventForUndoT
data class Params(
val roomId: String,
val eventId: String,
val reaction: String,
val myUserId: String
val reaction: String
)
data class Result(
@ -38,16 +38,17 @@ internal interface FindReactionEventForUndoTask : Task<FindReactionEventForUndoT
)
}
internal class DefaultFindReactionEventForUndoTask @Inject constructor(private val monarchy: Monarchy) : FindReactionEventForUndoTask {
internal class DefaultFindReactionEventForUndoTask @Inject constructor(private val monarchy: Monarchy,
@UserId private val userId: String) : FindReactionEventForUndoTask {
override suspend fun execute(params: FindReactionEventForUndoTask.Params): FindReactionEventForUndoTask.Result {
val eventId = Realm.getInstance(monarchy.realmConfiguration).use { realm ->
getReactionToRedact(realm, params.reaction, params.eventId, params.myUserId)?.eventId
getReactionToRedact(realm, params.reaction, params.eventId)?.eventId
}
return FindReactionEventForUndoTask.Result(eventId)
}
private fun getReactionToRedact(realm: Realm, reaction: String, eventId: String, userId: String): EventEntity? {
private fun getReactionToRedact(realm: Realm, reaction: String, eventId: String): EventEntity? {
val summary = EventAnnotationsSummaryEntity.where(realm, eventId).findFirst()
if (summary != null) {
summary.reactionsSummary.where()

View file

@ -20,6 +20,7 @@ import im.vector.matrix.android.internal.database.model.EventAnnotationsSummaryE
import im.vector.matrix.android.internal.database.model.EventEntity
import im.vector.matrix.android.internal.database.model.ReactionAggregatedSummaryEntityFields
import im.vector.matrix.android.internal.database.query.where
import im.vector.matrix.android.internal.di.UserId
import im.vector.matrix.android.internal.task.Task
import io.realm.Realm
import javax.inject.Inject
@ -30,8 +31,7 @@ internal interface UpdateQuickReactionTask : Task<UpdateQuickReactionTask.Params
val roomId: String,
val eventId: String,
val reaction: String,
val oppositeReaction: String,
val myUserId: String
val oppositeReaction: String
)
data class Result(
@ -40,17 +40,18 @@ internal interface UpdateQuickReactionTask : Task<UpdateQuickReactionTask.Params
)
}
internal class DefaultUpdateQuickReactionTask @Inject constructor(private val monarchy: Monarchy) : UpdateQuickReactionTask {
internal class DefaultUpdateQuickReactionTask @Inject constructor(private val monarchy: Monarchy,
@UserId private val userId: String) : UpdateQuickReactionTask {
override suspend fun execute(params: UpdateQuickReactionTask.Params): UpdateQuickReactionTask.Result {
var res: Pair<String?, List<String>?>? = null
monarchy.doWithRealm { realm ->
res = updateQuickReaction(realm, params.reaction, params.oppositeReaction, params.eventId, params.myUserId)
res = updateQuickReaction(realm, params.reaction, params.oppositeReaction, params.eventId)
}
return UpdateQuickReactionTask.Result(res?.first, res?.second ?: emptyList())
}
private fun updateQuickReaction(realm: Realm, reaction: String, oppositeReaction: String, eventId: String, myUserId: String): Pair<String?, List<String>?> {
private fun updateQuickReaction(realm: Realm, reaction: String, oppositeReaction: String, eventId: String): Pair<String?, List<String>?> {
// the emoji reaction has been selected, we need to check if we have reacted it or not
val existingSummary = EventAnnotationsSummaryEntity.where(realm, eventId).findFirst()
?: return Pair(reaction, null)
@ -68,7 +69,7 @@ internal class DefaultUpdateQuickReactionTask @Inject constructor(private val mo
val toRedact = aggregationForOppositeReaction?.sourceEvents?.mapNotNull {
// find source event
val entity = EventEntity.where(realm, it).findFirst()
if (entity?.sender == myUserId) entity.eventId else null
if (entity?.sender == userId) entity.eventId else null
}
return Pair(reaction, toRedact)
} else {
@ -77,7 +78,7 @@ internal class DefaultUpdateQuickReactionTask @Inject constructor(private val mo
val toRedact = aggregationForReaction.sourceEvents.mapNotNull {
// find source event
val entity = EventEntity.where(realm, it).findFirst()
if (entity?.sender == myUserId) entity.eventId else null
if (entity?.sender == userId) entity.eventId else null
}
return Pair(null, toRedact)
}

View file

@ -342,11 +342,11 @@ class PushrulesConditionTest {
TODO("not implemented") // To change body of created functions use File | Settings | File Templates.
}
override fun sendReaction(reaction: String, targetEventId: String): Cancelable {
override fun sendReaction(targetEventId: String, reaction: String): Cancelable {
TODO("not implemented") // To change body of created functions use File | Settings | File Templates.
}
override fun undoReaction(reaction: String, targetEventId: String, myUserId: String) {
override fun undoReaction(targetEventId: String, reaction: String): Cancelable {
TODO("not implemented") // To change body of created functions use File | Settings | File Templates.
}

View file

@ -30,9 +30,9 @@ sealed class RoomDetailActions {
data class TimelineEventTurnsVisible(val event: TimelineEvent) : RoomDetailActions()
data class TimelineEventTurnsInvisible(val event: TimelineEvent) : RoomDetailActions()
data class LoadMoreTimelineEvents(val direction: Timeline.Direction) : RoomDetailActions()
data class SendReaction(val reaction: String, val targetEventId: String) : RoomDetailActions()
data class SendReaction(val targetEventId: String, val reaction: String) : RoomDetailActions()
data class UndoReaction(val targetEventId: String, val reaction: String, val reason: String? = "") : RoomDetailActions()
data class RedactAction(val targetEventId: String, val reason: String? = "") : RoomDetailActions()
data class UndoReaction(val targetEventId: String, val key: String, val reason: String? = "") : RoomDetailActions()
data class UpdateQuickReactAction(val targetEventId: String, val selectedReaction: String, val add: Boolean) : RoomDetailActions()
data class NavigateToEvent(val eventId: String, val highlight: Boolean) : RoomDetailActions()
data class SetReadMarkerAction(val eventId: String) : RoomDetailActions()

View file

@ -420,7 +420,7 @@ class RoomDetailFragment :
val eventId = data.getStringExtra(EmojiReactionPickerActivity.EXTRA_EVENT_ID) ?: return
val reaction = data.getStringExtra(EmojiReactionPickerActivity.EXTRA_REACTION_RESULT) ?: return
// TODO check if already reacted with that?
roomDetailViewModel.process(RoomDetailActions.SendReaction(reaction, eventId))
roomDetailViewModel.process(RoomDetailActions.SendReaction(eventId, reaction))
}
}
}
@ -909,7 +909,7 @@ class RoomDetailFragment :
override fun onClickOnReactionPill(informationData: MessageInformationData, reaction: String, on: Boolean) {
if (on) {
// we should test the current real state of reaction on this event
roomDetailViewModel.process(RoomDetailActions.SendReaction(reaction, informationData.eventId))
roomDetailViewModel.process(RoomDetailActions.SendReaction(informationData.eventId, reaction))
} else {
// I need to redact a reaction
roomDetailViewModel.process(RoomDetailActions.UndoReaction(informationData.eventId, reaction))

View file

@ -445,7 +445,7 @@ class RoomDetailViewModel @AssistedInject constructor(@Assisted initialState: Ro
}
private fun handleSendReaction(action: RoomDetailActions.SendReaction) {
room.sendReaction(action.reaction, action.targetEventId)
room.sendReaction(action.targetEventId, action.reaction)
}
private fun handleRedactEvent(action: RoomDetailActions.RedactAction) {
@ -454,14 +454,14 @@ class RoomDetailViewModel @AssistedInject constructor(@Assisted initialState: Ro
}
private fun handleUndoReact(action: RoomDetailActions.UndoReaction) {
room.undoReaction(action.key, action.targetEventId, session.myUserId)
room.undoReaction(action.targetEventId, action.reaction)
}
private fun handleUpdateQuickReaction(action: RoomDetailActions.UpdateQuickReactAction) {
if (action.add) {
room.sendReaction(action.selectedReaction, action.targetEventId)
room.sendReaction(action.targetEventId, action.selectedReaction)
} else {
room.undoReaction(action.selectedReaction, action.targetEventId, session.myUserId)
room.undoReaction(action.targetEventId, action.selectedReaction)
}
}