Subscribe to a premium account from a standard linked account

This commit is contained in:
Thomas Ricouard 2024-06-16 10:12:06 +02:00
parent 4783422b3e
commit 76220bd930
6 changed files with 55 additions and 8 deletions

View file

@ -50,7 +50,11 @@ struct AccountDetailHeaderView: View {
if let latestEvent = watcher.latestEvent, let latestEvent = latestEvent as? StreamEventNotification {
if latestEvent.notification.account.id == viewModel.accountId {
Task {
try? await viewModel.followButtonViewModel?.refreshRelationship()
if viewModel.account?.isLinkedToPremiumAccount == true {
await viewModel.fetchAccount()
} else {
try? await viewModel.followButtonViewModel?.refreshRelationship()
}
}
}
}
@ -221,7 +225,7 @@ struct AccountDetailHeaderView: View {
.accessibilityRespondsToUserInteraction(false)
movedToView
joinedAtView
if viewModel.account?.isProAccount == true && viewModel.relationship?.following == false {
if viewModel.account?.isPremiumAccount == true && viewModel.relationship?.following == false || viewModel.account?.isLinkedToPremiumAccount == true && viewModel.premiumRelationship?.following == false {
tipView
}
}
@ -327,7 +331,11 @@ struct AccountDetailHeaderView: View {
Button {
isTipSheetPresented = true
Task {
try? await viewModel.followButtonViewModel?.follow()
if viewModel.account?.isLinkedToPremiumAccount == true {
try? await viewModel.followPremiumAccount()
} else {
try? await viewModel.followButtonViewModel?.follow()
}
}
} label: {
Text("$ Subscribe")

View file

@ -61,6 +61,10 @@ import SwiftUI
var featuredTags: [FeaturedTag] = []
var fields: [Account.Field] = []
var familiarFollowers: [Account] = []
var premiumAccount: Account?
var premiumRelationship: Relationship?
var selectedTab = Tab.statuses {
didSet {
switch selectedTab {
@ -112,8 +116,9 @@ import SwiftUI
guard let client else { return }
do {
let data = try await fetchAccountData(accountId: accountId, client: client)
accountState = .data(account: data.account)
try await fetchPremiumAccount(fromAccount: data.account, client: client)
account = data.account
fields = data.account.fields
featuredTags = data.featuredTags
@ -285,3 +290,28 @@ import SwiftUI
func statusDidDisappear(status _: Status) {}
}
extension AccountDetailViewModel {
private func fetchPremiumAccount(fromAccount: Account, client: Client) async throws {
if fromAccount.isLinkedToPremiumAccount, let acct = fromAccount.premiumAcct {
let results: SearchResults? = try await client.get(endpoint: Search.search(query: acct,
type: "accounts",
offset: nil,
following: nil),
forceVersion: .v2)
if let premiumAccount = results?.accounts.first {
self.premiumAccount = premiumAccount
var relationships: [Relationship] = try await client.get(endpoint: Accounts.relationships(ids: [premiumAccount.id]))
self.premiumRelationship = relationships.first
}
}
}
func followPremiumAccount() async throws {
if let premiumAccount {
premiumRelationship = try await client?.post(endpoint: Accounts.follow(id: premiumAccount.id,
notify: false,
reblogs: true))
}
}
}

View file

@ -181,7 +181,8 @@ public enum SettingsStartingPoint {
{
navigate(to: .hashTag(tag: tag, account: nil))
return .handled
} else if url.lastPathComponent.first == "@",
} else if url.lastPathComponent.first == "@" ||
(url.host() == AppInfo.premiumInstance && url.pathComponents.contains("users")),
let host = url.host,
!host.hasPrefix("www")
{

View file

@ -71,7 +71,15 @@ public final class Account: Codable, Identifiable, Hashable, Sendable, Equatable
header.lastPathComponent != "missing.png"
}
public var isProAccount: Bool {
public var isLinkedToPremiumAccount: Bool {
fields.first(where: { $0.value.asRawText.hasSuffix(AppInfo.premiumInstance) }) != nil
}
public var premiumAcct: String? {
fields.first(where: { $0.value.asRawText.hasSuffix(AppInfo.premiumInstance) })?.value.asRawText
}
public var isPremiumAccount: Bool {
url?.host() == AppInfo.premiumInstance
}

View file

@ -109,7 +109,7 @@ import SwiftUI
theme.tintColor.opacity(0.15)
} else if userMentionned {
theme.secondaryBackgroundColor
} else if status.account.isProAccount {
} else if status.account.isPremiumAccount {
.yellow.opacity(0.4)
} else {
theme.primaryBackgroundColor

View file

@ -6,7 +6,7 @@ struct StatusRowPremiumView: View {
let viewModel: StatusRowViewModel
var body: some View {
if viewModel.status.account.isProAccount {
if viewModel.status.account.isPremiumAccount {
Text("From a subscribed premium account")
.font(.scaledFootnote)
.foregroundStyle(.secondary)