From 30ffa63bc32dc03907c8021af99d386bfd92a56a Mon Sep 17 00:00:00 2001 From: Florian Renaud Date: Thu, 16 Jun 2022 15:48:24 +0200 Subject: [PATCH] Start DM - Fix first message not encrypted --- .../room/create/CreateLocalRoomTask.kt | 5 ++ .../create/CreateRoomFromLocalRoomTask.kt | 64 ++++++++++++++----- .../session/room/create/CreateRoomTask.kt | 3 +- 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateLocalRoomTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateLocalRoomTask.kt index 1fa2cfe35b3..25458819b18 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateLocalRoomTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateLocalRoomTask.kt @@ -40,6 +40,7 @@ import org.matrix.android.sdk.api.session.room.send.SendState import org.matrix.android.sdk.api.session.sync.model.RoomSyncSummary import org.matrix.android.sdk.api.session.user.UserService import org.matrix.android.sdk.api.session.user.model.User +import org.matrix.android.sdk.internal.crypto.DefaultCryptoService import org.matrix.android.sdk.internal.database.awaitNotEmptyResult import org.matrix.android.sdk.internal.database.helper.addTimelineEvent import org.matrix.android.sdk.internal.database.mapper.asDomain @@ -76,6 +77,7 @@ internal class DefaultCreateLocalRoomTask @Inject constructor( @SessionDatabase private val realmConfiguration: RealmConfiguration, private val createRoomBodyBuilder: CreateRoomBodyBuilder, private val userService: UserService, + private val cryptoService: DefaultCryptoService, private val clock: Clock, ) : CreateLocalRoomTask { @@ -166,6 +168,9 @@ internal class DefaultCreateLocalRoomTask @Inject constructor( roomMemberContentsByUser[event.stateKey] = event.getFixedRoomMemberContent() roomMemberEventHandler.handle(realm, roomId, event, false) } + + // Give info to crypto module + cryptoService.onStateEvent(roomId, event) } roomMemberContentsByUser.getOrPut(event.senderId) { diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateRoomFromLocalRoomTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateRoomFromLocalRoomTask.kt index 2d92cbc321c..486f25b2024 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateRoomFromLocalRoomTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateRoomFromLocalRoomTask.kt @@ -22,6 +22,7 @@ import com.google.i18n.phonenumbers.NumberParseException import com.google.i18n.phonenumbers.PhoneNumberUtil import com.zhuinden.monarchy.Monarchy import io.realm.Realm +import kotlinx.coroutines.TimeoutCancellationException import org.matrix.android.sdk.api.extensions.ensurePrefix import org.matrix.android.sdk.api.query.QueryStringValue import org.matrix.android.sdk.api.session.events.model.Event @@ -29,6 +30,7 @@ import org.matrix.android.sdk.api.session.events.model.EventType import org.matrix.android.sdk.api.session.events.model.toContent import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.identity.ThreePid +import org.matrix.android.sdk.api.session.room.failure.CreateRoomFailure import org.matrix.android.sdk.api.session.room.model.PowerLevelsContent import org.matrix.android.sdk.api.session.room.model.RoomAliasesContent import org.matrix.android.sdk.api.session.room.model.RoomAvatarContent @@ -44,11 +46,15 @@ import org.matrix.android.sdk.api.session.room.model.create.CreateRoomParams import org.matrix.android.sdk.api.session.room.model.create.CreateRoomPreset import org.matrix.android.sdk.api.session.room.model.tombstone.RoomTombstoneContent import org.matrix.android.sdk.api.session.room.send.SendState +import org.matrix.android.sdk.internal.database.awaitNotEmptyResult import org.matrix.android.sdk.internal.database.mapper.asDomain import org.matrix.android.sdk.internal.database.mapper.toEntity import org.matrix.android.sdk.internal.database.model.CurrentStateEventEntity import org.matrix.android.sdk.internal.database.model.EventEntity +import org.matrix.android.sdk.internal.database.model.EventEntityFields import org.matrix.android.sdk.internal.database.model.EventInsertType +import org.matrix.android.sdk.internal.database.model.RoomSummaryEntity +import org.matrix.android.sdk.internal.database.model.RoomSummaryEntityFields import org.matrix.android.sdk.internal.database.query.copyToRealmOrIgnore import org.matrix.android.sdk.internal.database.query.getOrCreate import org.matrix.android.sdk.internal.database.query.where @@ -60,6 +66,7 @@ import org.matrix.android.sdk.internal.task.Task import org.matrix.android.sdk.internal.util.awaitTransaction import org.matrix.android.sdk.internal.util.time.Clock import java.util.UUID +import java.util.concurrent.TimeUnit import javax.inject.Inject /** @@ -81,6 +88,9 @@ internal class DefaultCreateRoomFromLocalRoomTask @Inject constructor( private val clock: Clock, ) : CreateRoomFromLocalRoomTask { + private val realmConfiguration + get() = monarchy.realmConfiguration + override suspend fun execute(params: CreateRoomFromLocalRoomTask.Params): String { val replacementRoomId = stateEventDataSource.getStateEvent(params.localRoomId, EventType.STATE_ROOM_TOMBSTONE, QueryStringValue.NoCondition) ?.content?.toModel() @@ -92,6 +102,30 @@ internal class DefaultCreateRoomFromLocalRoomTask @Inject constructor( val createRoomParams = getCreateRoomParams(params) val roomId = createRoomTask.execute(createRoomParams) + + try { + awaitNotEmptyResult(realmConfiguration, TimeUnit.MINUTES.toMillis(1L)) { realm -> + realm.where(RoomSummaryEntity::class.java) + .equalTo(RoomSummaryEntityFields.ROOM_ID, roomId) + .equalTo( + RoomSummaryEntityFields.INVITED_MEMBERS_COUNT, + createRoomParams.invitedUserIds.size.minus(1) + createRoomParams.invite3pids.size + ) + } + awaitNotEmptyResult(realmConfiguration, TimeUnit.MINUTES.toMillis(1L)) { realm -> + EventEntity.whereRoomId(realm, roomId) + .equalTo(EventEntityFields.TYPE, EventType.STATE_ROOM_HISTORY_VISIBILITY) + } + if (createRoomParams.algorithm != null) { + awaitNotEmptyResult(realmConfiguration, TimeUnit.MINUTES.toMillis(1L)) { realm -> + EventEntity.whereRoomId(realm, roomId) + .equalTo(EventEntityFields.TYPE, EventType.STATE_ROOM_ENCRYPTION) + } + } + } catch (exception: TimeoutCancellationException) { + throw CreateRoomFailure.CreatedWithTimeout(roomId) + } + createTombstoneEvent(params, roomId) return roomId } @@ -109,19 +143,19 @@ internal class DefaultCreateRoomFromLocalRoomTask @Inject constructor( val stateEvents = CurrentStateEventEntity.whereRoomId(realm, params.localRoomId).findAll() stateEvents.forEach { event -> createRoomParams = when (event.type) { - EventType.STATE_ROOM_MEMBER -> handleRoomMemberEvent(realm, event, createRoomParams) + EventType.STATE_ROOM_MEMBER -> handleRoomMemberEvent(realm, event, createRoomParams) EventType.STATE_ROOM_HISTORY_VISIBILITY -> handleRoomHistoryVisibilityEvent(realm, event, createRoomParams) - EventType.STATE_ROOM_ALIASES -> handleRoomAliasesEvent(realm, event, createRoomParams) - EventType.STATE_ROOM_AVATAR -> handleRoomAvatarEvent(realm, event, createRoomParams) - EventType.STATE_ROOM_CANONICAL_ALIAS -> handleRoomCanonicalAliasEvent(realm, event, createRoomParams) - EventType.STATE_ROOM_GUEST_ACCESS -> handleRoomGuestAccessEvent(realm, event, createRoomParams) - EventType.STATE_ROOM_ENCRYPTION -> handleRoomEncryptionEvent(createRoomParams) - EventType.STATE_ROOM_POWER_LEVELS -> handleRoomPowerRoomLevelsEvent(realm, event, createRoomParams) - EventType.STATE_ROOM_NAME -> handleRoomNameEvent(realm, event, createRoomParams) - EventType.STATE_ROOM_TOPIC -> handleRoomTopicEvent(realm, event, createRoomParams) + EventType.STATE_ROOM_ALIASES -> handleRoomAliasesEvent(realm, event, createRoomParams) + EventType.STATE_ROOM_AVATAR -> handleRoomAvatarEvent(realm, event, createRoomParams) + EventType.STATE_ROOM_CANONICAL_ALIAS -> handleRoomCanonicalAliasEvent(realm, event, createRoomParams) + EventType.STATE_ROOM_GUEST_ACCESS -> handleRoomGuestAccessEvent(realm, event, createRoomParams) + EventType.STATE_ROOM_ENCRYPTION -> handleRoomEncryptionEvent(createRoomParams) + EventType.STATE_ROOM_POWER_LEVELS -> handleRoomPowerRoomLevelsEvent(realm, event, createRoomParams) + EventType.STATE_ROOM_NAME -> handleRoomNameEvent(realm, event, createRoomParams) + EventType.STATE_ROOM_TOPIC -> handleRoomTopicEvent(realm, event, createRoomParams) EventType.STATE_ROOM_THIRD_PARTY_INVITE -> handleRoomThirdPartyInviteEvent(event, createRoomParams) - EventType.STATE_ROOM_JOIN_RULES -> handleRoomJoinRulesEvent(realm, event, createRoomParams) - else -> createRoomParams + EventType.STATE_ROOM_JOIN_RULES -> handleRoomJoinRulesEvent(realm, event, createRoomParams) + else -> createRoomParams } } } @@ -212,7 +246,7 @@ internal class DefaultCreateRoomFromLocalRoomTask @Inject constructor( private fun handleRoomThirdPartyInviteEvent(event: CurrentStateEventEntity, params: CreateRoomParams): CreateRoomParams = params.apply { when { - event.stateKey.isEmail() -> invite3pids.add(ThreePid.Email(event.stateKey)) + event.stateKey.isEmail() -> invite3pids.add(ThreePid.Email(event.stateKey)) event.stateKey.isMsisdn() -> invite3pids.add(ThreePid.Msisdn(event.stateKey)) } } @@ -222,9 +256,9 @@ internal class DefaultCreateRoomFromLocalRoomTask @Inject constructor( preset = when { // If preset has already been set for direct chat, keep it preset == CreateRoomPreset.PRESET_TRUSTED_PRIVATE_CHAT -> CreateRoomPreset.PRESET_TRUSTED_PRIVATE_CHAT - content.joinRules == RoomJoinRules.PUBLIC -> CreateRoomPreset.PRESET_PUBLIC_CHAT - content.joinRules == RoomJoinRules.INVITE -> CreateRoomPreset.PRESET_PRIVATE_CHAT - else -> null + content.joinRules == RoomJoinRules.PUBLIC -> CreateRoomPreset.PRESET_PUBLIC_CHAT + content.joinRules == RoomJoinRules.INVITE -> CreateRoomPreset.PRESET_PRIVATE_CHAT + else -> null } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateRoomTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateRoomTask.kt index d76640573fc..137134e43fe 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateRoomTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/create/CreateRoomTask.kt @@ -54,8 +54,7 @@ internal class DefaultCreateRoomTask @Inject constructor( private val directChatsHelper: DirectChatsHelper, private val updateUserAccountDataTask: UpdateUserAccountDataTask, private val readMarkersTask: SetReadMarkersTask, - @SessionDatabase - private val realmConfiguration: RealmConfiguration, + @SessionDatabase private val realmConfiguration: RealmConfiguration, private val createRoomBodyBuilder: CreateRoomBodyBuilder, private val globalErrorReceiver: GlobalErrorReceiver, private val clock: Clock,