From 405b175bdf6418698db9cb2d7e0c3bc391ee2681 Mon Sep 17 00:00:00 2001 From: Marcus Kida Date: Wed, 27 Dec 2023 14:46:31 +0100 Subject: [PATCH] Implement alternative Author for DataSourceFacade.MenuContext (IOS-176) --- .../Provider/DataSourceFacade+Follow.swift | 9 +++++ .../Provider/DataSourceFacade+Status.swift | 3 +- ...er+NotificationTableViewCellDelegate.swift | 1 + ...Provider+StatusTableViewCellDelegate.swift | 5 ++- .../Scene/Profile/ProfileViewController.swift | 1 + .../Service/API/APIService+Follow.swift | 34 +++++++++++++++++++ 6 files changed, 51 insertions(+), 2 deletions(-) diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift index 2fc7e6c80..e3445115d 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift @@ -126,4 +126,13 @@ extension DataSourceFacade { for: user, authenticationBox: dependency.authContext.mastodonAuthenticationBox) } + + static func responseToShowHideReblogAction( + dependency: NeedsDependency & AuthContextProvider, + user: Mastodon.Entity.Account + ) async throws { + _ = try await dependency.context.apiService.toggleShowReblogs( + for: user, + authenticationBox: dependency.authContext.mastodonAuthenticationBox) + } } diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Status.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Status.swift index df8f634c6..26e11d026 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Status.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Status.swift @@ -144,7 +144,8 @@ extension DataSourceFacade { extension DataSourceFacade { struct MenuContext { - let author: ManagedObjectRecord? + let author: ManagedObjectRecord? // todo: Remove once IOS-192 is ready + let authorEntity: Mastodon.Entity.Account? let statusViewModel: StatusView.ViewModel? let button: UIButton? let barButtonItem: UIBarButtonItem? diff --git a/Mastodon/Protocol/Provider/DataSourceProvider+NotificationTableViewCellDelegate.swift b/Mastodon/Protocol/Provider/DataSourceProvider+NotificationTableViewCellDelegate.swift index 8d8e62bd9..546b5e974 100644 --- a/Mastodon/Protocol/Provider/DataSourceProvider+NotificationTableViewCellDelegate.swift +++ b/Mastodon/Protocol/Provider/DataSourceProvider+NotificationTableViewCellDelegate.swift @@ -44,6 +44,7 @@ extension NotificationTableViewCellDelegate where Self: DataSourceProvider & Aut action: action, menuContext: .init( author: author, + authorEntity: notification.entity.account, statusViewModel: nil, button: button, barButtonItem: nil diff --git a/Mastodon/Protocol/Provider/DataSourceProvider+StatusTableViewCellDelegate.swift b/Mastodon/Protocol/Provider/DataSourceProvider+StatusTableViewCellDelegate.swift index c0a6c2381..d380c4f36 100644 --- a/Mastodon/Protocol/Provider/DataSourceProvider+StatusTableViewCellDelegate.swift +++ b/Mastodon/Protocol/Provider/DataSourceProvider+StatusTableViewCellDelegate.swift @@ -471,8 +471,10 @@ extension StatusTableViewCellDelegate where Self: DataSourceProvider & AuthConte assertionFailure("only works for status data provider") return } + + let status = _status.reblog ?? _status + let _author: ManagedObjectRecord? = try await self.context.managedObjectContext.perform { - let status = _status.reblog ?? _status let request = MastodonUser.sortedFetchRequest request.predicate = MastodonUser.predicate(domain: self.authContext.mastodonAuthenticationBox.domain, id: status.entity.account.id) request.fetchLimit = 1 @@ -518,6 +520,7 @@ extension StatusTableViewCellDelegate where Self: DataSourceProvider & AuthConte action: action, menuContext: .init( author: author, + authorEntity: status.entity.account, statusViewModel: statusViewModel, button: button, barButtonItem: nil diff --git a/Mastodon/Scene/Profile/ProfileViewController.swift b/Mastodon/Scene/Profile/ProfileViewController.swift index 8ece9ac2b..21ebedee2 100644 --- a/Mastodon/Scene/Profile/ProfileViewController.swift +++ b/Mastodon/Scene/Profile/ProfileViewController.swift @@ -895,6 +895,7 @@ extension ProfileViewController: MastodonMenuDelegate { action: action, menuContext: DataSourceFacade.MenuContext( author: userRecord, + authorEntity: nil, statusViewModel: nil, button: nil, barButtonItem: self.moreMenuBarButtonItem diff --git a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Follow.swift b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Follow.swift index e31dbedce..b3a046bad 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Follow.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+Follow.swift @@ -195,4 +195,38 @@ extension APIService { return try result.get() } + + public func toggleShowReblogs( + for user: Mastodon.Entity.Account, + authenticationBox: MastodonAuthenticationBox + ) async throws -> Mastodon.Response.Content { + + let result: Result, Error> + + let relationship = try await Mastodon.API.Account.relationships( + session: session, + domain: authenticationBox.domain, + query: .init(ids: [user.id]), + authorization: authenticationBox.userAuthorization + ).singleOutput().value.first + + let oldShowReblogs = relationship?.showingReblogs == true + let newShowReblogs = (oldShowReblogs == false) + + do { + let response = try await Mastodon.API.Account.follow( + session: session, + domain: authenticationBox.domain, + accountID: user.id, + followQueryType: .follow(query: .init(reblogs: newShowReblogs)), + authorization: authenticationBox.userAuthorization + ).singleOutput() + + result = .success(response) + } catch { + result = .failure(error) + } + + return try result.get() + } }