|
| 1 | +class EventInfo { |
| 2 | + // god forbid a class have public members |
| 3 | + public id: string; |
| 4 | + public title: string; |
| 5 | + public startTime: string; |
| 6 | + public endTime?: string; |
| 7 | + public location: string; |
| 8 | + public imageUrl: string; |
| 9 | + public link: string; |
| 10 | + |
| 11 | + constructor( |
| 12 | + id: string, |
| 13 | + title: string, |
| 14 | + startTime: string, |
| 15 | + endTime: string | undefined, |
| 16 | + location: string, |
| 17 | + imageUrl: string |
| 18 | + ) { |
| 19 | + this.id = id; |
| 20 | + this.title = title; |
| 21 | + this.startTime = startTime; |
| 22 | + this.endTime = endTime; |
| 23 | + this.location = location; |
| 24 | + this.imageUrl = imageUrl; |
| 25 | + // would use link as getter but getters are not enumerable so it doesn't appear in JSON.stringify :skull: |
| 26 | + // maybe a cursed fix would be to use Object.defineProperty LOL |
| 27 | + this.link = `https://www.facebook.com/events/${id}`; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// We are altering the array in place, pray we do not alter it from another thread |
| 32 | +// I don't even know if concurrent modification exception is a thing in JS |
| 33 | +// Maybe this is a single threaded moment :icant: |
| 34 | +export function filterInPlace<T>( |
| 35 | + arr: T[], |
| 36 | + predicate: (value: T, index: number, array: T[]) => boolean |
| 37 | +): T[] { |
| 38 | + let write = 0; |
| 39 | + for (let read = 0; read < arr.length; read++) { |
| 40 | + const val = arr[read]; |
| 41 | + if (predicate(val, read, arr)) { |
| 42 | + arr[write++] = val; |
| 43 | + } |
| 44 | + } |
| 45 | + arr.length = write; |
| 46 | + return arr; |
| 47 | +} |
| 48 | + |
| 49 | +// This one is definitely not thread safe lmao |
| 50 | +// TODO fix with a mutex probably |
| 51 | +export function replaceInPlace<T>( |
| 52 | + arr: T[], |
| 53 | + predicate: (value: T, index: number, array: T[]) => boolean, |
| 54 | + replacement: T |
| 55 | +): number { |
| 56 | + const idx = arr.findIndex(predicate); |
| 57 | + if (idx !== -1) arr[idx] = replacement; |
| 58 | + return idx; |
| 59 | +} |
| 60 | + |
| 61 | +// we LOVE global variables |
| 62 | +export let eventInfo: EventInfo[] = []; |
| 63 | + |
| 64 | +interface FacebookEvent { |
| 65 | + id: string; |
| 66 | + name: string; |
| 67 | + cover?: { source: string }; |
| 68 | + place?: { name: string }; |
| 69 | + start_time: string; |
| 70 | + end_time?: string; |
| 71 | +} |
| 72 | + |
| 73 | +interface FacebookEventsResponse { |
| 74 | + data: FacebookEvent[]; |
| 75 | +} |
| 76 | + |
| 77 | +// this isn't in .env for different module compatiblity |
| 78 | +const FB_API_VERSION = "v23.0"; |
| 79 | + |
| 80 | +export async function fetchEvents() { |
| 81 | + const response = await fetch( |
| 82 | + `https://graph.facebook.com/${FB_API_VERSION}/${process.env.FB_EVENT_PAGE_ID}/events?access_token=${process.env.FB_ACCESS_TOKEN}&fields=id,name,cover,place,start_time,end_time` |
| 83 | + ); |
| 84 | + |
| 85 | + const res: FacebookEventsResponse = await response.json(); |
| 86 | + |
| 87 | + if (!res || !res.data) { |
| 88 | + console.log("No events found..."); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + const processed = res.data.map( |
| 93 | + (e) => |
| 94 | + new EventInfo( |
| 95 | + e.id, |
| 96 | + e.name, |
| 97 | + e.start_time, |
| 98 | + e.end_time, |
| 99 | + e.place?.name ?? "Everything everywhere all at once!!!", |
| 100 | + e.cover?.source || "/images/events/default_event.jpg" |
| 101 | + ) |
| 102 | + ); |
| 103 | + |
| 104 | + eventInfo = processed; |
| 105 | +} |
| 106 | + |
| 107 | +export async function fetchEvent(id: string) { |
| 108 | + const response = await fetch( |
| 109 | + `https://graph.facebook.com/${FB_API_VERSION}/${id}?access_token=${process.env.FB_ACCESS_TOKEN}&fields=id,name,cover,place,start_time,end_time` |
| 110 | + ); |
| 111 | + |
| 112 | + const res: FacebookEvent = await response.json(); |
| 113 | + |
| 114 | + if (!res) { |
| 115 | + throw new Error(`Couldn't get details for event ${id}`); |
| 116 | + } |
| 117 | + |
| 118 | + return new EventInfo( |
| 119 | + res.id, |
| 120 | + res.name, |
| 121 | + res.start_time, |
| 122 | + res.end_time, |
| 123 | + res.place?.name ?? "Everything everywhere all at once!!!", |
| 124 | + res.cover?.source || "/images/events/default_event.jpg" |
| 125 | + ); |
| 126 | +} |
0 commit comments