Refactor XCB actions code, add JSON responses

This commit is contained in:
Shadowfacts 2018-09-25 21:41:12 -04:00
parent 0bfc96fe38
commit 813ed27d29
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
6 changed files with 126 additions and 104 deletions

View File

@ -61,6 +61,7 @@
D641C77D213CB024004B4513 /* FollowNotificationTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D641C77C213CB024004B4513 /* FollowNotificationTableViewCell.swift */; }; D641C77D213CB024004B4513 /* FollowNotificationTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D641C77C213CB024004B4513 /* FollowNotificationTableViewCell.swift */; };
D641C77F213DC78A004B4513 /* InlineTextAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = D641C77E213DC78A004B4513 /* InlineTextAttachment.swift */; }; D641C77F213DC78A004B4513 /* InlineTextAttachment.swift in Sources */ = {isa = PBXBuildFile; fileRef = D641C77E213DC78A004B4513 /* InlineTextAttachment.swift */; };
D641C78F213DF2AA004B4513 /* VisibilityTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D641C78E213DF2AA004B4513 /* VisibilityTableViewController.swift */; }; D641C78F213DF2AA004B4513 /* VisibilityTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D641C78E213DF2AA004B4513 /* VisibilityTableViewController.swift */; };
D6434EB3215B1856001A919A /* XCBRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6434EB2215B1856001A919A /* XCBRequest.swift */; };
D646C956213B365700269FB5 /* LargeImageExpandAnimationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D646C955213B365700269FB5 /* LargeImageExpandAnimationController.swift */; }; D646C956213B365700269FB5 /* LargeImageExpandAnimationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D646C955213B365700269FB5 /* LargeImageExpandAnimationController.swift */; };
D646C958213B367000269FB5 /* LargeImageShrinkAnimationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D646C957213B367000269FB5 /* LargeImageShrinkAnimationController.swift */; }; D646C958213B367000269FB5 /* LargeImageShrinkAnimationController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D646C957213B367000269FB5 /* LargeImageShrinkAnimationController.swift */; };
D646C95A213B5D0500269FB5 /* LargeImageInteractionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D646C959213B5D0500269FB5 /* LargeImageInteractionController.swift */; }; D646C95A213B5D0500269FB5 /* LargeImageInteractionController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D646C959213B5D0500269FB5 /* LargeImageInteractionController.swift */; };
@ -94,7 +95,7 @@
D667E5F52135BCD50057A976 /* ConversationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D667E5F42135BCD50057A976 /* ConversationViewController.swift */; }; D667E5F52135BCD50057A976 /* ConversationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D667E5F42135BCD50057A976 /* ConversationViewController.swift */; };
D667E5F82135C3040057A976 /* Mastodon+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D667E5F72135C3040057A976 /* Mastodon+Equatable.swift */; }; D667E5F82135C3040057A976 /* Mastodon+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D667E5F72135C3040057A976 /* Mastodon+Equatable.swift */; };
D6757A7C2157E01900721E32 /* XCBManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6757A7B2157E01900721E32 /* XCBManager.swift */; }; D6757A7C2157E01900721E32 /* XCBManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6757A7B2157E01900721E32 /* XCBManager.swift */; };
D6757A7E2157E02600721E32 /* XCallbackURL.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6757A7D2157E02600721E32 /* XCallbackURL.swift */; }; D6757A7E2157E02600721E32 /* XCBRequestSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6757A7D2157E02600721E32 /* XCBRequestSpec.swift */; };
D6757A822157E8FA00721E32 /* XCBSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6757A812157E8FA00721E32 /* XCBSession.swift */; }; D6757A822157E8FA00721E32 /* XCBSession.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6757A812157E8FA00721E32 /* XCBSession.swift */; };
D679C09F215850EF00DA27FE /* XCBActions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D679C09E215850EF00DA27FE /* XCBActions.swift */; }; D679C09F215850EF00DA27FE /* XCBActions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D679C09E215850EF00DA27FE /* XCBActions.swift */; };
D6BED170212663DA00F02DA0 /* SwiftSoup.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D6BED16E212663DA00F02DA0 /* SwiftSoup.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; D6BED170212663DA00F02DA0 /* SwiftSoup.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D6BED16E212663DA00F02DA0 /* SwiftSoup.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
@ -222,6 +223,7 @@
D641C77C213CB024004B4513 /* FollowNotificationTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FollowNotificationTableViewCell.swift; sourceTree = "<group>"; }; D641C77C213CB024004B4513 /* FollowNotificationTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FollowNotificationTableViewCell.swift; sourceTree = "<group>"; };
D641C77E213DC78A004B4513 /* InlineTextAttachment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InlineTextAttachment.swift; sourceTree = "<group>"; }; D641C77E213DC78A004B4513 /* InlineTextAttachment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InlineTextAttachment.swift; sourceTree = "<group>"; };
D641C78E213DF2AA004B4513 /* VisibilityTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VisibilityTableViewController.swift; sourceTree = "<group>"; }; D641C78E213DF2AA004B4513 /* VisibilityTableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VisibilityTableViewController.swift; sourceTree = "<group>"; };
D6434EB2215B1856001A919A /* XCBRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBRequest.swift; sourceTree = "<group>"; };
D646C955213B365700269FB5 /* LargeImageExpandAnimationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LargeImageExpandAnimationController.swift; sourceTree = "<group>"; }; D646C955213B365700269FB5 /* LargeImageExpandAnimationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LargeImageExpandAnimationController.swift; sourceTree = "<group>"; };
D646C957213B367000269FB5 /* LargeImageShrinkAnimationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LargeImageShrinkAnimationController.swift; sourceTree = "<group>"; }; D646C957213B367000269FB5 /* LargeImageShrinkAnimationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LargeImageShrinkAnimationController.swift; sourceTree = "<group>"; };
D646C959213B5D0500269FB5 /* LargeImageInteractionController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LargeImageInteractionController.swift; sourceTree = "<group>"; }; D646C959213B5D0500269FB5 /* LargeImageInteractionController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LargeImageInteractionController.swift; sourceTree = "<group>"; };
@ -254,7 +256,7 @@
D667E5F42135BCD50057A976 /* ConversationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationViewController.swift; sourceTree = "<group>"; }; D667E5F42135BCD50057A976 /* ConversationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConversationViewController.swift; sourceTree = "<group>"; };
D667E5F72135C3040057A976 /* Mastodon+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Mastodon+Equatable.swift"; sourceTree = "<group>"; }; D667E5F72135C3040057A976 /* Mastodon+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Mastodon+Equatable.swift"; sourceTree = "<group>"; };
D6757A7B2157E01900721E32 /* XCBManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBManager.swift; sourceTree = "<group>"; }; D6757A7B2157E01900721E32 /* XCBManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBManager.swift; sourceTree = "<group>"; };
D6757A7D2157E02600721E32 /* XCallbackURL.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCallbackURL.swift; sourceTree = "<group>"; }; D6757A7D2157E02600721E32 /* XCBRequestSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBRequestSpec.swift; sourceTree = "<group>"; };
D6757A812157E8FA00721E32 /* XCBSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBSession.swift; sourceTree = "<group>"; }; D6757A812157E8FA00721E32 /* XCBSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBSession.swift; sourceTree = "<group>"; };
D679C09E215850EF00DA27FE /* XCBActions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBActions.swift; sourceTree = "<group>"; }; D679C09E215850EF00DA27FE /* XCBActions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCBActions.swift; sourceTree = "<group>"; };
D6BED16E212663DA00F02DA0 /* SwiftSoup.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SwiftSoup.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D6BED16E212663DA00F02DA0 /* SwiftSoup.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; path = SwiftSoup.framework; sourceTree = BUILT_PRODUCTS_DIR; };
@ -580,7 +582,8 @@
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
D6757A7B2157E01900721E32 /* XCBManager.swift */, D6757A7B2157E01900721E32 /* XCBManager.swift */,
D6757A7D2157E02600721E32 /* XCallbackURL.swift */, D6757A7D2157E02600721E32 /* XCBRequestSpec.swift */,
D6434EB2215B1856001A919A /* XCBRequest.swift */,
D6757A812157E8FA00721E32 /* XCBSession.swift */, D6757A812157E8FA00721E32 /* XCBSession.swift */,
D64F80E1215875CC00BEF393 /* XCBActionType.swift */, D64F80E1215875CC00BEF393 /* XCBActionType.swift */,
D679C09E215850EF00DA27FE /* XCBActions.swift */, D679C09E215850EF00DA27FE /* XCBActions.swift */,
@ -963,6 +966,7 @@
D64D0AAD2128D88B005A6F37 /* LocalData.swift in Sources */, D64D0AAD2128D88B005A6F37 /* LocalData.swift in Sources */,
D6C94D892139E6EC00CB5196 /* AttachmentView.swift in Sources */, D6C94D892139E6EC00CB5196 /* AttachmentView.swift in Sources */,
D6C94D872139E62700CB5196 /* LargeImageViewController.swift in Sources */, D6C94D872139E62700CB5196 /* LargeImageViewController.swift in Sources */,
D6434EB3215B1856001A919A /* XCBRequest.swift in Sources */,
D663626221360B1900C9CBA2 /* Preferences.swift in Sources */, D663626221360B1900C9CBA2 /* Preferences.swift in Sources */,
D6333B792139AEFD00CE884A /* Date+TimeAgo.swift in Sources */, D6333B792139AEFD00CE884A /* Date+TimeAgo.swift in Sources */,
D641C78F213DF2AA004B4513 /* VisibilityTableViewController.swift in Sources */, D641C78F213DF2AA004B4513 /* VisibilityTableViewController.swift in Sources */,
@ -984,7 +988,7 @@
D641C773213CAA25004B4513 /* NotificationsTableViewController.swift in Sources */, D641C773213CAA25004B4513 /* NotificationsTableViewController.swift in Sources */,
D6757A7C2157E01900721E32 /* XCBManager.swift in Sources */, D6757A7C2157E01900721E32 /* XCBManager.swift in Sources */,
D64A0CD32132153900640E3B /* HTMLContentLabel.swift in Sources */, D64A0CD32132153900640E3B /* HTMLContentLabel.swift in Sources */,
D6757A7E2157E02600721E32 /* XCallbackURL.swift in Sources */, D6757A7E2157E02600721E32 /* XCBRequestSpec.swift in Sources */,
D667E5F12134D5050057A976 /* UIViewController+Delegates.swift in Sources */, D667E5F12134D5050057A976 /* UIViewController+Delegates.swift in Sources */,
D663625F2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift in Sources */, D663625F2135C75500C9CBA2 /* ConversationMainStatusTableViewCell.swift in Sources */,
D667E5E721349D4C0057A976 /* ProfileTableViewController.swift in Sources */, D667E5E721349D4C0057A976 /* ProfileTableViewController.swift in Sources */,

