Add show account and show status XCB actions

This commit is contained in:
Shadowfacts 2018-09-25 08:39:27 -04:00
parent 0eeaa4da11
commit b537321057
4 changed files with 142 additions and 72 deletions

View File

@ -52,10 +52,12 @@ Dates in responses are encoded as Unix timestamps.
## Requests ## Requests
- [Accounts](#accounts) - [Accounts](#accounts)
- [`showAccount`](#showaccount)
- [`getCurrentUser`](#getcurrentuser) - [`getCurrentUser`](#getcurrentuser)
- [`getAccount`](#getaccount) - [`getAccount`](#getaccount)
- [`followUser`](#followuser) - [`followUser`](#followuser)
- [Statuses](#statuses) - [Statuses](#statuses)
- [`showStatus`](#showstatus)
- [`getStatus`](#getstatus) - [`getStatus`](#getstatus)
- [`postStatus`](#poststatus) - [`postStatus`](#poststatus)
- [`favoriteStatus`](#favoritestatus) - [`favoriteStatus`](#favoritestatus)
@ -70,6 +72,22 @@ Dates in responses are encoded as Unix timestamps.
### Accounts ### Accounts
#### `showAccount`
Presents the given account in Tusker.
##### Request
| Parameter (type) | Description | Optional |
| -------------------- | ------------------------------------------------------------ | -------- |
| `accountID` (string) | The instance-local ID of the account | Yes |
| `accountURL` (URL) | The URL of the remote account | Yes |
| `acct` (string) | The [qualified username](#qualifiedusernames) of the account | Yes |
##### Response
No data if successful.
#### `getCurrentUser` #### `getCurrentUser`
Retrieves the currently logged-in user. Retrieves the currently logged-in user.
@ -136,6 +154,21 @@ Follows the given account from the logged-in user's account. One of `accountID`,
### Statuses ### Statuses
#### `showStatus`
Presents the given status in Tusker.
##### Request
| Parameter (type) | Description | Optional |
| ------------------- | ----------------------------------- | -------- |
| `statusID` (string) | The instance-local ID of the status | Yes |
| `statusURL` (URL) | The URL of a remote status | Yes |
##### Response
No data if successful.
#### `getStatus` #### `getStatus`
Retrieves the given status details. One of `statusID` or `statusURL` must be provided. Retrieves the given status details. One of `statusID` or `statusURL` must be provided.

View File

@ -10,10 +10,12 @@ import Foundation
enum XCBActionType: String { enum XCBActionType: String {
// Statuses // Statuses
case showStatus
case postStatus case postStatus
case favoriteStatus case favoriteStatus
case reblogStatus case reblogStatus
// Accounts // Accounts
case showAccount
case getCurrentUser case getCurrentUser
case followUser case followUser

View File

@ -22,7 +22,101 @@ struct XCBActions {
navController.pushViewController(vc, animated: animated) navController.pushViewController(vc, animated: animated)
} }
static func getStatus(from url: XCallbackURL, session: XCBSession, completion: @escaping (Status) -> Void) {
if let id = url.arguments["statusID"] {
MastodonCache.status(for: id) { (status) in
if let status = status {
completion(status)
} else {
session.complete(with: .error, additionalData: [
"error": "Could not get status with ID \(id)"
])
}
}
} 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,
let status = results.statuses.first {
MastodonCache.add(status: status)
completion(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."
])
}
}
static func getAccount(from url: XCallbackURL, session: XCBSession, completion: @escaping (Account) -> Void) {
if let id = url.arguments["accountID"] {
MastodonCache.account(for: id) { (account) in
if let account = account {
completion(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 {
MastodonCache.add(account: account)
completion(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 {
MastodonCache.add(account: account)
completion(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
])
}
}
} else {
session.complete(with: .error, additionalData: [
"error": "No status provided. Specify either instance-local ID, account URL, or qualified username."
])
}
}
// MARK: - Statuses // MARK: - Statuses
static func showStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
getStatus(from: url, session: session) { (status) in
let vc = ConversationViewController.create(for: status.id)
DispatchQueue.main.async {
presentNav(vc, animated: true)
}
}
}
static func postStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { static func postStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
let mentioning = url.arguments["mentioning"] let mentioning = url.arguments["mentioning"]
let text = url.arguments["text"] let text = url.arguments["text"]
@ -101,36 +195,19 @@ struct XCBActions {
} }
} }
if let id = url.arguments["statusID"] { getStatus(from: url, session: session, completion: favorite)
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"] {
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"
])
}
} }
// MARK: - Accounts // MARK: - Accounts
static func showAccount(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
getAccount(from: url, session: session) { (account) in
let vc = ProfileTableViewController.create(for: account.id)
DispatchQueue.main.async {
presentNav(vc, animated: true)
}
}
}
static func getCurrentUser(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { static func getCurrentUser(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) {
let account = MastodonController.shared.account! let account = MastodonController.shared.account!
session.complete(with: .success, additionalData: [ session.complete(with: .success, additionalData: [
@ -183,50 +260,6 @@ struct XCBActions {
} }
} }
if let id = url.arguments["accountID"] { getAccount(from: url, session: session, completion: follow)
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

@ -12,10 +12,12 @@ class XCBManager {
static var specs: [XCallbackURLSpec] = [ static var specs: [XCallbackURLSpec] = [
// Statuses // Statuses
XCallbackURLSpec(type: .showStatus, arguments: ["statusID": true, "statusURL": true], canRunSilently: false, action: XCBActions.showStatus),
XCallbackURLSpec(type: .postStatus, arguments: ["mentioning": true, "text": true], canRunSilently: true, action: XCBActions.postStatus), XCallbackURLSpec(type: .postStatus, arguments: ["mentioning": true, "text": true], canRunSilently: true, action: XCBActions.postStatus),
XCallbackURLSpec(type: .favoriteStatus, arguments: ["statusID": true, "statusURL": true], canRunSilently: true, action: XCBActions.favoriteStatus), XCallbackURLSpec(type: .favoriteStatus, arguments: ["statusID": true, "statusURL": true], canRunSilently: true, action: XCBActions.favoriteStatus),
XCallbackURLSpec(type: .reblogStatus, arguments: ["statusID": true, "statusURL": true], canRunSilently: true, action: XCBActions.reblogStatus), XCallbackURLSpec(type: .reblogStatus, arguments: ["statusID": true, "statusURL": true], canRunSilently: true, action: XCBActions.reblogStatus),
// Accounts // Accounts
XCallbackURLSpec(type: .showAccount, arguments: ["accountID": true, "accountURL": true, "acct": true], canRunSilently: false, action: XCBActions.showAccount),
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) XCallbackURLSpec(type: .followUser, arguments: ["accountID": true, "accountURL": true, "acct": true], canRunSilently: true, action: XCBActions.followUser)
] ]