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

62 lines
1.7 KiB
TypeScript

import { postgrestLocalUrl, patreonVideoAccessBenefitId, giteaUrl } from './constants'
import { IAuthData } from '@/app/components/auth';
import { type IPatron } from '@futureporn/types'
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[]> {
let patrons = []
try {
const res = await fetch(`${postgrestLocalUrl}/patrons`);
const data = await res.json();
if (!data) throw new Error(`no patron data was available. ${JSON.stringify(data)}`);
patrons = data.map((patron: IPatron) => patron.username)
} catch (e) {
console.error('failed to get patrons~ list')
console.error(e)
return [] as IPatron[]
}
return patrons
}
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
}