|
| 1 | +import crypto from "crypto"; |
| 2 | +import { RequestHandler } from "express"; |
| 3 | +import { eventInfo, eventInfoMutex, fetchEvent } from "../data/eventData"; |
| 4 | +import { filterInPlace, replaceInPlace } from "../util"; |
| 5 | + |
| 6 | +interface FacebookWebhookPayload { |
| 7 | + object: string; |
| 8 | + entry: Array<{ |
| 9 | + id: string; |
| 10 | + changes: Array<{ |
| 11 | + field: string; |
| 12 | + value: { |
| 13 | + event_id: string; |
| 14 | + item: string; |
| 15 | + verb: string; |
| 16 | + }; |
| 17 | + }>; |
| 18 | + }>; |
| 19 | +} |
| 20 | + |
| 21 | +const verifySignature = ( |
| 22 | + rawBody: Buffer, |
| 23 | + signatureHeader?: string |
| 24 | +): boolean => { |
| 25 | + if (!signatureHeader) return false; |
| 26 | + const [algo, signature] = signatureHeader.split("="); |
| 27 | + if (algo !== "sha256") return false; |
| 28 | + |
| 29 | + const expected = crypto |
| 30 | + .createHmac("sha256", process.env.FB_APP_SECRET as string) |
| 31 | + .update(rawBody) |
| 32 | + .digest("hex"); |
| 33 | + |
| 34 | + return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected)); |
| 35 | +}; |
| 36 | + |
| 37 | +export const EventsWebhookVerifier: RequestHandler = (req, res) => { |
| 38 | + const mode = req.query["hub.mode"]; |
| 39 | + const token = req.query["hub.verify_token"]; |
| 40 | + const challenge = req.query["hub.challenge"]; |
| 41 | + |
| 42 | + if (mode === "subscribe" && token === process.env.FB_WEBHOOK_VERIFY_TOKEN) { |
| 43 | + return res.status(200).send(challenge); |
| 44 | + } |
| 45 | + |
| 46 | + res.sendStatus(403); |
| 47 | +}; |
| 48 | + |
| 49 | +/* |
| 50 | +Sample webhook payload |
| 51 | +https://developers.facebook.com/docs/graph-api/webhooks/getting-started/webhooks-for-pages -- for the outer wrapper |
| 52 | +https://developers.facebook.com/docs/graph-api/webhooks/reference/page/#feed -- for the inner objects |
| 53 | +
|
| 54 | +{ |
| 55 | + "object": "page", |
| 56 | + "entry": [ |
| 57 | + { |
| 58 | + "id": "PAGE_ID", |
| 59 | + "time": 1623242342342, |
| 60 | + "changes": [ |
| 61 | + { |
| 62 | + "field": "events", |
| 63 | + "value": { |
| 64 | + "event_id": "123456789", |
| 65 | + "verb": "create", // also "edit" or "delete" |
| 66 | + "published": 1 |
| 67 | + } |
| 68 | + } |
| 69 | + ] |
| 70 | + } |
| 71 | + ] |
| 72 | +} |
| 73 | +*/ |
| 74 | + |
| 75 | +export const EventsWebhookUpdate: RequestHandler = async (req, res) => { |
| 76 | + const signature = req.headers["x-hub-signature-256"]; |
| 77 | + if ( |
| 78 | + !req.rawBody || |
| 79 | + typeof signature !== "string" || |
| 80 | + !verifySignature(req.rawBody, signature) |
| 81 | + ) { |
| 82 | + return res.sendStatus(401); |
| 83 | + } |
| 84 | + |
| 85 | + const notif: FacebookWebhookPayload = req.body; |
| 86 | + if ( |
| 87 | + !notif || |
| 88 | + !notif.entry || |
| 89 | + notif.object !== "page" || |
| 90 | + notif.entry.length === 0 |
| 91 | + ) { |
| 92 | + return res.sendStatus(400); |
| 93 | + } |
| 94 | + |
| 95 | + for (const entry of notif.entry) { |
| 96 | + if (entry.id !== process.env.FB_EVENT_PAGE_ID) continue; |
| 97 | + |
| 98 | + for (const change of entry.changes) { |
| 99 | + if (change.field !== "feed" || change.value.item !== "event") continue; |
| 100 | + |
| 101 | + try { |
| 102 | + if (change.value.verb === "delete") { |
| 103 | + await eventInfoMutex.runExclusive(() => |
| 104 | + filterInPlace(eventInfo, (val) => val.id !== change.value.event_id) |
| 105 | + ); |
| 106 | + console.log(`Deleted event: ${change.value.event_id}`); |
| 107 | + } else if (change.value.verb === "edit") { |
| 108 | + const newEvent = await fetchEvent(change.value.event_id); |
| 109 | + |
| 110 | + eventInfoMutex.runExclusive(() => |
| 111 | + replaceInPlace( |
| 112 | + eventInfo, |
| 113 | + (val) => val.id === change.value.event_id, |
| 114 | + newEvent |
| 115 | + ) |
| 116 | + ); |
| 117 | + console.log(`Edited event: ${change.value.event_id}`); |
| 118 | + } else if (change.value.verb === "add") { |
| 119 | + const newEvent = await fetchEvent(change.value.event_id); |
| 120 | + await eventInfoMutex.runExclusive(() => eventInfo.push(newEvent)); |
| 121 | + console.log(`Added event: ${change.value.event_id}`); |
| 122 | + } else { |
| 123 | + console.warn( |
| 124 | + `Unknown verb "${change.value.verb}" for event ${change.value.event_id}` |
| 125 | + ); |
| 126 | + } |
| 127 | + } catch (err) { |
| 128 | + console.error( |
| 129 | + `Error processing event: ${change.value.event_id}:\n${err}` |
| 130 | + ); |
| 131 | + return res.sendStatus(500); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + res.sendStatus(200); |
| 137 | +}; |
0 commit comments