2018-01-09 06:53:49 +01:00
|
|
|
//
|
|
|
|
// SendToMicroBlogCommand.swift
|
2018-08-29 07:18:24 +02:00
|
|
|
// NetNewsWire
|
2018-01-09 06:53:49 +01:00
|
|
|
//
|
|
|
|
// Created by Brent Simmons on 1/8/18.
|
|
|
|
// Copyright © 2018 Ranchero Software. All rights reserved.
|
|
|
|
//
|
|
|
|
|
2018-02-03 07:51:32 +01:00
|
|
|
import AppKit
|
2018-07-24 03:29:08 +02:00
|
|
|
import Articles
|
2024-03-21 04:49:15 +01:00
|
|
|
import Core
|
|
|
|
import AppKitExtras
|
2018-01-09 06:53:49 +01:00
|
|
|
|
|
|
|
// Not undoable.
|
|
|
|
|
2023-06-26 01:48:37 +02:00
|
|
|
final class SendToMicroBlogCommand: SendToCommand {
|
2018-01-09 07:10:56 +01:00
|
|
|
|
2023-06-26 01:48:37 +02:00
|
|
|
let title = "Micro.blog"
|
|
|
|
let image: RSImage? = AppAssets.microblogIcon
|
2018-01-09 06:53:49 +01:00
|
|
|
|
2018-01-14 19:56:49 +01:00
|
|
|
private let microBlogApp = UserApp(bundleID: "blog.micro.mac")
|
2018-01-12 07:18:46 +01:00
|
|
|
|
2018-01-10 07:04:45 +01:00
|
|
|
func canSendObject(_ object: Any?, selectedText: String?) -> Bool {
|
2018-01-09 06:53:49 +01:00
|
|
|
|
2018-01-14 19:56:49 +01:00
|
|
|
microBlogApp.updateStatus()
|
2018-01-12 07:18:46 +01:00
|
|
|
guard microBlogApp.existsOnDisk, let article = (object as? ArticlePasteboardWriter)?.article, let _ = article.preferredLink else {
|
2018-01-10 07:04:45 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2018-01-09 06:53:49 +01:00
|
|
|
}
|
|
|
|
|
2024-03-20 07:05:30 +01:00
|
|
|
@MainActor func sendObject(_ object: Any?, selectedText: String?) {
|
2018-01-10 07:04:45 +01:00
|
|
|
|
|
|
|
guard canSendObject(object, selectedText: selectedText) else {
|
|
|
|
return
|
|
|
|
}
|
2018-01-12 07:18:46 +01:00
|
|
|
guard let article = (object as? ArticlePasteboardWriter)?.article else {
|
|
|
|
return
|
|
|
|
}
|
2018-01-10 07:04:45 +01:00
|
|
|
|
2024-04-23 02:25:56 +02:00
|
|
|
Task { @MainActor in
|
|
|
|
guard await microBlogApp.launchIfNeeded(), microBlogApp.bringToFront() else {
|
|
|
|
return
|
|
|
|
}
|
2018-01-14 21:00:09 +01:00
|
|
|
|
2024-04-23 02:25:56 +02:00
|
|
|
// TODO: get text from contentHTML or contentText if no title and no selectedText.
|
|
|
|
// TODO: consider selectedText.
|
2018-01-10 07:04:45 +01:00
|
|
|
|
2024-04-23 02:25:56 +02:00
|
|
|
let s = article.attributionString + article.linkString
|
2018-01-09 06:53:49 +01:00
|
|
|
|
2024-04-23 02:25:56 +02:00
|
|
|
let urlQueryDictionary = ["text": s]
|
|
|
|
guard let urlQueryString = urlQueryDictionary.urlQueryString else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
guard let url = URL(string: "microblog://post?" + urlQueryString) else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
NSWorkspace.shared.open(url)
|
|
|
|
}
|
2018-01-09 06:53:49 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 07:10:56 +01:00
|
|
|
|
2018-01-14 21:00:09 +01:00
|
|
|
private extension Article {
|
|
|
|
|
2024-03-20 07:05:30 +01:00
|
|
|
@MainActor var attributionString: String {
|
2018-01-14 21:00:09 +01:00
|
|
|
|
|
|
|
// Feed name, or feed name + author name (if author is specified per-article).
|
|
|
|
// Includes trailing space.
|
|
|
|
|
2024-02-26 08:12:21 +01:00
|
|
|
if let feedName = feed?.nameForDisplay, let authorName = authors?.first?.name {
|
2018-01-14 21:00:09 +01:00
|
|
|
return feedName + ", " + authorName + ": "
|
|
|
|
}
|
2024-02-26 08:12:21 +01:00
|
|
|
if let feedName = feed?.nameForDisplay {
|
2018-01-14 21:00:09 +01:00
|
|
|
return feedName + ": "
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
var linkString: String {
|
|
|
|
|
|
|
|
// Title + link or just title (if no link) or just link if no title
|
2018-01-09 07:10:56 +01:00
|
|
|
|
2018-01-14 21:00:09 +01:00
|
|
|
if let title = title, let link = preferredLink {
|
|
|
|
return "[" + title + "](" + link + ")"
|
|
|
|
}
|
|
|
|
if let preferredLink = preferredLink {
|
|
|
|
return preferredLink
|
|
|
|
}
|
|
|
|
if let title = title {
|
|
|
|
return title
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|