fp/services/next/app/lib/patreon.ts

62 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-11-05 19:48:21 +00:00
import { postgrestLocalUrl, patreonVideoAccessBenefitId, giteaUrl } from './constants'
2024-07-10 22:11:18 +00:00
import { IAuthData } from '@/app/components/auth';
2024-11-05 19:48:21 +00:00
import { type IPatron } from '@futureporn/types'
2024-01-20 16:16:14 +00:00
export interface ICampaign {
pledgeSum: number;
patronCount: number;
}
export interface IMarshalledCampaign {
data: {
attributes: {
pledge_sum: number,
patron_count: number
}
}
}
export function isEntitledToPatronVideoAccess(authData: IAuthData): boolean {
if (!authData.user?.patreonBenefits) return false;
const patreonBenefits = authData.user.patreonBenefits
return (patreonBenefits.includes(patreonVideoAccessBenefitId))
}
export async function getPatrons(): Promise<IPatron[]> {
2024-11-05 19:48:21 +00:00
let patrons = []
2024-08-13 21:18:01 +00:00
try {
2024-11-05 19:48:21 +00:00
const res = await fetch(`${postgrestLocalUrl}/patrons`);
2024-08-13 21:18:01 +00:00
const data = await res.json();
2024-11-05 19:48:21 +00:00
if (!data) throw new Error(`no patron data was available. ${JSON.stringify(data)}`);
patrons = data.map((patron: IPatron) => patron.username)
2024-08-13 21:18:01 +00:00
} catch (e) {
2024-11-05 19:48:21 +00:00
console.error('failed to get patrons~ list')
2024-08-13 21:18:01 +00:00
console.error(e)
return [] as IPatron[]
}
2024-11-05 19:48:21 +00:00
return patrons
2024-01-20 16:16:14 +00:00
}
export async function getCampaign(): Promise<ICampaign> {
const res = await fetch('https://www.patreon.com/api/campaigns/8012692', {
headers: {
accept: 'application/json'
},
next: {
revalidate: 43200 // 12 hour cache
}
})
const campaignData = await res.json();
const data = {
patronCount: campaignData.data.attributes.patron_count,
pledgeSum: campaignData.data.attributes.campaign_pledge_sum
}
return data
}