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
|
2018-01-14 19:56:49 +01:00
|
|
|
import RSCore
|
2018-01-09 06:53:49 +01:00
|
|
|
|
|
|
|
// Not undoable.
|
|
|
|
|
2018-01-09 07:10:56 +01:00
|
|
|
final class SendToMicroBlogCommand: SendToCommand {
|
|
|
|
|
2018-01-14 21:13:25 +01:00
|
|
|
let title = "Micro.blog"
|
2018-01-09 07:10:56 +01:00
|
|
|
|
2018-01-12 07:18:46 +01:00
|
|
|
var image: NSImage? {
|
|
|
|
return microBlogApp.icon
|
2018-01-09 07:10:56 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-01-10 07:04:45 +01:00
|
|
|
func sendObject(_ object: Any?, selectedText: String?) {
|
|
|
|
|
|
|
|
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-14 19:56:49 +01:00
|
|
|
guard microBlogApp.launchIfNeeded(), microBlogApp.bringToFront() else {
|
2018-01-10 07:04:45 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: get text from contentHTML or contentText if no title and no selectedText.
|
2018-01-14 21:00:09 +01:00
|
|
|
// TODO: consider selectedText.
|
|
|
|
|
|
|
|
let s = article.attributionString + article.linkString
|
2018-01-10 07:04:45 +01:00
|
|
|
|
2018-01-14 18:36:24 +01:00
|
|
|
let urlQueryDictionary = ["text": s]
|
|
|
|
guard let urlQueryString = urlQueryDictionary.urlQueryString() else {
|
2018-01-10 07:04:45 +01:00
|
|
|
return
|
|
|
|
}
|
2018-01-14 18:36:24 +01:00
|
|
|
guard let url = URL(string: "microblog://post?" + urlQueryString) else {
|
2018-01-10 07:04:45 +01:00
|
|
|
return
|
|
|
|
}
|
2018-01-09 06:53:49 +01:00
|
|
|
|
2018-01-10 07:04:45 +01:00
|
|
|
let _ = try? NSWorkspace.shared.open(url, options: [], configuration: [:])
|
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 {
|
|
|
|
|
|
|
|
var attributionString: String {
|
|
|
|
|
|
|
|
// Feed name, or feed name + author name (if author is specified per-article).
|
|
|
|
// Includes trailing space.
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
if let feedName = webFeed?.nameForDisplay, let authorName = authors?.first?.name {
|
2018-01-14 21:00:09 +01:00
|
|
|
return feedName + ", " + authorName + ": "
|
|
|
|
}
|
2019-11-15 03:11:41 +01:00
|
|
|
if let feedName = webFeed?.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 ""
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|