Remove MastodonCache usgae from XCBActions

This commit is contained in:
Shadowfacts 2020-05-06 23:02:12 -04:00
parent ebbfc7a132
commit 1e41c8fa17
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
4 changed files with 48 additions and 47 deletions

View File

@ -41,27 +41,30 @@ class MastodonCachePersistentStore: NSPersistentContainer {
} }
} }
private func upsert(status: Status, incrementReferenceCount: Bool) { @discardableResult
private func upsert(status: Status, incrementReferenceCount: Bool) -> StatusMO {
if let statusMO = self.status(for: status.id, in: self.backgroundContext) { if let statusMO = self.status(for: status.id, in: self.backgroundContext) {
statusMO.updateFrom(apiStatus: status, container: self) statusMO.updateFrom(apiStatus: status, container: self)
if incrementReferenceCount { if incrementReferenceCount {
statusMO.incrementReferenceCount() statusMO.incrementReferenceCount()
} }
return statusMO
} else { } else {
let statusMO = StatusMO(apiStatus: status, container: self, context: self.backgroundContext) let statusMO = StatusMO(apiStatus: status, container: self, context: self.backgroundContext)
if incrementReferenceCount { if incrementReferenceCount {
statusMO.incrementReferenceCount() statusMO.incrementReferenceCount()
} }
return statusMO
} }
} }
func addOrUpdate(status: Status, incrementReferenceCount: Bool, completion: (() -> Void)? = nil) { func addOrUpdate(status: Status, incrementReferenceCount: Bool, completion: ((StatusMO) -> Void)? = nil) {
backgroundContext.perform { backgroundContext.perform {
self.upsert(status: status, incrementReferenceCount: incrementReferenceCount) let statusMO = self.upsert(status: status, incrementReferenceCount: incrementReferenceCount)
if self.backgroundContext.hasChanges { if self.backgroundContext.hasChanges {
try! self.backgroundContext.save() try! self.backgroundContext.save()
} }
completion?() completion?(statusMO)
self.statusSubject.send(status.id) self.statusSubject.send(status.id)
} }
} }
@ -89,28 +92,30 @@ class MastodonCachePersistentStore: NSPersistentContainer {
} }
} }
private func upsert(account: Account) { @discardableResult
private func upsert(account: Account) -> AccountMO {
if let accountMO = self.account(for: account.id, in: self.backgroundContext) { if let accountMO = self.account(for: account.id, in: self.backgroundContext) {
accountMO.updateFrom(apiAccount: account, container: self) accountMO.updateFrom(apiAccount: account, container: self)
return accountMO
} else { } else {
_ = AccountMO(apiAccount: account, container: self, context: self.backgroundContext) return AccountMO(apiAccount: account, container: self, context: self.backgroundContext)
} }
} }
func addOrUpdate(account: Account, completion: (() -> Void)? = nil) { func addOrUpdate(account: Account, completion: ((AccountMO) -> Void)? = nil) {
backgroundContext.perform { backgroundContext.perform {
self.upsert(account: account) let accountMO = self.upsert(account: account)
if self.backgroundContext.hasChanges { if self.backgroundContext.hasChanges {
try! self.backgroundContext.save() try! self.backgroundContext.save()
} }
completion?() completion?(accountMO)
self.accountSubject.send(account.id) self.accountSubject.send(account.id)
} }
} }
func addAll(accounts: [Account], completion: (() -> Void)? = nil) { func addAll(accounts: [Account], completion: (() -> Void)? = nil) {
backgroundContext.perform { backgroundContext.perform {
accounts.forEach(self.upsert(account:)) accounts.forEach { self.upsert(account: $0) }
if self.backgroundContext.hasChanges { if self.backgroundContext.hasChanges {
try! self.backgroundContext.save() try! self.backgroundContext.save()
} }

View File

@ -173,8 +173,7 @@ class ComposeViewController: UIViewController {
let request = Client.getStatus(id: inReplyToID) let request = Client.getStatus(id: inReplyToID)
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
guard case let .success(status, _) = response else { return } guard case let .success(status, _) = response else { return }
self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: true) { self.mastodonController.persistentContainer.addOrUpdate(status: status, incrementReferenceCount: true) { (status) in
guard let status = self.mastodonController.persistentContainer.status(for: inReplyToID) else { return }
DispatchQueue.main.async { DispatchQueue.main.async {
self.updateInReplyTo(inReplyTo: status) self.updateInReplyTo(inReplyTo: status)
loadingVC.removeViewAndController() loadingVC.removeViewAndController()

View File

@ -90,7 +90,7 @@ class ProfileTableViewController: EnhancedTableViewController {
} }
return return
} }
self.mastodonController.persistentContainer.addOrUpdate(account: account) { self.mastodonController.persistentContainer.addOrUpdate(account: account) { (_) in
DispatchQueue.main.async { DispatchQueue.main.async {
self.updateAccountUI() self.updateAccountUI()
self.tableView.reloadData() self.tableView.reloadData()

View File

@ -38,62 +38,62 @@ struct XCBActions {
private static func getStatus(from request: XCBRequest, session: XCBSession, completion: @escaping (Status) -> Void) { private static func getStatus(from request: XCBRequest, session: XCBSession, completion: @escaping (Status) -> Void) {
if let id = request.arguments["statusID"] { if let id = request.arguments["statusID"] {
mastodonController.cache.status(for: id) { (status) in let request = Client.getStatus(id: id)
if let status = status { mastodonController.run(request) { (response) in
completion(status) guard case let .success(status, _) = response else {
} else {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": "Could not get status with ID \(id)" "error": "Could not get status with ID \(id)"
]) ])
return
} }
completion(status)
} }
} else if let searchQuery = request.arguments["statusURL"] { } else if let searchQuery = request.arguments["statusURL"] {
let request = Client.search(query: searchQuery) let request = Client.search(query: searchQuery)
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
if case let .success(results, _) = response, if case let .success(results, _) = response,
let status = results.statuses.first { let status = results.statuses.first {
mastodonController.cache.add(status: status)
completion(status) completion(status)
} else { } else {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": "Could not find status by searching '\(searchQuery)'" "error": "Could not find status by searching '\(searchQuery)'"
]) ])
} }
} }
} else { } else {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": "No status provided. Specify either instance-local statusID or remote statusURL." "error": "No status provided. Specify either instance-local statusID or remote statusURL."
]) ])
} }
} }
private static func getAccount(from request: XCBRequest, session: XCBSession, completion: @escaping (Account) -> Void) { private static func getAccount(from request: XCBRequest, session: XCBSession, completion: @escaping (Account) -> Void) {
if let id = request.arguments["accountID"] { if let id = request.arguments["accountID"] {
mastodonController.cache.account(for: id) { (account) in let request = Client.getAccount(id: id)
if let account = account { mastodonController.run(request) { (response) in
completion(account) guard case let .success(account, _) = response else {
} else {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": "Could not get account with ID \(id)" "error": "Could not get account with ID \(id)"
]) ])
return
} }
completion(account)
} }
} else if let searchQuery = request.arguments["accountURL"] { } else if let searchQuery = request.arguments["accountURL"] {
let request = Client.search(query: searchQuery) let request = Client.search(query: searchQuery)
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
if case let .success(results, _) = response { if case let .success(results, _) = response {
if let account = results.accounts.first { if let account = results.accounts.first {
mastodonController.cache.add(account: account)
completion(account) completion(account)
} else { } else {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": "Could not find account by searching '\(searchQuery)'" "error": "Could not find account by searching '\(searchQuery)'"
]) ])
} }
} else if case let .failure(error) = response { } else if case let .failure(error) = response {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": error.localizedDescription "error": error.localizedDescription
]) ])
} }
} }
} else if let acct = request.arguments["acct"] { } else if let acct = request.arguments["acct"] {
@ -101,23 +101,22 @@ struct XCBActions {
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
if case let .success(accounts, _) = response { if case let .success(accounts, _) = response {
if let account = accounts.first { if let account = accounts.first {
mastodonController.cache.add(account: account)
completion(account) completion(account)
} else { } else {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": "Could not find account \(acct)" "error": "Could not find account \(acct)"
]) ])
} }
} else if case let .failure(error) = response { } else if case let .failure(error) = response {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": error.localizedDescription "error": error.localizedDescription
]) ])
} }
} }
} else { } else {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": "No status provided. Specify either instance-local ID, account URL, or qualified username." "error": "No status provided. Specify either instance-local ID, account URL, or qualified username."
]) ])
} }
} }
@ -142,7 +141,7 @@ struct XCBActions {
guard CharacterCounter.count(text: status) <= mastodonController.instance.maxStatusCharacters ?? 500 else { guard CharacterCounter.count(text: status) <= mastodonController.instance.maxStatusCharacters ?? 500 else {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": "Too many characters. Instance maximum is \(mastodonController.instance.maxStatusCharacters ?? 500)" "error": "Too many characters. Instance maximum is \(mastodonController.instance.maxStatusCharacters ?? 500)"
]) ])
return return
} }
let request = Client.createStatus(text: status, visibility: Preferences.shared.defaultPostVisibility) let request = Client.createStatus(text: status, visibility: Preferences.shared.defaultPostVisibility)
@ -151,11 +150,11 @@ struct XCBActions {
session.complete(with: .success, additionalData: [ session.complete(with: .success, additionalData: [
"statusURL": status.url?.absoluteString, "statusURL": status.url?.absoluteString,
"statusURI": status.uri "statusURI": status.uri
]) ])
} else if case let .failure(error) = response { } else if case let .failure(error) = response {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": error.localizedDescription "error": error.localizedDescription
]) ])
} }
} }
} else { } else {
@ -179,7 +178,7 @@ struct XCBActions {
} catch { } catch {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": error.localizedDescription "error": error.localizedDescription
]) ])
return return
} }
} }
@ -192,7 +191,7 @@ struct XCBActions {
"posted": status.createdAt.timeIntervalSince1970.description, "posted": status.createdAt.timeIntervalSince1970.description,
"content": content, "content": content,
"reblog": status.reblog?.id "reblog": status.reblog?.id
]) ])
} }
} }
@ -208,16 +207,15 @@ struct XCBActions {
func performAction(status: Status, completion: ((Status) -> Void)?) { func performAction(status: Status, completion: ((Status) -> Void)?) {
mastodonController.run(request(status.id)) { (response) in mastodonController.run(request(status.id)) { (response) in
if case let .success(status, _) = response { if case let .success(status, _) = response {
mastodonController.cache.add(status: status)
completion?(status) completion?(status)
session.complete(with: .success, additionalData: [ session.complete(with: .success, additionalData: [
"statusURL": status.url?.absoluteString, "statusURL": status.url?.absoluteString,
"statusURI": status.uri "statusURI": status.uri
]) ])
} else if case let .failure(error) = response { } else if case let .failure(error) = response {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": error.localizedDescription "error": error.localizedDescription
]) ])
} }
} }
} }
@ -271,7 +269,7 @@ struct XCBActions {
"url": account.url.absoluteString, "url": account.url.absoluteString,
"avatarURL": account.avatar.absoluteString, "avatarURL": account.avatar.absoluteString,
"headerURL": account.header.absoluteString "headerURL": account.header.absoluteString
]) ])
} }
} }
@ -286,22 +284,21 @@ struct XCBActions {
"url": account.url.absoluteString, "url": account.url.absoluteString,
"avatarURL": account.avatar.absoluteString, "avatarURL": account.avatar.absoluteString,
"headerURL": account.header.absoluteString "headerURL": account.header.absoluteString
]) ])
} }
static func followUser(_ request: XCBRequest, _ 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.run(request) { (response) in mastodonController.run(request) { (response) in
if case let .success(relationship, _) = response { if case .success(_, _) = response {
mastodonController.cache.add(relationship: relationship)
session.complete(with: .success, additionalData: [ session.complete(with: .success, additionalData: [
"url": account.url.absoluteString "url": account.url.absoluteString
]) ])
} else if case let .failure(error) = response { } else if case let .failure(error) = response {
session.complete(with: .error, additionalData: [ session.complete(with: .error, additionalData: [
"error": error.localizedDescription "error": error.localizedDescription
]) ])
} }
} }
} }