mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-11 15:53:30 +01:00
[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:
@@ -154,6 +154,7 @@
|
||||
<attribute name="identifier" attributeType="String"/>
|
||||
<attribute name="isPatron" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
|
||||
<attribute name="name" attributeType="String"/>
|
||||
<relationship name="pledges" toMany="YES" deletionRule="Cascade" destinationEntity="Pledge" inverseName="account" inverseEntity="Pledge"/>
|
||||
<uniquenessConstraints>
|
||||
<uniquenessConstraint>
|
||||
<constraint value="identifier"/>
|
||||
@@ -169,6 +170,40 @@
|
||||
</uniquenessConstraint>
|
||||
</uniquenessConstraints>
|
||||
</entity>
|
||||
<entity name="Pledge" representedClassName="Pledge" syncable="YES">
|
||||
<attribute name="amount" attributeType="Decimal" defaultValueString="0"/>
|
||||
<attribute name="campaignURL" attributeType="URI"/>
|
||||
<attribute name="identifier" attributeType="String"/>
|
||||
<relationship name="account" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PatreonAccount" inverseName="pledges" inverseEntity="PatreonAccount"/>
|
||||
<relationship name="rewards" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="PledgeReward" inverseName="pledge" inverseEntity="PledgeReward"/>
|
||||
<relationship name="tiers" optional="YES" toMany="YES" deletionRule="Cascade" destinationEntity="PledgeTier" inverseName="pledge" inverseEntity="PledgeTier"/>
|
||||
<uniquenessConstraints>
|
||||
<uniquenessConstraint>
|
||||
<constraint value="identifier"/>
|
||||
</uniquenessConstraint>
|
||||
</uniquenessConstraints>
|
||||
</entity>
|
||||
<entity name="PledgeReward" representedClassName="PledgeReward" syncable="YES">
|
||||
<attribute name="identifier" attributeType="String"/>
|
||||
<attribute name="name" attributeType="String"/>
|
||||
<relationship name="pledge" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Pledge" inverseName="rewards" inverseEntity="Pledge"/>
|
||||
<uniquenessConstraints>
|
||||
<uniquenessConstraint>
|
||||
<constraint value="identifier"/>
|
||||
</uniquenessConstraint>
|
||||
</uniquenessConstraints>
|
||||
</entity>
|
||||
<entity name="PledgeTier" representedClassName="PledgeTier" syncable="YES">
|
||||
<attribute name="amount" attributeType="Decimal" defaultValueString="0.0"/>
|
||||
<attribute name="identifier" attributeType="String"/>
|
||||
<attribute name="name" attributeType="String"/>
|
||||
<relationship name="pledge" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="Pledge" inverseName="tiers" inverseEntity="Pledge"/>
|
||||
<uniquenessConstraints>
|
||||
<uniquenessConstraint>
|
||||
<constraint value="identifier"/>
|
||||
</uniquenessConstraint>
|
||||
</uniquenessConstraints>
|
||||
</entity>
|
||||
<entity name="RefreshAttempt" representedClassName="RefreshAttempt" syncable="YES">
|
||||
<attribute name="date" attributeType="Date" usesScalarValueType="NO"/>
|
||||
<attribute name="errorDescription" optional="YES" attributeType="String"/>
|
||||
|
||||
@@ -262,7 +262,36 @@ open class MergePolicy: RSTRelationshipPreservingMergePolicy
|
||||
{
|
||||
featuredAppIDsBySourceID[databaseObject.identifier] = contextSource.featuredApps?.map { $0.bundleIdentifier }
|
||||
}
|
||||
|
||||
|
||||
case let databasePledge as Pledge:
|
||||
guard let contextPledge = conflict.conflictingObjects.first as? Pledge else { break }
|
||||
|
||||
// Tiers
|
||||
let contextTierIDs = Set(contextPledge._tiers.lazy.compactMap { $0 as? PledgeTier }.map { $0.identifier })
|
||||
for case let databaseTier as PledgeTier in databasePledge._tiers where !contextTierIDs.contains(databaseTier.identifier)
|
||||
{
|
||||
// Tier ID does NOT exist in context, so delete existing databaseTier.
|
||||
databaseTier.managedObjectContext?.delete(databaseTier)
|
||||
}
|
||||
|
||||
// Rewards
|
||||
let contextRewardIDs = Set(contextPledge._rewards.lazy.compactMap { $0 as? PledgeReward }.map { $0.identifier })
|
||||
for case let databaseReward as PledgeReward in databasePledge._rewards where !contextRewardIDs.contains(databaseReward.identifier)
|
||||
{
|
||||
// Reward ID does NOT exist in context, so delete existing databaseReward.
|
||||
databaseReward.managedObjectContext?.delete(databaseReward)
|
||||
}
|
||||
|
||||
case let databaseAccount as PatreonAccount:
|
||||
guard let contextAccount = conflict.conflictingObjects.first as? PatreonAccount else { break }
|
||||
|
||||
let contextPledgeIDs = Set(contextAccount._pledges.lazy.compactMap { $0 as? Pledge }.map { $0.identifier })
|
||||
for case let databasePledge as Pledge in databaseAccount._pledges where !contextPledgeIDs.contains(databasePledge.identifier)
|
||||
{
|
||||
// Pledge ID does NOT exist in context, so delete existing databasePledge.
|
||||
databasePledge.managedObjectContext?.delete(databasePledge)
|
||||
}
|
||||
|
||||
default: break
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,10 @@ public class PatreonAccount: NSManagedObject, Fetchable
|
||||
|
||||
@NSManaged public var isPatron: Bool
|
||||
|
||||
/* Relationships */
|
||||
@nonobjc public var pledges: Set<Pledge> { _pledges as! Set<Pledge> }
|
||||
@NSManaged @objc(pledges) internal var _pledges: NSSet
|
||||
|
||||
private override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?)
|
||||
{
|
||||
super.init(entity: entity, insertInto: context)
|
||||
@@ -31,6 +35,23 @@ public class PatreonAccount: NSManagedObject, Fetchable
|
||||
self.name = account.name
|
||||
self.firstName = account.firstName
|
||||
|
||||
let pledges = account.pledges?.compactMap { patron -> Pledge? in
|
||||
// First ensure pledge is active.
|
||||
guard patron.status == .active else { return nil }
|
||||
|
||||
guard let pledge = Pledge(patron: patron, context: context) else { return nil }
|
||||
|
||||
let tiers = patron.tiers.map { PledgeTier(tier: $0, context: context) }
|
||||
pledge._tiers = Set(tiers) as NSSet
|
||||
|
||||
let rewards = patron.benefits.map { PledgeReward(benefit: $0, context: context) }
|
||||
pledge._rewards = Set(rewards) as NSSet
|
||||
|
||||
return pledge
|
||||
} ?? []
|
||||
|
||||
self._pledges = Set(pledges) as NSSet
|
||||
|
||||
if let altstorePledge = account.pledges?.first(where: { $0.campaign?.identifier == PatreonAPI.altstoreCampaignID })
|
||||
{
|
||||
let isActivePatron = (altstorePledge.status == .active)
|
||||
54
AltStoreCore/Model/Patreon/Pledge.swift
Normal file
54
AltStoreCore/Model/Patreon/Pledge.swift
Normal file
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Pledge.swift
|
||||
// AltStoreCore
|
||||
//
|
||||
// Created by Riley Testut on 10/24/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
|
||||
@objc(Pledge)
|
||||
public class Pledge: NSManagedObject, Fetchable
|
||||
{
|
||||
/* Properties */
|
||||
@NSManaged public private(set) var identifier: String
|
||||
@NSManaged public private(set) var campaignURL: URL
|
||||
|
||||
@nonobjc public var amount: Decimal { _amount as Decimal }
|
||||
@NSManaged @objc(amount) private var _amount: NSDecimalNumber
|
||||
|
||||
/* Relationships */
|
||||
@NSManaged public private(set) var account: PatreonAccount?
|
||||
|
||||
@nonobjc public var tiers: Set<PledgeTier> { _tiers as! Set<PledgeTier> }
|
||||
@NSManaged @objc(tiers) internal var _tiers: NSSet
|
||||
|
||||
@nonobjc public var rewards: Set<PledgeReward> { _rewards as! Set<PledgeReward> }
|
||||
@NSManaged @objc(rewards) internal var _rewards: NSSet
|
||||
|
||||
private override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?)
|
||||
{
|
||||
super.init(entity: entity, insertInto: context)
|
||||
}
|
||||
|
||||
init?(patron: PatreonAPI.Patron, context: NSManagedObjectContext)
|
||||
{
|
||||
guard let amount = patron.pledgeAmount, let campaignURL = patron.campaign?.url else { return nil }
|
||||
|
||||
super.init(entity: Pledge.entity(), insertInto: context)
|
||||
|
||||
self.identifier = patron.identifier
|
||||
self._amount = amount as NSDecimalNumber
|
||||
self.campaignURL = campaignURL
|
||||
}
|
||||
}
|
||||
|
||||
public extension Pledge
|
||||
{
|
||||
@nonobjc class func fetchRequest() -> NSFetchRequest<Pledge>
|
||||
{
|
||||
return NSFetchRequest<Pledge>(entityName: "Pledge")
|
||||
}
|
||||
}
|
||||
42
AltStoreCore/Model/Patreon/PledgeReward.swift
Normal file
42
AltStoreCore/Model/Patreon/PledgeReward.swift
Normal file
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// PledgeReward.swift
|
||||
// AltStoreCore
|
||||
//
|
||||
// Created by Riley Testut on 10/24/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
|
||||
@objc(PledgeReward)
|
||||
public class PledgeReward: NSManagedObject, Fetchable
|
||||
{
|
||||
/* Properties */
|
||||
@NSManaged public private(set) var name: String
|
||||
@NSManaged public private(set) var identifier: String
|
||||
|
||||
/* Relationships */
|
||||
@NSManaged public private(set) var pledge: Pledge?
|
||||
|
||||
private override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?)
|
||||
{
|
||||
super.init(entity: entity, insertInto: context)
|
||||
}
|
||||
|
||||
init(benefit: PatreonAPI.Benefit, context: NSManagedObjectContext)
|
||||
{
|
||||
super.init(entity: PledgeReward.entity(), insertInto: context)
|
||||
|
||||
self.name = benefit.name
|
||||
self.identifier = benefit.identifier.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
public extension PledgeReward
|
||||
{
|
||||
@nonobjc class func fetchRequest() -> NSFetchRequest<PledgeReward>
|
||||
{
|
||||
return NSFetchRequest<PledgeReward>(entityName: "PledgeReward")
|
||||
}
|
||||
}
|
||||
46
AltStoreCore/Model/Patreon/PledgeTier.swift
Normal file
46
AltStoreCore/Model/Patreon/PledgeTier.swift
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// PledgeTier.swift
|
||||
// AltStoreCore
|
||||
//
|
||||
// Created by Riley Testut on 10/24/23.
|
||||
// Copyright © 2023 Riley Testut. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
|
||||
@objc(PledgeTier)
|
||||
public class PledgeTier: NSManagedObject, Fetchable
|
||||
{
|
||||
/* Properties */
|
||||
@NSManaged public private(set) var name: String
|
||||
@NSManaged public private(set) var identifier: String
|
||||
|
||||
@nonobjc public var amount: Decimal { _amount as Decimal } // In USD
|
||||
@NSManaged @objc(amount) private var _amount: NSDecimalNumber
|
||||
|
||||
/* Relationships */
|
||||
@NSManaged public private(set) var pledge: Pledge?
|
||||
|
||||
private override init(entity: NSEntityDescription, insertInto context: NSManagedObjectContext?)
|
||||
{
|
||||
super.init(entity: entity, insertInto: context)
|
||||
}
|
||||
|
||||
init(tier: PatreonAPI.Tier, context: NSManagedObjectContext)
|
||||
{
|
||||
super.init(entity: PledgeTier.entity(), insertInto: context)
|
||||
|
||||
self.name = tier.name
|
||||
self.identifier = tier.identifier
|
||||
self._amount = tier.amount as NSDecimalNumber
|
||||
}
|
||||
}
|
||||
|
||||
public extension PledgeTier
|
||||
{
|
||||
@nonobjc class func fetchRequest() -> NSFetchRequest<PledgeTier>
|
||||
{
|
||||
return NSFetchRequest<PledgeTier>(entityName: "PledgeTier")
|
||||
}
|
||||
}
|
||||
@@ -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)!
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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:))
|
||||
|
||||
Reference in New Issue
Block a user