-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathroute.ts
More file actions
82 lines (72 loc) · 2.27 KB
/
Copy pathroute.ts
File metadata and controls
82 lines (72 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {
ParseWebhookEvent,
parseWebhookEvent,
verifyAppKeyWithNeynar,
} from "@farcaster/miniapp-node";
import { NextRequest } from "next/server";
import {
deleteUserNotificationDetails,
setUserNotificationDetails,
} from "~/lib/kv";
import { sendFrameNotification } from "~/lib/notifs";
export async function POST(request: NextRequest) {
const requestJson = await request.json();
let data;
try {
data = await parseWebhookEvent(requestJson, verifyAppKeyWithNeynar);
} catch (e: unknown) {
const error = e as ParseWebhookEvent.ErrorType;
switch (error.name) {
case "VerifyJsonFarcasterSignature.InvalidDataError":
case "VerifyJsonFarcasterSignature.InvalidEventDataError":
// The request data is invalid
return Response.json(
{ success: false, error: error.message },
{ status: 400 }
);
case "VerifyJsonFarcasterSignature.InvalidAppKeyError":
// The app key is invalid
return Response.json(
{ success: false, error: error.message },
{ status: 401 }
);
case "VerifyJsonFarcasterSignature.VerifyAppKeyError":
// Internal error verifying the app key (caller may want to try again)
return Response.json(
{ success: false, error: error.message },
{ status: 500 }
);
}
}
const fid = data.fid;
const event = data.event;
switch (event.event) {
case "miniapp_added":
if (event.notificationDetails) {
await setUserNotificationDetails(fid, event.notificationDetails);
await sendFrameNotification({
fid,
title: "Welcome to Frames v2",
body: "Mini app is now added to your client",
});
} else {
await deleteUserNotificationDetails(fid);
}
break;
case "miniapp_removed":
await deleteUserNotificationDetails(fid);
break;
case "notifications_enabled":
await setUserNotificationDetails(fid, event.notificationDetails);
await sendFrameNotification({
fid,
title: "Ding ding ding",
body: "Notifications are now enabled",
});
break;
case "notifications_disabled":
await deleteUserNotificationDetails(fid);
break;
}
return Response.json({ success: true });
}