diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+Donation.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+Donation.swift index 93e8abfc7..39b8c1418 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+Donation.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+Donation.swift @@ -3,24 +3,20 @@ import Combine import Foundation import MastodonSDK +import MastodonCore extension HomeTimelineViewModel { func askForDonationIfPossible() async { - var userAuthentication = authContext.mastodonAuthenticationBox + let userAuthentication = authContext.mastodonAuthenticationBox .authentication - var accountCreatedAt = userAuthentication.accountCreatedAt - if accountCreatedAt == nil { - do { - let updated = try await AuthenticationViewModel.verifyAndSaveAuthentication(context: context, domain: userAuthentication.domain, clientID: userAuthentication.clientID, clientSecret: userAuthentication.clientSecret, userToken: userAuthentication.userAccessToken - ) - accountCreatedAt = updated.createdAt - } catch { - return - } + guard let accountCreatedAt = userAuthentication.accountCreatedAt else { + let updated = try? await context.apiService.accountVerifyCredentials(domain: userAuthentication.domain, authorization: authContext.mastodonAuthenticationBox.userAuthorization) + guard let accountCreatedAt = updated?.createdAt else { return } + AuthenticationServiceProvider.shared.updateAccountCreatedAt(accountCreatedAt, forAuthentication: userAuthentication) + return } - guard let accountCreatedAt = accountCreatedAt else { return } guard Mastodon.Entity.DonationCampaign.isEligibleForDonationsBanner( domain: userAuthentication.domain, diff --git a/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift b/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift index 8e2068973..305d2fdc8 100644 --- a/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift +++ b/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift @@ -100,6 +100,15 @@ public extension AuthenticationServiceProvider { return try? JSONDecoder().decode(MastodonAuthentication.self, from: data) } } + + func updateAccountCreatedAt(_ newCreatedAt: Date, forAuthentication outdated: MastodonAuthentication) { + authentications = authentications.map { authentication in + guard authentication == outdated else { + return authentication + } + return outdated.updating(accountCreatedAt: newCreatedAt) + } + } func migrateLegacyAuthentications(in context: NSManagedObjectContext) { do { diff --git a/MastodonSDK/Sources/MastodonCore/MastodonAuthentication.swift b/MastodonSDK/Sources/MastodonCore/MastodonAuthentication.swift index 6acd333f4..d8fd5a81f 100644 --- a/MastodonSDK/Sources/MastodonCore/MastodonAuthentication.swift +++ b/MastodonSDK/Sources/MastodonCore/MastodonAuthentication.swift @@ -115,6 +115,7 @@ public struct MastodonAuthentication: Codable, Hashable, UserIdentifier { createdAt: Date? = nil, updatedAt: Date? = nil, activedAt: Date? = nil, + accountCreatedAt: Date? = nil, userID: String? = nil, instanceConfiguration: InstanceConfiguration? = nil ) -> Self { @@ -131,7 +132,7 @@ public struct MastodonAuthentication: Codable, Hashable, UserIdentifier { activedAt: activedAt ?? self.activedAt, userID: userID ?? self.userID, instanceConfiguration: instanceConfiguration ?? self.instanceConfiguration, - accountCreatedAt: self.accountCreatedAt + accountCreatedAt: accountCreatedAt ?? self.accountCreatedAt ) } @@ -180,6 +181,10 @@ public struct MastodonAuthentication: Codable, Hashable, UserIdentifier { func updating(activatedAt: Date) -> Self { copy(activedAt: activatedAt) } + + func updating(accountCreatedAt: Date) -> Self { + copy(accountCreatedAt: accountCreatedAt) + } var authorization: Mastodon.API.OAuth.Authorization { .init(accessToken: userAccessToken)