Skip to content

Commit

Permalink
feat: cache contacts size
Browse files Browse the repository at this point in the history
  • Loading branch information
ohassine committed Jan 29, 2025
1 parent e21b0a8 commit 970b146
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ fun interface CurrentClientIdProvider {
suspend operator fun invoke(): Either<CoreFailure, ClientId>
}

fun interface ContactsSizeProvider {
suspend operator fun invoke(): Either<CoreFailure, Int>
}

internal fun interface SelfTeamIdProvider {
suspend operator fun invoke(): Either<CoreFailure, TeamId?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ interface UserRepository {
suspend fun migrateUserToTeam(teamName: String): Either<CoreFailure, CreateUserTeam>
suspend fun updateTeamId(userId: UserId, teamId: TeamId): Either<StorageFailure, Unit>
suspend fun isClientMlsCapable(userId: UserId, clientId: ClientId): Either<StorageFailure, Boolean>
suspend fun getContactsSize(): Either<StorageFailure, Int>
}

@Suppress("LongParameterList", "TooManyFunctions")
Expand Down Expand Up @@ -386,8 +387,8 @@ internal class UserDataSource internal constructor(
userProfile = userProfileDTO,
connectionState = ConnectionEntity.State.ACCEPTED,
userTypeEntity =
if (userProfileDTO.service != null) UserTypeEntity.SERVICE
else userTypeEntityMapper.teamRoleCodeToUserType(mapTeamMemberDTO[userProfileDTO.id.value]?.permissions?.own)
if (userProfileDTO.service != null) UserTypeEntity.SERVICE
else userTypeEntityMapper.teamRoleCodeToUserType(mapTeamMemberDTO[userProfileDTO.id.value]?.permissions?.own)
)
}
val otherUsers = listUserProfileDTO
Expand Down Expand Up @@ -708,6 +709,10 @@ internal class UserDataSource internal constructor(
clientDAO.isMLSCapable(userId.toDao(), clientId.value)
}

override suspend fun getContactsSize(): Either<StorageFailure, Int> = wrapStorageRequest {
userDAO.countUsers()
}

companion object {
internal const val SELF_USER_ID_KEY = "selfUserID"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import com.wire.kalium.logic.data.event.EventDataSource
import com.wire.kalium.logic.data.event.EventRepository
import com.wire.kalium.logic.data.featureConfig.FeatureConfigDataSource
import com.wire.kalium.logic.data.featureConfig.FeatureConfigRepository
import com.wire.kalium.logic.data.id.ContactsSizeProvider
import com.wire.kalium.logic.data.id.CurrentClientIdProvider
import com.wire.kalium.logic.data.id.FederatedIdMapper
import com.wire.kalium.logic.data.id.GroupID
Expand Down Expand Up @@ -498,6 +499,7 @@ class UserSessionScope internal constructor(
)

private var _clientId: ClientId? = null
private var _contactsSize: Int? = null

@OptIn(DelicateKaliumApi::class) // Use the uncached client ID in order to create the cache itself.
private suspend fun clientId(): Either<CoreFailure, ClientId> =
Expand All @@ -507,6 +509,13 @@ class UserSessionScope internal constructor(
}
}

private suspend fun contactsSize(): Either<CoreFailure, Int> =
if (_contactsSize != null) Either.Right(_contactsSize!!) else {
userRepository.getContactsSize().onSuccess {
_contactsSize = it
}
}

private val userScopedLogger: KaliumLogger = kaliumLogger.withUserDeviceData {
KaliumLogger.UserClientData(userId.toLogString(), _clientId?.value?.obfuscateId() ?: "")
}
Expand All @@ -527,6 +536,8 @@ class UserSessionScope internal constructor(
)

val clientIdProvider = CurrentClientIdProvider { clientId() }
val contactsSizeProvider = ContactsSizeProvider { contactsSize() }

private val mlsSelfConversationIdProvider: MLSSelfConversationIdProvider by lazy {
MLSSelfConversationIdProviderImpl(
conversationRepository
Expand Down Expand Up @@ -1205,19 +1216,22 @@ class UserSessionScope internal constructor(
callRepository
)

internal val keyPackageManager: KeyPackageManager = KeyPackageManagerImpl(featureSupport,
internal val keyPackageManager: KeyPackageManager = KeyPackageManagerImpl(
featureSupport,
incrementalSyncRepository,
lazy { clientRepository },
lazy { client.refillKeyPackages },
lazy { client.mlsKeyPackageCountUseCase },
lazy { users.timestampKeyRepository })
internal val keyingMaterialsManager: KeyingMaterialsManager = KeyingMaterialsManagerImpl(featureSupport,
internal val keyingMaterialsManager: KeyingMaterialsManager = KeyingMaterialsManagerImpl(
featureSupport,
incrementalSyncRepository,
lazy { clientRepository },
lazy { conversations.updateMLSGroupsKeyingMaterials },
lazy { users.timestampKeyRepository })

val mlsClientManager: MLSClientManager = MLSClientManagerImpl(clientIdProvider,
val mlsClientManager: MLSClientManager = MLSClientManagerImpl(
clientIdProvider,
isAllowedToRegisterMLSClient,
incrementalSyncRepository,
lazy { slowSyncRepository },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,7 @@ UPDATE User SET team = ? WHERE qualified_id = ?;
selectNameByMessageId:
SELECT name FROM User
WHERE qualified_id = (SELECT Message.sender_user_id FROM Message WHERE Message.id = :messageId AND Message.conversation_id = :conversationId);

countUsers:
SELECT COUNT(*)
FROM User WHERE connection_status == 'ACCEPTED';
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,5 @@ interface UserDAO {
suspend fun getUsersMinimizedByQualifiedIDs(qualifiedIDs: List<QualifiedIDEntity>): List<UserEntityMinimized>
suspend fun getNameAndHandle(userId: UserIDEntity): NameAndHandleEntity?
suspend fun updateTeamId(userId: UserIDEntity, teamId: String)
suspend fun countUsers(): Int?
}
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,12 @@ class UserDAOImpl internal constructor(
}

override suspend fun updateTeamId(userId: UserIDEntity, teamId: String) {
userQueries.updateTeamId(teamId, userId)
withContext(queriesContext) {
userQueries.updateTeamId(teamId, userId)
}
}

override suspend fun countUsers(): Int? = withContext(queriesContext) {
userQueries.countUsers().executeAsOneOrNull()?.toInt()
}
}

0 comments on commit 970b146

Please sign in to comment.