Files
NetNewsWire/Shared/ExtensionPoints/SendToMicroBlogCommand.swift
T

92 lines
2.2 KiB
Swift
Raw Normal View History

//
// SendToMicroBlogCommand.swift
2018-08-28 22:18:24 -07:00
// NetNewsWire
//
// Created by Brent Simmons on 1/8/18.
// Copyright © 2018 Ranchero Software. All rights reserved.
//
import AppKit
import Articles
import RSCore
// Not undoable.
2025-11-13 17:11:34 -08:00
@MainActor final class SendToMicroBlogCommand: SendToCommand {
2023-06-25 16:48:37 -07:00
let title = "Micro.blog"
2025-11-19 10:38:18 -08:00
let image: RSImage? = Assets.Images.microblog
private let microBlogApp = UserApp(bundleID: "blog.micro.mac")
func canSendObject(_ object: Any?, selectedText: String?) -> Bool {
microBlogApp.updateStatus()
2026-01-01 16:53:37 -08:00
guard microBlogApp.existsOnDisk, let article = (object as? ArticlePasteboardWriter)?.article, article.preferredLink != nil else {
return false
}
return true
}
2025-11-05 11:22:10 -08:00
func sendObject(_ object: Any?, selectedText: String?) {
guard canSendObject(object, selectedText: selectedText) else {
return
}
guard let article = (object as? ArticlePasteboardWriter)?.article else {
return
}
Task {
guard await microBlogApp.launchIfNeeded(), microBlogApp.bringToFront() else {
return
}
// TODO: get text from contentHTML or contentText if no title and no selectedText.
// The selection, when there is one, is quoted below the link.
var s = article.attributionString + article.linkString
if let selectedText {
s += "\n\n> " + selectedText
}
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)
}
}
}
2025-11-13 17:11:34 -08:00
@MainActor private extension Article {
2018-01-14 12:00:09 -08:00
var attributionString: String {
// Feed name, or feed name + author name (if author is specified per-article).
// Includes trailing space.
2025-11-02 15:12:00 -08:00
if let feedName = feed?.nameForDisplay, let authorName = authors?.first?.name {
2018-01-14 12:00:09 -08:00
return feedName + ", " + authorName + ": "
}
2025-11-02 15:12:00 -08:00
if let feedName = feed?.nameForDisplay {
2018-01-14 12:00:09 -08:00
return feedName + ": "
}
return ""
}
var linkString: String {
// Title + link or just title (if no link) or just link if no title
2026-01-01 16:53:37 -08:00
if let title, let link = preferredLink {
2018-01-14 12:00:09 -08:00
return "[" + title + "](" + link + ")"
}
2026-01-01 16:53:37 -08:00
if let preferredLink {
2018-01-14 12:00:09 -08:00
return preferredLink
}
2026-01-01 16:53:37 -08:00
if let title {
2018-01-14 12:00:09 -08:00
return title
}
return ""
}
}