update notify route

This commit is contained in:
Martin Dimitrov 2025-06-07 16:30:14 -07:00
parent e17bb267fe
commit 8750c2ed82

View File

@ -4,49 +4,46 @@ import { jsonMsgSuffix, sendMessageToUser } from "../../../utils/discord";
import { getMetricFromRegistry, withMetrics } from "../../../common/DoormanHandler"; import { getMetricFromRegistry, withMetrics } from "../../../common/DoormanHandler";
import { NotifyMetrics, registerMetrics } from "../../../metrics/NotifyMetrics"; import { NotifyMetrics, registerMetrics } from "../../../metrics/NotifyMetrics";
import { Counter, Summary } from "prom-client"; import { Counter, Summary } from "prom-client";
import { z } from "zod";
import zu from "zod_utilz";
import { setResponseJson } from "../../../utils/responseUtils";
import { UserAgentHeader } from "../../../utils/blockUserAgent";
export const NotifyRequestSchema = z.object({
key: z.string(),
export interface NotifyRequest extends ServerlessEventObject { // these must be arrays
door: string; discordUser: zu.stringToJSON().refine(arg => arg instanceof Array),
msg: zu.stringToJSON().refine(arg => arg instanceof Array),
json: zu.stringToJSON().refine(arg => arg instanceof Array)
});
key: string; export type NotifyRequest = z.infer<typeof NotifyRequestSchema>;
// these are arrays in the request export interface NotifyRequestTwilio extends ServerlessEventObject<NotifyRequest, UserAgentHeader> { }
discordUser: string;
msg: string;
json: string;
}
export const handler: ServerlessFunctionSignature<TwilioContext, NotifyRequest> = withMetrics('notify', async (context, event, callback, metricsRegistry) => { export const handler: ServerlessFunctionSignature<TwilioContext, NotifyRequestTwilio> = withMetrics('notify', async (context, event, callback, metricsRegistry) => {
const response = new Twilio.Response(); const response = new Twilio.Response();
registerMetrics(metricsRegistry); registerMetrics(metricsRegistry);
const req = NotifyRequestSchema.parse(event);
// secure notify endpoint // secure notify endpoint
if (event.key !== context.NOTIFY_SECRET_KEY) { if (req.key !== context.NOTIFY_SECRET_KEY) {
getMetricFromRegistry<Counter>(metricsRegistry, NotifyMetrics.UNAUTHENTICATED_CALL).inc(1); getMetricFromRegistry<Counter>(metricsRegistry, NotifyMetrics.UNAUTHENTICATED_CALL).inc(1);
response setResponseJson(response, 401, {
.setStatusCode(401) err: "Unauthenticated call", event
.appendHeader('Content-Type', 'application/json') });
.setBody({ err: "Unauthenticated call", event });
return callback(null, response); return callback(null, response);
} }
let users: string[]; let users: string[] = req.discordUser as string[];
let msgs: string[]; let msgs: string[] = req.msg as string[];
let jsons: string[]; let jsons: string[] = req.json as string[];
let promises = []; let promises = [];
try { try {
users = JSON.parse(event.discordUser);
console.log(users);
msgs = JSON.parse(event.msg);
console.log("before parsing", event.json);
jsons = JSON.parse(event.json);
console.log("after parsing", event.json);
const recordNotifyLatency = getMetricFromRegistry<Summary>(metricsRegistry, NotifyMetrics.DISCORD_LATENCY) const recordNotifyLatency = getMetricFromRegistry<Summary>(metricsRegistry, NotifyMetrics.DISCORD_LATENCY)
.startTimer(); .startTimer();
@ -66,10 +63,7 @@ export const handler: ServerlessFunctionSignature<TwilioContext, NotifyRequest>
); );
} catch (e) { } catch (e) {
console.error(e); console.error(e);
response setResponseJson(response, 500, { err: e, event });
.setStatusCode(500)
.appendHeader('Content-Type', 'application/json')
.setBody({ err: e, event });
return callback(null, response); return callback(null, response);
} }