Tusker/Tusker/XCallbackURL/XCBActions.swift

137 lines
5.8 KiB
Swift

//
// XCBActions.swift
// Tusker
//
// Created by Shadowfacts on 9/23/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import UIKit
import Pachyderm
struct XCBActions {
// MARK: - Utils
static func presentModally(_ vc: UIViewController, animated: Bool, completion: (() -> Void)? = nil) {
UIApplication.shared.keyWindow!.rootViewController!.present(vc, animated: animated, completion: completion)
}
static func presentNav(_ vc: UIViewController, animated: Bool) {
let tabBarController = UIApplication.shared.keyWindow!.rootViewController! as! UITabBarController
let navController = tabBarController.selectedViewController as! UINavigationController
navController.pushViewController(vc, animated: animated)
}
// MARK: - Statuses
static func postStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
let mentioning = url.arguments["mentioning"]
let text = url.arguments["text"]
if silent ?? false {
var status = ""
if let mentioning = mentioning { status += mentioning }
if let text = text { status += text }
let request = MastodonController.shared.client.createStatus(text: status, visibility: Preferences.shared.defaultPostVisibility)
MastodonController.shared.client.run(request) { response in
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
])
}
}
} else {
let vc = ComposeViewController.create(for: session, mentioning: mentioning, text: text)
presentModally(vc, animated: true)
}
}
static func favoriteStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
func performAction(status: Status, completion: ((Status) -> Void)?) {
let request = Status.favourite(status)
MastodonController.shared.client.run(request) { (response) in
if case let .success(status, _) = response {
MastodonCache.add(status: status)
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 {
let vc = ConversationViewController.create(for: status.id)
DispatchQueue.main.async {
presentNav(vc, animated: true)
}
let alertController = UIAlertController(title: "Favorite status?", message: nil, preferredStyle: .alert)
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 {
presentModally(alertController, animated: true)
}
}
}
if let id = url.arguments["statusID"] {
MastodonCache.status(for: id) { (status) in
if let status = status {
favorite(status)
}
}
} else if let searchQuery = url.arguments["statusURL"] ?? url.arguments["statusURI"] {
let request = MastodonController.shared.client.search(query: searchQuery)
MastodonController.shared.client.run(request) { (response) in
if case let .success(results, _) = response,
let status = results.statuses.first {
favorite(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/statusURI"
])
}
}
// MARK: - Accounts
static func getCurrentUser(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
let account = MastodonController.shared.account!
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,
])
}
}