Add follow user XCB action

This commit is contained in:
Shadowfacts 2018-09-24 14:41:28 -04:00
parent 3ff80b238e
commit 4b2071af6a
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 94 additions and 3 deletions

View File

@ -14,6 +14,7 @@ enum XCBActionType: String {
case favoriteStatus
// Accounts
case getCurrentUser
case followUser
var path: String {
return "/\(rawValue)"

View File

@ -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
])
}
}
}
}
}

View File

@ -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?