From a01b09364ff368e5be14b97daf8f48b40791ec3a Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 23 Sep 2018 22:16:25 -0400 Subject: [PATCH] Add favorite status XCB action --- Tusker/XCallbackURL/XCBActionType.swift | 3 + Tusker/XCallbackURL/XCBActions.swift | 85 ++++++++++++++++++++++++- Tusker/XCallbackURL/XCBManager.swift | 1 + 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/Tusker/XCallbackURL/XCBActionType.swift b/Tusker/XCallbackURL/XCBActionType.swift index 1e534fd6..238e4510 100644 --- a/Tusker/XCallbackURL/XCBActionType.swift +++ b/Tusker/XCallbackURL/XCBActionType.swift @@ -9,7 +9,10 @@ import Foundation enum XCBActionType: String { + // Statuses case postStatus + case favoriteStatus + // Accounts case getCurrentUser var path: String { diff --git a/Tusker/XCallbackURL/XCBActions.swift b/Tusker/XCallbackURL/XCBActions.swift index 49f4e74d..b897aa2f 100644 --- a/Tusker/XCallbackURL/XCBActions.swift +++ b/Tusker/XCallbackURL/XCBActions.swift @@ -7,10 +7,22 @@ // import UIKit +import Pachyderm struct XCBActions { - // MARK: - Posts + // 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"] @@ -34,7 +46,76 @@ struct XCBActions { } } else { let vc = ComposeViewController.create(for: session, mentioning: mentioning, text: text) - UIApplication.shared.keyWindow!.rootViewController!.present(vc, animated: true) + 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" + ]) } } diff --git a/Tusker/XCallbackURL/XCBManager.swift b/Tusker/XCallbackURL/XCBManager.swift index ad7340f4..ed90bfa2 100644 --- a/Tusker/XCallbackURL/XCBManager.swift +++ b/Tusker/XCallbackURL/XCBManager.swift @@ -13,6 +13,7 @@ class XCBManager { static var specs: [XCallbackURLSpec] = [ // Statuses XCallbackURLSpec(type: .postStatus, arguments: ["mentioning": true, "text": true], canRunSilently: true, action: XCBActions.postStatus), + XCallbackURLSpec(type: .favoriteStatus, arguments: ["statusID": true, "statusURL": true, "statusURI": true], canRunSilently: true, action: XCBActions.favoriteStatus), // Accounts XCallbackURLSpec(type: .getCurrentUser, arguments: [:], canRunSilently: false, action: XCBActions.getCurrentUser) ]