Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update federation flag when fetching server config [WPB-14728] #3143

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ internal class CustomServerConfigDataSource internal constructor(
val storedConfigId = serverConfigurationDAO.configByLinks(serverConfigMapper.toEntity(links))?.id
if (storedConfigId != null) {
// if already exists then just update it
serverConfigurationDAO.updateApiVersion(storedConfigId, metadata.commonApiVersion.version)
serverConfigurationDAO.updateServerMetaData(
id = storedConfigId,
federation = metadata.federation,
commonApiVersion = metadata.commonApiVersion.version
)
if (metadata.federation) serverConfigurationDAO.setFederationToTrue(storedConfigId)
storedConfigId
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ internal interface ServerConfigRepository {
suspend fun fetchApiVersionAndStore(links: ServerConfig.Links): Either<CoreFailure, ServerConfig>

/**
* update the api version of a locally stored config
* update the api version and federation status of a locally stored config
*/
suspend fun updateConfigApiVersion(serverConfig: ServerConfig): Either<CoreFailure, Unit>
suspend fun updateConfigMetaData(serverConfig: ServerConfig): Either<CoreFailure, Unit>

/**
* Return the server links and metadata for the given userId
Expand Down Expand Up @@ -86,7 +86,11 @@ internal class ServerConfigDataSource(
val storedConfigId = dao.configByLinks(serverConfigMapper.toEntity(links))?.id
if (storedConfigId != null) {
// if already exists then just update it
dao.updateApiVersion(storedConfigId, metadata.commonApiVersion.version)
dao.updateServerMetaData(
id = storedConfigId,
federation = metadata.federation,
commonApiVersion = metadata.commonApiVersion.version
)
if (metadata.federation) dao.setFederationToTrue(storedConfigId)
storedConfigId
} else {
Expand Down Expand Up @@ -126,9 +130,17 @@ internal class ServerConfigDataSource(
storeConfig(links, metaData)
}

override suspend fun updateConfigApiVersion(serverConfig: ServerConfig): Either<CoreFailure, Unit> =
fetchMetadata(serverConfig.links)
.flatMap { wrapStorageRequest { dao.updateApiVersion(serverConfig.id, it.commonApiVersion.version) } }
override suspend fun updateConfigMetaData(serverConfig: ServerConfig): Either<CoreFailure, Unit> =
fetchMetadata(serverConfig.links)
.flatMap { newMetaData ->
wrapStorageRequest {
dao.updateServerMetaData(
id = serverConfig.id,
federation = newMetaData.federation,
commonApiVersion = newMetaData.commonApiVersion.version
)
}
}

override suspend fun configForUser(userId: UserId): Either<StorageFailure, ServerConfig> =
wrapStorageRequest { dao.configForUser(userId.toDao()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,6 @@ class UpdateApiVersionsUseCaseImpl internal constructor(
} else {
null
}
serverConfigRepoProvider(serverConfig, proxyCredentials).updateConfigApiVersion(serverConfig)
serverConfigRepoProvider(serverConfig, proxyCredentials).updateConfigMetaData(serverConfig)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class CustomServerConfigRepositoryTest {
arrangement.serverConfigurationDAO.insert(any())
}.wasNotInvoked()
coVerify {
arrangement.serverConfigurationDAO.updateApiVersion(any(), any())
arrangement.serverConfigurationDAO.updateServerMetaData(any(), any(), any())
}.wasInvoked(exactly = once)
coVerify {
arrangement.serverConfigurationDAO.setFederationToTrue(any())
Expand Down Expand Up @@ -106,7 +106,7 @@ class CustomServerConfigRepositoryTest {
arrangement.serverConfigurationDAO.insert(any())
}.wasInvoked(exactly = once)
coVerify {
arrangement.serverConfigurationDAO.updateApiVersion(any(), any())
arrangement.serverConfigurationDAO.updateServerMetaData(any(), any(), any())
}.wasNotInvoked()
coVerify {
arrangement.serverConfigurationDAO.setFederationToTrue(any())
Expand Down Expand Up @@ -145,7 +145,7 @@ class CustomServerConfigRepositoryTest {
arrangement.serverConfigurationDAO.insert(any())
}.wasInvoked(exactly = once)
coVerify {
arrangement.serverConfigurationDAO.updateApiVersion(any(), any())
arrangement.serverConfigurationDAO.updateServerMetaData(any(), any(), any())
}.wasNotInvoked()
coVerify {
arrangement.serverConfigurationDAO.setFederationToTrue(any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class ServerConfigRepositoryTest {
arrangement.serverConfigDAO.insert(any())
}.wasNotInvoked()
coVerify {
arrangement.serverConfigDAO.updateApiVersion(any(), any())
arrangement.serverConfigDAO.updateServerMetaData(any(), any(), any())
}.wasInvoked(exactly = once)
coVerify {
arrangement.serverConfigDAO.setFederationToTrue(any())
Expand Down Expand Up @@ -159,7 +159,7 @@ class ServerConfigRepositoryTest {
arrangement.serverConfigDAO.insert(any())
}.wasInvoked(exactly = once)
coVerify {
arrangement.serverConfigDAO.updateApiVersion(any(), any())
arrangement.serverConfigDAO.updateServerMetaData(any(), any(), any())
}.wasNotInvoked()
coVerify {
arrangement.serverConfigDAO.setFederationToTrue(any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class UpdateApiVersionUseCaseTest {
)
)
)
withUpdateConfigApiVersion(serverConfig1, Either.Right(Unit))
withUpdateConfigMetaData(serverConfig1, Either.Right(Unit))
}

updateApiVersionsUseCase()
Expand All @@ -88,7 +88,7 @@ class UpdateApiVersionUseCaseTest {
}.wasNotInvoked()

coVerify {
arrangement.serverConfigRepository1.updateConfigApiVersion(eq(serverConfig1))
arrangement.serverConfigRepository1.updateConfigMetaData(eq(serverConfig1))
}.wasInvoked(exactly = once)
}

Expand All @@ -111,7 +111,7 @@ class UpdateApiVersionUseCaseTest {
)
)
)
withUpdateConfigApiVersion(serverConfig1, Either.Right(Unit))
withUpdateConfigMetaData(serverConfig1, Either.Right(Unit))
}

updateApiVersionsUseCase()
Expand All @@ -122,7 +122,7 @@ class UpdateApiVersionUseCaseTest {
}.wasNotInvoked()

coVerify {
arrangement.serverConfigRepository1.updateConfigApiVersion(any())
arrangement.serverConfigRepository1.updateConfigMetaData(any())
}.wasInvoked(exactly = once)
}

Expand All @@ -145,7 +145,7 @@ class UpdateApiVersionUseCaseTest {
)
)
)
withUpdateConfigApiVersion(serverConfig1, Either.Right(Unit))
withUpdateConfigMetaData(serverConfig1, Either.Right(Unit))
withProxyCredForUser(userId1.toDao(), ProxyCredentialsEntity("user", "pass"))
}

Expand All @@ -158,7 +158,7 @@ class UpdateApiVersionUseCaseTest {
}.wasInvoked(exactly = once)

coVerify {
arrangement.serverConfigRepository1.updateConfigApiVersion(any())
arrangement.serverConfigRepository1.updateConfigMetaData(any())
}.wasInvoked(exactly = once)
}

Expand Down Expand Up @@ -190,8 +190,8 @@ class UpdateApiVersionUseCaseTest {
)
)
)
withUpdateConfigApiVersion(serverConfig1, Either.Right(Unit))
withUpdateConfigApiVersion(serverConfig2, Either.Right(Unit))
withUpdateConfigMetaData(serverConfig1, Either.Right(Unit))
withUpdateConfigMetaData(serverConfig2, Either.Right(Unit))
withProxyCredForUser(userId2.toDao(), ProxyCredentialsEntity("user", "pass"))
}

Expand All @@ -208,11 +208,11 @@ class UpdateApiVersionUseCaseTest {
}.wasNotInvoked()

coVerify {
arrangement.serverConfigRepository1.updateConfigApiVersion(any())
arrangement.serverConfigRepository1.updateConfigMetaData(any())
}.wasInvoked(exactly = once)

coVerify {
arrangement.serverConfigRepository2.updateConfigApiVersion(any())
arrangement.serverConfigRepository2.updateConfigMetaData(any())
}.wasInvoked(exactly = once)

}
Expand Down Expand Up @@ -249,16 +249,16 @@ class UpdateApiVersionUseCaseTest {
}.returns(result)
}

suspend fun withUpdateConfigApiVersion(
suspend fun withUpdateConfigMetaData(
serverConfig: ServerConfig,
result: Either<CoreFailure, Unit>
) {
when (serverConfig.id) {
serverConfig1.id ->
coEvery { serverConfigRepository1.updateConfigApiVersion(any()) }
coEvery { serverConfigRepository1.updateConfigMetaData(any()) }
.returns(result)

serverConfig2.id -> coEvery { serverConfigRepository2.updateConfigApiVersion(any()) }
serverConfig2.id -> coEvery { serverConfigRepository2.updateConfigMetaData(any()) }
.returns(result)

else -> throw IllegalArgumentException("Unexpected server config: $serverConfig")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.wire.kalium.persistence.db.PlatformDatabaseData
import com.wire.kalium.persistence.db.globalDatabaseProvider
import com.wire.kalium.persistence.util.FileNameUtil
import com.wire.kalium.util.KaliumDispatcherImpl
import kotlinx.coroutines.test.TestDispatcher

actual abstract class GlobalDBBaseTest {

Expand All @@ -34,7 +35,7 @@ actual abstract class GlobalDBBaseTest {
context.deleteDatabase(FileNameUtil.globalDBName())
}

actual fun createDatabase(): GlobalDatabaseBuilder = globalDatabaseProvider(
actual fun createDatabase(dispatcher: TestDispatcher): GlobalDatabaseBuilder = globalDatabaseProvider(
platformDatabaseData = PlatformDatabaseData(ApplicationProvider.getApplicationContext()),
queriesContext = KaliumDispatcherImpl.unconfined,
passphrase = GlobalDatabaseSecret("test_db_secret".toByteArray()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
package com.wire.kalium.persistence

import com.wire.kalium.persistence.db.GlobalDatabaseBuilder
import kotlinx.coroutines.test.TestDispatcher

actual abstract class GlobalDBBaseTest {
actual fun deleteDatabase() {
TODO("Not yet implemented")
}

actual fun createDatabase(): GlobalDatabaseBuilder {
actual fun createDatabase(dispatcher: TestDispatcher): GlobalDatabaseBuilder {
TODO("Not yet implemented")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.wire.kalium.persistence.db.StorageData
import com.wire.kalium.persistence.db.globalDatabaseProvider
import com.wire.kalium.persistence.util.FileNameUtil
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestDispatcher
import platform.Foundation.NSCachesDirectory
import platform.Foundation.NSFileManager
import platform.Foundation.NSUserDomainMask
Expand All @@ -37,7 +38,7 @@ actual abstract class GlobalDBBaseTest {
deleteDatabase(FileNameUtil.globalDBName(), storePath)
}

actual fun createDatabase(): GlobalDatabaseBuilder {
actual fun createDatabase(dispatcher: TestDispatcher): GlobalDatabaseBuilder {
return globalDatabaseProvider(
PlatformDatabaseData(StorageData.FileBacked(storePath)), StandardTestDispatcher(), null, false
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ insert:
INSERT OR FAIL INTO ServerConfiguration(id, apiBaseUrl, accountBaseUrl, webSocketBaseUrl, blackListUrl, teamsUrl, websiteUrl, title, isOnPremises, federation, domain, commonApiVersion, apiProxyHost, apiProxyNeedsAuthentication, apiProxyPort)
VALUES( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?,?);

updateApiVersion:
UPDATE ServerConfiguration SET commonApiVersion = ? WHERE id = ?;
updateServerMetaData:
UPDATE ServerConfiguration SET federation = ?, commonApiVersion = ? WHERE id = ?;

/** this function will be used when a config get updated from v0 where domain can be null */
updateApiVersionAndDomain:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ interface ServerConfigurationDAO {
suspend fun allConfig(): List<ServerConfigEntity>
fun configById(id: String): ServerConfigEntity?
suspend fun configByLinks(links: ServerConfigEntity.Links): ServerConfigEntity?
suspend fun updateApiVersion(id: String, commonApiVersion: Int)
suspend fun updateServerMetaData(id: String, federation: Boolean, commonApiVersion: Int)
suspend fun updateApiVersionAndDomain(id: String, domain: String, commonApiVersion: Int)
suspend fun configForUser(userId: UserIDEntity): ServerConfigEntity?
suspend fun setFederationToTrue(id: String)
Expand Down Expand Up @@ -209,8 +209,10 @@ internal class ServerConfigurationDAOImpl internal constructor(
}.executeAsOneOrNull()
}

override suspend fun updateApiVersion(id: String, commonApiVersion: Int) = withContext(queriesContext) {
queries.updateApiVersion(commonApiVersion, id)
override suspend fun updateServerMetaData(id: String, federation: Boolean, commonApiVersion: Int) {
withContext(queriesContext) {
queries.updateServerMetaData(federation, commonApiVersion, id)
}
}

override suspend fun updateApiVersionAndDomain(id: String, domain: String, commonApiVersion: Int) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ value class GlobalDatabaseSecret(val value: ByteArray)
class GlobalDatabaseBuilder internal constructor(
private val sqlDriver: SqlDriver,
private val platformDatabaseData: PlatformDatabaseData,
private val queriesContext: CoroutineContext = KaliumDispatcherImpl.io
protected val queriesContext: CoroutineContext = KaliumDispatcherImpl.io
) {

internal val database: GlobalDatabase = GlobalDatabase(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
package com.wire.kalium.persistence

import com.wire.kalium.persistence.db.GlobalDatabaseBuilder
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.TestDispatcher

expect abstract class GlobalDBBaseTest() {
fun deleteDatabase()
fun createDatabase(): GlobalDatabaseBuilder
fun createDatabase(dispatcher: TestDispatcher): GlobalDatabaseBuilder
}
Loading
Loading