View File

@ -23,8 +23,8 @@ struct XCBActions {
navController.pushViewController(vc, animated: animated) navController.pushViewController(vc, animated: animated)
} }
static func getStatus(from url: XCallbackURL, session: XCBSession, completion: @escaping (Status) -> Void) { static func getStatus(from request: XCBRequest, session: XCBSession, completion: @escaping (Status) -> Void) {
if let id = url.arguments["statusID"] { if let id = request.arguments["statusID"] {
MastodonCache.status(for: id) { (status) in MastodonCache.status(for: id) { (status) in
if let status = status { if let status = status {
completion(status) completion(status)
@ -34,7 +34,7 @@ struct XCBActions {
]) ])
} }
} }
} else if let searchQuery = url.arguments["statusURL"] { } else if let searchQuery = request.arguments["statusURL"] {
let request = MastodonController.shared.client.search(query: searchQuery) let request = MastodonController.shared.client.search(query: searchQuery)
MastodonController.shared.client.run(request) { (response) in MastodonController.shared.client.run(request) { (response) in
if case let .success(results, _) = response, if case let .success(results, _) = response,
@ -54,8 +54,8 @@ struct XCBActions {
} }
} }
static func getAccount(from url: XCallbackURL, session: XCBSession, completion: @escaping (Account) -> Void) { static func getAccount(from request: XCBRequest, session: XCBSession, completion: @escaping (Account) -> Void) {
if let id = url.arguments["accountID"] { if let id = request.arguments["accountID"] {
MastodonCache.account(for: id) { (account) in MastodonCache.account(for: id) { (account) in
if let account = account { if let account = account {
completion(account) completion(account)
@ -65,7 +65,7 @@ struct XCBActions {
]) ])
} }
} }
} else if let searchQuery = url.arguments["accountURL"] { } else if let searchQuery = request.arguments["accountURL"] {
let request = MastodonController.shared.client.search(query: searchQuery) let request = MastodonController.shared.client.search(query: searchQuery)
MastodonController.shared.client.run(request) { (response) in MastodonController.shared.client.run(request) { (response) in
if case let .success(results, _) = response { if case let .success(results, _) = response {
@ -83,7 +83,7 @@ struct XCBActions {
]) ])
} }
} }
} else if let acct = url.arguments["acct"] { } else if let acct = request.arguments["acct"] {
let request = MastodonController.shared.client.searchForAccount(query: acct) let request = MastodonController.shared.client.searchForAccount(query: acct)
MastodonController.shared.client.run(request) { (response) in MastodonController.shared.client.run(request) { (response) in
if case let .success(accounts, _) = response { if case let .success(accounts, _) = response {
@ -109,8 +109,8 @@ struct XCBActions {
} }
// MARK: - Statuses // MARK: - Statuses
static func showStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { static func showStatus(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
getStatus(from: url, session: session) { (status) in getStatus(from: request, session: session) { (status) in
let vc = ConversationViewController.create(for: status.id) let vc = ConversationViewController.create(for: status.id)
DispatchQueue.main.async { DispatchQueue.main.async {
presentNav(vc, animated: true) presentNav(vc, animated: true)
@ -118,9 +118,9 @@ struct XCBActions {
} }
} }
static func postStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { static func postStatus(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
let mentioning = url.arguments["mentioning"] let mentioning = request.arguments["mentioning"]
let text = url.arguments["text"] let text = request.arguments["text"]
if silent ?? false { if silent ?? false {
var status = "" var status = ""
@ -145,9 +145,9 @@ struct XCBActions {
} }
} }
static func getStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { static func getStatus(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
getStatus(from: url, session: session) { (status) in getStatus(from: request, session: session) { (status) in
let html = Bool(url.arguments["html"] ?? "false") ?? false let html = Bool(request.arguments["html"] ?? "false") ?? false
let content: String let content: String
if html { if html {
content = status.content content = status.content
@ -175,15 +175,15 @@ struct XCBActions {
} }
} }
static func favoriteStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { static func favoriteStatus(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
statusAction(request: Status.favourite, alertTitle: "Favorite status?", url, session, silent) statusAction(request: Status.favourite, alertTitle: "Favorite status?", request, session, silent)
} }
static func reblogStatus(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { static func reblogStatus(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
statusAction(request: Status.reblog, alertTitle: "Reblog status?", url, session, silent) statusAction(request: Status.reblog, alertTitle: "Reblog status?", request, session, silent)
} }
static func statusAction(request: @escaping (Status) -> Request<Status>, alertTitle: String, _ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { static func statusAction(request: @escaping (Status) -> Request<Status>, alertTitle: String, _ url: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
func performAction(status: Status, completion: ((Status) -> Void)?) { func performAction(status: Status, completion: ((Status) -> Void)?) {
MastodonController.shared.client.run(request(status)) { (response) in MastodonController.shared.client.run(request(status)) { (response) in
if case let .success(status, _) = response { if case let .success(status, _) = response {
@ -230,8 +230,8 @@ struct XCBActions {
} }
// MARK: - Accounts // MARK: - Accounts
static func showAccount(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { static func showAccount(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
getAccount(from: url, session: session) { (account) in getAccount(from: request, session: session) { (account) in
let vc = ProfileTableViewController.create(for: account.id) let vc = ProfileTableViewController.create(for: account.id)
DispatchQueue.main.async { DispatchQueue.main.async {
presentNav(vc, animated: true) presentNav(vc, animated: true)
@ -239,8 +239,8 @@ struct XCBActions {
} }
} }
static func getAccount(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { static func getAccount(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
getAccount(from: url, session: session) { (account) in getAccount(from: request, session: session) { (account) in
session.complete(with: .success, additionalData: [ session.complete(with: .success, additionalData: [
"username": account.acct, "username": account.acct,
"displayName": account.displayName, "displayName": account.displayName,
@ -254,7 +254,7 @@ struct XCBActions {
} }
} }
static func getCurrentUser(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { static func getCurrentUser(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
let account = MastodonController.shared.account! let account = MastodonController.shared.account!
session.complete(with: .success, additionalData: [ session.complete(with: .success, additionalData: [
"username": account.acct, "username": account.acct,
@ -268,7 +268,7 @@ struct XCBActions {
]) ])
} }
static func followUser(_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) { static func followUser(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) {
func performAction(_ account: Account) { func performAction(_ account: Account) {
let request = Account.follow(account.id) let request = Account.follow(account.id)
MastodonController.shared.client.run(request) { (response) in MastodonController.shared.client.run(request) { (response) in
@ -306,6 +306,6 @@ struct XCBActions {
} }
} }
getAccount(from: url, session: session, completion: follow) getAccount(from: request, session: session, completion: follow)
} }
} }

View File

@ -10,18 +10,18 @@ import UIKit
class XCBManager { class XCBManager {
static var specs: [XCallbackURLSpec] = [ static var specs: [XCBRequestSpec] = [
// Statuses // Statuses
XCallbackURLSpec(type: .showStatus, arguments: ["statusID": true, "statusURL": true], canRunSilently: false, action: XCBActions.showStatus), XCBRequestSpec(type: .showStatus, arguments: ["statusID": true, "statusURL": true], canRunSilently: false, action: XCBActions.showStatus),
XCallbackURLSpec(type: .getStatus, arguments: ["statusID": true, "statusURL": true, "html": true], canRunSilently: false, action: XCBActions.getStatus), XCBRequestSpec(type: .getStatus, arguments: ["statusID": true, "statusURL": true, "html": true], canRunSilently: false, action: XCBActions.getStatus),
XCallbackURLSpec(type: .postStatus, arguments: ["mentioning": true, "text": true], canRunSilently: true, action: XCBActions.postStatus), XCBRequestSpec(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), XCBRequestSpec(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), XCBRequestSpec(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), XCBRequestSpec(type: .showAccount, arguments: ["accountID": true, "accountURL": true, "acct": true], canRunSilently: false, action: XCBActions.showAccount),
XCallbackURLSpec(type: .getAccount, arguments: ["accountID": true, "accountURL": true, "acct": true], canRunSilently: false, action: XCBActions.getAccount), XCBRequestSpec(type: .getAccount, arguments: ["accountID": true, "accountURL": true, "acct": true], canRunSilently: false, action: XCBActions.getAccount),
XCallbackURLSpec(type: .getCurrentUser, arguments: [:], canRunSilently: false, action: XCBActions.getCurrentUser), XCBRequestSpec(type: .getCurrentUser, arguments: [:], canRunSilently: false, action: XCBActions.getCurrentUser),
XCallbackURLSpec(type: .followUser, arguments: ["accountID": true, "accountURL": true, "acct": true], canRunSilently: true, action: XCBActions.followUser) XCBRequestSpec(type: .followUser, arguments: ["accountID": true, "accountURL": true, "acct": true], canRunSilently: true, action: XCBActions.followUser)
] ]
static var currentSession: XCBSession? static var currentSession: XCBSession?
@ -29,14 +29,14 @@ class XCBManager {
static func handle(url: URL) -> Bool { static func handle(url: URL) -> Bool {
guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else { return false } guard let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else { return false }
if let spec = specs.first(where: { $0.matches(components) }) { if let spec = specs.first(where: { $0.matches(components) }) {
let xcbURL = XCallbackURL(spec: spec, components: components) let request = XCBRequest(spec: spec, components: components)
return spec.handle(url: xcbURL) return spec.handle(request: request)
} }
return false return false
} }
static func createSession(type: XCBActionType, url: XCallbackURL) -> XCBSession { static func createSession(type: XCBActionType, request: XCBRequest) -> XCBSession {
let session = XCBSession(type: type, success: url.success, error: url.error, cancel: url.cancel) let session = XCBSession(type: type, request: request)
currentSession = session currentSession = session
return session return session
} }

View File

@ -0,0 +1,44 @@
//
// XCBRequest.swift
// Tusker
//
// Created by Shadowfacts on 9/25/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
struct XCBRequest {
let path: String
let arguments: [String: String]
let json: Bool
let silent: Bool
let source: String?
let success: URL?
let error: URL?
let cancel: URL?
init(spec: XCBRequestSpec, components: URLComponents) {
self.path = spec.path
let queryItems = components.queryItems!
self.arguments = spec.arguments.reduce(into: [String: String](), { (result, el) in
if let value = queryItems.first(where: { $0.name == el.key })?.value {
result[el.key] = value
}
})
if let arg = arguments["json"] {
json = Bool(arg) ?? false
} else {
json = false
}
if spec.canRunSilently, let arg = arguments["silent"] {
silent = Bool(arg) ?? false
} else {
silent = false
}
source = queryItems.first(where: { $0.name == "x-source" }).flatMap { $0.value }
success = queryItems.first(where: { $0.name == "x-success" }).flatMap { $0.value }.flatMap { URL(string: $0) }
error = queryItems.first(where: { $0.name == "x-error" }).flatMap { $0.value }.flatMap { URL(string: $0) }
cancel = queryItems.first(where: { $0.name == "x-cancel" }).flatMap { $0.value }.flatMap { URL(string: $0) }
}
}

View File

@ -8,9 +8,9 @@
import UIKit import UIKit
typealias XCBAction = (_ url: XCallbackURL, _ session: XCBSession, _ silent: Bool?) -> Void typealias XCBAction = (_ url: XCBRequest, _ session: XCBSession, _ silent: Bool?) -> Void
struct XCallbackURLSpec { struct XCBRequestSpec {
let path: String let path: String
let type: XCBActionType let type: XCBActionType
@ -27,28 +27,29 @@ struct XCallbackURLSpec {
if canRunSilently { if canRunSilently {
arguments["silent"] = true arguments["silent"] = true
} }
arguments["json"] = true
self.arguments = arguments self.arguments = arguments
} }
func handle(url: XCallbackURL) -> Bool { func handle(request: XCBRequest) -> Bool {
let session = XCBManager.createSession(type: type, url: url) let session = XCBManager.createSession(type: type, request: request)
if canRunSilently && url.silent { if canRunSilently && request.silent {
if let source = url.source { if let source = request.source {
let permission = Preferences.shared.silentActions[source] ?? .undecided let permission = Preferences.shared.silentActions[source] ?? .undecided
switch permission { switch permission {
case .accepted: case .accepted:
action(url, session, true) action(request, session, true)
case .rejected: case .rejected:
action(url, session, false) action(request, session, false)
case .undecided: case .undecided:
let alert = UIAlertController(title: "\(source) wants to perform actions silently", message: "Accepting will allow \(source) to perform actions without your confirmation, rejecting will always prompt for confirmation.", preferredStyle: .alert) let alert = UIAlertController(title: "\(source) wants to perform actions silently", message: "Accepting will allow \(source) to perform actions without your confirmation, rejecting will always prompt for confirmation.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Accept", style: .default, handler: { (_) in alert.addAction(UIAlertAction(title: "Accept", style: .default, handler: { (_) in
Preferences.shared.silentActions[source] = .accepted Preferences.shared.silentActions[source] = .accepted
self.action(url, session, true) self.action(request, session, true)
})) }))
alert.addAction(UIAlertAction(title: "Reject", style: .default, handler: { (_) in alert.addAction(UIAlertAction(title: "Reject", style: .default, handler: { (_) in
Preferences.shared.silentActions[source] = .rejected Preferences.shared.silentActions[source] = .rejected
self.action(url, session, false) self.action(request, session, false)
})) }))
UIApplication.shared.keyWindow!.rootViewController!.present(alert, animated: true) UIApplication.shared.keyWindow!.rootViewController!.present(alert, animated: true)
} }
@ -58,43 +59,14 @@ struct XCallbackURLSpec {
]) ])
} }
} else { } else {
action(url, session, nil) action(request, session, nil)
} }
return true return true
} }
} }
struct XCallbackURL { extension XCBRequestSpec {
let path: String
let arguments: [String: String]
let silent: Bool
let source: String?
let success: URL?
let error: URL?
let cancel: URL?
init(spec: XCallbackURLSpec, components: URLComponents) {
self.path = spec.path
let queryItems = components.queryItems!
self.arguments = spec.arguments.reduce(into: [String: String](), { (result, el) in
if let value = queryItems.first(where: { $0.name == el.key })?.value {
result[el.key] = value
}
})
if spec.canRunSilently {
silent = Bool(arguments["silent"] ?? "false") ?? false
} else {
silent = false
}
source = queryItems.first(where: { $0.name == "x-source" }).flatMap { $0.value }
success = queryItems.first(where: { $0.name == "x-success" }).flatMap { $0.value }.flatMap { URL(string: $0) }
error = queryItems.first(where: { $0.name == "x-error" }).flatMap { $0.value }.flatMap { URL(string: $0) }
cancel = queryItems.first(where: { $0.name == "x-cancel" }).flatMap { $0.value }.flatMap { URL(string: $0) }
}
}
extension XCallbackURLSpec {
func matches(_ components: URLComponents) -> Bool { func matches(_ components: URLComponents) -> Bool {
guard path == components.path else { return false } guard path == components.path else { return false }
for (name, optional) in arguments { for (name, optional) in arguments {

View File

@ -9,33 +9,35 @@
import UIKit import UIKit
class XCBSession { class XCBSession {
let type: XCBActionType static let encoder = JSONEncoder()
let success: URL?
let error: URL?
let cancel: URL?
init(type: XCBActionType, success: URL?, error: URL?, cancel: URL?) { let type: XCBActionType
let request: XCBRequest
init(type: XCBActionType, request: XCBRequest) {
self.type = type self.type = type
self.success = success self.request = request
self.error = error
self.cancel = cancel
} }
func complete(with result: XCBSessionResult, additionalData: [String: String?]? = nil) { func complete(with result: XCBSessionResult, additionalData: [String: String?]? = nil) {
let url = result == .success ? success : result == .error ? error : cancel guard var url = result == .success ? request.success : result == .error ? request.error : request.cancel else { return }
if var url = url {
XCBManager.currentSession = nil XCBManager.currentSession = nil
if let additionalData = additionalData { if let additionalData = additionalData {
var components = URLComponents(url: url, resolvingAgainstBaseURL: true)! var components = URLComponents(url: url, resolvingAgainstBaseURL: true)!
components.queryItems = components.queryItems ?? [] components.queryItems = components.queryItems ?? []
if request.json {
let data = try! XCBSession.encoder.encode(additionalData)
let response = String(data: data, encoding: .utf8)
components.queryItems!.append(URLQueryItem(name: "response", value: response))
} else {
components.queryItems!.append(contentsOf: additionalData.map(URLQueryItem.init)) components.queryItems!.append(contentsOf: additionalData.map(URLQueryItem.init))
}
url = components.url! url = components.url!
} }
DispatchQueue.main.async { DispatchQueue.main.async {
UIApplication.shared.open(url, options: [:]) UIApplication.shared.open(url, options: [:])
} }
} }
}
} }
enum XCBSessionResult { enum XCBSessionResult {