From a18bcac8b87eeebf470f7d932f606ea57d5f2cf8 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 5 Jan 2020 14:00:39 -0500 Subject: [PATCH] Pachyderm: Change Client request methods to be static, like all other models Tusker: Add run method to MastodonController and no-longer expose API client object --- Pachyderm/Client.swift | 76 +++++++++---------- .../FollowAccountActivity.swift | 2 +- .../UnfollowAccountActivity.swift | 2 +- .../BookmarkStatusActivity.swift | 2 +- .../Status Activities/PinStatusActivity.swift | 2 +- .../UnbookmarkStatusActivity.swift | 2 +- .../UnpinStatusActivity.swift | 2 +- Tusker/Controllers/MastodonController.swift | 18 +++-- Tusker/MastodonCache.swift | 12 +-- .../BookmarksTableViewController.swift | 12 +-- .../Compose/ComposeViewController.swift | 8 +- .../ConversationTableViewController.swift | 2 +- .../Explore/ExploreViewController.swift | 10 +-- .../EditListAccountsViewController.swift | 8 +- .../NotificationsTableViewController.swift | 14 ++-- .../InstanceSelectorTableViewController.swift | 2 +- .../Profile/ProfileTableViewController.swift | 2 +- .../Search/SearchResultsViewController.swift | 4 +- ...ActionAccountListTableViewController.swift | 2 +- .../TimelineTableViewController.swift | 14 ++-- ...llowRequestNotificationTableViewCell.swift | 4 +- .../Status/BaseStatusTableViewCell.swift | 4 +- .../Status/TimelineStatusTableViewCell.swift | 4 +- Tusker/XCallbackURL/XCBActions.swift | 20 ++--- 24 files changed, 118 insertions(+), 110 deletions(-) diff --git a/Pachyderm/Client.swift b/Pachyderm/Client.swift index 89c9a573..e0de6b61 100644 --- a/Pachyderm/Client.swift +++ b/Pachyderm/Client.swift @@ -130,32 +130,32 @@ public class Client { } // MARK: - Self - public func getSelfAccount() -> Request { + public static func getSelfAccount() -> Request { return Request(method: .get, path: "/api/v1/accounts/verify_credentials") } - public func getFavourites() -> Request<[Status]> { + public static func getFavourites() -> Request<[Status]> { return Request<[Status]>(method: .get, path: "/api/v1/favourites") } - public func getRelationships(accounts: [String]? = nil) -> Request<[Relationship]> { + public static func getRelationships(accounts: [String]? = nil) -> Request<[Relationship]> { return Request<[Relationship]>(method: .get, path: "/api/v1/accounts/relationships", queryParameters: "id" => accounts) } - public func getInstance() -> Request { + public static func getInstance() -> Request { return Request(method: .get, path: "/api/v1/instance") } - public func getCustomEmoji() -> Request<[Emoji]> { + public static func getCustomEmoji() -> Request<[Emoji]> { return Request<[Emoji]>(method: .get, path: "/api/v1/custom_emojis") } // MARK: - Accounts - public func getAccount(id: String) -> Request { + public static func getAccount(id: String) -> Request { return Request(method: .get, path: "/api/v1/accounts/\(id)") } - public func searchForAccount(query: String, limit: Int? = nil, following: Bool? = nil) -> Request<[Account]> { + public static func searchForAccount(query: String, limit: Int? = nil, following: Bool? = nil) -> Request<[Account]> { return Request<[Account]>(method: .get, path: "/api/v1/accounts/search", queryParameters: [ "q" => query, "limit" => limit, @@ -164,32 +164,32 @@ public class Client { } // MARK: - Blocks - public func getBlocks() -> Request<[Account]> { + public static func getBlocks() -> Request<[Account]> { return Request<[Account]>(method: .get, path: "/api/v1/blocks") } - public func getDomainBlocks() -> Request<[String]> { + public static func getDomainBlocks() -> Request<[String]> { return Request<[String]>(method: .get, path: "api/v1/domain_blocks") } - public func block(domain: String) -> Request { + public static func block(domain: String) -> Request { return Request(method: .post, path: "/api/v1/domain_blocks", body: .parameters([ "domain" => domain ])) } - public func unblock(domain: String) -> Request { + public static func unblock(domain: String) -> Request { return Request(method: .delete, path: "/api/v1/domain_blocks", body: .parameters([ "domain" => domain ])) } // MARK: - Filters - public func getFilters() -> Request<[Filter]> { + public static func getFilters() -> Request<[Filter]> { return Request<[Filter]>(method: .get, path: "/api/v1/filters") } - public func createFilter(phrase: String, context: [Filter.Context], irreversible: Bool? = nil, wholeWord: Bool? = nil, expiresAt: Date? = nil) -> Request { + public static func createFilter(phrase: String, context: [Filter.Context], irreversible: Bool? = nil, wholeWord: Bool? = nil, expiresAt: Date? = nil) -> Request { return Request(method: .post, path: "/api/v1/filters", body: .parameters([ "phrase" => phrase, "irreversible" => irreversible, @@ -198,40 +198,40 @@ public class Client { ] + "context" => context.contextStrings)) } - public func getFilter(id: String) -> Request { + public static func getFilter(id: String) -> Request { return Request(method: .get, path: "/api/v1/filters/\(id)") } // MARK: - Follows - public func getFollowRequests(range: RequestRange = .default) -> Request<[Account]> { + public static func getFollowRequests(range: RequestRange = .default) -> Request<[Account]> { var request = Request<[Account]>(method: .get, path: "/api/v1/follow_requests") request.range = range return request } - public func getFollowSuggestions() -> Request<[Account]> { + public static func getFollowSuggestions() -> Request<[Account]> { return Request<[Account]>(method: .get, path: "/api/v1/suggestions") } - public func followRemote(acct: String) -> Request { + public static func followRemote(acct: String) -> Request { return Request(method: .post, path: "/api/v1/follows", body: .parameters(["uri" => acct])) } // MARK: - Lists - public func getLists() -> Request<[List]> { + public static func getLists() -> Request<[List]> { return Request<[List]>(method: .get, path: "/api/v1/lists") } - public func getList(id: String) -> Request { + public static func getList(id: String) -> Request { return Request(method: .get, path: "/api/v1/lists/\(id)") } - public func createList(title: String) -> Request { + public static func createList(title: String) -> Request { return Request(method: .post, path: "/api/v1/lists", body: .parameters(["title" => title])) } // MARK: - Media - public func upload(attachment: FormAttachment, description: String? = nil, focus: (Float, Float)? = nil) -> Request { + public static func upload(attachment: FormAttachment, description: String? = nil, focus: (Float, Float)? = nil) -> Request { return Request(method: .post, path: "/api/v1/media", body: .formData([ "description" => description, "focus" => focus @@ -239,14 +239,14 @@ public class Client { } // MARK: - Mutes - public func getMutes(range: RequestRange) -> Request<[Account]> { + public static func getMutes(range: RequestRange) -> Request<[Account]> { var request = Request<[Account]>(method: .get, path: "/api/v1/mutes") request.range = range return request } // MARK: - Notifications - public func getNotifications(excludeTypes: [Notification.Kind], range: RequestRange = .default) -> Request<[Notification]> { + public static func getNotifications(excludeTypes: [Notification.Kind], range: RequestRange = .default) -> Request<[Notification]> { var request = Request<[Notification]>(method: .get, path: "/api/v1/notifications", queryParameters: "exclude_types" => excludeTypes.map { $0.rawValue } ) @@ -254,16 +254,16 @@ public class Client { return request } - public func clearNotifications() -> Request { + public static func clearNotifications() -> Request { return Request(method: .post, path: "/api/v1/notifications/clear") } // MARK: - Reports - public func getReports() -> Request<[Report]> { + public static func getReports() -> Request<[Report]> { return Request<[Report]>(method: .get, path: "/api/v1/reports") } - public func report(account: Account, statuses: [Status], comment: String) -> Request { + public static func report(account: Account, statuses: [Status], comment: String) -> Request { return Request(method: .post, path: "/api/v1/reports", body: .parameters([ "account_id" => account.id, "comment" => comment @@ -271,7 +271,7 @@ public class Client { } // MARK: - Search - public func search(query: String, resolve: Bool? = nil, limit: Int? = nil) -> Request { + public static func search(query: String, resolve: Bool? = nil, limit: Int? = nil) -> Request { return Request(method: .get, path: "/api/v2/search", queryParameters: [ "q" => query, "resolve" => resolve, @@ -280,18 +280,18 @@ public class Client { } // MARK: - Statuses - public func getStatus(id: String) -> Request { + public static func getStatus(id: String) -> Request { return Request(method: .get, path: "/api/v1/statuses/\(id)") } - public func createStatus(text: String, - contentType: StatusContentType = .plain, - inReplyTo: String? = nil, - media: [Attachment]? = nil, - sensitive: Bool? = nil, - spoilerText: String? = nil, - visibility: Status.Visibility? = nil, - language: String? = nil) -> Request { + public static func createStatus(text: String, + contentType: StatusContentType = .plain, + inReplyTo: String? = nil, + media: [Attachment]? = nil, + sensitive: Bool? = nil, + spoilerText: String? = nil, + visibility: Status.Visibility? = nil, + language: String? = nil) -> Request { return Request(method: .post, path: "/api/v1/statuses", body: .parameters([ "status" => text, "content_type" => contentType.mimeType, @@ -304,13 +304,13 @@ public class Client { } // MARK: - Timelines - public func getStatuses(timeline: Timeline, range: RequestRange = .default) -> Request<[Status]> { + public static func getStatuses(timeline: Timeline, range: RequestRange = .default) -> Request<[Status]> { return timeline.request(range: range) } // MARK: Bookmarks - public func getBookmarks(range: RequestRange = .default) -> Request<[Status]> { + public static func getBookmarks(range: RequestRange = .default) -> Request<[Status]> { var request = Request<[Status]>(method: .get, path: "/api/v1/bookmarks") request.range = range return request diff --git a/Tusker/Activities/Account Activities/FollowAccountActivity.swift b/Tusker/Activities/Account Activities/FollowAccountActivity.swift index 6747ec0f..f0dc74f2 100644 --- a/Tusker/Activities/Account Activities/FollowAccountActivity.swift +++ b/Tusker/Activities/Account Activities/FollowAccountActivity.swift @@ -28,7 +28,7 @@ class FollowAccountActivity: AccountActivity { UIImpactFeedbackGenerator(style: .medium).impactOccurred() let request = Account.follow(account.id) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in if case let .success(relationship, _) = response { MastodonCache.add(relationship: relationship) } else { diff --git a/Tusker/Activities/Account Activities/UnfollowAccountActivity.swift b/Tusker/Activities/Account Activities/UnfollowAccountActivity.swift index 303042a5..94daf756 100644 --- a/Tusker/Activities/Account Activities/UnfollowAccountActivity.swift +++ b/Tusker/Activities/Account Activities/UnfollowAccountActivity.swift @@ -28,7 +28,7 @@ class UnfollowAccountActivity: AccountActivity { UIImpactFeedbackGenerator(style: .medium).impactOccurred() let request = Account.unfollow(account.id) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in if case let .success(relationship, _) = response { MastodonCache.add(relationship: relationship) } else { diff --git a/Tusker/Activities/Status Activities/BookmarkStatusActivity.swift b/Tusker/Activities/Status Activities/BookmarkStatusActivity.swift index b553af06..df1fcfc9 100644 --- a/Tusker/Activities/Status Activities/BookmarkStatusActivity.swift +++ b/Tusker/Activities/Status Activities/BookmarkStatusActivity.swift @@ -27,7 +27,7 @@ class BookmarkStatusActivity: StatusActivity { guard let status = status else { return } let request = Status.bookmark(status) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in if case let .success(status, _) = response { MastodonCache.add(status: status) } else { diff --git a/Tusker/Activities/Status Activities/PinStatusActivity.swift b/Tusker/Activities/Status Activities/PinStatusActivity.swift index 66c56c10..f73d0474 100644 --- a/Tusker/Activities/Status Activities/PinStatusActivity.swift +++ b/Tusker/Activities/Status Activities/PinStatusActivity.swift @@ -26,7 +26,7 @@ class PinStatusActivity: StatusActivity { guard let status = status else { return } let request = Status.pin(status) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in if case let .success(status, _) = response { MastodonCache.add(status: status) } else { diff --git a/Tusker/Activities/Status Activities/UnbookmarkStatusActivity.swift b/Tusker/Activities/Status Activities/UnbookmarkStatusActivity.swift index 0857348d..e88a3549 100644 --- a/Tusker/Activities/Status Activities/UnbookmarkStatusActivity.swift +++ b/Tusker/Activities/Status Activities/UnbookmarkStatusActivity.swift @@ -27,7 +27,7 @@ class UnbookmarkStatusActivity: StatusActivity { guard let status = status else { return } let request = Status.unbookmark(status) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in if case let .success(status, _) = response { MastodonCache.add(status: status) } else { diff --git a/Tusker/Activities/Status Activities/UnpinStatusActivity.swift b/Tusker/Activities/Status Activities/UnpinStatusActivity.swift index 2fc2fa85..134e852d 100644 --- a/Tusker/Activities/Status Activities/UnpinStatusActivity.swift +++ b/Tusker/Activities/Status Activities/UnpinStatusActivity.swift @@ -26,7 +26,7 @@ class UnpinStatusActivity: StatusActivity { guard let status = status else { return } let request = Status.unpin(status) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in if case let .success(status, _) = response { MastodonCache.add(status: status) } else { diff --git a/Tusker/Controllers/MastodonController.swift b/Tusker/Controllers/MastodonController.swift index bf376e9b..6c841c49 100644 --- a/Tusker/Controllers/MastodonController.swift +++ b/Tusker/Controllers/MastodonController.swift @@ -11,11 +11,15 @@ import Pachyderm class MastodonController { - static var client: Client! + private static var client: Client! static var account: Account! static var instance: Instance! + static var accessToken: String? { + client?.accessToken + } + private init() {} static func createClient() { @@ -28,6 +32,10 @@ class MastodonController { client.accessToken = LocalData.shared.accessToken } + static func run(_ request: Request, completion: @escaping Client.Callback) { + client.run(request, completion: completion) + } + static func registerApp(completion: @escaping () -> Void) { guard LocalData.shared.clientID == nil, LocalData.shared.clientSecret == nil else { @@ -55,8 +63,8 @@ class MastodonController { if account != nil { completion?(account) } else { - let request = client.getSelfAccount() - client.run(request) { response in + let request = Client.getSelfAccount() + run(request) { response in guard case let .success(account, _) = response else { fatalError() } self.account = account MastodonCache.add(account: account) @@ -66,8 +74,8 @@ class MastodonController { } static func getOwnInstance() { - let request = client.getInstance() - client.run(request) { (response) in + let request = Client.getInstance() + run(request) { (response) in guard case let .success(instance, _) = response else { fatalError() } self.instance = instance } diff --git a/Tusker/MastodonCache.swift b/Tusker/MastodonCache.swift index 5d888f28..70b5482d 100644 --- a/Tusker/MastodonCache.swift +++ b/Tusker/MastodonCache.swift @@ -37,8 +37,8 @@ class MastodonCache { } static func status(for id: String, completion: @escaping (Status?) -> Void) { - let request = MastodonController.client.getStatus(id: id) - MastodonController.client.run(request) { response in + let request = Client.getStatus(id: id) + MastodonController.run(request) { response in guard case let .success(status, _) = response else { completion(nil) return @@ -67,8 +67,8 @@ class MastodonCache { } static func account(for id: String, completion: @escaping (Account?) -> Void) { - let request = MastodonController.client.getAccount(id: id) - MastodonController.client.run(request) { response in + let request = Client.getAccount(id: id) + MastodonController.run(request) { response in guard case let .success(account, _) = response else { completion(nil) return @@ -96,8 +96,8 @@ class MastodonCache { } static func relationship(for id: String, completion: @escaping (Relationship?) -> Void) { - let request = MastodonController.client.getRelationships(accounts: [id]) - MastodonController.client.run(request) { response in + let request = Client.getRelationships(accounts: [id]) + MastodonController.run(request) { response in guard case let .success(relationships, _) = response, let relationship = relationships.first else { completion(nil) diff --git a/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift b/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift index ec478fb9..142213af 100644 --- a/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift +++ b/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift @@ -44,8 +44,8 @@ class BookmarksTableViewController: EnhancedTableViewController { tableView.prefetchDataSource = self - let request = MastodonController.client.getBookmarks() - MastodonController.client.run(request) { (response) in + let request = Client.getBookmarks() + MastodonController.run(request) { (response) in guard case let .success(statuses, pagination) = response else { fatalError() } MastodonCache.addAll(statuses: statuses) self.statuses.append(contentsOf: statuses.map { ($0.id, .unknown) }) @@ -81,8 +81,8 @@ class BookmarksTableViewController: EnhancedTableViewController { return } - let request = MastodonController.client.getBookmarks(range: older) - MastodonController.client.run(request) { (response) in + let request = Client.getBookmarks(range: older) + MastodonController.run(request) { (response) in guard case let .success(newStatuses, pagination) = response else { fatalError() } self.older = pagination?.older MastodonCache.addAll(statuses: newStatuses) @@ -107,7 +107,7 @@ class BookmarksTableViewController: EnhancedTableViewController { let unbookmarkAction = UIContextualAction(style: .destructive, title: NSLocalizedString("Unbookmark", comment: "unbookmark action title")) { (action, view, completion) in let request = Status.unbookmark(status) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in guard case let .success(newStatus, _) = response else { fatalError() } MastodonCache.add(status: newStatus) self.statuses.remove(at: indexPath.row) @@ -131,7 +131,7 @@ class BookmarksTableViewController: EnhancedTableViewController { return [ UIAction(title: NSLocalizedString("Unbookmark", comment: "unbookmark action title"), image: UIImage(systemName: "bookmark.fill"), identifier: .init("unbookmark"), discoverabilityTitle: nil, attributes: [], state: .off, handler: { (_) in let request = Status.unbookmark(status) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in guard case let .success(newStatus, _) = response else { fatalError() } MastodonCache.add(status: newStatus) self.statuses.remove(at: indexPath.row) diff --git a/Tusker/Screens/Compose/ComposeViewController.swift b/Tusker/Screens/Compose/ComposeViewController.swift index 4caace12..6dbabc22 100644 --- a/Tusker/Screens/Compose/ComposeViewController.swift +++ b/Tusker/Screens/Compose/ComposeViewController.swift @@ -480,8 +480,8 @@ class ComposeViewController: UIViewController { compAttachment.getData { (data, mimeType) in self.postProgressView.step() - let request = MastodonController.client.upload(attachment: FormAttachment(mimeType: mimeType, data: data, fileName: "file"), description: description) - MastodonController.client.run(request) { (response) in + let request = Client.upload(attachment: FormAttachment(mimeType: mimeType, data: data, fileName: "file"), description: description) + MastodonController.run(request) { (response) in guard case let .success(attachment, _) = response else { fatalError() } attachments[index] = attachment @@ -499,7 +499,7 @@ class ComposeViewController: UIViewController { group.notify(queue: .main) { let attachments = attachments.compactMap { $0 } - let request = MastodonController.client.createStatus(text: text, + let request = Client.createStatus(text: text, contentType: Preferences.shared.statusContentType, inReplyTo: self.inReplyToID, media: attachments, @@ -507,7 +507,7 @@ class ComposeViewController: UIViewController { spoilerText: contentWarning, visibility: visibility, language: nil) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in guard case let .success(status, _) = response else { fatalError() } self.postedStatus = status MastodonCache.add(status: status) diff --git a/Tusker/Screens/Conversation/ConversationTableViewController.swift b/Tusker/Screens/Conversation/ConversationTableViewController.swift index 1807dcf2..92410f58 100644 --- a/Tusker/Screens/Conversation/ConversationTableViewController.swift +++ b/Tusker/Screens/Conversation/ConversationTableViewController.swift @@ -58,7 +58,7 @@ class ConversationTableViewController: EnhancedTableViewController { guard let mainStatus = MastodonCache.status(for: mainStatusID) else { fatalError("Missing cached status \(mainStatusID)") } let request = Status.getContext(mainStatus) - MastodonController.client.run(request) { response in + MastodonController.run(request) { response in guard case let .success(context, _) = response else { fatalError() } let parents = self.getDirectParents(of: mainStatus, from: context.ancestors) MastodonCache.addAll(statuses: parents) diff --git a/Tusker/Screens/Explore/ExploreViewController.swift b/Tusker/Screens/Explore/ExploreViewController.swift index 02d487e7..a7f6fc12 100644 --- a/Tusker/Screens/Explore/ExploreViewController.swift +++ b/Tusker/Screens/Explore/ExploreViewController.swift @@ -106,8 +106,8 @@ class ExploreViewController: EnhancedTableViewController { } func reloadLists() { - let request = MastodonController.client.getLists() - MastodonController.client.run(request) { (response) in + let request = Client.getLists() + MastodonController.run(request) { (response) in guard case let .success(lists, _) = response else { fatalError() } @@ -143,7 +143,7 @@ class ExploreViewController: EnhancedTableViewController { alert.addAction(UIAlertAction(title: NSLocalizedString("Delete List", comment: "delete list alert confirm button"), style: .destructive, handler: { (_) in let request = List.delete(list) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in guard case .success(_, _) = response else { fatalError() } @@ -189,8 +189,8 @@ class ExploreViewController: EnhancedTableViewController { fatalError() } - let request = MastodonController.client.createList(title: title) - MastodonController.client.run(request) { (response) in + let request = Client.createList(title: title) + MastodonController.run(request) { (response) in guard case let .success(list, _) = response else { fatalError() } self.reloadLists() diff --git a/Tusker/Screens/Lists/EditListAccountsViewController.swift b/Tusker/Screens/Lists/EditListAccountsViewController.swift index ff7e2c1d..7484d1dd 100644 --- a/Tusker/Screens/Lists/EditListAccountsViewController.swift +++ b/Tusker/Screens/Lists/EditListAccountsViewController.swift @@ -70,7 +70,7 @@ class EditListAccountsViewController: EnhancedTableViewController { func loadAccounts() { let request = List.getAccounts(list) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in guard case let .success(accounts, pagination) = response else { fatalError() } @@ -109,7 +109,7 @@ class EditListAccountsViewController: EnhancedTableViewController { fatalError() } let request = List.update(self.list, title: text) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in guard case .success(_, _) = response else { fatalError() } @@ -143,7 +143,7 @@ extension EditListAccountsViewController { } let request = List.remove(editListAccountsController!.list, accounts: [id]) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in guard case .success(_, _) = response else { fatalError() } @@ -157,7 +157,7 @@ extension EditListAccountsViewController { extension EditListAccountsViewController: SearchResultsViewControllerDelegate { func selectedSearchResult(account accountID: String) { let request = List.add(list, accounts: [accountID]) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in guard case .success(_, _) = response else { fatalError() } diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index c68ac440..ae6ccf35 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -56,8 +56,8 @@ class NotificationsTableViewController: EnhancedTableViewController { tableView.prefetchDataSource = self - let request = MastodonController.client.getNotifications(excludeTypes: excludedTypes) - MastodonController.client.run(request) { result in + let request = Client.getNotifications(excludeTypes: excludedTypes) + MastodonController.run(request) { result in guard case let .success(notifications, pagination) = result else { fatalError() } let groups = NotificationGroup.createGroups(notifications: notifications, only: self.groupTypes) @@ -124,8 +124,8 @@ class NotificationsTableViewController: EnhancedTableViewController { if indexPath.row == groups.count - 1 { guard let older = older else { return } - let request = MastodonController.client.getNotifications(excludeTypes: excludedTypes, range: older) - MastodonController.client.run(request) { result in + let request = Client.getNotifications(excludeTypes: excludedTypes, range: older) + MastodonController.run(request) { result in guard case let .success(newNotifications, pagination) = result else { fatalError() } let groups = NotificationGroup.createGroups(notifications: newNotifications, only: self.groupTypes) @@ -182,7 +182,7 @@ class NotificationsTableViewController: EnhancedTableViewController { .map(Pachyderm.Notification.dismiss(id:)) .forEach { (request) in group.enter() - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in group.leave() } } @@ -196,8 +196,8 @@ class NotificationsTableViewController: EnhancedTableViewController { @objc func refreshNotifications(_ sender: Any) { guard let newer = newer else { return } - let request = MastodonController.client.getNotifications(excludeTypes: excludedTypes, range: newer) - MastodonController.client.run(request) { result in + let request = Client.getNotifications(excludeTypes: excludedTypes, range: newer) + MastodonController.run(request) { result in guard case let .success(newNotifications, pagination) = result else { fatalError() } let groups = NotificationGroup.createGroups(notifications: newNotifications, only: self.groupTypes) diff --git a/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift b/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift index 9d48ca6f..8cad403c 100644 --- a/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift +++ b/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift @@ -107,7 +107,7 @@ class InstanceSelectorTableViewController: UITableViewController { let components = parseURLComponents(input: domain) let client = Client(baseURL: components.url!) - let request = client.getInstance() + let request = Client.getInstance() client.run(request) { (response) in var snapshot = self.dataSource.snapshot() snapshot.deleteItems(snapshot.itemIdentifiers(inSection: .selected)) diff --git a/Tusker/Screens/Profile/ProfileTableViewController.swift b/Tusker/Screens/Profile/ProfileTableViewController.swift index ecf12bfd..45b520e0 100644 --- a/Tusker/Screens/Profile/ProfileTableViewController.swift +++ b/Tusker/Screens/Profile/ProfileTableViewController.swift @@ -130,7 +130,7 @@ class ProfileTableViewController: EnhancedTableViewController { func getStatuses(for range: RequestRange = .default, onlyPinned: Bool = false, completion: @escaping Client.Callback<[Status]>) { let request = Account.getStatuses(accountID, range: range, onlyMedia: false, pinned: onlyPinned, excludeReplies: !Preferences.shared.showRepliesInProfiles) - MastodonController.client.run(request, completion: completion) + MastodonController.run(request, completion: completion) } func sendMessageMentioning() { diff --git a/Tusker/Screens/Search/SearchResultsViewController.swift b/Tusker/Screens/Search/SearchResultsViewController.swift index e1388f42..ad77eed3 100644 --- a/Tusker/Screens/Search/SearchResultsViewController.swift +++ b/Tusker/Screens/Search/SearchResultsViewController.swift @@ -117,8 +117,8 @@ class SearchResultsViewController: EnhancedTableViewController { activityIndicator.startAnimating() } - let request = MastodonController.client.search(query: query, resolve: true, limit: 10) - MastodonController.client.run(request) { (response) in + let request = Client.search(query: query, resolve: true, limit: 10) + MastodonController.run(request) { (response) in guard case let .success(results, _) = response else { fatalError() } DispatchQueue.main.async { diff --git a/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift b/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift index c5ccc6d2..9ccf9234 100644 --- a/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift +++ b/Tusker/Screens/Status Action Account List/StatusActionAccountListTableViewController.swift @@ -75,7 +75,7 @@ class StatusActionAccountListTableViewController: EnhancedTableViewController { tableView.tableFooterView = UIActivityIndicatorView(style: .large) let request = actionType == .favorite ? Status.getFavourites(status) : Status.getReblogs(status) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in guard case let .success(accounts, _) = response else { fatalError() } MastodonCache.addAll(accounts: accounts) DispatchQueue.main.async { diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index c5096da3..f38cacd2 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -56,13 +56,13 @@ class TimelineTableViewController: EnhancedTableViewController { tableView.prefetchDataSource = self - guard MastodonController.client?.accessToken != nil else { return } + guard MastodonController.accessToken != nil else { return } loadInitialStatuses() } func loadInitialStatuses() { - let request = MastodonController.client.getStatuses(timeline: timeline) - MastodonController.client.run(request) { response in + let request = Client.getStatuses(timeline: timeline) + MastodonController.run(request) { response in guard case let .success(statuses, pagination) = response else { fatalError() } MastodonCache.addAll(statuses: statuses) self.timelineSegments.insert(statuses.map { ($0.id, .unknown) }, at: 0) @@ -99,8 +99,8 @@ class TimelineTableViewController: EnhancedTableViewController { indexPath.row == timelineSegments[indexPath.section].count - 1 { guard let older = older else { return } - let request = MastodonController.client.getStatuses(timeline: timeline, range: older) - MastodonController.client.run(request) { response in + let request = Client.getStatuses(timeline: timeline, range: older) + MastodonController.run(request) { response in guard case let .success(newStatuses, pagination) = response else { fatalError() } self.older = pagination?.older MastodonCache.addAll(statuses: newStatuses) @@ -124,8 +124,8 @@ class TimelineTableViewController: EnhancedTableViewController { @objc func refreshStatuses(_ sender: Any) { guard let newer = newer else { return } - let request = MastodonController.client.getStatuses(timeline: timeline, range: newer) - MastodonController.client.run(request) { response in + let request = Client.getStatuses(timeline: timeline, range: newer) + MastodonController.run(request) { response in guard case let .success(newStatuses, pagination) = response else { fatalError() } self.newer = pagination?.newer MastodonCache.addAll(statuses: newStatuses) diff --git a/Tusker/Views/Notifications/FollowRequestNotificationTableViewCell.swift b/Tusker/Views/Notifications/FollowRequestNotificationTableViewCell.swift index d2587f43..8a5ca23f 100644 --- a/Tusker/Views/Notifications/FollowRequestNotificationTableViewCell.swift +++ b/Tusker/Views/Notifications/FollowRequestNotificationTableViewCell.swift @@ -89,7 +89,7 @@ class FollowRequestNotificationTableViewCell: UITableViewCell { @IBAction func rejectButtonPressed() { let request = Account.rejectFollowRequest(account) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in guard case let .success(relationship, _) = response else { fatalError() } MastodonCache.add(relationship: relationship) DispatchQueue.main.async { @@ -106,7 +106,7 @@ class FollowRequestNotificationTableViewCell: UITableViewCell { @IBAction func acceptButtonPressed() { let request = Account.authorizeFollowRequest(account) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in guard case let .success(relationship, _) = response else { fatalError() } MastodonCache.add(relationship: relationship) DispatchQueue.main.async { diff --git a/Tusker/Views/Status/BaseStatusTableViewCell.swift b/Tusker/Views/Status/BaseStatusTableViewCell.swift index f17fe0ac..f6b062e1 100644 --- a/Tusker/Views/Status/BaseStatusTableViewCell.swift +++ b/Tusker/Views/Status/BaseStatusTableViewCell.swift @@ -247,7 +247,7 @@ class BaseStatusTableViewCell: UITableViewCell { let realStatus: Status = status.reblog ?? status let request = (favorited ? Status.favourite : Status.unfavourite)(realStatus) - MastodonController.client.run(request) { response in + MastodonController.run(request) { response in DispatchQueue.main.async { if case let .success(newStatus, _) = response { self.favorited = newStatus.favourited ?? false @@ -272,7 +272,7 @@ class BaseStatusTableViewCell: UITableViewCell { let realStatus: Status = status.reblog ?? status let request = (reblogged ? Status.reblog : Status.unreblog)(realStatus) - MastodonController.client.run(request) { response in + MastodonController.run(request) { response in DispatchQueue.main.async { if case let .success(newStatus, _) = response { self.reblogged = newStatus.reblogged ?? false diff --git a/Tusker/Views/Status/TimelineStatusTableViewCell.swift b/Tusker/Views/Status/TimelineStatusTableViewCell.swift index 15be357b..a80e4254 100644 --- a/Tusker/Views/Status/TimelineStatusTableViewCell.swift +++ b/Tusker/Views/Status/TimelineStatusTableViewCell.swift @@ -158,7 +158,7 @@ extension TimelineStatusTableViewCell: TableViewSwipeActionProvider { favoriteColor = UIColor(displayP3Red: 1, green: 204/255, blue: 0, alpha: 1) } let favorite = UIContextualAction(style: .normal, title: favoriteTitle) { (action, view, completion) in - MastodonController.client.run(favoriteRequest, completion: { response in + MastodonController.run(favoriteRequest, completion: { response in DispatchQueue.main.async { guard case let .success(status, _) = response else { completion(false) @@ -185,7 +185,7 @@ extension TimelineStatusTableViewCell: TableViewSwipeActionProvider { reblogColor = tintColor } let reblog = UIContextualAction(style: .normal, title: reblogTitle) { (action, view, completion) in - MastodonController.client.run(reblogRequest, completion: { response in + MastodonController.run(reblogRequest, completion: { response in DispatchQueue.main.async { guard case let .success(status, _) = response else { completion(false) diff --git a/Tusker/XCallbackURL/XCBActions.swift b/Tusker/XCallbackURL/XCBActions.swift index 15b42eab..d90d1920 100644 --- a/Tusker/XCallbackURL/XCBActions.swift +++ b/Tusker/XCallbackURL/XCBActions.swift @@ -41,8 +41,8 @@ struct XCBActions { } } } else if let searchQuery = request.arguments["statusURL"] { - let request = MastodonController.client.search(query: searchQuery) - MastodonController.client.run(request) { (response) in + let request = Client.search(query: searchQuery) + MastodonController.run(request) { (response) in if case let .success(results, _) = response, let status = results.statuses.first { MastodonCache.add(status: status) @@ -72,8 +72,8 @@ struct XCBActions { } } } else if let searchQuery = request.arguments["accountURL"] { - let request = MastodonController.client.search(query: searchQuery) - MastodonController.client.run(request) { (response) in + let request = Client.search(query: searchQuery) + MastodonController.run(request) { (response) in if case let .success(results, _) = response { if let account = results.accounts.first { MastodonCache.add(account: account) @@ -90,8 +90,8 @@ struct XCBActions { } } } else if let acct = request.arguments["acct"] { - let request = MastodonController.client.searchForAccount(query: acct) - MastodonController.client.run(request) { (response) in + let request = Client.searchForAccount(query: acct) + MastodonController.run(request) { (response) in if case let .success(accounts, _) = response { if let account = accounts.first { MastodonCache.add(account: account) @@ -138,8 +138,8 @@ struct XCBActions { ]) return } - let request = MastodonController.client.createStatus(text: status, visibility: Preferences.shared.defaultPostVisibility) - MastodonController.client.run(request) { response in + let request = Client.createStatus(text: status, visibility: Preferences.shared.defaultPostVisibility) + MastodonController.run(request) { response in if case let .success(status, _) = response { session.complete(with: .success, additionalData: [ "statusURL": status.url?.absoluteString, @@ -199,7 +199,7 @@ struct XCBActions { static func statusAction(request: @escaping (Status) -> Request, alertTitle: String, _ url: XCBRequest, _ session: XCBSession, _ silent: Bool?) { func performAction(status: Status, completion: ((Status) -> Void)?) { - MastodonController.client.run(request(status)) { (response) in + MastodonController.run(request(status)) { (response) in if case let .success(status, _) = response { MastodonCache.add(status: status) completion?(status) @@ -285,7 +285,7 @@ struct XCBActions { static func followUser(_ request: XCBRequest, _ session: XCBSession, _ silent: Bool?) { func performAction(_ account: Account) { let request = Account.follow(account.id) - MastodonController.client.run(request) { (response) in + MastodonController.run(request) { (response) in if case let .success(relationship, _) = response { MastodonCache.add(relationship: relationship) session.complete(with: .success, additionalData: [