Update accountCreatedAt date if missing

Contributes to iOS-252
This commit is contained in:
shannon 2024-11-12 15:41:25 -05:00
parent b1a4571c53
commit 60f7f91114
3 changed files with 22 additions and 12 deletions

View File

@ -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,

View File

@ -101,6 +101,15 @@ public extension AuthenticationServiceProvider {
}
}
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 {
let legacyAuthentications = try context.fetch(MastodonAuthenticationLegacy.sortedFetchRequest)

View File

@ -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
)
}
@ -181,6 +182,10 @@ public struct MastodonAuthentication: Codable, Hashable, UserIdentifier {
copy(activedAt: activatedAt)
}
func updating(accountCreatedAt: Date) -> Self {
copy(accountCreatedAt: accountCreatedAt)
}
var authorization: Mastodon.API.OAuth.Authorization {
.init(accessToken: userAccessToken)
}