Skip to content

Commit

Permalink
Make WebDavDocument room entity fully immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkup committed Jan 8, 2025
1 parent d7c7044 commit edc56ab
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 44 deletions.
28 changes: 14 additions & 14 deletions app/src/main/kotlin/at/bitfire/davdroid/db/WebDavDocument.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,30 @@ import java.time.Instant
data class WebDavDocument(

@PrimaryKey(autoGenerate = true)
var id: Long = 0,
val id: Long = 0,

/** refers to the [WebDavMount] the document belongs to */
val mountId: Long,

/** refers to parent document (*null* when this document is a root document) */
var parentId: Long?,
val parentId: Long?,

/** file name (without any slashes) */
var name: String,
var isDirectory: Boolean = false,
val name: String,
val isDirectory: Boolean = false,

var displayName: String? = null,
var mimeType: MediaType? = null,
var eTag: String? = null,
var lastModified: Long? = null,
var size: Long? = null,
val displayName: String? = null,
val mimeType: MediaType? = null,
val eTag: String? = null,
val lastModified: Long? = null,
val size: Long? = null,

var mayBind: Boolean? = null,
var mayUnbind: Boolean? = null,
var mayWriteContent: Boolean? = null,
val mayBind: Boolean? = null,
val mayUnbind: Boolean? = null,
val mayWriteContent: Boolean? = null,

var quotaAvailable: Long? = null,
var quotaUsed: Long? = null
val quotaAvailable: Long? = null,
val quotaUsed: Long? = null

) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ interface WebDavDocumentDao {
displayName = mount.name
)
val id = insertOrReplace(newDoc)
newDoc.id = id
return newDoc
return newDoc.copy(id = id)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,7 @@ class DavDocumentsProvider: DocumentsProvider() {
// successfully moved
}

doc.parentId = dstParent.id
documentDao.update(doc)
documentDao.update(doc.copy(parentId = dstParent.id))

actor.notifyFolderChanged(sourceParentDocumentId)
actor.notifyFolderChanged(targetParentDocumentId)
Expand Down Expand Up @@ -449,8 +448,7 @@ class DavDocumentsProvider: DocumentsProvider() {
dav.move(newLocation, false) {
// successfully renamed
}
doc.name = newName
documentDao.update(doc)
documentDao.update(doc.copy(name = newName))

actor.notifyFolderChanged(doc.parentId)
return doc.id.toString()
Expand Down Expand Up @@ -531,9 +529,7 @@ class DavDocumentsProvider: DocumentsProvider() {
val now = System.currentTimeMillis()
if (!readAccess /* write access */) {
// write access, update file size
doc.size = transferred
doc.lastModified = now
documentDao.update(doc)
documentDao.update(doc.copy(size = transferred, lastModified = now))
}

actor.notifyFolderChanged(doc.parentId)
Expand Down Expand Up @@ -658,7 +654,7 @@ class DavDocumentsProvider: DocumentsProvider() {
folder.propfind(1, *DAV_FILE_FIELDS) { response, relation ->
logger.fine("$relation $response")

val resource: WebDavDocument =
var resource: WebDavDocument =
when (relation) {
Response.HrefRelation.SELF -> // it's about the parent
parent
Expand All @@ -671,26 +667,19 @@ class DavDocumentsProvider: DocumentsProvider() {
}
}

response[ResourceType::class.java]?.types?.let { types ->
resource.isDirectory = types.contains(ResourceType.COLLECTION)
}

resource.displayName = response[DisplayName::class.java]?.displayName
resource.mimeType = response[GetContentType::class.java]?.type
response[GetETag::class.java]?.let { getETag ->
if (!getETag.weak)
resource.eTag = resource.eTag
}
resource.lastModified = response[GetLastModified::class.java]?.lastModified?.toEpochMilli()
resource.size = response[GetContentLength::class.java]?.contentLength

val privs = response[CurrentUserPrivilegeSet::class.java]
resource.mayBind = privs?.mayBind
resource.mayUnbind = privs?.mayUnbind
resource.mayWriteContent = privs?.mayWriteContent

resource.quotaAvailable = response[QuotaAvailableBytes::class.java]?.quotaAvailableBytes
resource.quotaUsed = response[QuotaUsedBytes::class.java]?.quotaUsedBytes
resource = resource.copy(
isDirectory = response[ResourceType::class.java]?.types?.contains(ResourceType.COLLECTION) ?: resource.isDirectory,
displayName = response[DisplayName::class.java]?.displayName,
mimeType = response[GetContentType::class.java]?.type,
eTag = response[GetETag::class.java]?.takeIf { !it.weak }?.let { resource.eTag },
lastModified = response[GetLastModified::class.java]?.lastModified?.toEpochMilli(),
size = response[GetContentLength::class.java]?.contentLength,
mayBind = response[CurrentUserPrivilegeSet::class.java]?.mayBind,
mayUnbind = response[CurrentUserPrivilegeSet::class.java]?.mayUnbind,
mayWriteContent = response[CurrentUserPrivilegeSet::class.java]?.mayWriteContent,
quotaAvailable = response[QuotaAvailableBytes::class.java]?.quotaAvailableBytes,
quotaUsed = response[QuotaUsedBytes::class.java]?.quotaUsedBytes,
)

if (resource == parent)
documentDao.update(resource)
Expand Down

0 comments on commit edc56ab

Please sign in to comment.