Skip to content

Commit 2dbc5db

Browse files
fix: incorrect shows the tier for the user with a gifted membership for patreon (#10)
(cherry picked from commit 3c3d93c) Co-authored-by: Lucas Yang <[email protected]>
1 parent abe7196 commit 2dbc5db

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/providers/patreon.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export async function fetchPatreonSponsors(token: string): Promise<Sponsorship[]
2727
const userCampaignId = userData.data[0].id
2828

2929
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`
3131
do {
3232
// Get pledges from the campaign
3333
const sponsorshipData = await $fetch(sponsorshipApi, {
@@ -45,15 +45,18 @@ export async function fetchPatreonSponsors(token: string): Promise<Sponsorship[]
4545
.map((membership: any) => ({
4646
membership,
4747
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,
4952
),
5053
})),
5154
)
5255
sponsorshipApi = sponsorshipData.links?.next
5356
} while (sponsorshipApi)
5457

55-
const processed = sponsors.map(
56-
(raw: any): Sponsorship => ({
58+
const processed = sponsors.map((raw: any): Sponsorship => {
59+
const sponsor: Sponsorship = {
5760
sponsor: {
5861
avatarUrl: raw.patron.attributes.image_url,
5962
login: raw.patron.attributes.first_name,
@@ -62,13 +65,21 @@ export async function fetchPatreonSponsors(token: string): Promise<Sponsorship[]
6265
linkUrl: raw.patron.attributes.url,
6366
},
6467
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),
6769
privacyLevel: 'PUBLIC', // Patreon is all public
6870
tierName: 'Patreon',
6971
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+
})
7283

7384
return processed
7485
}

0 commit comments

Comments
 (0)