Skip to content

Open source CapCut alternative.

TypeScript

feat(ios): a saved link streams from the account

David Le1f2c0a4de7bb
diff --git a/apps/ios/Donkey/Controllers/CutCloudClient.swift b/apps/ios/Donkey/Controllers/CutCloudClient.swift
index 2c8b7bb3..05944961 100644
--- a/apps/ios/Donkey/Controllers/CutCloudClient.swift
+++ b/apps/ios/Donkey/Controllers/CutCloudClient.swift
@@ -38,6 +38,12 @@ final class CutCloudClient: NSObject {
return request
}
+ fileprivate func libraryMediaPath(_ fileName: String) -> String {
+ "/api/cut-cloud/library/media/" + (fileName.addingPercentEncoding(
+ withAllowedCharacters: .urlPathAllowed
+ ) ?? fileName)
+ }
+
private func send(_ request: URLRequest) async throws -> Data {
let data: Data
let response: URLResponse
@@ -101,6 +107,14 @@ final class CutCloudClient: NSObject {
// MARK: - Media up-sync
+extension CutCloudClient: InspirationStreaming {
+ /// The account's own copy, behind the API's redirect: an inspiration card
+ /// streams the CDN directly and nothing is stored on the phone.
+ func libraryMediaURL(fileName: String) async throws -> URL {
+ try await resolveRedirect(libraryMediaPath(fileName))
+ }
+}
+
extension CutCloudClient: CloudSyncServicing {
private struct PresignResponse: Decodable {
var fileName: String
@@ -269,18 +283,21 @@ extension CutCloudClient: CloudSyncServicing {
return try decode(QueuedJob.self, from: data).jobId
}
- func importedLink(jobId: String) async throws -> JobOutcome<ImportedLink> {
+ func importedLink(jobId: String) async throws -> LinkImport {
struct JobStatus: Decodable {
struct Result: Decodable {
struct Asset: Decodable {
var id: String
var fileName: String
var type: String
+ var posterFile: String?
}
var assets: [Asset]?
+ var text: String?
}
var state: String
var result: Result?
+ var error: String?
}
let data = try await send(try request("GET", "/api/cut-cloud/jobs/\(jobId)"))
let status = try decode(JobStatus.self, from: data)
@@ -289,25 +306,26 @@ extension CutCloudClient: CloudSyncServicing {
return .running
case "done":
// A source that was only words brings back no media; the card
- // stays the link it was saved as.
- guard let asset = status.result?.assets?.first else { return .failed }
- return .done(
+ // stays the link it was saved as, carrying what the source said.
+ guard let asset = status.result?.assets?.first else {
+ return .noMedia(text: status.result?.text)
+ }
+ return .ready(
ImportedLink(
assetId: asset.id,
fileName: asset.fileName,
- isVideo: asset.type == "video"
+ isVideo: asset.type == "video",
+ posterFile: asset.posterFile,
+ text: status.result?.text
)
)
default:
- return .failed
+ return .failed(status.error ?? "The fetch failed.")
}
}
func downloadLibraryMedia(fileName: String, to destination: URL) async throws {
- let path = "/api/cut-cloud/library/media/" + (fileName.addingPercentEncoding(
- withAllowedCharacters: .urlPathAllowed
- ) ?? fileName)
- let url = try await resolveRedirect(path)
+ let url = try await resolveRedirect(libraryMediaPath(fileName))
let temporary: URL
do {
(temporary, _) = try await URLSession.shared.download(from: url)
diff --git a/apps/ios/Donkey/DonkeyApp.swift b/apps/ios/Donkey/DonkeyApp.swift
index 14119e5d..1f9da9b0 100644
--- a/apps/ios/Donkey/DonkeyApp.swift
+++ b/apps/ios/Donkey/DonkeyApp.swift
@@ -62,6 +62,7 @@ final class AppWiring {
sync.ideas = ideas
media.sync = sync
ideas.onLocalChange = { sync.kick() }
+ ideas.cloud = cloud
self.app = app
self.ideas = ideas
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CloudSync.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CloudSync.swift
index 3cfd918b..8ca1b7ae 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CloudSync.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/CloudSync.swift
@@ -157,22 +157,47 @@ nonisolated public struct RemoteNotes: Equatable, Sendable {
}
/// What the cloud brought back for a saved link, once its import job is done.
+/// The media stays on the account's shelf; the phone keeps these references
+/// and streams from them.
nonisolated public struct ImportedLink: Equatable, Sendable {
/// The library asset the worker registered, so a delete here takes the
/// cloud copy with it.
public var assetId: String
- /// The media's name on the cloud shelf, for the download.
+ /// The media's name on the cloud shelf, for the stream URL.
public var fileName: String
public var isVideo: Bool
+ /// The source's cover, stored beside the media under the same prefix.
+ public var posterFile: String?
+ /// The source's own words, when it had any.
+ public var text: String?
- public init(assetId: String, fileName: String, isVideo: Bool) {
+ public init(
+ assetId: String,
+ fileName: String,
+ isVideo: Bool,
+ posterFile: String? = nil,
+ text: String? = nil
+ ) {
self.assetId = assetId
self.fileName = fileName
self.isVideo = isVideo
+ self.posterFile = posterFile
+ self.text = text
}
}
-/// Where a queued cloud job stands.
+/// How a link's import job stands, as the phone polls it.
+nonisolated public enum LinkImport: Equatable, Sendable {
+ /// Queued or running.
+ case running
+ /// The worker landed media on the account's shelf.
+ case ready(ImportedLink)
+ /// The source was only words. Whatever it said comes back with it.
+ case noMedia(text: String?)
+ /// The fetch failed, in the worker's own words.
+ case failed(String)
+}
+
/// Where a queued cloud render stands, as the phone polls it.
nonisolated public enum RenderProgress: Equatable, Sendable {
/// Waiting for a worker to claim it.
@@ -183,12 +208,6 @@ nonisolated public enum RenderProgress: Equatable, Sendable {
case failed(String)
}
-nonisolated public enum JobOutcome<Value: Sendable>: Sendable {
- case running
- case done(Value)
- case failed
-}
-
nonisolated public enum CloudSyncError: Error, Equatable {
/// The server refused with a reason worth reading out loud — out of
/// renders for the day, out of room. The message is the server's own.
@@ -218,10 +237,10 @@ public protocol CloudSyncServicing: AnyObject {
/// fetches the media into the Inspiration folder). Returns the job id the
/// phone follows to bring the media down.
func importInspirationLink(_ url: URL) async throws -> String
- /// Where an import job stands. `done` carries the first piece of media
- /// the source yielded; a source that was only words comes back `failed`.
- func importedLink(jobId: String) async throws -> JobOutcome<ImportedLink>
- /// Download one library media file to a local URL.
+ /// Where an import job stands.
+ func importedLink(jobId: String) async throws -> LinkImport
+ /// Download one library media file to a local URL. Posters only: a link's
+ /// video stays in the cloud and streams.
func downloadLibraryMedia(fileName: String, to destination: URL) async throws
func fetchUsage() async throws -> StorageUsage
func fetchNotes() async throws -> RemoteNotes
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/DonkeyStore.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/DonkeyStore.swift
index e32599ed..d307deee 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/DonkeyStore.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/DonkeyStore.swift
@@ -83,12 +83,20 @@ final class InspirationRecord {
var linkSyncedAt: Date?
/// The import job the worker is running for this link, while it runs.
var importJobId: String?
- /// The media the cloud brought back for this link, downloaded here.
- var fetchedFileName: String?
- var fetchedIsVideo: Bool?
- /// The source had no media to bring back, or the fetch failed. The card
- /// stays a link and nothing retries.
- var importFailed: Bool?
+ /// The media the cloud fetched for this link, on the account's shelf. The
+ /// bytes stay there and the card streams them.
+ var cloudFileName: String?
+ var cloudIsVideo: Bool?
+ /// The source's cover, downloaded here so the card paints without the
+ /// network.
+ var posterFileName: String?
+ /// The source's own words, shown on the card.
+ var sourceText: String?
+ /// The source was only words: there is no media to stream and nothing to
+ /// retry.
+ var noMedia: Bool?
+ /// Why the last attempt failed. Set while the card offers another try.
+ var importError: String?
init(id: UUID, linkURL: String?, mediaFileName: String?, isVideo: Bool, createdAt: Date) {
self.id = id
@@ -100,20 +108,47 @@ final class InspirationRecord {
var item: InspirationItem? {
if let linkURL, let url = URL(string: linkURL) {
+ let cloud = cloudFileName.map {
+ InspirationCloudMedia(
+ assetId: remoteAssetId ?? "",
+ fileName: $0,
+ isVideo: cloudIsVideo ?? false,
+ posterFileName: posterFileName
+ )
+ }
+ // Only a live job is fetching. A link the cloud took whose job
+ // is gone — an import from a build that landed media differently,
+ // a job that outlived its row — has nothing working on it, and the
+ // card says so.
+ let state: InspirationImport = if let importError {
+ .failed(importError)
+ } else if cloud != nil {
+ .ready
+ } else if noMedia == true {
+ .noMedia
+ } else if linkSyncedAt == nil {
+ .queued
+ } else if importJobId != nil {
+ .fetching
+ } else {
+ .failed("The fetch didn't finish.")
+ }
return InspirationItem(
id: id,
kind: .link(url),
createdAt: createdAt,
- fetched: fetchedFileName.map {
- InspirationMedia(fileName: $0, isVideo: fetchedIsVideo ?? false)
- },
- importState: importFailed == true
- ? .failed
- : (linkSyncedAt == nil ? .waiting : .fetching)
+ cloud: cloud,
+ sourceText: sourceText,
+ importState: state
)
}
if let mediaFileName {
- return InspirationItem(id: id, kind: .media(fileName: mediaFileName, isVideo: isVideo), createdAt: createdAt)
+ return InspirationItem(
+ id: id,
+ kind: .media(fileName: mediaFileName, isVideo: isVideo),
+ createdAt: createdAt,
+ importState: .ready
+ )
}
return nil
}
@@ -284,9 +319,7 @@ public final class DonkeyStore: IdeasStoring, RecordingStoring, SyncJournalStori
public func deleteInspiration(id: UUID) throws {
let descriptor = FetchDescriptor<InspirationRecord>(predicate: #Predicate { $0.id == id })
if let record = try context.fetch(descriptor).first {
- for fileName in [record.mediaFileName, record.fetchedFileName].compactMap({ $0 }) {
- try? FileManager.default.removeItem(at: mediaDirectory.appending(path: fileName))
- }
+ removeFiles(of: record)
if let remote = record.remoteAssetId {
context.insert(TombstoneRecord(kind: SyncTombstone.Kind.libraryAsset.rawValue, remoteId: remote, stamp: .now))
}
@@ -464,9 +497,7 @@ public final class DonkeyStore: IdeasStoring, RecordingStoring, SyncJournalStori
public func removeInspirationFromCloudDelete(id: UUID) throws {
let descriptor = FetchDescriptor<InspirationRecord>(predicate: #Predicate { $0.id == id })
guard let record = try context.fetch(descriptor).first else { return }
- for fileName in [record.mediaFileName, record.fetchedFileName].compactMap({ $0 }) {
- try? FileManager.default.removeItem(at: mediaDirectory.appending(path: fileName))
- }
+ removeFiles(of: record)
context.delete(record)
try context.save()
}
@@ -505,6 +536,7 @@ public final class DonkeyStore: IdeasStoring, RecordingStoring, SyncJournalStori
guard let record = try context.fetch(descriptor).first else { return }
record.linkSyncedAt = .now
record.importJobId = jobId
+ record.importError = nil
try context.save()
}
@@ -513,35 +545,81 @@ public final class DonkeyStore: IdeasStoring, RecordingStoring, SyncJournalStori
return try context.fetch(descriptor).first?.importJobId
}
- /// Where a link's fetched media is written. The name is the item's, so a
- /// repeated fetch overwrites rather than piling up files.
- public func fetchedMediaDestination(_ id: UUID, isVideo: Bool) -> URL {
- mediaDirectory.appending(path: id.uuidString + "-source" + (isVideo ? ".mp4" : ".jpg"))
+ /// Every file this item owns on the phone: what it holds now, and anything
+ /// else written under its id — media an earlier build brought down before
+ /// links started streaming.
+ private func removeFiles(of record: InspirationRecord) {
+ for fileName in [record.mediaFileName, record.posterFileName].compactMap({ $0 }) {
+ try? FileManager.default.removeItem(at: mediaDirectory.appending(path: fileName))
+ }
+ let prefix = record.id.uuidString
+ let names = (try? FileManager.default.contentsOfDirectory(atPath: mediaDirectory.localPath)) ?? []
+ for name in names where name.hasPrefix(prefix) {
+ try? FileManager.default.removeItem(at: mediaDirectory.appending(path: name))
+ }
}
+ /// Where a link's poster is written. The name is the item's, so a repeated
+ /// fetch overwrites rather than piling up files.
+ public func posterDestination(_ id: UUID) -> URL {
+ mediaDirectory.appending(path: id.uuidString + "-poster.jpg")
+ }
+
+ /// The cloud holds this link's media. Only the poster is here; the video
+ /// or photo streams from the account's shelf.
public func markInspirationFetched(
_ id: UUID,
fileName: String,
isVideo: Bool,
+ posterFileName: String?,
+ sourceText: String?,
remoteAssetId: String
) throws {
let descriptor = FetchDescriptor<InspirationRecord>(predicate: #Predicate { $0.id == id })
guard let record = try context.fetch(descriptor).first else { return }
- record.fetchedFileName = fileName
- record.fetchedIsVideo = isVideo
+ record.cloudFileName = fileName
+ record.cloudIsVideo = isVideo
+ record.posterFileName = posterFileName
+ record.sourceText = sourceText ?? record.sourceText
record.remoteAssetId = remoteAssetId
record.importJobId = nil
- record.importFailed = nil
+ record.noMedia = nil
+ record.importError = nil
+ try context.save()
+ }
+
+ /// The source was only words. The card keeps them and stays a link; there
+ /// is nothing to retry.
+ public func markInspirationNoMedia(_ id: UUID, sourceText: String?) throws {
+ let descriptor = FetchDescriptor<InspirationRecord>(predicate: #Predicate { $0.id == id })
+ guard let record = try context.fetch(descriptor).first else { return }
+ record.importJobId = nil
+ record.noMedia = true
+ record.sourceText = sourceText
+ record.importError = nil
+ try context.save()
+ }
+
+ /// The attempt failed, in the words the card shows. A job that came back
+ /// with the failure is finished with: it is dropped, and the item waits
+ /// for the retry.
+ public func markInspirationImportFailed(_ id: UUID, message: String, clearJob: Bool) throws {
+ let descriptor = FetchDescriptor<InspirationRecord>(predicate: #Predicate { $0.id == id })
+ guard let record = try context.fetch(descriptor).first else { return }
+ if clearJob { record.importJobId = nil }
+ record.importError = message
try context.save()
}
- /// The cloud brought nothing back. The card stays a link and the job is
- /// not asked about again.
- public func markInspirationImportFailed(_ id: UUID) throws {
+ /// Back in the queue: the failure clears and the next pass hands the link
+ /// over again.
+ public func retryInspirationImport(id: UUID) throws {
let descriptor = FetchDescriptor<InspirationRecord>(predicate: #Predicate { $0.id == id })
guard let record = try context.fetch(descriptor).first else { return }
+ record.importError = nil
record.importJobId = nil
- record.importFailed = true
+ record.linkSyncedAt = nil
+ record.noMedia = nil
try context.save()
}
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Ideas.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Ideas.swift
index 936bbf3d..d322d9ac 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Ideas.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/Ideas.swift
@@ -103,50 +103,77 @@ nonisolated public struct InspirationMedia: Equatable, Sendable {
}
}
+/// What the cloud fetched for a saved link. The media lives on the account's
+/// library shelf — the same shelf the web Library shows — and the phone
+/// streams it from there; only the poster comes down.
+nonisolated public struct InspirationCloudMedia: Equatable, Sendable {
+ /// The library asset, so a delete here takes the cloud copy with it.
+ public var assetId: String
+ /// The media's name on the shelf, for the stream URL.
+ public var fileName: String
+ public var isVideo: Bool
+ /// The poster this phone downloaded beside it, if the source had one.
+ public var posterFileName: String?
+
+ public init(assetId: String, fileName: String, isVideo: Bool, posterFileName: String? = nil) {
+ self.assetId = assetId
+ self.fileName = fileName
+ self.isVideo = isVideo
+ self.posterFileName = posterFileName
+ }
+}
+
/// Where a saved link stands with the cloud fetch that turns it into media.
nonisolated public enum InspirationImport: Equatable, Sendable {
- /// Nothing has been asked of the cloud yet — the item was just saved, or
- /// the phone is offline.
- case waiting
- /// The worker is fetching the source.
+ /// Saved here; the cloud has not taken the job yet.
+ case queued
+ /// A worker is fetching the source.
case fetching
- /// The source had no media to bring back, or the fetch failed. The card
- /// stays a link.
- case failed
+ /// The cloud holds media for this link.
+ case ready
+ /// The source was only words. The card stays a link and carries its text.
+ case noMedia
+ /// The last attempt failed, and says why. The card offers another try.
+ case failed(String)
}
nonisolated public struct InspirationItem: Identifiable, Equatable, Sendable {
public var id: UUID
public var kind: InspirationKind
public var createdAt: Date
- /// A saved link whose media the cloud fetched and this phone downloaded.
- /// The card plays it in place, the way a photo-library import plays.
- public var fetched: InspirationMedia?
+ /// What the cloud fetched for a saved link. The bytes stay in the account,
+ /// and the card streams them.
+ public var cloud: InspirationCloudMedia?
+ /// The source's own words — a tweet's text, a video's title and
+ /// description — quoted on the card beside its media.
+ public var sourceText: String?
/// Where the fetch of a link stands. Media saved from the photo library
- /// has nothing to fetch and reads as `.waiting`.
+ /// has nothing to fetch and reads as `.ready`.
public var importState: InspirationImport
public init(
id: UUID = UUID(),
kind: InspirationKind,
createdAt: Date = .now,
- fetched: InspirationMedia? = nil,
- importState: InspirationImport = .waiting
+ cloud: InspirationCloudMedia? = nil,
+ sourceText: String? = nil,
+ importState: InspirationImport = .queued
) {
self.id = id
self.kind = kind
self.createdAt = createdAt
- self.fetched = fetched
+ self.cloud = cloud
+ self.sourceText = sourceText
self.importState = importState
}
- /// The media this card shows: what the phone imported, or what the cloud
- /// brought back for a link.
- public var media: InspirationMedia? {
+ /// Media on this phone: a photo-library import. A link's media is the
+ /// cloud's and streams from there.
+ public var localMedia: InspirationMedia? {
if case .media(let fileName, let isVideo) = kind {
return InspirationMedia(fileName: fileName, isVideo: isVideo)
}
- return fetched
+ return nil
}
/// The source link, for a saved link.
@@ -180,10 +207,19 @@ public protocol IdeasStoring: AnyObject {
func addLink(_ url: URL) throws -> InspirationItem
func addMedia(data: Data, isVideo: Bool) throws -> InspirationItem
func deleteInspiration(id: UUID) throws
+ /// Put a link's import back in the queue after a failure.
+ func retryInspirationImport(id: UUID) throws
/// Absolute location of a stored media file.
func mediaURL(fileName: String) -> URL
}
+/// The account's cloud shelf, as an inspiration card needs it: one signed URL
+/// per file, minted on demand. The bytes stay in the cloud; the card streams
+/// them.
+public protocol InspirationStreaming: AnyObject, Sendable {
+ func libraryMediaURL(fileName: String) async throws -> URL
+}
+
nonisolated public enum IdeasFilter: String, CaseIterable, Sendable {
case all, notes, inspiration
}
@@ -204,6 +240,10 @@ public final class IdeasModel {
private let store: any IdeasStoring
+ /// The cloud, for streaming a link's fetched media. Wired by the app
+ /// entry; nil in tests, where cards paint from what the store holds.
+ public weak var cloud: (any InspirationStreaming)?
+
public init(store: any IdeasStoring) {
self.store = store
reloadFromStore()
@@ -362,5 +402,18 @@ public final class IdeasModel {
onLocalChange?()
}
+ /// Ask the cloud for this link again. The failure clears, the item goes
+ /// back in the queue, and the next sync pass hands it over.
+ public func retryInspiration(id: UUID) {
+ try? store.retryInspirationImport(id: id)
+ reloadFromStore()
+ onLocalChange?()
+ }
+
+ /// Where this card streams from: a signed URL for the account's own copy.
+ public func streamURL(for media: InspirationCloudMedia) async -> URL? {
+ try? await cloud?.libraryMediaURL(fileName: media.fileName)
+ }
+
public func mediaURL(fileName: String) -> URL { store.mediaURL(fileName: fileName) }
}
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift
index 70ab15b3..5746abc8 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitModels/SyncEngine.swift
@@ -82,15 +82,18 @@ public protocol SyncJournalStoring: AnyObject {
func markInspirationLinkSynced(_ id: UUID, jobId: String) throws
/// The job still fetching this link, if one is.
func inspirationImportJobId(_ id: UUID) throws -> String?
- /// Where the fetched media is written on this phone.
- func fetchedMediaDestination(_ id: UUID, isVideo: Bool) -> URL
+ /// Where the source's poster is written on this phone.
+ func posterDestination(_ id: UUID) -> URL
func markInspirationFetched(
_ id: UUID,
fileName: String,
isVideo: Bool,
+ posterFileName: String?,
+ sourceText: String?,
remoteAssetId: String
) throws
- func markInspirationImportFailed(_ id: UUID) throws
+ func markInspirationNoMedia(_ id: UUID, sourceText: String?) throws
+ func markInspirationImportFailed(_ id: UUID, message: String, clearJob: Bool) throws
// Tombstones
func tombstones() throws -> [SyncTombstone]
@@ -550,9 +553,12 @@ public final class SyncEngine {
// MARK: Inspiration
/// Saved links, both halves of the round trip: a link the cloud has not
- /// been told about is queued, and a link it is fetching is followed until
- /// the media can come down onto this phone. A card shows the spinner from
- /// the moment the link is saved and the video itself the moment it lands.
+ /// been told about is handed over, and a link it is fetching is followed
+ /// until the media lands on the account's shelf. The media stays there —
+ /// the card streams it — so only the poster comes down.
+ ///
+ /// Every attempt that fails writes why on the item, so a card can never
+ /// spin on a request that is no longer being made.
private func syncInspirationLinks() async {
var landed = false
for item in ideas?.inspiration ?? [] {
@@ -564,9 +570,12 @@ public final class SyncEngine {
try? journal.markInspirationLinkSynced(item.id, jobId: jobId)
landed = true
} catch CloudSyncError.unauthorized {
+ note(item.id, "Sign in again to fetch this link.", &landed)
return
+ } catch CloudSyncError.refused(let message) {
+ note(item.id, message, &landed)
} catch {
- // Transient or capped for the day; the next kick retries.
+ note(item.id, "Couldn't reach the cloud. Trying again.", &landed)
}
continue
}
@@ -575,32 +584,51 @@ public final class SyncEngine {
switch try await service.importedLink(jobId: job) {
case .running:
continue
- case .failed:
- try? journal.markInspirationImportFailed(item.id)
+ case .noMedia(let text):
+ try? journal.markInspirationNoMedia(item.id, sourceText: text)
landed = true
- case .done(let imported):
- let destination = journal.fetchedMediaDestination(item.id, isVideo: imported.isVideo)
- try await service.downloadLibraryMedia(
- fileName: imported.fileName,
- to: destination
- )
+ case .failed(let message):
+ try? journal.markInspirationImportFailed(item.id, message: message, clearJob: true)
+ landed = true
+ case .ready(let imported):
try? journal.markInspirationFetched(
item.id,
- fileName: destination.lastPathComponent,
+ fileName: imported.fileName,
isVideo: imported.isVideo,
+ posterFileName: await poster(imported, for: item.id),
+ sourceText: imported.text,
remoteAssetId: imported.assetId
)
landed = true
}
} catch CloudSyncError.unauthorized {
+ note(item.id, "Sign in again to fetch this link.", &landed)
return
} catch {
- // Transient; the next pass asks after the job again.
+ note(item.id, "Couldn't reach the cloud. Trying again.", &landed)
}
}
if landed { ideas?.reloadFromStore() }
}
+ /// Say on the card why the last attempt failed. The link keeps its place
+ /// in the queue, so the next pass tries again on its own.
+ private func note(_ id: UUID, _ message: String, _ landed: inout Bool) {
+ try? journal.markInspirationImportFailed(id, message: message, clearJob: false)
+ landed = true
+ }
+
+ /// The source's cover, brought down beside the item so its card paints
+ /// without the network. The media itself stays in the cloud.
+ private func poster(_ imported: ImportedLink, for id: UUID) async -> String? {
+ guard let posterFile = imported.posterFile else { return nil }
+ let destination = journal.posterDestination(id)
+ guard (try? await service.downloadLibraryMedia(fileName: posterFile, to: destination)) != nil else {
+ return nil
+ }
+ return destination.lastPathComponent
+ }
+
private func uploadPendingInspirationMedia() async {
for item in ideas?.inspiration ?? [] {
guard case .media(let fileName, let isVideo) = item.kind else { continue }
diff --git a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift
index 975b31d1..a2c63b84 100644
--- a/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift
+++ b/apps/ios/DonkeyKit/Sources/DonkeyKitUI/IdeasScreen.swift
@@ -483,13 +483,13 @@ struct InspirationCard: View {
var ideas: IdeasModel
var body: some View {
- // A saved link becomes its media: the cloud fetches the source and the
- // phone brings the video or photo down, so the card plays what the
- // link pointed at. Until it lands the card holds the source's name.
- if let media = item.media {
- let url = ideas.mediaURL(fileName: media.fileName)
+ // A photo-library import plays off this phone. A saved link's media is
+ // the cloud's: the worker fetched it into the account's library — the
+ // same shelf the web shows — and the card streams it from there.
+ if let local = item.localMedia {
+ let url = ideas.mediaURL(fileName: local.fileName)
MediaTile(ratio: 3 / 4) {
- if media.isVideo {
+ if local.isVideo {
VideoPlayer(player: AVPlayer(url: url))
} else if let image = UIImage(contentsOfFile: url.localPath) {
Image(uiImage: image)
@@ -497,46 +497,115 @@ struct InspirationCard: View {
.scaledToFill()
}
}
+ } else if let cloud = item.cloud {
+ CloudMediaCard(media: cloud, text: item.sourceText, ideas: ideas)
} else if let link = item.link {
- LinkCard(url: link, state: item.importState)
+ LinkCard(url: link, state: item.importState, text: item.sourceText) {
+ ideas.retryInspiration(id: item.id)
+ }
+ }
+ }
+}
+
+/// A link's media, streamed from the account. The poster came down with the
+/// fetch, so the card paints before the stream resolves and still paints with
+/// no network at all.
+struct CloudMediaCard: View {
+ let media: InspirationCloudMedia
+ let text: String?
+ var ideas: IdeasModel
+
+ @State private var player: AVPlayer?
+ @State private var streamed: URL?
+
+ var body: some View {
+ VStack(alignment: .leading, spacing: 6) {
+ MediaTile(ratio: 3 / 4) {
+ if media.isVideo, let player {
+ VideoPlayer(player: player)
+ } else if let poster {
+ Image(uiImage: poster)
+ .resizable()
+ .scaledToFill()
+ } else if let streamed, !media.isVideo {
+ AsyncImage(url: streamed) { image in
+ image.resizable().scaledToFill()
+ } placeholder: {
+ ProgressView()
+ }
+ } else {
+ ProgressView()
+ }
+ }
+ if let text, !text.isEmpty {
+ Text(text)
+ .font(.caption)
+ .foregroundStyle(.secondary)
+ .lineLimit(3)
+ }
}
+ .task(id: media.fileName) {
+ guard streamed == nil else { return }
+ guard let url = await ideas.streamURL(for: media) else { return }
+ streamed = url
+ if media.isVideo { player = AVPlayer(url: url) }
+ }
+ }
+
+ private var poster: UIImage? {
+ guard let name = media.posterFileName else { return nil }
+ return UIImage(contentsOfFile: ideas.mediaURL(fileName: name).localPath)
}
}
-/// A saved link with no media yet: fetching, or a source that had none.
+/// A saved link with no media of its own: on its way, a source that was only
+/// words, or an attempt that failed and says why.
struct LinkCard: View {
let url: URL
let state: InspirationImport
+ let text: String?
+ let onRetry: () -> Void
var body: some View {
let host = url.host()?.replacingOccurrences(of: "www.", with: "") ?? "link"
- Link(destination: url) {
- VStack(alignment: .leading, spacing: 8) {
+ VStack(alignment: .leading, spacing: 8) {
+ Link(destination: url) {
Label(host, systemImage: "link")
.font(.footnote.weight(.bold))
.lineLimit(1)
- if state == .failed {
- Text(url.absoluteString)
+ }
+ .buttonStyle(.plain)
+
+ switch state {
+ case .queued, .fetching, .ready:
+ HStack(spacing: 8) {
+ ProgressView()
+ Text(state == .queued ? "Waiting to fetch…" : "Fetching…")
.font(.caption)
.foregroundStyle(.secondary)
- .lineLimit(4)
- .multilineTextAlignment(.leading)
- } else {
- HStack(spacing: 8) {
- ProgressView()
- Text("Fetching…")
- .font(.caption)
- .foregroundStyle(.secondary)
- }
}
- Spacer(minLength: 0)
+ case .noMedia:
+ Text(text ?? url.absoluteString)
+ .font(.caption)
+ .foregroundStyle(.secondary)
+ .lineLimit(6)
+ .multilineTextAlignment(.leading)
+ case .failed(let message):
+ Text(message)
+ .font(.caption)
+ .foregroundStyle(.secondary)
+ .lineLimit(4)
+ .multilineTextAlignment(.leading)
+ Button("Try again", action: onRetry)
+ .font(.caption.weight(.semibold))
+ .buttonStyle(.borderless)
}
- .padding(14)
- .frame(maxWidth: .infinity, minHeight: 120, alignment: .topLeading)
- .background(.fill.tertiary)
- .clipShape(RoundedRectangle(cornerRadius: 16))
+ Spacer(minLength: 0)
}
- .buttonStyle(.plain)
+ .padding(14)
+ .frame(maxWidth: .infinity, minHeight: 120, alignment: .topLeading)
+ .background(.fill.tertiary)
+ .clipShape(RoundedRectangle(cornerRadius: 16))
}
}
diff --git a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
index 5287e655..11bc13f5 100644
--- a/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
+++ b/apps/ios/DonkeyKit/Tests/DonkeyKitModelsTests/DonkeyKitModelsTests.swift
@@ -145,8 +145,14 @@ import Testing
/// predates folders answers without one.
var reportsFolders = true
/// What an import job answers with once it is asked about.
- var imported: JobOutcome<ImportedLink> = .done(
- ImportedLink(assetId: "asset-link", fileName: "source.mp4", isVideo: true)
+ var imported: LinkImport = .ready(
+ ImportedLink(
+ assetId: "asset-link",
+ fileName: "source.mp4",
+ isVideo: true,
+ posterFile: "source.mp4.poster.jpg",
+ text: "What the source said"
+ )
)
var usage = StorageUsage(bytes: 0, quotaBytes: 1_000_000)
var uploadResult: Result<RemoteAsset, CloudSyncError> =
@@ -176,14 +182,19 @@ import Testing
RemoteLibrary(assetIds: libraryAssets, deletedIds: deletedLibraryIds)
}
+ /// Set to make the queue call fail the way a phone with no route out
+ /// does.
+ var importFailure: CloudSyncError?
+
func importInspirationLink(_ url: URL) async throws -> String {
+ if let importFailure { throw importFailure }
links.append(url)
return "job-\(links.count)"
}
- func importedLink(jobId: String) async throws -> JobOutcome<ImportedLink> {
+ func importedLink(jobId: String) async throws -> LinkImport {
polled.append(jobId)
- if case .done(let link) = imported { libraryAssets.insert(link.assetId) }
+ if case .ready(let link) = imported { libraryAssets.insert(link.assetId) }
return imported
}
@@ -421,35 +432,77 @@ import Testing
#expect(rig.ideas.notes.isEmpty)
}
- @Test func inspirationLinkImportsOnceAndLandsItsMedia() async throws {
+ @Test func inspirationLinkImportsOnceAndStreamsFromTheCloud() async throws {
let rig = try makeRig()
#expect(rig.ideas.addInspiration(urlText: "youtube.com/watch?v=9"))
await rig.engine.run()
#expect(rig.cloud.links.map(\.host) == ["youtube.com"])
// The queue pass only hands the link over; the next pass follows the
- // job and brings the media down.
+ // job to what the cloud landed.
await rig.engine.run()
#expect(rig.cloud.links.count == 1)
- #expect(rig.cloud.downloads == ["source.mp4"])
+ // Only the poster comes down: the media itself stays on the shelf.
+ #expect(rig.cloud.downloads == ["source.mp4.poster.jpg"])
let item = try #require(rig.ideas.inspiration.first)
- #expect(item.media?.isVideo == true)
- // Nothing is asked of the job again once its media is here.
+ #expect(item.cloud?.fileName == "source.mp4")
+ #expect(item.cloud?.isVideo == true)
+ #expect(item.cloud?.posterFileName != nil)
+ #expect(item.sourceText == "What the source said")
+ #expect(item.importState == .ready)
+ // Nothing is asked of the job again once the media is on the shelf.
await rig.engine.run()
#expect(rig.cloud.downloads.count == 1)
}
- @Test func inspirationLinkWithNoMediaStaysALink() async throws {
+ @Test func inspirationLinkWithNoMediaKeepsWhatTheSourceSaid() async throws {
let rig = try makeRig()
- rig.cloud.imported = .failed
+ rig.cloud.imported = .noMedia(text: "An article, all words")
#expect(rig.ideas.addInspiration(urlText: "example.com/article"))
await rig.engine.run()
await rig.engine.run()
let item = try #require(rig.ideas.inspiration.first)
- #expect(item.media == nil)
- #expect(item.importState == .failed)
+ #expect(item.cloud == nil)
+ #expect(item.importState == .noMedia)
+ #expect(item.sourceText == "An article, all words")
#expect(rig.cloud.downloads.isEmpty)
}
+ @Test func inspirationLinkSaysWhyItFailedAndRetriesOnAsk() async throws {
+ let rig = try makeRig()
+ rig.cloud.imported = .failed("yt-dlp came back empty.")
+ #expect(rig.ideas.addInspiration(urlText: "instagram.com/reel/9"))
+ await rig.engine.run()
+ await rig.engine.run()
+ let item = try #require(rig.ideas.inspiration.first)
+ #expect(item.importState == .failed("yt-dlp came back empty."))
+ // A failed fetch is not asked after again on its own.
+ await rig.engine.run()
+ #expect(rig.cloud.links.count == 1)
+ // The card's retry queues a fresh job.
+ rig.cloud.imported = .ready(
+ ImportedLink(assetId: "asset-link", fileName: "reel.mp4", isVideo: true)
+ )
+ rig.ideas.retryInspiration(id: item.id)
+ await rig.engine.run()
+ await rig.engine.run()
+ #expect(rig.cloud.links.count == 2)
+ #expect(try #require(rig.ideas.inspiration.first).cloud?.fileName == "reel.mp4")
+ }
+
+ @Test func inspirationLinkTheCloudNeverTookSaysSo() async throws {
+ let rig = try makeRig()
+ rig.cloud.importFailure = CloudSyncError.transport
+ #expect(rig.ideas.addInspiration(urlText: "instagram.com/reel/7"))
+ await rig.engine.run()
+ let item = try #require(rig.ideas.inspiration.first)
+ #expect(item.importState == .failed("Couldn't reach the cloud. Trying again."))
+ // It keeps its place in the queue: the next pass hands it over.
+ rig.cloud.importFailure = nil
+ await rig.engine.run()
+ #expect(rig.cloud.links.count == 1)
+ #expect(try #require(rig.ideas.inspiration.first).importState == .fetching)
+ }
+
@Test func folderPushesAheadOfTheNoteFiledInIt() async throws {
let rig = try makeRig()
let folder = try #require(rig.ideas.addFolder(named: "Scripts"))
diff --git a/docs/guides/ios-app.md b/docs/guides/ios-app.md
index 370b0148..04765ba9 100644
--- a/docs/guides/ios-app.md
+++ b/docs/guides/ios-app.md
@@ -24,10 +24,16 @@ URLs; an interrupted upload re-presigns under the name it already claimed, and
a retried completion lands on the asset it already made. Recordings arrive in
the desktop library marked as camera clips (the Camera Roll tab), inspiration
media lands in the Inspiration folder, and a saved inspiration link queues a
-cloud-side import: the worker fetches the source, the phone follows the job,
-and the media comes back down onto the phone, so the card that was a link
-plays the video the link pointed at. A source that turns out to be only words
-stays a link. Notes merge both ways by last-writer-wins on the edit stamp,
+cloud-side import: the worker fetches the source into the account's Inspiration
+folder, where the web Library shows it, and the phone streams it from there —
+only the poster comes down, so a card paints with no network and the video
+itself never takes up room on the phone. A source that turns out to be only
+words stays a link and keeps what it said.
+
+A card always says where its link stands: waiting to be handed over, fetching,
+or failed with the reason and a way to try again. Every attempt that fails
+writes that reason on the item, so a card can never spin on a request nothing
+is making any more. Notes merge both ways by last-writer-wins on the edit stamp,
with tombstones so a delete made offline on either side still lands on the
other. The folders notes file into travel with them, under ids whichever
device made them chose; a folder carries no tombstone, so a folder the cloud