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 let latestEvent = watcher.latestEvent, let latestEvent = latestEvent as? StreamEventNotification {
if latestEvent.notification.account.id == viewModel.accountId { if latestEvent.notification.account.id == viewModel.accountId {
Task { 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) .accessibilityRespondsToUserInteraction(false)
movedToView movedToView
joinedAtView 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 tipView
} }
} }
@ -327,7 +331,11 @@ struct AccountDetailHeaderView: View {
Button { Button {
isTipSheetPresented = true isTipSheetPresented = true
Task { Task {
try? await viewModel.followButtonViewModel?.follow() if viewModel.account?.isLinkedToPremiumAccount == true {
try? await viewModel.followPremiumAccount()
} else {
try? await viewModel.followButtonViewModel?.follow()
}
} }
} label: { } label: {
Text("$ Subscribe") Text("$ Subscribe")

View file

@ -61,6 +61,10 @@ import SwiftUI
var featuredTags: [FeaturedTag] = [] var featuredTags: [FeaturedTag] = []
var fields: [Account.Field] = [] var fields: [Account.Field] = []
var familiarFollowers: [Account] = [] var familiarFollowers: [Account] = []
var premiumAccount: Account?
var premiumRelationship: Relationship?
var selectedTab = Tab.statuses { var selectedTab = Tab.statuses {
didSet { didSet {
switch selectedTab { switch selectedTab {
@ -112,8 +116,9 @@ import SwiftUI
guard let client else { return } guard let client else { return }
do { do {
let data = try await fetchAccountData(accountId: accountId, client: client) let data = try await fetchAccountData(accountId: accountId, client: client)
accountState = .data(account: data.account) accountState = .data(account: data.account)
try await fetchPremiumAccount(fromAccount: data.account, client: client)
account = data.account account = data.account
fields = data.account.fields fields = data.account.fields
featuredTags = data.featuredTags featuredTags = data.featuredTags
@ -285,3 +290,28 @@ import SwiftUI
func statusDidDisappear(status _: Status) {} 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)) navigate(to: .hashTag(tag: tag, account: nil))
return .handled return .handled
} else if url.lastPathComponent.first == "@", } else if url.lastPathComponent.first == "@" ||
(url.host() == AppInfo.premiumInstance && url.pathComponents.contains("users")),
let host = url.host, let host = url.host,
!host.hasPrefix("www") !host.hasPrefix("www")
{ {

View file

@ -71,7 +71,15 @@ public final class Account: Codable, Identifiable, Hashable, Sendable, Equatable
header.lastPathComponent != "missing.png" 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 url?.host() == AppInfo.premiumInstance
} }

View file

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

View file

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