[AltStoreCore] Adds Pledge, PledgeReward, and PledgeTier

Allows us to cache pledges for current user, which can be used to determine if user has access to Patreon-only apps.
This commit is contained in:
Riley Testut
2023-11-20 13:55:28 -06:00
parent 7ed2dc8291
commit d59ced9208
10 changed files with 261 additions and 6 deletions

View File

@@ -113,10 +113,10 @@ public extension PatreonAPI
var components = URLComponents(string: "/api/oauth2/v2/identity")!
components.queryItems = [URLQueryItem(name: "include", value: "memberships.campaign.tiers,memberships.currently_entitled_tiers.benefits"),
URLQueryItem(name: "fields[user]", value: "first_name,full_name"),
URLQueryItem(name: "fields[member]", value: "full_name,patron_status")]
URLQueryItem(name: "fields[tier]", value: "title"),
URLQueryItem(name: "fields[tier]", value: "title,amount_cents"),
URLQueryItem(name: "fields[benefit]", value: "title"),
URLQueryItem(name: "fields[campaign]", value: "url"),
URLQueryItem(name: "fields[member]", value: "full_name,patron_status,currently_entitled_amount_cents")]
let requestURL = components.url(relativeTo: self.baseURL)!
let request = URLRequest(url: requestURL)
@@ -149,9 +149,9 @@ public extension PatreonAPI
{
var components = URLComponents(string: "/api/oauth2/v2/campaigns/\(PatreonAPI.altstoreCampaignID)/members")!
components.queryItems = [URLQueryItem(name: "include", value: "currently_entitled_tiers,currently_entitled_tiers.benefits"),
URLQueryItem(name: "fields[tier]", value: "title"),
URLQueryItem(name: "fields[tier]", value: "title,amount_cents"),
URLQueryItem(name: "fields[benefit]", value: "title"),
URLQueryItem(name: "fields[member]", value: "full_name,patron_status"),
URLQueryItem(name: "fields[member]", value: "full_name,patron_status,currently_entitled_amount_cents"),
URLQueryItem(name: "page[size]", value: "1000")]
let requestURL = components.url(relativeTo: self.baseURL)!

View File

@@ -16,6 +16,7 @@ extension PatreonAPI
{
var full_name: String?
var patron_status: String?
var currently_entitled_amount_cents: Int32 // In campaign's currency
}
struct PatronRelationships: Decodable
@@ -40,6 +41,7 @@ extension PatreonAPI
{
public var name: String?
public var identifier: String
public var pledgeAmount: Decimal?
public var status: Status
// Relationships
@@ -51,6 +53,7 @@ extension PatreonAPI
{
self.name = response.attributes.full_name
self.identifier = response.id
self.pledgeAmount = Decimal(response.attributes.currently_entitled_amount_cents) / 100
if let status = response.attributes.patron_status
{

View File

@@ -15,6 +15,7 @@ extension PatreonAPI
struct TierAttributes: Decodable
{
var title: String
var amount_cents: Int32 // In USD
}
struct TierRelationships: Decodable
@@ -29,6 +30,7 @@ extension PatreonAPI
{
public var name: String
public var identifier: String
public var amount: Decimal
// Relationships
public var benefits: [Benefit] = []
@@ -38,6 +40,9 @@ extension PatreonAPI
self.name = response.attributes.title
self.identifier = response.id
let amount = Decimal(response.attributes.amount_cents) / 100
self.amount = amount
guard let included, let benefitIDs = response.relationships?.benefits?.data.map(\.id) else { return }
let benefits = benefitIDs.compactMap { included.benefits[$0] }.map(Benefit.init(response:))