@@ -14,9 +14,35 @@ import { IntraWebhook } from "../utils/types";
1414import Logger , { LogType } from "../utils/logger" ;
1515import { Request , Response , NextFunction } from "express" ;
1616import * as Checks from "../checks/evaluators" ;
17+ import db from "../db" ;
1718
1819/*============================================================================*/
1920
21+ // Intra, like many distributed systems will sometimes deliver the same webhook > 1 times
22+ // This is where we filter those situations
23+ async function filterAlreadyDeliveredWebhook ( req : Request ) {
24+ const headerKey = "x-delivery" ;
25+ const id = req . headers [ headerKey ] ;
26+ if ( ! id || typeof id !== "string" ) {
27+ Logger . log ( `WEBHOOK: Request header "${ headerKey } " has invalid value of: "${ id } "` ) ;
28+ return false ;
29+ }
30+
31+ const hasDelivery = await db . hasWebhookDelivery ( id ) . catch ( ( e ) => {
32+ Logger . log ( `WEBHOOK: Could not load from db: ${ e } ` ) ;
33+ return false ;
34+ } ) ;
35+ if ( hasDelivery ) {
36+ return true ;
37+ }
38+
39+ await db . addWebhookDelivery ( id , JSON . stringify ( req . body ) ) . catch ( ( e ) => {
40+ Logger . log ( `WEBHOOK: Could not add webhook delivery: ${ e } ` ) ;
41+ } ) ;
42+
43+ return false ;
44+ }
45+
2046/**
2147 * Filters for requests and sends back corresponding error.
2248 * @param req The incoming request.
@@ -161,6 +187,7 @@ webhookApp.post("/create", filterHook(Env.WEBHOOK_CREATE_SECRET), async (req: Re
161187 } ) ;
162188 if ( yeetEval != undefined ) {
163189 await Intra . deleteEvaluation ( yeetEval ) ;
190+ res . status ( 200 ) . send ( ) ;
164191 return Logger . log ( `Evaluation cancelled: These users shouldn't evaluate each other, yikes.` ) ;
165192 }
166193 }
@@ -198,7 +225,7 @@ webhookApp.post("/create", filterHook(Env.WEBHOOK_CREATE_SECRET), async (req: Re
198225webhookApp . post ( "/delete" , filterHook ( Env . WEBHOOK_DELETE_SECRET ) , async ( req : Request , res : Response ) => {
199226 const hook : IntraWebhook . Root = req . body ;
200227
201- Logger . log ( `Evaluation destroyed: ${ hook . team . name } -> ${ hook . project . name } ` ) ;
228+ Logger . log ( `Evaluation destroyed: ${ hook . team . name } -> ${ hook . project . name } -> ID: ${ req . headers [ "x-delivery" ] } ` ) ;
202229 if ( hook . user && hook . user . id != Config . botID ) {
203230 res . status ( 204 ) . send ( ) ;
204231 return Logger . log ( "Ignored: Webhook does not concern bot." ) ;
@@ -232,7 +259,7 @@ webhookApp.post("/delete", filterHook(Env.WEBHOOK_DELETE_SECRET), async (req: Re
232259webhookApp . post ( "/update" , filterHook ( Env . WEBHOOK_UPDATE_SECRET ) , async ( req : Request , res : Response ) => {
233260 const hook : IntraWebhook . Root = req . body ;
234261
235- Logger . log ( `Evaluation update: ${ hook . team . name } -> ${ hook . project . name } ` ) ;
262+ Logger . log ( `Evaluation update: ${ hook . team . name } -> ${ hook . project . name } -> ID: ${ req . headers [ "x-delivery" ] } ` ) ;
236263
237264 try {
238265 const check = await DB . exists ( hook . team . id ) ;
0 commit comments