Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/providers/patreon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function fetchPatreonSponsors(token: string): Promise<Sponsorship[]
const userCampaignId = userData.data[0].id

const sponsors: any[] = []
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`
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`
do {
// Get pledges from the campaign
const sponsorshipData = await $fetch(sponsorshipApi, {
Expand All @@ -45,15 +45,18 @@ export async function fetchPatreonSponsors(token: string): Promise<Sponsorship[]
.map((membership: any) => ({
membership,
patron: sponsorshipData.included.find(
(v: any) => v.id === membership.relationships.user.data.id,
(v: any) => v.type === 'user' && v.id === membership.relationships.user.data.id,
),
tier: sponsorshipData.included.find(
(v: any) => v.type === 'tier' && v.id === membership.relationships.currently_entitled_tiers.data[0]?.id,
),
})),
)
sponsorshipApi = sponsorshipData.links?.next
} while (sponsorshipApi)

const processed = sponsors.map(
(raw: any): Sponsorship => ({
const processed = sponsors.map((raw: any): Sponsorship => {
const sponsor: Sponsorship = {
sponsor: {
avatarUrl: raw.patron.attributes.image_url,
login: raw.patron.attributes.first_name,
Expand All @@ -62,13 +65,21 @@ export async function fetchPatreonSponsors(token: string): Promise<Sponsorship[]
linkUrl: raw.patron.attributes.url,
},
isOneTime: false, // One-time pledges not supported
// The "former_patron" and "declined_patron" both is past sponsors
monthlyDollars: ['former_patron', 'declined_patron'].includes(raw.membership.attributes.patron_status) ? -1 : Math.floor(raw.membership.attributes.currently_entitled_amount_cents / 100),
monthlyDollars: Math.floor(raw.membership.attributes.currently_entitled_amount_cents / 100),
privacyLevel: 'PUBLIC', // Patreon is all public
tierName: 'Patreon',
createdAt: raw.membership.attributes.pledge_relationship_start,
}),
)
}

// The "former_patron" and "declined_patron" both is past sponsors
if (['former_patron', 'declined_patron'].includes(raw.membership.attributes.patron_status))
sponsor.monthlyDollars = -1
// If the sponsor is not a patron but has a gifted membership, we can still show the tier amount
else if (sponsor.monthlyDollars <= 0 && (raw.tier?.attributes.amount_cents || 0) > 0)
sponsor.monthlyDollars = Math.floor(raw.tier.attributes.amount_cents / 100)

return sponsor
})

return processed
}