Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -30,7 +30,7 @@ class DocumentsApi(private val client: HttpClient, private val baseUrl: String)
lastSync: Instant,
token: String
): ResultData<List<Document>> {
val response = client.post("$baseUrl/api/document/folder/diff") {
val response = client.post("$baseUrl/api/workspace/document/folder/diff") {
contentType(ContentType.Application.Json)
setBody(FolderDiffRequest(folderId, workspaceId, lastSync.toEpochMilliseconds()))
header(HttpHeaders.Authorization, "Bearer $token")
Expand All @@ -39,6 +39,7 @@ class DocumentsApi(private val client: HttpClient, private val baseUrl: String)
return if (response.status.isSuccess()) {
ResultData.Complete(response.body<List<DocumentApi>>().map { it.toModel() })
} else {
println("getFolderNewDocuments failed. response: $response")
ResultData.Error()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class FolderSync(
private val documentConflictHandler: DocumentConflictHandler,
private val folderRepository: FolderRepository,
private val authRepository: AuthRepository,
private val minSyncInternal: Duration = 3.seconds
private val minSyncInternal: Duration = 2.seconds
) {

private var lastSuccessfulSync: Instant = Instant.DISTANT_PAST
Expand Down Expand Up @@ -64,13 +64,19 @@ class FolderSync(
authToken
)

val newDocuments = if (response is ResultData.Complete) response.data else return
val newDocuments = if (response is ResultData.Complete) {
response.data
} else {
println("newDocuments failed.")
return
}
println("Sync. received ${newDocuments.size} new documents")
// println("Documents: ${newDocuments.joinToString(separator = "\n\n")}")

// Then, load the outdated documents.
// These documents were updated locally, but were not sent to the backend yet
val localOutdatedDocs = documentRepository.loadOutdatedDocuments(folderId, workspaceId)
val localOutdatedDocs = documentRepository.loadOutdatedDocumentsByFolder(folderId, workspaceId)
println("Sync. loaded ${localOutdatedDocs.size} outdated documents")

// Resolve conflicts of documents that were updated both locally and in the backend.
// Documents will be saved locally by documentConflictHandler.handleConflict
Expand Down
3 changes: 0 additions & 3 deletions application/core/ollama/config/ktlint/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
<file name="src/commonMain/kotlin/io/writeopia/OllamaRepository.kt">
<error line="56" column="83" source="standard:function-expression-body" />
</file>
<file name="src/commonMain/kotlin/io/writeopia/api/OllamaApi.kt">
<error line="193" column="72" source="standard:function-expression-body" />
</file>
<file name="src/commonMain/kotlin/io/writeopia/persistence/OllamaSqlDao.kt">
<error line="10" column="17" source="standard:backing-property-naming" />
<error line="34" column="79" source="standard:function-expression-body" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ internal fun MobileChooseNoteScreen(

val hasSelectedNotes by chooseNoteViewModel.hasSelectedNotes.collectAsState()
val editState by chooseNoteViewModel.editState.collectAsState()

val folderEdit = chooseNoteViewModel.editFolderState.collectAsState().value

val showFab by derivedStateOf { !editState && !hasSelectedNotes }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class InMemoryDocumentRepository : DocumentRepository {
override suspend fun stopListeningForFoldersByParentId(parentId: String, workspaceId: String) {
}

override suspend fun loadOutdatedDocuments(folderId: String, workspaceId: String): List<Document> = emptyList()
override suspend fun loadOutdatedDocumentsByFolder(folderId: String, workspaceId: String): List<Document> = emptyList()

override suspend fun loadOutdatedDocumentsForWorkspace(workspaceId: String): List<Document> =
emptyList()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<baseline version="1.0">
<file name="src/commonMain/kotlin/io/writeopia/sdk/persistence/dao/DocumentEntityDao.kt">
<error line="48" column="12" source="standard:argument-list-wrapping" />
<error line="48" column="140" source="standard:max-line-length" />
<error line="48" column="161" source="standard:argument-list-wrapping" />
<error line="52" column="141" source="standard:max-line-length" />
</file>
</baseline>
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface DocumentEntityDao {
suspend fun deleteDocuments(vararg documents: DocumentEntity)

@Query("DELETE FROM $DOCUMENT_ENTITY WHERE workspace_id = :userId")
suspend fun deleteDocumentsByUserId(userId: String)
suspend fun purgeDocumentsByUserId(userId: String)

@Query("SELECT * FROM $DOCUMENT_ENTITY ORDER BY $DOCUMENT_ENTITY.last_updated_at LIMIT 10")
suspend fun selectByLastUpdated(): List<DocumentEntity>
Expand All @@ -45,8 +45,14 @@ interface DocumentEntityDao {
@Query("SELECT * FROM $DOCUMENT_ENTITY WHERE $DOCUMENT_ENTITY.parent_id = :id")
suspend fun loadDocumentsByParentId(id: String): List<DocumentEntity>

@Query("SELECT * FROM $DOCUMENT_ENTITY WHERE $DOCUMENT_ENTITY.parent_id = :folderId AND $DOCUMENT_ENTITY.last_updated_at > $DOCUMENT_ENTITY.last_synced_at")
suspend fun loadOutdatedDocumentsByFolderId(folderId: String): List<DocumentEntity>
@Query(
"SELECT * " +
"FROM $DOCUMENT_ENTITY " +
"LEFT OUTER JOIN $STORY_UNIT_ENTITY ON $DOCUMENT_ENTITY.id = $STORY_UNIT_ENTITY.document_id " +
"WHERE $DOCUMENT_ENTITY.parent_id = :folderId AND ($DOCUMENT_ENTITY.last_updated_at > $DOCUMENT_ENTITY.last_synced_at OR $DOCUMENT_ENTITY.last_synced_at IS NULL) " +
"ORDER BY $DOCUMENT_ENTITY.created_at, $STORY_UNIT_ENTITY.position"
)
suspend fun loadOutdatedDocumentsByFolderId(folderId: String): Map<DocumentEntity, List<StoryStepEntity>>

@Query("SELECT * FROM $DOCUMENT_ENTITY")
suspend fun loadAllDocuments(): List<DocumentEntity>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import io.writeopia.sdk.search.DocumentSearch
import io.writeopia.sdk.repository.DocumentRepository
import io.writeopia.sdk.persistence.dao.DocumentEntityDao
import io.writeopia.sdk.persistence.dao.StoryUnitEntityDao
import io.writeopia.sdk.persistence.entity.document.DocumentEntity
import io.writeopia.sdk.persistence.entity.story.StoryStepEntity
import io.writeopia.sdk.persistence.parse.toEntity
import io.writeopia.sdk.persistence.parse.toModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.datetime.Clock
import kotlinx.datetime.Instant
import kotlin.collections.component1
import kotlin.collections.component2
Expand Down Expand Up @@ -136,18 +136,24 @@ class RoomDocumentRepository(
}

override suspend fun deleteDocument(document: Document) {
documentEntityDao.deleteDocuments(document.toEntity())
documentEntityDao.updateDocument(
document.toEntity()
.copy(
lastUpdatedAt = Clock.System.now().toEpochMilliseconds(),
isDeleted = true
)
)
}

override suspend fun deleteDocumentByIds(ids: Set<String>) {
// The user ID is not relevant in the way to delete documents
documentEntityDao.deleteDocuments(
*ids.map {
DocumentEntity.createById(
it,
"",
parentId = ""
)
documentEntityDao.updateDocument(
*ids.mapNotNull {
documentEntityDao.loadDocumentById(it)
?.copy(
lastUpdatedAt = Clock.System.now().toEpochMilliseconds(),
isDeleted = true
)
}.toTypedArray()
)
}
Expand All @@ -162,7 +168,7 @@ class RoomDocumentRepository(
}

override suspend fun deleteByWorkspace(userId: String) {
documentEntityDao.deleteDocumentsByUserId(userId)
documentEntityDao.purgeDocumentsByUserId(userId)
}

override suspend fun moveDocumentsToWorkspace(oldUserId: String, newUserId: String) {
Expand Down Expand Up @@ -225,16 +231,15 @@ class RoomDocumentRepository(
) {
}

override suspend fun loadOutdatedDocuments(
override suspend fun loadOutdatedDocumentsByFolder(
folderId: String,
workspaceId: String
): List<Document> =
documentEntityDao.loadOutdatedDocumentsByFolderId(folderId).map { entity ->
val content = loadInnerSteps(
storyUnitEntityDao?.loadDocumentContent(entity.id) ?: emptyList()
)
entity.toModel(content = content)
}
documentEntityDao.loadOutdatedDocumentsByFolderId(folderId)
.map { (documentEntity, storyEntity) ->
val content = loadInnerSteps(storyEntity)
documentEntity.toModel(content)
}

override suspend fun loadOutdatedDocumentsForWorkspace(workspaceId: String): List<Document> =
documentEntityDao.loadOutdatedDocumentsWithContentForWorkspace(workspaceId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ fun DocumentEntity.toModel(content: Map<Int, StoryStep> = emptyMap()) = Document
favorite = favorite,
parentId = parentId,
isLocked = isLocked,
lastSyncedAt = lastSyncedAt?.let(Instant::fromEpochMilliseconds)
lastSyncedAt = lastSyncedAt?.let(Instant::fromEpochMilliseconds),
deleted = isDeleted
)

fun Document.toEntity() = DocumentEntity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class SqlDelightDocumentRepository(
)
}

override suspend fun loadOutdatedDocuments(
override suspend fun loadOutdatedDocumentsByFolder(
folderId: String,
workspaceId: String
): List<Document> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.writeopia.sdk.models.document.Document
import io.writeopia.sdk.models.document.MenuItem
import io.writeopia.sdk.models.story.StoryStep
import io.writeopia.sdk.models.story.StoryTypes
import io.writeopia.sdk.models.workspace.Workspace
import io.writeopia.sdk.persistence.sqldelight.dao.DocumentSqlDao
import io.writeopia.sdk.sql.WriteopiaDb
import kotlinx.coroutines.test.runTest
Expand Down Expand Up @@ -156,7 +155,7 @@ class SqlDelightDocumentRepositoryTest {

documentRepository.saveDocument(document)

val newDocument = documentRepository.loadOutdatedDocuments("root", workspaceId)
val newDocument = documentRepository.loadOutdatedDocumentsByFolder("root", workspaceId)

assertEquals(1, newDocument.size)
assertEquals(documentId, newDocument.first().id)
Expand Down Expand Up @@ -184,7 +183,7 @@ class SqlDelightDocumentRepositoryTest {

documentRepository.saveDocument(document)

val newDocument = documentRepository.loadOutdatedDocuments("root", workspaceId)
val newDocument = documentRepository.loadOutdatedDocumentsByFolder("root", workspaceId)

assertEquals(1, newDocument.size)
assertEquals(documentId, newDocument.first().id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface DocumentRepository : DocumentUpdate, DocumentSearch {
instant: Instant
): List<Document>

suspend fun loadOutdatedDocuments(folderId: String, workspaceId: String): List<Document>
suspend fun loadOutdatedDocumentsByFolder(folderId: String, workspaceId: String): List<Document>

suspend fun loadOutdatedDocumentsForWorkspace(workspaceId: String): List<Document>

Expand Down