2018-01-11 09:51:25 +01:00
|
|
|
|
//
|
|
|
|
|
// Feed+Scriptability.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
|
// NetNewsWire
|
2018-01-11 09:51:25 +01:00
|
|
|
|
//
|
|
|
|
|
// Created by Olof Hellman on 1/10/18.
|
|
|
|
|
// Copyright © 2018 Olof Hellman. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2024-04-03 06:43:06 +02:00
|
|
|
|
import Parser
|
2018-01-11 09:51:25 +01:00
|
|
|
|
import Account
|
2018-07-24 03:29:08 +02:00
|
|
|
|
import Articles
|
2018-01-11 09:51:25 +01:00
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
@objc(ScriptableFeed)
|
2024-03-19 05:08:37 +01:00
|
|
|
|
@objcMembers class ScriptableFeed: NSObject, UniqueIdScriptingObject, ScriptingObjectContainer {
|
2018-01-11 09:51:25 +01:00
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
let feed:Feed
|
2018-01-11 09:51:25 +01:00
|
|
|
|
let container:ScriptingObjectContainer
|
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
init (_ feed:Feed, container:ScriptingObjectContainer) {
|
|
|
|
|
self.feed = feed
|
2018-01-11 09:51:25 +01:00
|
|
|
|
self.container = container
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc(objectSpecifier)
|
|
|
|
|
override var objectSpecifier: NSScriptObjectSpecifier? {
|
|
|
|
|
let scriptObjectSpecifier = self.container.makeFormUniqueIDScriptObjectSpecifier(forObject:self)
|
|
|
|
|
return (scriptObjectSpecifier)
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 09:26:46 +01:00
|
|
|
|
@objc(scriptingSpecifierDescriptor)
|
|
|
|
|
func scriptingSpecifierDescriptor() -> NSScriptObjectSpecifier {
|
|
|
|
|
return (self.objectSpecifier ?? NSScriptObjectSpecifier() )
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
|
// MARK: --- ScriptingObject protocol ---
|
|
|
|
|
|
|
|
|
|
var scriptingKey: String {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
return "feeds"
|
2018-01-11 09:51:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: --- UniqueIdScriptingObject protocol ---
|
|
|
|
|
|
|
|
|
|
// 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
|
2018-04-26 06:40:50 +02:00
|
|
|
|
@objc(uniqueId)
|
2024-03-26 05:10:37 +01:00
|
|
|
|
@MainActor var scriptingUniqueId:Any {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
return feed.feedID
|
2018-01-11 09:51:25 +01:00
|
|
|
|
}
|
2018-01-20 07:31:17 +01:00
|
|
|
|
|
|
|
|
|
// MARK: --- ScriptingObjectContainer protocol ---
|
2018-01-11 09:51:25 +01:00
|
|
|
|
|
2018-01-20 07:31:17 +01:00
|
|
|
|
var scriptingClassDescription: NSScriptClassDescription {
|
|
|
|
|
return self.classDescription as! NSScriptClassDescription
|
|
|
|
|
}
|
2018-02-20 09:26:46 +01:00
|
|
|
|
|
2018-03-05 03:43:29 +01:00
|
|
|
|
func deleteElement(_ element:ScriptingObject) {
|
|
|
|
|
}
|
2018-02-20 09:26:46 +01:00
|
|
|
|
|
2018-03-05 03:43:29 +01:00
|
|
|
|
// MARK: --- handle NSCreateCommand ---
|
2018-02-20 09:26:46 +01:00
|
|
|
|
|
|
|
|
|
class 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
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-26 05:10:37 +01:00
|
|
|
|
@MainActor class func scriptableFeed(_ feed:Feed, account:Account, folder:Folder?) -> ScriptableFeed {
|
2018-03-05 03:43:29 +01:00
|
|
|
|
let scriptableAccount = ScriptableAccount(account)
|
|
|
|
|
if let folder = folder {
|
|
|
|
|
let scriptableFolder = ScriptableFolder(folder, container:scriptableAccount)
|
2024-02-26 08:12:21 +01:00
|
|
|
|
return ScriptableFeed(feed, container:scriptableFolder)
|
2018-03-05 03:43:29 +01:00
|
|
|
|
} else {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
return ScriptableFeed(feed, container:scriptableAccount)
|
2018-02-20 09:26:46 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-20 07:05:30 +01:00
|
|
|
|
@MainActor class func handleCreateElement(command:NSCreateCommand) -> Any? {
|
2018-03-05 03:43:29 +01:00
|
|
|
|
guard command.isCreateCommand(forClass:"Feed") else { return nil }
|
2018-02-20 09:26:46 +01:00
|
|
|
|
guard let arguments = command.arguments else {return nil}
|
2018-03-05 03:43:29 +01:00
|
|
|
|
let titleFromArgs = command.property(forKey:"name") as? String
|
|
|
|
|
let (account, folder) = command.accountAndFolderForNewChild()
|
2018-02-20 09:26:46 +01:00
|
|
|
|
guard let url = self.urlForNewFeed(arguments:arguments) else {return nil}
|
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
|
if let existingFeed = account.existingFeed(withURL:url) {
|
2019-08-03 20:04:52 +02:00
|
|
|
|
return scriptableFeed(existingFeed, account:account, folder:folder).objectSpecifier
|
2018-02-20 09:26:46 +01:00
|
|
|
|
}
|
2019-05-28 16:45:02 +02:00
|
|
|
|
|
|
|
|
|
let container: Container = folder != nil ? folder! : account
|
|
|
|
|
|
2019-08-03 20:04:52 +02: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().
|
2018-02-20 09:26:46 +01:00
|
|
|
|
command.suspendExecution()
|
|
|
|
|
|
2024-04-03 05:46:28 +02:00
|
|
|
|
Task { @MainActor in
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
let feed = try await account.createFeed(url: url, name: titleFromArgs, container: container, validateFeed: true)
|
2024-02-26 08:12:21 +01:00
|
|
|
|
NotificationCenter.default.post(name: .UserDidAddFeed, object: self, userInfo: [UserInfoKey.feed: feed])
|
2019-05-28 16:45:02 +02:00
|
|
|
|
let scriptableFeed = self.scriptableFeed(feed, account:account, folder:folder)
|
|
|
|
|
command.resumeExecution(withResult:scriptableFeed.objectSpecifier)
|
2024-04-03 05:46:28 +02:00
|
|
|
|
} catch {
|
2019-05-09 00:41:19 +02:00
|
|
|
|
command.resumeExecution(withResult:nil)
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-03 05:46:28 +02:00
|
|
|
|
|
|
|
|
|
return nil
|
2018-02-20 09:26:46 +01:00
|
|
|
|
}
|
2018-01-20 07:31:17 +01:00
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
|
// MARK: --- Scriptable properties ---
|
2018-03-05 03:43:29 +01:00
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
|
@objc(url)
|
2024-03-26 05:10:37 +01:00
|
|
|
|
@MainActor var url:String {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
return self.feed.url
|
2018-01-11 09:51:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc(name)
|
2024-03-26 05:10:37 +01:00
|
|
|
|
@MainActor var name:String {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
return self.feed.name ?? ""
|
2018-01-11 09:51:25 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-20 07:31:17 +01:00
|
|
|
|
@objc(homePageURL)
|
2024-03-26 05:10:37 +01:00
|
|
|
|
@MainActor var homePageURL:String {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
return self.feed.homePageURL ?? ""
|
2018-01-20 07:31:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc(iconURL)
|
2024-03-26 05:10:37 +01:00
|
|
|
|
@MainActor var iconURL:String {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
return self.feed.iconURL ?? ""
|
2018-01-20 07:31:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc(faviconURL)
|
2024-03-26 05:10:37 +01:00
|
|
|
|
@MainActor var faviconURL:String {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
return self.feed.faviconURL ?? ""
|
2018-01-20 07:31:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-20 07:49:01 +01:00
|
|
|
|
@objc(opmlRepresentation)
|
|
|
|
|
var opmlRepresentation:String {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
return self.feed.OPMLString(indentLevel:0)
|
2018-01-20 07:49:01 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-08 09:11:52 +01:00
|
|
|
|
// MARK: --- scriptable elements ---
|
|
|
|
|
|
2018-01-20 07:31:17 +01:00
|
|
|
|
@objc(authors)
|
2024-03-26 05:10:37 +01:00
|
|
|
|
@MainActor var authors:NSArray {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
let feedAuthors = feed.authors ?? []
|
2018-01-20 07:31:17 +01:00
|
|
|
|
return feedAuthors.map { ScriptableAuthor($0, container:self) } as NSArray
|
|
|
|
|
}
|
2018-02-08 09:11:52 +01:00
|
|
|
|
|
|
|
|
|
@objc(valueInAuthorsWithUniqueID:)
|
2024-03-26 05:10:37 +01:00
|
|
|
|
@MainActor func valueInAuthors(withUniqueID id:String) -> ScriptableAuthor? {
|
2024-02-26 08:12:21 +01:00
|
|
|
|
guard let author = feed.authors?.first(where:{$0.authorID == id}) else { return nil }
|
2018-02-08 09:11:52 +01:00
|
|
|
|
return ScriptableAuthor(author, container:self)
|
|
|
|
|
}
|
2018-01-24 09:06:34 +01:00
|
|
|
|
|
2024-03-19 05:08:37 +01:00
|
|
|
|
// @objc(articles)
|
|
|
|
|
// var articles:NSArray {
|
|
|
|
|
// let feedArticles = (try? feed.fetchArticles()) ?? Set<Article>()
|
|
|
|
|
// // 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
|
|
|
|
|
// }
|
2018-02-08 09:11:52 +01:00
|
|
|
|
|
2024-03-19 05:08:37 +01:00
|
|
|
|
// @objc(valueInArticlesWithUniqueID:)
|
|
|
|
|
// func valueInArticles(withUniqueID id:String) -> ScriptableArticle? {
|
|
|
|
|
// let articles = (try? feed.fetchArticles()) ?? Set<Article>()
|
|
|
|
|
// guard let article = articles.first(where:{$0.uniqueID == id}) else { return nil }
|
|
|
|
|
// return ScriptableArticle(article, container:self)
|
|
|
|
|
// }
|
2018-01-20 07:31:17 +01:00
|
|
|
|
|
2018-01-11 09:51:25 +01:00
|
|
|
|
}
|