2018-09-23 23:04:39 +00:00
|
|
|
//
|
|
|
|
// XCBActions.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 9/23/18.
|
|
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2018-09-24 02:16:25 +00:00
|
|
|
import Pachyderm
|
2018-09-26 01:13:51 +00:00
|
|
|
import SwiftSoup
|
2018-09-23 23:04:39 +00:00
|
|
|
|
|
|
|
struct XCBActions {
|
|
|
|
|
2018-09-24 02:16:25 +00:00
|
|
|
// MARK: - Utils
|
2020-01-08 02:29:15 +00:00
|
|
|
private static var mastodonController: MastodonController {
|
|
|
|
let scene = UIApplication.shared.activeOrBackgroundScene!
|
|
|
|
return scene.session.mastodonController!
|
|
|
|
}
|
2020-01-05 20:25:07 +00:00
|
|
|
|
2019-09-16 01:00:57 +00:00
|
|
|
private static func getMainTabBarController() -> MainTabBarViewController {
|
2020-01-07 23:39:19 +00:00
|
|
|
let scene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first!
|
|
|
|
let window = scene.windows.first { $0.isKeyWindow }!
|
|
|
|
return window.rootViewController as! MainTabBarViewController
|
2019-09-16 01:00:57 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 19:31:31 +00:00
|
|
|
private static func show(_ vc: UIViewController) {
|
2019-09-16 01:00:57 +00:00
|
|
|
let tabBarController = getMainTabBarController()
|
|
|
|
if tabBarController.presentedViewController != nil {
|
|
|
|
tabBarController.presentedViewController?.dismiss(animated: false)
|
|
|
|
}
|
|
|
|
tabBarController.selectedViewController!.show(vc, sender: nil)
|
2019-01-19 19:31:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static func present(_ vc: UIViewController, animated: Bool = true) {
|
2019-09-16 01:00:57 +00:00
|
|
|
getMainTabBarController().present(vc, animated: animated)
|
2019-01-19 19:31:31 +00:00
|
|
|
}
|
2018-09-24 02:16:25 +00:00
|
|
|
|
2018-10-20 14:54:59 +00:00
|
|
|
private static func getStatus(from request: XCBRequest, session: XCBSession, completion: @escaping (Status) -> Void) {
|
2018-09-26 01:41:12 +00:00
|
|
|
if let id = request.arguments["statusID"] {
|
2020-01-06 00:54:28 +00:00
|
|
|
mastodonController.cache.status(for: id) { (status) in
|
2018-09-25 12:39:27 +00:00
|
|
|
if let status = status {
|
|
|
|
completion(status)
|
|
|
|
} else {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": "Could not get status with ID \(id)"
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 01:41:12 +00:00
|
|
|
} else if let searchQuery = request.arguments["statusURL"] {
|
2020-01-05 19:00:39 +00:00
|
|
|
let request = Client.search(query: searchQuery)
|
2020-01-05 20:25:07 +00:00
|
|
|
mastodonController.run(request) { (response) in
|
2018-09-25 12:39:27 +00:00
|
|
|
if case let .success(results, _) = response,
|
|
|
|
let status = results.statuses.first {
|
2020-01-06 00:54:28 +00:00
|
|
|
mastodonController.cache.add(status: status)
|
2018-09-25 12:39:27 +00:00
|
|
|
completion(status)
|
|
|
|
} else {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": "Could not find status by searching '\(searchQuery)'"
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": "No status provided. Specify either instance-local statusID or remote statusURL."
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 14:54:59 +00:00
|
|
|
private static func getAccount(from request: XCBRequest, session: XCBSession, completion: @escaping (Account) -> Void) {
|
2018-09-26 01:41:12 +00:00
|
|
|
if let id = request.arguments["accountID"] {
|
2020-01-06 00:54:28 +00:00
|
|
|
mastodonController.cache.account(for: id) { (account) in
|
2018-09-25 12:39:27 +00:00
|
|
|
if let account = account {
|
|
|
|
completion(account)
|
|
|
|
} else {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": "Could not get account with ID \(id)"
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 01:41:12 +00:00
|
|
|
} else if let searchQuery = request.arguments["accountURL"] {
|
2020-01-05 19:00:39 +00:00
|
|
|
let request = Client.search(query: searchQuery)
|
2020-01-05 20:25:07 +00:00
|
|
|
mastodonController.run(request) { (response) in
|
2018-09-25 12:39:27 +00:00
|
|
|
if case let .success(results, _) = response {
|
|
|
|
if let account = results.accounts.first {
|
2020-01-06 00:54:28 +00:00
|
|
|
mastodonController.cache.add(account: account)
|
2018-09-25 12:39:27 +00:00
|
|
|
completion(account)
|
|
|
|
} else {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": "Could not find account by searching '\(searchQuery)'"
|
|
|
|
])
|
|
|
|
}
|
|
|
|
} else if case let .failure(error) = response {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": error.localizedDescription
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 01:41:12 +00:00
|
|
|
} else if let acct = request.arguments["acct"] {
|
2020-01-05 19:00:39 +00:00
|
|
|
let request = Client.searchForAccount(query: acct)
|
2020-01-05 20:25:07 +00:00
|
|
|
mastodonController.run(request) { (response) in
|
2018-09-25 12:39:27 +00:00
|
|
|
if case let .success(accounts, _) = response {
|
|
|
|
if let account = accounts.first {
|
2020-01-06 00:54:28 +00:00
|
|
|
mastodonController.cache.add(account: account)
|
2018-09-25 12:39:27 +00:00
|
|
|
completion(account)
|
|
|
|
} else {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": "Could not find account \(acct)"
|
|
|
|
])
|
|
|
|
}
|
|
|
|
} else if case let .failure(error) = response {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": error.localizedDescription
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": "No status provided. Specify either instance-local ID, account URL, or qualified username."
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-24 02:16:25 +00:00
|
|
|
// MARK: - Statuses
|
2018-09-26 01:41:12 +00:00
|
|
|
static func showStatus(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
|
|
|
|
getStatus(from: request, session: session) { (status) in
|
2018-09-25 12:39:27 +00:00
|
|
|
DispatchQueue.main.async {
|
2020-01-05 20:25:07 +00:00
|
|
|
let vc = ConversationTableViewController(for: status.id, mastodonController: mastodonController)
|
2019-01-19 19:31:31 +00:00
|
|
|
show(vc)
|
2018-09-25 12:39:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 01:41:12 +00:00
|
|
|
static func postStatus(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
|
|
|
|
let mentioning = request.arguments["mentioning"]
|
|
|
|
let text = request.arguments["text"]
|
2018-09-23 23:04:39 +00:00
|
|
|
|
2018-09-24 01:10:45 +00:00
|
|
|
if silent ?? false {
|
2018-09-23 23:04:39 +00:00
|
|
|
var status = ""
|
|
|
|
if let mentioning = mentioning { status += mentioning }
|
|
|
|
if let text = text { status += text }
|
2020-01-05 20:25:07 +00:00
|
|
|
guard CharacterCounter.count(text: status) <= mastodonController.instance.maxStatusCharacters ?? 500 else {
|
2018-09-30 02:20:17 +00:00
|
|
|
session.complete(with: .error, additionalData: [
|
2020-01-05 20:25:07 +00:00
|
|
|
"error": "Too many characters. Instance maximum is \(mastodonController.instance.maxStatusCharacters ?? 500)"
|
2018-09-30 02:20:17 +00:00
|
|
|
])
|
|
|
|
return
|
|
|
|
}
|
2020-01-05 19:00:39 +00:00
|
|
|
let request = Client.createStatus(text: status, visibility: Preferences.shared.defaultPostVisibility)
|
2020-01-05 20:25:07 +00:00
|
|
|
mastodonController.run(request) { response in
|
2018-09-23 23:04:39 +00:00
|
|
|
if case let .success(status, _) = response {
|
|
|
|
session.complete(with: .success, additionalData: [
|
|
|
|
"statusURL": status.url?.absoluteString,
|
|
|
|
"statusURI": status.uri
|
|
|
|
])
|
|
|
|
} else if case let .failure(error) = response {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": error.localizedDescription
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 01:10:45 +00:00
|
|
|
} else {
|
2020-01-05 20:25:07 +00:00
|
|
|
let compose = ComposeViewController(mentioningAcct: mentioning, text: text, mastodonController: mastodonController)
|
2018-10-20 16:03:18 +00:00
|
|
|
compose.xcbSession = session
|
|
|
|
let vc = UINavigationController(rootViewController: compose)
|
2019-01-19 19:31:31 +00:00
|
|
|
present(vc)
|
2018-09-24 02:16:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 01:41:12 +00:00
|
|
|
static func getStatus(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
|
|
|
|
getStatus(from: request, session: session) { (status) in
|
|
|
|
let html = Bool(request.arguments["html"] ?? "false") ?? false
|
2018-09-26 01:13:51 +00:00
|
|
|
let content: String
|
|
|
|
if html {
|
|
|
|
content = status.content
|
|
|
|
} else {
|
|
|
|
do {
|
|
|
|
let doc = try SwiftSoup.parse(status.content)
|
|
|
|
content = try doc.body()!.text()
|
|
|
|
} catch {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": error.localizedDescription
|
|
|
|
])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
session.complete(with: .success, additionalData: [
|
|
|
|
"url": status.url?.absoluteString,
|
|
|
|
"uri": status.uri,
|
|
|
|
"id": status.id,
|
|
|
|
"account": status.account.acct,
|
|
|
|
"inReplyTo": status.inReplyToID,
|
|
|
|
"posted": status.createdAt.timeIntervalSince1970.description,
|
|
|
|
"content": content,
|
|
|
|
"reblog": status.reblog?.id
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 01:41:12 +00:00
|
|
|
static func favoriteStatus(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
|
|
|
|
statusAction(request: Status.favourite, alertTitle: "Favorite status?", request, session, silent)
|
2018-09-24 23:56:27 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 01:41:12 +00:00
|
|
|
static func reblogStatus(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
|
|
|
|
statusAction(request: Status.reblog, alertTitle: "Reblog status?", request, session, silent)
|
2018-09-24 23:56:27 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 01:41:12 +00:00
|
|
|
static func statusAction(request: @escaping (Status) -> Request<Status>, alertTitle: String, _ url: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
|
2018-09-24 02:16:25 +00:00
|
|
|
func performAction(status: Status, completion: ((Status) -> Void)?) {
|
2020-01-05 20:25:07 +00:00
|
|
|
mastodonController.run(request(status)) { (response) in
|
2018-09-24 02:16:25 +00:00
|
|
|
if case let .success(status, _) = response {
|
2020-01-06 00:54:28 +00:00
|
|
|
mastodonController.cache.add(status: status)
|
2018-09-24 02:16:25 +00:00
|
|
|
completion?(status)
|
|
|
|
session.complete(with: .success, additionalData: [
|
|
|
|
"statusURL": status.url?.absoluteString,
|
|
|
|
"statusURI": status.uri
|
|
|
|
])
|
|
|
|
} else if case let .failure(error) = response {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": error.localizedDescription
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func favorite(_ status: Status) {
|
|
|
|
if silent ?? false {
|
|
|
|
performAction(status: status, completion: nil)
|
|
|
|
} else {
|
2020-01-05 20:25:07 +00:00
|
|
|
let vc = ConversationTableViewController(for: status.id, mastodonController: mastodonController)
|
2018-09-24 02:16:25 +00:00
|
|
|
DispatchQueue.main.async {
|
2019-01-19 19:31:31 +00:00
|
|
|
show(vc)
|
2018-09-24 02:16:25 +00:00
|
|
|
}
|
2018-09-24 23:56:27 +00:00
|
|
|
let alertController = UIAlertController(title: alertTitle, message: nil, preferredStyle: .alert)
|
2018-09-24 02:16:25 +00:00
|
|
|
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (_) in
|
|
|
|
performAction(status: status, completion: { (status) in
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
vc.tableView.reloadData()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (_) in
|
|
|
|
session.complete(with: .cancel)
|
|
|
|
}))
|
|
|
|
DispatchQueue.main.async {
|
2019-01-19 19:31:31 +00:00
|
|
|
present(alertController)
|
2018-09-24 02:16:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-25 12:39:27 +00:00
|
|
|
getStatus(from: url, session: session, completion: favorite)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Accounts
|
2018-09-26 01:41:12 +00:00
|
|
|
static func showAccount(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
|
|
|
|
getAccount(from: request, session: session) { (account) in
|
2018-09-25 12:39:27 +00:00
|
|
|
DispatchQueue.main.async {
|
2020-01-05 20:25:07 +00:00
|
|
|
let vc = ProfileTableViewController(accountID: account.id, mastodonController: mastodonController)
|
2019-01-19 19:31:31 +00:00
|
|
|
show(vc)
|
2018-09-24 02:16:25 +00:00
|
|
|
}
|
2018-09-23 23:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-24 01:35:33 +00:00
|
|
|
|
2018-09-26 01:41:12 +00:00
|
|
|
static func getAccount(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
|
|
|
|
getAccount(from: request, session: session) { (account) in
|
2018-09-26 01:20:34 +00:00
|
|
|
session.complete(with: .success, additionalData: [
|
|
|
|
"username": account.acct,
|
|
|
|
"displayName": account.displayName,
|
|
|
|
"locked": account.locked.description,
|
|
|
|
"followers": account.followersCount.description,
|
|
|
|
"following": account.followingCount.description,
|
|
|
|
"url": account.url.absoluteString,
|
|
|
|
"avatarURL": account.avatar.absoluteString,
|
|
|
|
"headerURL": account.header.absoluteString
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 01:41:12 +00:00
|
|
|
static func getCurrentUser(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
|
2020-01-05 20:25:07 +00:00
|
|
|
let account = mastodonController.account!
|
2018-09-24 01:35:33 +00:00
|
|
|
session.complete(with: .success, additionalData: [
|
|
|
|
"username": account.acct,
|
|
|
|
"displayName": account.displayName,
|
|
|
|
"locked": account.locked.description,
|
|
|
|
"followers": account.followersCount.description,
|
|
|
|
"following": account.followingCount.description,
|
|
|
|
"url": account.url.absoluteString,
|
|
|
|
"avatarURL": account.avatar.absoluteString,
|
2018-09-26 01:20:34 +00:00
|
|
|
"headerURL": account.header.absoluteString
|
2018-09-24 01:35:33 +00:00
|
|
|
])
|
|
|
|
}
|
2018-09-24 18:41:28 +00:00
|
|
|
|
2018-09-26 01:41:12 +00:00
|
|
|
static func followUser(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
|
2018-09-24 18:41:28 +00:00
|
|
|
func performAction(_ account: Account) {
|
|
|
|
let request = Account.follow(account.id)
|
2020-01-05 20:25:07 +00:00
|
|
|
mastodonController.run(request) { (response) in
|
2018-09-24 18:41:28 +00:00
|
|
|
if case let .success(relationship, _) = response {
|
2020-01-06 00:54:28 +00:00
|
|
|
mastodonController.cache.add(relationship: relationship)
|
2018-09-24 18:41:28 +00:00
|
|
|
session.complete(with: .success, additionalData: [
|
|
|
|
"url": account.url.absoluteString
|
|
|
|
])
|
|
|
|
} else if case let .failure(error) = response {
|
|
|
|
session.complete(with: .error, additionalData: [
|
|
|
|
"error": error.localizedDescription
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func follow(_ account: Account) {
|
|
|
|
if silent ?? false {
|
|
|
|
performAction(account)
|
|
|
|
} else {
|
2020-01-05 20:25:07 +00:00
|
|
|
let vc = ProfileTableViewController(accountID: account.id, mastodonController: mastodonController)
|
2018-09-24 18:41:28 +00:00
|
|
|
DispatchQueue.main.async {
|
2019-01-19 19:31:31 +00:00
|
|
|
show(vc)
|
2018-09-24 18:41:28 +00:00
|
|
|
}
|
|
|
|
let alertController = UIAlertController(title: "Follow \(account.realDisplayName)?", message: nil, preferredStyle: .alert)
|
|
|
|
alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (_) in
|
|
|
|
performAction(account)
|
|
|
|
}))
|
|
|
|
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (_) in
|
|
|
|
session.complete(with: .cancel)
|
|
|
|
}))
|
|
|
|
DispatchQueue.main.async {
|
2019-01-19 19:31:31 +00:00
|
|
|
present(alertController)
|
2018-09-24 18:41:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 01:41:12 +00:00
|
|
|
getAccount(from: request, session: session, completion: follow)
|
2018-09-24 18:41:28 +00:00
|
|
|
}
|
2019-09-15 22:12:49 +00:00
|
|
|
|
|
|
|
// MARK: - Search
|
|
|
|
|
|
|
|
static func search(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
|
|
|
|
let query = request.arguments["query"]!
|
|
|
|
|
2019-09-16 01:00:57 +00:00
|
|
|
let tabBarController = getMainTabBarController()
|
2019-12-17 05:22:25 +00:00
|
|
|
if let navigationController = tabBarController.getTabController(tab: .explore) as? UINavigationController,
|
|
|
|
let exploreController = navigationController.viewControllers.first as? ExploreViewController {
|
|
|
|
|
|
|
|
tabBarController.select(tab: .explore)
|
2019-09-15 22:12:49 +00:00
|
|
|
navigationController.popToRootViewController(animated: false)
|
2019-12-17 05:22:25 +00:00
|
|
|
|
|
|
|
exploreController.loadViewIfNeeded()
|
|
|
|
exploreController.searchController.isActive = true
|
|
|
|
|
|
|
|
exploreController.searchController.searchBar.text = query
|
|
|
|
exploreController.resultsController.performSearch(query: query)
|
2019-09-15 22:12:49 +00:00
|
|
|
} else {
|
|
|
|
session.complete(with: .error)
|
|
|
|
}
|
|
|
|
}
|
2018-09-23 23:04:39 +00:00
|
|
|
}
|