mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-11-19 09:26:07 -05:00
use neynar notifs
This commit is contained in:
@@ -3,6 +3,7 @@ import { NextRequest } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { setUserNotificationDetails } from "~/lib/kv";
|
||||
import { sendFrameNotification } from "~/lib/notifs";
|
||||
import { sendNeynarFrameNotification } from "~/lib/neynar";
|
||||
|
||||
const requestSchema = z.object({
|
||||
fid: z.number(),
|
||||
@@ -10,6 +11,10 @@ const requestSchema = z.object({
|
||||
});
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
// If Neynar is enabled, we don't need to store notification details
|
||||
// as they will be managed by Neynar's system
|
||||
const neynarEnabled = process.env.NEYNAR_API_KEY && process.env.NEYNAR_CLIENT_ID;
|
||||
|
||||
const requestJson = await request.json();
|
||||
const requestBody = requestSchema.safeParse(requestJson);
|
||||
|
||||
@@ -20,13 +25,18 @@ export async function POST(request: NextRequest) {
|
||||
);
|
||||
}
|
||||
|
||||
await setUserNotificationDetails(
|
||||
requestBody.data.fid,
|
||||
requestBody.data.notificationDetails
|
||||
);
|
||||
// Only store notification details if not using Neynar
|
||||
if (!neynarEnabled) {
|
||||
await setUserNotificationDetails(
|
||||
Number(requestBody.data.fid),
|
||||
requestBody.data.notificationDetails
|
||||
);
|
||||
}
|
||||
|
||||
const sendResult = await sendFrameNotification({
|
||||
fid: requestBody.data.fid,
|
||||
// Use appropriate notification function based on Neynar status
|
||||
const sendNotification = neynarEnabled ? sendNeynarFrameNotification : sendFrameNotification;
|
||||
const sendResult = await sendNotification({
|
||||
fid: Number(requestBody.data.fid),
|
||||
title: "Test notification",
|
||||
body: "Sent at " + new Date().toISOString(),
|
||||
});
|
||||
|
||||
@@ -11,6 +11,13 @@ import {
|
||||
import { sendFrameNotification } from "~/lib/notifs";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
// If Neynar is enabled, we don't need to handle webhooks here
|
||||
// as they will be handled by Neynar's webhook endpoint
|
||||
const neynarEnabled = process.env.NEYNAR_API_KEY && process.env.NEYNAR_CLIENT_ID;
|
||||
if (neynarEnabled) {
|
||||
return Response.json({ success: true });
|
||||
}
|
||||
|
||||
const requestJson = await request.json();
|
||||
|
||||
let data;
|
||||
@@ -45,6 +52,8 @@ export async function POST(request: NextRequest) {
|
||||
const fid = data.fid;
|
||||
const event = data.event;
|
||||
|
||||
// Only handle notifications if Neynar is not enabled
|
||||
// When Neynar is enabled, notifications are handled through their webhook
|
||||
switch (event.event) {
|
||||
case "frame_added":
|
||||
if (event.notificationDetails) {
|
||||
@@ -57,12 +66,12 @@ export async function POST(request: NextRequest) {
|
||||
} else {
|
||||
await deleteUserNotificationDetails(fid);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case "frame_removed":
|
||||
await deleteUserNotificationDetails(fid);
|
||||
|
||||
break;
|
||||
|
||||
case "notifications_enabled":
|
||||
await setUserNotificationDetails(fid, event.notificationDetails);
|
||||
await sendFrameNotification({
|
||||
@@ -70,11 +79,10 @@ export async function POST(request: NextRequest) {
|
||||
title: "Ding ding ding",
|
||||
body: "Notifications are now enabled",
|
||||
});
|
||||
|
||||
break;
|
||||
|
||||
case "notifications_disabled":
|
||||
await deleteUserNotificationDetails(fid);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user