Skip to content

Commit

Permalink
Aggregation is basically working
Browse files Browse the repository at this point in the history
  • Loading branch information
livid committed Sep 9, 2023
1 parent 432addf commit 2c63bc1
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
26 changes: 18 additions & 8 deletions Planet/Entities/MyArticleModel+Save.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ extension MyArticleModel {

// MARK: - Save to Public/:planet_id/:article_id/index.html

// TODO: Fix a potential crash here
func obtainCoverImageCID() -> String? {
if let coverImageURL = getAttachmentURL(name: "_cover.png") {
if FileManager.default.fileExists(atPath: coverImageURL.path) {
do {
let coverImageCID = try IPFSDaemon.shared.getFileCIDv0(url: coverImageURL)
return coverImageCID
} catch {
return nil
}
}
}
return nil
}

/// Save the article into UUID/index.html along with its attachments.
func savePublic() throws {
let started: Date = Date()
Expand All @@ -59,21 +74,16 @@ extension MyArticleModel {
// Save cover image
//TODO: Clean up the logic here
// MARK: Cover Image

let coverImageText = self.getCoverImageText()

saveCoverImage(
with: coverImageText,
filename: publicCoverImagePath.path,
imageSize: NSSize(width: 512, height: 512)
)
let coverImageCID: String? = {
if let coverImageURL = getAttachmentURL(name: "_cover.png"),
let coverImageCID = try? IPFSDaemon.shared.getFileCIDv0(url: coverImageURL)
{
return coverImageCID
}
return nil
}()
var coverImageCID: String? = obtainCoverImageCID()

if let attachments = self.attachments, attachments.count == 0 {
if self.planet.templateName == "Croptop" {
// _cover.png CID is only needed by Croptop now
Expand Down
6 changes: 5 additions & 1 deletion Planet/Entities/MyPlanetModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,7 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl
audioFilename: article.audioFilename,
attachments: article.attachments
)
newArticle.tags = article.tags
newArticle.planet = self
try newArticle.save()
let publicBasePath = newArticle.publicBasePath
Expand Down Expand Up @@ -1681,10 +1682,11 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl
}
}
Task {
try? newArticle.savePublic()
try? await newArticle.savePublic()
}
DispatchQueue.main.async {
self.articles.append(newArticle)
PlanetStore.shared.refreshSelectedArticles()
}
} else {
debugPrint("Aggregation: Skipping \(article.id), already saved")
Expand All @@ -1696,8 +1698,10 @@ class MyPlanetModel: Equatable, Hashable, Identifiable, ObservableObject, Codabl
}
}
}
self.tags = self.consolidateTags()
try? save()
try? await savePublic()
try? await publish()
Task { @MainActor in
PlanetStore.shared.refreshSelectedArticles()
}
Expand Down
11 changes: 10 additions & 1 deletion Planet/Entities/PlanetStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ enum PlanetDetailViewType: Hashable, Equatable {
@Published var isMigrating = false
@Published var isRebuilding = false
@Published var rebuildTasks: Int = 0
@Published var isQuickSharing = false // use in macOS 12 only.
@Published var isQuickSharing = false // use in macOS 12 only.

@Published var isAggregating: Bool = false // at any time, only one aggregation task is allowed.

@Published var isShowingWalletConnectV1QRCode: Bool = false
@Published var isShowingWalletAccount: Bool = false
Expand Down Expand Up @@ -337,6 +339,13 @@ enum PlanetDetailViewType: Hashable, Equatable {
}

func aggregate() async {
guard isAggregating == false else {
return
}
defer {
isAggregating = false
}
isAggregating = true
Task {
await withTaskGroup(of: Void.self) { taskGroup in
for myPlanet in myPlanets {
Expand Down
2 changes: 1 addition & 1 deletion Planet/versioning.xcconfig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CURRENT_PROJECT_VERSION = 1625
CURRENT_PROJECT_VERSION = 1626
2 changes: 1 addition & 1 deletion PlanetLite/AppContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct AppContentView: View {

let dropDelegate: AppContentDropDelegate

let timer = Timer.publish(every: 30, on: .current, in: .common).autoconnect()
let timer = Timer.publish(every: 300, on: .current, in: .common).autoconnect()

init() {
_planetStore = StateObject(wrappedValue: PlanetStore.shared)
Expand Down
3 changes: 1 addition & 2 deletions PlanetLite/Croptop/AggregationSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,8 @@ struct AggregationSettings: View {
Task {
try planet.save()
Task(priority: .background) {
try await planet.rebuild()
try await planet.aggregate()
}
NotificationCenter.default.post(name: .loadArticle, object: nil)
}
dismiss()
} label: {
Expand Down

0 comments on commit 2c63bc1

Please sign in to comment.