Inkspring / app · inkspring.ai / native-mac / Sources / Inkspring / XArticleDrafts.swift
| 1 | import Foundation |
| 2 | import InkspringSchedule |
| 3 | |
| 4 | extension WorkspaceRecord { |
| 5 | var isXArticle: Bool { kind == "article" && (self["platform"] == "x" || self["platform"].isEmpty) } |
| 6 | var isLinkedInWritingArticle: Bool { kind == "article" && self["platform"] == "linkedin" && fields["writingOnly"] as? Bool == true } |
| 7 | } |
| 8 | |
| 9 | extension Workspace { |
| 10 | func linkedArticleDraft(from expected: WorkspaceRecord) throws -> WorkspaceRecord { |
| 11 | try withChanges { |
| 12 | guard var post = records.first(where: { $0.kind == "draft" && $0.id == expected.id }), |
| 13 | post["updatedAt"] == expected["updatedAt"], post["platform"] == "x", |
| 14 | post["format"] != "reply", |
| 15 | !post.body.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, |
| 16 | !["posting", "posted", "uncertain"].contains(post.scheduleStatus) else { |
| 17 | throw WorkspaceError("Save the current X post text before creating an article.") |
| 18 | } |
| 19 | if let article = records.first(where: { |
| 20 | $0.id == post["xArticleID"] && $0.projectID == post.projectID && $0.isXArticle |
| 21 | }) { |
| 22 | return article |
| 23 | } |
| 24 | var article = WorkspaceRecord.new("article", projectID: post.projectID) |
| 25 | article["title"] = post.title |
| 26 | article["body"] = ["followup", "thread"].contains(post["format"]) |
| 27 | ? XThreadText.posts(in: post.body).first ?? post.body : post.body |
| 28 | article["sourceContentID"] = post.referenceID ?? post.id |
| 29 | article = try save(article) |
| 30 | post["xArticleID"] = article.id |
| 31 | try save(post) |
| 32 | return article |
| 33 | } |
| 34 | } |
| 35 | } |