From 121d36aa738954cb0c420343e89d7ecc56952986 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 24 Sep 2018 14:41:28 -0400 Subject: [PATCH] Add follow user XCB action --- Tusker/XCallbackURL/XCBActionType.swift | 1 + Tusker/XCallbackURL/XCBActions.swift | 91 ++++++++++++++++++++++++- Tusker/XCallbackURL/XCBManager.swift | 5 +- 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/Tusker/XCallbackURL/XCBActionType.swift b/Tusker/XCallbackURL/XCBActionType.swift index 238e4510..60a1cad1 100644 --- a/Tusker/XCallbackURL/XCBActionType.swift +++ b/Tusker/XCallbackURL/XCBActionType.swift @@ -14,6 +14,7 @@ enum XCBActionType: String { case favoriteStatus // Accounts case getCurrentUser + case followUser var path: String { return "/\(rawValue)" diff --git a/Tusker/XCallbackURL/XCBActions.swift b/Tusker/XCallbackURL/XCBActions.swift index b897aa2f..7b51a400 100644 --- a/Tusker/XCallbackURL/XCBActions.swift +++ b/Tusker/XCallbackURL/XCBActions.swift @@ -98,9 +98,13 @@ struct XCBActions { MastodonCache.status(for: id) { (status) in if let status = status { favorite(status) + } else { + session.complete(with: .error, additionalData: [ + "error": "Could not get status with ID \(id)" + ]) } } - } else if let searchQuery = url.arguments["statusURL"] ?? url.arguments["statusURI"] { + } else if let searchQuery = url.arguments["statusURL"] { let request = MastodonController.shared.client.search(query: searchQuery) MastodonController.shared.client.run(request) { (response) in if case let .success(results, _) = response, @@ -133,4 +137,89 @@ struct XCBActions { "headerURL": account.header.absoluteString, ]) } + + static func followUser(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { + func performAction(_ account: Account) { + let request = Account.follow(account.id) + MastodonController.shared.client.run(request) { (response) in + if case let .success(relationship, _) = response { + MastodonCache.add(relationship: relationship) + 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 { + let vc = ProfileTableViewController.create(for: account.id) + DispatchQueue.main.async { + presentNav(vc, animated: true) + } + 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 { + presentModally(alertController, animated: true) + } + } + } + + if let id = url.arguments["accountID"] { + MastodonCache.account(for: id) { (account) in + if let account = account { + follow(account) + } else { + session.complete(with: .error, additionalData: [ + "error": "Could not get account with ID \(id)" + ]) + } + } + } else if let searchQuery = url.arguments["accountURL"] { + let request = MastodonController.shared.client.search(query: searchQuery) + MastodonController.shared.client.run(request) { (response) in + if case let .success(results, _) = response { + if let account = results.accounts.first { + follow(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 + ]) + } + } + } else if let acct = url.arguments["acct"] { + let request = MastodonController.shared.client.searchForAccount(query: acct) + MastodonController.shared.client.run(request) { (response) in + if case let .success(accounts, _) = response { + if let account = accounts.first { + follow(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 + ]) + } + } + } + } } diff --git a/Tusker/XCallbackURL/XCBManager.swift b/Tusker/XCallbackURL/XCBManager.swift index ed90bfa2..7ae728ca 100644 --- a/Tusker/XCallbackURL/XCBManager.swift +++ b/Tusker/XCallbackURL/XCBManager.swift @@ -13,9 +13,10 @@ 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), + XCallbackURLSpec(type: .favoriteStatus, arguments: ["statusID": true, "statusURL": true], canRunSilently: true, action: XCBActions.favoriteStatus), // Accounts - XCallbackURLSpec(type: .getCurrentUser, arguments: [:], canRunSilently: false, action: XCBActions.getCurrentUser) + XCallbackURLSpec(type: .getCurrentUser, arguments: [:], canRunSilently: false, action: XCBActions.getCurrentUser), + XCallbackURLSpec(type: .followUser, arguments: ["accountID": true, "accountURL": true, "acct": true], canRunSilently: true, action: XCBActions.followUser) ] static var currentSession: XCBSession?