@@ -27,7 +27,7 @@ export async function fetchPatreonSponsors(token: string): Promise<Sponsorship[]
27
27
const userCampaignId = userData . data [ 0 ] . id
28
28
29
29
const sponsors : any [ ] = [ ]
30
- let sponsorshipApi = `https://www.patreon.com/api/oauth2/v2/campaigns/${ userCampaignId } /members?include=user&fields%5Bmember%5D=currently_entitled_amount_cents,patron_status,pledge_relationship_start,lifetime_support_cents&fields%5Buser%5D=image_url,url,first_name,full_name&page%5Bcount%5D=100`
30
+ let sponsorshipApi = `https://www.patreon.com/api/oauth2/v2/campaigns/${ userCampaignId } /members?include=user,currently_entitled_tiers &fields%5Bmember%5D=currently_entitled_amount_cents,patron_status,pledge_relationship_start,lifetime_support_cents&fields%5Buser%5D=image_url,url,first_name,full_name&fields%5Btier%5D=amount_cents &page%5Bcount%5D=100`
31
31
do {
32
32
// Get pledges from the campaign
33
33
const sponsorshipData = await $fetch ( sponsorshipApi , {
@@ -45,15 +45,18 @@ export async function fetchPatreonSponsors(token: string): Promise<Sponsorship[]
45
45
. map ( ( membership : any ) => ( {
46
46
membership,
47
47
patron : sponsorshipData . included . find (
48
- ( v : any ) => v . id === membership . relationships . user . data . id ,
48
+ ( v : any ) => v . type === 'user' && v . id === membership . relationships . user . data . id ,
49
+ ) ,
50
+ tier : sponsorshipData . included . find (
51
+ ( v : any ) => v . type === 'tier' && v . id === membership . relationships . currently_entitled_tiers . data [ 0 ] ?. id ,
49
52
) ,
50
53
} ) ) ,
51
54
)
52
55
sponsorshipApi = sponsorshipData . links ?. next
53
56
} while ( sponsorshipApi )
54
57
55
- const processed = sponsors . map (
56
- ( raw : any ) : Sponsorship => ( {
58
+ const processed = sponsors . map ( ( raw : any ) : Sponsorship => {
59
+ const sponsor : Sponsorship = {
57
60
sponsor : {
58
61
avatarUrl : raw . patron . attributes . image_url ,
59
62
login : raw . patron . attributes . first_name ,
@@ -62,13 +65,21 @@ export async function fetchPatreonSponsors(token: string): Promise<Sponsorship[]
62
65
linkUrl : raw . patron . attributes . url ,
63
66
} ,
64
67
isOneTime : false , // One-time pledges not supported
65
- // The "former_patron" and "declined_patron" both is past sponsors
66
- monthlyDollars : [ 'former_patron' , 'declined_patron' ] . includes ( raw . membership . attributes . patron_status ) ? - 1 : Math . floor ( raw . membership . attributes . currently_entitled_amount_cents / 100 ) ,
68
+ monthlyDollars : Math . floor ( raw . membership . attributes . currently_entitled_amount_cents / 100 ) ,
67
69
privacyLevel : 'PUBLIC' , // Patreon is all public
68
70
tierName : 'Patreon' ,
69
71
createdAt : raw . membership . attributes . pledge_relationship_start ,
70
- } ) ,
71
- )
72
+ }
73
+
74
+ // The "former_patron" and "declined_patron" both is past sponsors
75
+ if ( [ 'former_patron' , 'declined_patron' ] . includes ( raw . membership . attributes . patron_status ) )
76
+ sponsor . monthlyDollars = - 1
77
+ // If the sponsor is not a patron but has a gifted membership, we can still show the tier amount
78
+ else if ( sponsor . monthlyDollars <= 0 && ( raw . tier ?. attributes . amount_cents || 0 ) > 0 )
79
+ sponsor . monthlyDollars = Math . floor ( raw . tier . attributes . amount_cents / 100 )
80
+
81
+ return sponsor
82
+ } )
72
83
73
84
return processed
74
85
}
0 commit comments