2018-01-11 00:51:25 -08:00
|
|
|
|
//
|
|
|
|
|
|
// Feed+Scriptability.swift
|
2018-08-28 22:18:24 -07:00
|
|
|
|
// NetNewsWire
|
2018-01-11 00:51:25 -08:00
|
|
|
|
//
|
|
|
|
|
|
// Created by Olof Hellman on 1/10/18.
|
|
|
|
|
|
// Copyright © 2018 Olof Hellman. All rights reserved.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2018-02-20 00:26:46 -08:00
|
|
|
|
import RSParser
|
2018-01-11 00:51:25 -08:00
|
|
|
|
import Account
|
2018-07-23 18:29:08 -07:00
|
|
|
|
import Articles
|
2018-01-11 00:51:25 -08:00
|
|
|
|
|
2025-11-03 09:32:50 -08:00
|
|
|
|
@objc(ScriptableFeed)
|
2025-11-18 15:29:29 -08:00
|
|
|
|
@MainActor final class ScriptableFeed: NSObject, UniqueIDScriptingObject, @preconcurrency ScriptingObjectContainer {
|
2026-01-01 17:55:12 -08:00
|
|
|
|
let feed: Feed
|
|
|
|
|
|
nonisolated(unsafe) let container: ScriptingObjectContainer
|
2018-01-11 00:51:25 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
init (_ feed: Feed, container: ScriptingObjectContainer) {
|
|
|
|
|
|
self.feed = feed
|
|
|
|
|
|
self.container = container
|
|
|
|
|
|
}
|
2018-01-11 00:51:25 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
@objc(objectSpecifier)
|
|
|
|
|
|
nonisolated override var objectSpecifier: NSScriptObjectSpecifier? {
|
|
|
|
|
|
let scriptObjectSpecifier = self.container.makeFormUniqueIDScriptObjectSpecifier(forObject: self)
|
|
|
|
|
|
return scriptObjectSpecifier
|
|
|
|
|
|
}
|
2018-01-11 00:51:25 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
@objc(scriptingSpecifierDescriptor)
|
|
|
|
|
|
func scriptingSpecifierDescriptor() -> NSScriptObjectSpecifier {
|
|
|
|
|
|
objectSpecifier ?? NSScriptObjectSpecifier()
|
|
|
|
|
|
}
|
2018-02-20 00:26:46 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
// MARK: - ScriptingObject protocol
|
2018-01-11 00:51:25 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
nonisolated var scriptingKey: String {
|
|
|
|
|
|
"feeds"
|
|
|
|
|
|
}
|
2018-01-11 00:51:25 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
// MARK: - UniqueIdScriptingObject protocol
|
2018-01-11 00:51:25 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
// I am not sure if account should prefer to be specified by name or by ID
|
|
|
|
|
|
// but in either case it seems like the accountID would be used as the keydata, so I chose ID
|
|
|
|
|
|
@objc(uniqueId)
|
|
|
|
|
|
nonisolated var scriptingUniqueID: Any {
|
|
|
|
|
|
feed.feedID
|
|
|
|
|
|
}
|
2018-01-19 22:31:17 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
// MARK: - ScriptingObjectContainer protocol
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
nonisolated var scriptingClassDescription: NSScriptClassDescription {
|
|
|
|
|
|
classDescription as! NSScriptClassDescription
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
func deleteElement(_ element: ScriptingObject) {
|
|
|
|
|
|
}
|
2018-02-20 00:26:46 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
// MARK: - Handle NSCreateCommand
|
2018-02-20 00:26:46 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
static func urlForNewFeed(arguments: [String: Any]) -> String? {
|
|
|
|
|
|
var url: String?
|
|
|
|
|
|
if let withDataParam = arguments["ObjectData"] {
|
|
|
|
|
|
if let objectDataDescriptor = withDataParam as? NSAppleEventDescriptor {
|
|
|
|
|
|
url = objectDataDescriptor.stringValue
|
|
|
|
|
|
}
|
|
|
|
|
|
} else if let withPropsParam = arguments["ObjectProperties"] as? [String: Any] {
|
|
|
|
|
|
url = withPropsParam["url"] as? String
|
|
|
|
|
|
}
|
|
|
|
|
|
return url
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
static func scriptableFeed(_ feed: Feed, account: Account, folder: Folder?) -> ScriptableFeed {
|
|
|
|
|
|
let scriptableAccount = ScriptableAccount(account)
|
|
|
|
|
|
if let folder = folder {
|
|
|
|
|
|
let scriptableFolder = ScriptableFolder(folder, container: scriptableAccount)
|
|
|
|
|
|
return ScriptableFeed(feed, container: scriptableFolder)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return ScriptableFeed(feed, container: scriptableAccount)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
static func scriptableFeed(for feed: Feed) -> ScriptableFeed? {
|
2026-01-01 17:25:02 -08:00
|
|
|
|
guard let account = feed.account else {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
// Find the proper container hierarchy
|
|
|
|
|
|
let containers = account.existingContainers(withFeed: feed)
|
|
|
|
|
|
var folder: Folder?
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
// Check if feed is in a folder
|
|
|
|
|
|
for container in containers {
|
|
|
|
|
|
if let foundFolder = container as? Folder {
|
|
|
|
|
|
folder = foundFolder
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
return scriptableFeed(feed, account: account, folder: folder)
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
static func handleCreateElement(command: NSCreateCommand) -> Any? {
|
|
|
|
|
|
guard command.isCreateCommand(forClass: "Feed") else {
|
2026-01-01 17:25:02 -08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
guard let arguments = command.arguments else {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
let titleFromArgs = command.property(forKey: "name") as? String
|
|
|
|
|
|
let (account, folder) = command.accountAndFolderForNewChild()
|
|
|
|
|
|
guard let url = self.urlForNewFeed(arguments: arguments) else {
|
2026-01-01 17:25:02 -08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
if let existingFeed = account.existingFeed(withURL: url) {
|
|
|
|
|
|
return scriptableFeed(existingFeed, account: account, folder: folder).objectSpecifier
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2019-05-28 09:45:02 -05:00
|
|
|
|
let container: Container = folder != nil ? folder! : account
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
// We need to download the feed and parse it.
|
|
|
|
|
|
// RSParser does the callback for the download on the main thread.
|
|
|
|
|
|
// Because we can't wait here (on the main thread) for the callback, we have to return from this function.
|
|
|
|
|
|
// Generally, returning from an AppleEvent handler function means that handling the Apple event is over,
|
|
|
|
|
|
// but we don’t yet have the result of the event yet, so we prevent the Apple event from returning by calling
|
|
|
|
|
|
// suspendExecution(). When we get the callback, we supply the event result and call resumeExecution().
|
|
|
|
|
|
command.suspendExecution()
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2025-11-02 15:40:05 -08:00
|
|
|
|
account.createFeed(url: url, name: titleFromArgs, container: container, validateFeed: true) { result in
|
2019-05-08 17:41:19 -05:00
|
|
|
|
switch result {
|
2019-05-10 10:14:24 -05:00
|
|
|
|
case .success(let feed):
|
2025-11-02 16:17:42 -08:00
|
|
|
|
NotificationCenter.default.post(name: .UserDidAddFeed, object: self, userInfo: [UserInfoKey.feed: feed])
|
2026-01-01 17:25:02 -08:00
|
|
|
|
let scriptableFeed = self.scriptableFeed(feed, account: account, folder: folder)
|
|
|
|
|
|
command.resumeExecution(withResult: scriptableFeed.objectSpecifier)
|
2019-05-08 17:41:19 -05:00
|
|
|
|
case .failure:
|
2026-01-01 17:25:02 -08:00
|
|
|
|
command.resumeExecution(withResult: nil)
|
2019-05-08 17:41:19 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2018-01-19 22:31:17 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
// MARK: - Scriptable properties
|
2018-03-04 18:43:29 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
@objc(url)
|
|
|
|
|
|
var url: String {
|
|
|
|
|
|
feed.url
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
@objc(name)
|
|
|
|
|
|
var name: String {
|
|
|
|
|
|
feed.name ?? ""
|
|
|
|
|
|
}
|
2018-01-11 00:51:25 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
@objc(homePageURL)
|
|
|
|
|
|
var homePageURL: String {
|
2026-01-01 17:25:02 -08:00
|
|
|
|
feed.homePageURL ?? ""
|
2026-01-01 17:55:12 -08:00
|
|
|
|
}
|
2018-01-19 22:31:17 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
@objc(iconURL)
|
|
|
|
|
|
var iconURL: String {
|
|
|
|
|
|
feed.iconURL ?? ""
|
|
|
|
|
|
}
|
2018-01-19 22:31:17 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
@objc(faviconURL)
|
|
|
|
|
|
var faviconURL: String {
|
|
|
|
|
|
feed.faviconURL ?? ""
|
|
|
|
|
|
}
|
2018-01-19 22:31:17 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
@objc(opmlRepresentation)
|
|
|
|
|
|
var opmlRepresentation: String {
|
|
|
|
|
|
feed.OPMLString(indentLevel: 0)
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
// MARK: - Scriptable elements
|
2018-02-08 00:11:52 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
@objc(authors)
|
|
|
|
|
|
var authors: NSArray {
|
|
|
|
|
|
let feedAuthors = feed.authors ?? []
|
|
|
|
|
|
return feedAuthors.map { ScriptableAuthor($0, container: self) } as NSArray
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
@objc(valueInAuthorsWithUniqueID:)
|
|
|
|
|
|
func valueInAuthors(withUniqueID id: String) -> ScriptableAuthor? {
|
2026-01-01 17:25:02 -08:00
|
|
|
|
guard let author = feed.authors?.first(where: {$0.authorID == id}) else {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2026-01-01 17:55:12 -08:00
|
|
|
|
return ScriptableAuthor(author, container: self)
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
@objc(articles)
|
|
|
|
|
|
var articles: NSArray {
|
2026-06-10 16:59:28 -07:00
|
|
|
|
let feedArticles = feed.fetchArticles()
|
2026-01-01 17:55:12 -08:00
|
|
|
|
// the articles are a set, use the sorting algorithm from the viewer
|
|
|
|
|
|
let sortedArticles = feedArticles.sorted(by: {
|
|
|
|
|
|
return $0.logicalDatePublished > $1.logicalDatePublished
|
|
|
|
|
|
})
|
|
|
|
|
|
return sortedArticles.map { ScriptableArticle($0, container: self) } as NSArray
|
|
|
|
|
|
}
|
2025-11-05 11:22:10 -08:00
|
|
|
|
|
2026-01-01 17:55:12 -08:00
|
|
|
|
@objc(valueInArticlesWithUniqueID:)
|
|
|
|
|
|
func valueInArticles(withUniqueID id: String) -> ScriptableArticle? {
|
2026-06-10 16:59:28 -07:00
|
|
|
|
let articles = feed.fetchArticles()
|
2026-01-01 17:25:02 -08:00
|
|
|
|
guard let article = articles.first(where: {$0.uniqueID == id}) else {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2026-01-01 17:55:12 -08:00
|
|
|
|
return ScriptableArticle(article, container: self)
|
|
|
|
|
|
}
|
2018-01-11 00:51:25 -08:00
|
|
|
|
}
|