Skip to content

Commit 55fb440

Browse files
author
Oleksandr Dzhychko
committed
feat(model-client): Add the possibility to set the userId in model client V2
1 parent fbd3796 commit 55fb440

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

model-client/src/commonMain/kotlin/org/modelix/model/client2/ModelClientV2.kt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ import kotlin.time.Duration.Companion.seconds
5757
class ModelClientV2(
5858
private val httpClient: HttpClient,
5959
val baseUrl: String,
60+
private var clientProvidedUserId: String?,
6061
) : IModelClientV2, Closable {
6162
private var clientId: Int = 0
6263
private var idGenerator: IIdGenerator = IdGeneratorDummy()
63-
private var userId: String? = null
64+
private var serverProvidedUserId: String? = null
6465
private val kvStore = MapBasedStore()
6566
val store = ObjectStoreCache(kvStore) // TODO the store will accumulate garbage
6667

@@ -80,19 +81,30 @@ class ModelClientV2(
8081
}
8182

8283
suspend fun updateUserId() {
83-
userId = httpClient.get {
84+
serverProvidedUserId = httpClient.get {
8485
url {
8586
takeFrom(baseUrl)
8687
appendPathSegments("user-id")
8788
}
8889
}.bodyAsText()
8990
}
9091

92+
/**
93+
* Set or remove the client provided user ID.
94+
*
95+
* When the used ID is removed by passing null, [[getUserId]] might return the [[serverProvidedUserId]].
96+
*
97+
* @param userId A new user ID, or null to remove the old one.
98+
*/
99+
fun setClientProvideUserId(userId: String?) {
100+
clientProvidedUserId = userId
101+
}
102+
91103
override fun getClientId(): Int = clientId
92104

93105
override fun getIdGenerator(): IIdGenerator = idGenerator
94106

95-
override fun getUserId(): String? = userId
107+
override fun getUserId(): String? = clientProvidedUserId ?: serverProvidedUserId
96108

97109
override suspend fun initRepository(repository: RepositoryId): IVersion {
98110
val response = httpClient.post {
@@ -283,11 +295,13 @@ abstract class ModelClientV2Builder {
283295
protected var httpClient: HttpClient? = null
284296
protected var baseUrl: String = "https://localhost/model/v2"
285297
protected var authTokenProvider: (() -> String?)? = null
298+
protected var userId: String? = null
286299

287300
fun build(): ModelClientV2 {
288301
return ModelClientV2(
289302
httpClient?.config { configureHttpClient(this) } ?: createHttpClient(),
290303
baseUrl,
304+
userId,
291305
)
292306
}
293307

@@ -306,6 +320,11 @@ abstract class ModelClientV2Builder {
306320
return this
307321
}
308322

323+
fun userId(userId: String?): ModelClientV2Builder {
324+
this.userId = userId
325+
return this
326+
}
327+
309328
protected open fun configureHttpClient(config: HttpClientConfig<*>) {
310329
config.apply {
311330
expectSuccess = true

model-client/src/commonMain/kotlin/org/modelix/model/client2/ReplicatedModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ class ReplicatedModel(
128128
}
129129
}
130130

131+
suspend fun getCurrentVersion(): CLVersion {
132+
return localModel.getCurrentVersion()
133+
}
134+
131135
private enum class State {
132136
New,
133137
Starting,

model-server/src/test/kotlin/org/modelix/model/server/ModelClientV2Test.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,36 @@ class ModelClientV2Test {
118118
setOf(repositoryId.getBranchReference(), branchId),
119119
)
120120
}
121+
122+
@Test
123+
fun `user id can be provided to client after creation`() = runTest {
124+
val url = "http://localhost/v2"
125+
val modelClient = ModelClientV2
126+
.builder()
127+
.url(url)
128+
.client(client)
129+
.build()
130+
.also { it.init() }
131+
132+
val userId = "a_user_id"
133+
modelClient.setClientProvideUserId(userId)
134+
135+
assertEquals(userId, modelClient.getUserId())
136+
}
137+
138+
@Test
139+
fun `user id provided by client can be removed`() = runTest {
140+
val url = "http://localhost/v2"
141+
val modelClient = ModelClientV2
142+
.builder()
143+
.url(url)
144+
.client(client)
145+
.userId("a_user_id")
146+
.build()
147+
.also { it.init() }
148+
149+
modelClient.setClientProvideUserId(null)
150+
151+
assertEquals("localhost", modelClient.getUserId())
152+
}
121153
}

model-server/src/test/kotlin/org/modelix/model/server/ReplicatedRepositoryTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,33 @@ class ReplicatedRepositoryTest {
478478
branch.getRootNode().allChildren.map { (it as PNodeAdapter).nodeId.toString(16) }.toSortedSet()
479479
}
480480
}
481+
482+
@Test
483+
fun `used id specified for client is used in replicated model`() = runTest {
484+
val userId = "a_user_id"
485+
val url = "http://localhost/v2"
486+
val modelClient = ModelClientV2
487+
.builder()
488+
.url(url)
489+
.client(client)
490+
.userId(userId)
491+
.build()
492+
.also { it.init() }
493+
val repositoryId = RepositoryId("repo1")
494+
modelClient.initRepository(repositoryId)
495+
val replicatedModel = modelClient.getReplicatedModel(repositoryId.getBranchReference())
496+
val branch = replicatedModel.start() as OTBranch
497+
val initialVersion = modelClient.pull(replicatedModel.branchRef, null) as CLVersion
498+
499+
branch.computeWriteT {
500+
it.addNewChild(ITree.ROOT_ID, "role", -1, null as IConceptReference?)
501+
}
502+
while (replicatedModel.getCurrentVersion() == initialVersion) {
503+
delay(10)
504+
}
505+
506+
assertEquals(userId, replicatedModel.getCurrentVersion().author)
507+
}
481508
}
482509

483510
private fun IBranch.treeHash(): String = computeReadT { t -> (t.tree as CLTree).hash }

0 commit comments

Comments
 (0)