From 736c3b515fbf45c88e5b116582207fa798e7b7a8 Mon Sep 17 00:00:00 2001 From: Marcus Kida Date: Tue, 7 Feb 2023 12:36:13 +0100 Subject: [PATCH] fix(Widget): User lookup for widget does not yield correct result --- .../Handler/FollowersCountIntentHandler.swift | 11 +---------- .../Handler/MultiFollowersCountIntentHandler.swift | 2 +- .../MastodonSDK/Entity/Mastodon+Entity+Account.swift | 11 +++++++++-- .../FollowersCount/FollowersCountWidget.swift | 4 ++-- .../MultiFollowersCountWidget.swift | 4 ++-- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/MastodonIntent/Handler/FollowersCountIntentHandler.swift b/MastodonIntent/Handler/FollowersCountIntentHandler.swift index 170508325..fa5b53bc2 100644 --- a/MastodonIntent/Handler/FollowersCountIntentHandler.swift +++ b/MastodonIntent/Handler/FollowersCountIntentHandler.swift @@ -30,15 +30,6 @@ class FollowersCountIntentHandler: INExtension, FollowersCountIntentHandling { .apiService .search(query: .init(q: searchTerm), authenticationBox: authenticationBox) - return INObjectCollection(items: results.value.accounts.map { $0.acctWithDomain(localDomain: authenticationBox.domain) as NSString }) - } -} - -extension Mastodon.Entity.Account { - func acctWithDomain(localDomain: String) -> String { - guard acct.contains("@") else { - return "\(acct)@\(localDomain)" - } - return acct + return INObjectCollection(items: results.value.accounts.map { $0.acctWithDomainIfMissing(authenticationBox.domain) as NSString }) } } diff --git a/MastodonIntent/Handler/MultiFollowersCountIntentHandler.swift b/MastodonIntent/Handler/MultiFollowersCountIntentHandler.swift index 785949e2d..f49398630 100644 --- a/MastodonIntent/Handler/MultiFollowersCountIntentHandler.swift +++ b/MastodonIntent/Handler/MultiFollowersCountIntentHandler.swift @@ -22,6 +22,6 @@ class MultiFollowersCountIntentHandler: INExtension, MultiFollowersCountIntentHa .apiService .search(query: .init(q: searchTerm), authenticationBox: authenticationBox) - return INObjectCollection(items: results.value.accounts.map { $0.acctWithDomain(localDomain: authenticationBox.domain) as NSString }) + return INObjectCollection(items: results.value.accounts.map { $0.acctWithDomainIfMissing(authenticationBox.domain) as NSString }) } } diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Account.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Account.swift index 13f3c0a71..0e90a5ccd 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Account.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Account.swift @@ -82,6 +82,13 @@ extension Mastodon.Entity { case muteExpiresAt = "mute_expires_at" } } - - +} + +extension Mastodon.Entity.Account { + public func acctWithDomainIfMissing(_ localDomain: String) -> String { + guard acct.contains("@") else { + return "\(acct)@\(localDomain)" + } + return acct + } } diff --git a/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift b/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift index d490d2084..c8e53fb1c 100644 --- a/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift +++ b/WidgetExtension/Variants/FollowersCount/FollowersCountWidget.swift @@ -88,7 +88,7 @@ private extension FollowersCountWidgetProvider { guard let desiredAccount = configuration.account ?? authBox.authenticationRecord.object( in: WidgetExtension.appContext.managedObjectContext - )?.user.acct + )?.user.acctWithDomain else { return completion(.unconfigured) } @@ -99,7 +99,7 @@ private extension FollowersCountWidgetProvider { .search(query: .init(q: desiredAccount, type: .accounts), authenticationBox: authBox) .value .accounts - .first(where: { $0.acct == desiredAccount }) + .first(where: { $0.acctWithDomainIfMissing(authBox.domain) == desiredAccount }) else { return completion(.unconfigured) } diff --git a/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift b/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift index b53c9af6c..9049a5a79 100644 --- a/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift +++ b/WidgetExtension/Variants/MultiFollowersCount/MultiFollowersCountWidget.swift @@ -88,7 +88,7 @@ private extension MultiFollowersCountWidgetProvider { desiredAccounts = configuredAccounts } else if let currentlyLoggedInAccount = authBox.authenticationRecord.object( in: WidgetExtension.appContext.managedObjectContext - )?.user.acct { + )?.user.acctWithDomain { desiredAccounts = [currentlyLoggedInAccount] } else { return completion(.unconfigured) @@ -103,7 +103,7 @@ private extension MultiFollowersCountWidgetProvider { .search(query: .init(q: desiredAccount, type: .accounts), authenticationBox: authBox) .value .accounts - .first(where: { $0.acct == desiredAccount }) + .first(where: { $0.acctWithDomainIfMissing(authBox.domain) == desiredAccount }) else { continue }