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

Provide file lock-ability metadata in file provider item user info #18

Merged
merged 2 commits into from
Jul 30, 2024
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
8 changes: 8 additions & 0 deletions Sources/NextcloudFileProviderKit/Item/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ public class Item: NSObject, NSFileProviderItem {
return [.userReadable, .userWritable]
}

public var userInfo: [AnyHashable : Any]? {
[
// Can be used to display lock/unlock context menu entries for FPUIActions
"isLockable": !self.contentType.conforms(to: .directory) && !metadata.lock,
"isUnlockable": !self.contentType.conforms(to: .directory) && metadata.lock,
]
}

@available(macOS 13.0, *)
public var contentPolicy: NSFileProviderContentPolicy {
#if os(macOS)
Expand Down
88 changes: 88 additions & 0 deletions Tests/NextcloudFileProviderKitTests/ItemPropertyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,92 @@ final class ItemPropertyTests: XCTestCase {
)
XCTAssertTrue(item.contentType.conforms(to: .bundle))
}

func testItemUserInfoLockingPropsFolder() {
let metadata = ItemMetadata()
metadata.ocId = "test-id"
metadata.etag = "test-etag"
metadata.account = Self.account.ncKitAccount
metadata.name = "test"
metadata.fileName = "test"
metadata.fileNameView = "test"
metadata.serverUrl = Self.account.davFilesUrl
metadata.urlBase = Self.account.serverUrl
metadata.userId = Self.account.username
metadata.user = Self.account.username
metadata.directory = true
metadata.contentType = "httpd/unix-directory"
metadata.lockTime = .init()
metadata.lockTimeOut = .init()

let item = Item(
metadata: metadata,
parentItemIdentifier: .rootContainer,
remoteInterface: MockRemoteInterface(account: Self.account)
)

XCTAssertNotNil(item.userInfo?["isLockable"])
XCTAssertNotNil(item.userInfo?["isUnlockable"])
XCTAssertFalse(item.userInfo?["isLockable"] as? Bool ?? true) // directories not lockable
XCTAssertFalse(item.userInfo?["isUnlockable"] as? Bool ?? true)
}

func testItemUserInfoLockingPropsFileLocked() {
let metadata = ItemMetadata()
metadata.ocId = "test-id"
metadata.etag = "test-etag"
metadata.account = Self.account.ncKitAccount
metadata.name = "test.txt"
metadata.fileName = "test.txt"
metadata.fileNameView = "test.txt"
metadata.serverUrl = Self.account.davFilesUrl
metadata.urlBase = Self.account.serverUrl
metadata.userId = Self.account.username
metadata.user = Self.account.username
metadata.date = .init()
metadata.size = 12
metadata.lock = true
metadata.lockOwner = Self.account.username
metadata.lockOwnerEditor = "testEditor"
metadata.lockTime = .init()
metadata.lockTimeOut = .init().addingTimeInterval(6_000_000)

let item = Item(
metadata: metadata,
parentItemIdentifier: .rootContainer,
remoteInterface: MockRemoteInterface(account: Self.account)
)

XCTAssertNotNil(item.userInfo?["isLockable"])
XCTAssertNotNil(item.userInfo?["isUnlockable"])
XCTAssertFalse(item.userInfo?["isLockable"] as? Bool ?? true)
XCTAssertTrue(item.userInfo?["isUnlockable"] as? Bool ?? false) // locked file, can unlock
}

func testItemUserInfoLockingPropsFileUnlocked() {
let metadata = ItemMetadata()
metadata.ocId = "test-id"
metadata.etag = "test-etag"
metadata.account = Self.account.ncKitAccount
metadata.name = "test.txt"
metadata.fileName = "test.txt"
metadata.fileNameView = "test.txt"
metadata.serverUrl = Self.account.davFilesUrl
metadata.urlBase = Self.account.serverUrl
metadata.userId = Self.account.username
metadata.user = Self.account.username
metadata.date = .init()
metadata.size = 12

let item = Item(
metadata: metadata,
parentItemIdentifier: .rootContainer,
remoteInterface: MockRemoteInterface(account: Self.account)
)

XCTAssertNotNil(item.userInfo?["isLockable"])
XCTAssertNotNil(item.userInfo?["isUnlockable"])
XCTAssertTrue(item.userInfo?["isLockable"] as? Bool ?? false) // unlocked file, can lock
XCTAssertFalse(item.userInfo?["isUnlockable"] as? Bool ?? true)
}
}
Loading