Skip to content

Commit eb2298b

Browse files
leandroBorgesFerreiraLeandro Ferreira
andauthored
Separating notes per user and team (#457)
* sketch * Adding user for the documents * Fixing compilation * Update WriteopiaStateManagerTest.kt --------- Co-authored-by: Leandro Ferreira <[email protected]>
1 parent 1a01097 commit eb2298b

43 files changed

Lines changed: 344 additions & 263 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

application/core/auth_core/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ kotlin {
2929
sourceSets {
3030
val commonMain by getting {
3131
dependencies {
32+
implementation(project(":writeopia"))
3233
implementation(project(":writeopia_models"))
3334
implementation(project(":application:core:models"))
3435

application/core/auth_core/src/commonMain/kotlin/io/writeopia/auth/core/manager/AuthRepository.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package io.writeopia.auth.core.manager
22

33
import io.writeopia.common.utils.ResultData
44
import io.writeopia.sdk.models.user.WriteopiaUser
5+
import io.writeopia.sdk.repository.UserRepository
56
import kotlinx.coroutines.flow.Flow
67
import kotlinx.coroutines.flow.flow
78

8-
interface AuthRepository {
9+
interface AuthRepository : UserRepository {
910

10-
fun listenForUser(): Flow<WriteopiaUser> = flow { emit(getUser()) }
11+
override fun listenForUser(): Flow<WriteopiaUser> = flow { emit(getUser()) }
1112

12-
suspend fun getUser(): WriteopiaUser
13+
override suspend fun getUser(): WriteopiaUser
1314

1415
suspend fun isLoggedIn(): Boolean
1516

application/core/documents/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ kotlin {
3636
implementation(project(":plugins:writeopia_serialization"))
3737

3838
implementation(project(":application:core:utils"))
39+
implementation(project(":application:core:auth_core"))
3940
implementation(project(":application:core:models"))
4041
implementation(project(":application:core:common_ui"))
4142
implementation(project(":application:core:persistence_bridge"))

application/core/documents/src/commonMain/kotlin/io/writeopia/core/folders/repository/NotesUseCase.kt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.writeopia.core.folders.repository
22

3+
import io.writeopia.auth.core.manager.AuthRepository
34
import io.writeopia.common.utils.NotesNavigation
45
import io.writeopia.common.utils.collections.merge
56
import io.writeopia.commonui.dtos.MenuItemUi
@@ -23,6 +24,7 @@ class NotesUseCase private constructor(
2324
private val documentRepository: DocumentRepository,
2425
private val notesConfig: ConfigurationRepository,
2526
private val folderRepository: FolderRepository,
27+
private val authRepository: AuthRepository
2628
) {
2729

2830
suspend fun createFolder(name: String, userId: String) {
@@ -43,8 +45,12 @@ class NotesUseCase private constructor(
4345
suspend fun updateDocumentById(id: String, documentChange: (Document) -> Document) {
4446
documentRepository.loadDocumentById(id)
4547
?.let(documentChange)
46-
?.let { newDocument -> documentRepository.saveDocumentMetadata(newDocument) }
47-
?.also { documentRepository.refreshDocuments() }
48+
?.let { newDocument ->
49+
documentRepository.saveDocumentMetadata(
50+
newDocument,
51+
authRepository.getUser().id
52+
)
53+
}?.also { documentRepository.refreshDocuments() }
4854
}
4955

5056
suspend fun moveItem(menuItem: MenuItemUi, parentId: String) {
@@ -142,7 +148,7 @@ class NotesUseCase private constructor(
142148
}
143149

144150
suspend fun saveDocumentDb(document: Document) {
145-
documentRepository.saveDocument(document)
151+
documentRepository.saveDocument(document, authRepository.getUser().id)
146152
documentRepository.refreshDocuments()
147153
}
148154

@@ -180,7 +186,7 @@ class NotesUseCase private constructor(
180186
}.map { document ->
181187
document.duplicateWithNewIds()
182188
}.forEach { document ->
183-
documentRepository.saveDocument(document)
189+
documentRepository.saveDocument(document, authRepository.getUser().id)
184190
}
185191

186192
documentRepository.refreshDocuments()
@@ -231,7 +237,7 @@ class NotesUseCase private constructor(
231237
parentId = newFolder.id
232238
)
233239
}.forEach { document ->
234-
documentRepository.saveDocument(document)
240+
documentRepository.saveDocument(document, authRepository.getUser().id)
235241
}
236242

237243
newFolder
@@ -270,11 +276,13 @@ class NotesUseCase private constructor(
270276
documentRepository: DocumentRepository,
271277
notesConfig: ConfigurationRepository,
272278
folderRepository: FolderRepository,
279+
authRepository: AuthRepository
273280
): NotesUseCase =
274281
instance ?: NotesUseCase(
275282
documentRepository,
276283
notesConfig,
277284
folderRepository,
285+
authRepository
278286
).also {
279287
instance = it
280288
}

application/core/documents/src/commonMain/kotlin/io/writeopia/core/folders/sync/DocumentConflictHandler.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package io.writeopia.core.folders.sync
22

3+
import io.writeopia.auth.core.manager.AuthRepository
34
import io.writeopia.sdk.models.document.Document
45
import io.writeopia.sdk.repository.DocumentRepository
56
import kotlinx.datetime.Clock
67

7-
class DocumentConflictHandler(private val documentRepository: DocumentRepository) {
8+
class DocumentConflictHandler(
9+
private val documentRepository: DocumentRepository,
10+
private val authRepository: AuthRepository
11+
) {
812

913
/**
1014
* Handle conflicts with documents that were updated both locally and in the backend.
@@ -19,11 +23,13 @@ class DocumentConflictHandler(private val documentRepository: DocumentRepository
1923
externalDocuments: List<Document>
2024
): List<Document> {
2125
val now = Clock.System.now()
22-
2326
// Todo: Implement!! Save external documents and remove localDocuments. A more complex
2427
// handling of conflicts can be implemented in the future.
2528
externalDocuments.forEach { document ->
26-
documentRepository.saveDocument(document.copy(lastSyncedAt = now))
29+
documentRepository.saveDocument(
30+
document.copy(lastSyncedAt = now),
31+
authRepository.getUser().id
32+
)
2733
}
2834

2935
return (localDocuments.toSet() - externalDocuments.toSet()).toList()

application/core/documents/src/commonMain/kotlin/io/writeopia/core/folders/sync/DocumentsSync.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class DocumentsSync(
1818
* This logic is atomic. If it fails, the whole process must be tried again in a future time.
1919
* The sync time of the folder will only be updated with everything works correctly.
2020
*/
21-
suspend fun syncFolder(folderId: String) {
21+
suspend fun syncFolder(folderId: String, userId: String) {
2222
val lastSync = documentRepository.loadDocumentById(folderId)?.lastSyncedAt
2323

2424
// First, receive the documents for the backend.
@@ -43,7 +43,7 @@ class DocumentsSync(
4343
// If everything ran accordingly, update the sync time of the folder.
4444
documentsNotSent.forEach { document ->
4545
val newDocument = document.copy(lastSyncedAt = now)
46-
documentRepository.saveDocumentMetadata(newDocument)
46+
documentRepository.saveDocumentMetadata(newDocument, userId)
4747
}
4848

4949
documentRepository.refreshDocuments()

application/core/ollama/src/commonMain/kotlin/io/writeopia/OllamaRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class OllamaRepository(
6464
}
6565

6666
suspend fun getConfiguredOllamaUrl(id: String = "disconnected_user"): String? =
67-
ollamaDao?.getConfiguration()?.url
67+
ollamaDao?.getConfiguration(id)?.url
6868

6969
suspend fun deleteModel(model: String, url: String): ResultData<Boolean> =
7070
ollamaApi.removeModel(model, url)

application/core/persistence_sqldelight/src/commonMain/kotlin/io/writeopia/sqldelight/theme/UiConfigurationSqlDelightDao.kt

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,22 @@ class UiConfigurationSqlDelightDao(database: WriteopiaDb?) {
2929
)
3030
}
3131

32-
// if (userId == uiConfiguration.user_id) {
32+
if (userId == uiConfiguration.user_id) {
3333
configurationState.value = uiConfiguration
34-
// }
34+
}
3535
}
3636

3737
suspend fun getConfigurationByUserId(userId: String): UiConfigurationEntity? =
38-
uiConfigurationQueries?.selectConfigurationByUserId(
39-
"disconnected_user"
40-
// userId
41-
)?.awaitAsOneOrNull()
38+
uiConfigurationQueries?.selectConfigurationByUserId(userId)?.awaitAsOneOrNull()
4239

4340
fun listenForConfigurationByUserId(
44-
getUserId: suspend () -> String,
41+
userId: String,
4542
coroutineScope: CoroutineScope
4643
): Flow<UiConfigurationEntity?> {
47-
coroutineScope.launch(Dispatchers.Default) {
48-
val id = "disconnected_user"
49-
// getUserId()
44+
this.userId = userId
5045

51-
userId = id
52-
val config = getConfigurationByUserId(id)
46+
coroutineScope.launch(Dispatchers.Default) {
47+
val config = getConfigurationByUserId(userId)
5348
configurationState.value = config
5449
}
5550

application/core/theme/src/commonMain/kotlin/io/writeopia/repository/UiConfigurationMemoryRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class UiConfigurationMemoryRepository : UiConfigurationRepository {
3030
}
3131

3232
override fun listenForUiConfiguration(
33-
getUserId: suspend () -> String,
33+
getUserId: String,
3434
coroutineScope: CoroutineScope
3535
): Flow<UiConfiguration?> = uiConfiguration
3636
}

application/core/theme/src/commonMain/kotlin/io/writeopia/repository/UiConfigurationRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface UiConfigurationRepository {
1212
suspend fun updateConfiguration(userId: String, change: (UiConfiguration) -> UiConfiguration)
1313

1414
fun listenForUiConfiguration(
15-
getUserId: suspend () -> String,
15+
getUserId: String,
1616
coroutineScope: CoroutineScope
1717
): Flow<UiConfiguration?>
1818
}

0 commit comments

Comments
 (0)