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-23 23:04:39 +00:00
|
|
|
|
|
|
|
struct XCBActions {
|
|
|
|
|
2018-09-24 02:16:25 +00:00
|
|
|
// 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
|
2018-09-24 01:10:45 +00:00
|
|
|
static func postStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
|
2018-09-23 23:04:39 +00:00
|
|
|
let mentioning = url.arguments["mentioning"]
|
|
|
|
let text = url.arguments["text"]
|
|
|
|
|
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 }
|
2018-09-24 01:10:45 +00:00
|
|
|
let request = MastodonController.shared.client.createStatus(text: status, visibility: Preferences.shared.defaultPostVisibility)
|
2018-09-23 23:04:39 +00:00
|
|
|
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
|
|
|
|
])
|
|
|
|
}
|
|
|
|
}
|
2018-09-24 01:10:45 +00:00
|
|
|
} else {
|
2018-09-23 23:04:39 +00:00
|
|
|
let vc = ComposeViewController.create(for: session, mentioning: mentioning, text: text)
|
2018-09-24 02:16:25 +00:00
|
|
|
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"
|
|
|
|
])
|
2018-09-23 23:04:39 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-24 01:35:33 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
])
|
|
|
|
}
|
2018-09-23 23:04:39 +00:00
|
|
|
}
|