327 lines
8.6 KiB
TypeScript
327 lines
8.6 KiB
TypeScript
import { NextResponse } from 'next/server'
|
||
import { sdk } from '@farcaster/miniapp-sdk'
|
||
|
||
/**
|
||
* FarCastor Frame Interaction Handler
|
||
*
|
||
* This endpoint handles interactions from FarCastor Frames including:
|
||
* - Frame button clicks
|
||
* - Vote submissions
|
||
* - Share actions
|
||
* - Navigation
|
||
*/
|
||
|
||
export async function POST(request: Request) {
|
||
try {
|
||
const body = await request.json()
|
||
const { untrustedData } = body
|
||
|
||
if (!untrustedData) {
|
||
return NextResponse.json({ error: 'Missing untrustedData' }, { status: 400 })
|
||
}
|
||
|
||
const { fid, message } = untrustedData
|
||
const buttonIndex = untrustedData.buttonIndex
|
||
const inputText = untrustedData.inputText
|
||
const pollId = extractPollId(untrustedData)
|
||
|
||
console.log('Frame interaction:', {
|
||
fid,
|
||
buttonIndex,
|
||
pollId,
|
||
inputText,
|
||
message: message?.substring(0, 100)
|
||
})
|
||
|
||
// Route based on button clicked
|
||
switch (buttonIndex) {
|
||
case 1:
|
||
return handlePrimaryAction(pollId, untrustedData)
|
||
case 2:
|
||
return handleSecondaryAction(pollId, untrustedData)
|
||
case 3:
|
||
return handleTertiaryAction(pollId, untrustedData)
|
||
default:
|
||
return handleDefaultAction(pollId, untrustedData)
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('Frame interaction error:', error)
|
||
return NextResponse.json(
|
||
{ error: 'Internal server error' },
|
||
{ status: 500 }
|
||
)
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Extract poll ID from frame data or URL
|
||
*/
|
||
function extractPollId(untrustedData: any): string | null {
|
||
// Try to extract from various sources
|
||
const sources = [
|
||
untrustedData.url,
|
||
untrustedData.inputText,
|
||
untrustedData?.pollcelo?.pollId
|
||
]
|
||
|
||
for (const source of sources) {
|
||
if (typeof source === 'string') {
|
||
const pollIdMatch = source.match(/\/poll\/([^\/\?]+)/)
|
||
if (pollIdMatch) {
|
||
return pollIdMatch[1]
|
||
}
|
||
}
|
||
}
|
||
|
||
return null
|
||
}
|
||
|
||
/**
|
||
* Handle primary button action (usually "Vote Now" or "View Results")
|
||
*/
|
||
async function handlePrimaryAction(pollId: string | null, untrustedData: any) {
|
||
if (pollId) {
|
||
// Navigate to poll page
|
||
const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
|
||
const pollUrl = `${appUrl}/poll/${pollId}`
|
||
|
||
return NextResponse.json({
|
||
type: 'frame',
|
||
frame: {
|
||
version: 'vNext',
|
||
imageUrl: `${appUrl}/api/og?pollId=${pollId}`,
|
||
buttons: [
|
||
{
|
||
title: '๐ณ๏ธ Vote Now',
|
||
action: 'link',
|
||
target: pollUrl
|
||
},
|
||
{
|
||
title: '๐ Live Results',
|
||
action: 'link',
|
||
target: pollUrl
|
||
},
|
||
{
|
||
title: '๐ Share Poll',
|
||
action: 'link',
|
||
target: `https://warpcast.com/~/compose?text=${encodeURIComponent(`Vote on this poll!`)}&embeds[]=${encodeURIComponent(pollUrl)}`
|
||
}
|
||
],
|
||
postUrl: `${appUrl}/api/frame/interact`,
|
||
state: { pollId }
|
||
}
|
||
})
|
||
} else {
|
||
// Navigate to home page
|
||
const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
|
||
|
||
return NextResponse.json({
|
||
type: 'frame',
|
||
frame: {
|
||
version: 'vNext',
|
||
imageUrl: `${appUrl}/api/og?mode=home`,
|
||
buttons: [
|
||
{
|
||
title: '๐ณ๏ธ View Today\'s Poll',
|
||
action: 'link',
|
||
target: appUrl
|
||
},
|
||
{
|
||
title: '๐ Browse History',
|
||
action: 'link',
|
||
target: `${appUrl}/history`
|
||
},
|
||
{
|
||
title: 'โน๏ธ How it Works',
|
||
action: 'link',
|
||
target: `${appUrl}/about`
|
||
}
|
||
],
|
||
postUrl: `${appUrl}/api/frame/interact`
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Handle secondary button action
|
||
*/
|
||
async function handleSecondaryAction(pollId: string | null, untrustedData: any) {
|
||
const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
|
||
|
||
if (pollId) {
|
||
// Show detailed results or statistics
|
||
const resultsUrl = `${appUrl}/poll/${pollId}?view=results`
|
||
|
||
return NextResponse.json({
|
||
type: 'frame',
|
||
frame: {
|
||
version: 'vNext',
|
||
imageUrl: `${appUrl}/api/og?pollId=${pollId}&mode=results`,
|
||
buttons: [
|
||
{
|
||
title: '๐ Full Results',
|
||
action: 'link',
|
||
target: resultsUrl
|
||
},
|
||
{
|
||
title: '๐ Leaderboard',
|
||
action: 'link',
|
||
target: `${appUrl}/leaderboard`
|
||
},
|
||
{
|
||
title: '๐ New Poll',
|
||
action: 'link',
|
||
target: appUrl
|
||
}
|
||
],
|
||
postUrl: `${appUrl}/api/frame/interact`,
|
||
state: { pollId, view: 'results' }
|
||
}
|
||
})
|
||
} else {
|
||
// Show user history or portfolio
|
||
return NextResponse.json({
|
||
type: 'frame',
|
||
frame: {
|
||
version: 'vNext',
|
||
imageUrl: `${appUrl}/api/og?mode=home`,
|
||
buttons: [
|
||
{
|
||
title: '๐ My History',
|
||
action: 'link',
|
||
target: `${appUrl}/history?user=${untrustedData.fid}`
|
||
},
|
||
{
|
||
title: '๐ฐ My Winnings',
|
||
action: 'link',
|
||
target: `${appUrl}/winnings?user=${untrustedData.fid}`
|
||
}
|
||
],
|
||
postUrl: `${appUrl}/api/frame/interact`,
|
||
state: { userFid: untrustedData.fid }
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Handle tertiary button action
|
||
*/
|
||
async function handleTertiaryAction(pollId: string | null, untrustedData: any) {
|
||
const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
|
||
|
||
if (pollId) {
|
||
// Share functionality
|
||
const shareText = `Vote on this poll and win cUSD! ๐`
|
||
const pollUrl = `${appUrl}/poll/${pollId}`
|
||
|
||
return NextResponse.json({
|
||
type: 'frame',
|
||
frame: {
|
||
version: 'vNext',
|
||
imageUrl: `${appUrl}/api/og?pollId=${pollId}`,
|
||
buttons: [
|
||
{
|
||
title: '๐ข Share on Warpcast',
|
||
action: 'link',
|
||
target: `https://warpcast.com/~/compose?text=${encodeURIComponent(shareText)}&embeds[]=${encodeURIComponent(pollUrl)}`
|
||
},
|
||
{
|
||
title: '๐ Copy Link',
|
||
action: 'link',
|
||
target: `https://warpcast.com/~/compose?text=${encodeURIComponent(pollUrl)}`
|
||
},
|
||
{
|
||
title: '๐ฑ Back to Poll',
|
||
action: 'link',
|
||
target: pollUrl
|
||
}
|
||
],
|
||
postUrl: `${appUrl}/api/frame/interact`,
|
||
state: { pollId, action: 'share' }
|
||
}
|
||
})
|
||
} else {
|
||
// Help or about page
|
||
return NextResponse.json({
|
||
type: 'frame',
|
||
frame: {
|
||
version: 'vNext',
|
||
imageUrl: `${appUrl}/api/og?mode=home`,
|
||
buttons: [
|
||
{
|
||
title: 'โ How it Works',
|
||
action: 'link',
|
||
target: `${appUrl}/how-it-works`
|
||
},
|
||
{
|
||
title: '๐ก Create Poll',
|
||
action: 'link',
|
||
target: `${appUrl}/create`
|
||
},
|
||
{
|
||
title: '๐ Back Home',
|
||
action: 'link',
|
||
target: appUrl
|
||
}
|
||
],
|
||
postUrl: `${appUrl}/api/frame/interact`,
|
||
state: { action: 'help' }
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Handle default action (fallback)
|
||
*/
|
||
async function handleDefaultAction(pollId: string | null, untrustedData: any) {
|
||
const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'
|
||
|
||
return NextResponse.json({
|
||
type: 'frame',
|
||
frame: {
|
||
version: 'vNext',
|
||
imageUrl: pollId
|
||
? `${appUrl}/api/og?pollId=${pollId}`
|
||
: `${appUrl}/api/og?mode=home`,
|
||
buttons: [
|
||
{
|
||
title: '๐ณ๏ธ Vote Now',
|
||
action: 'link',
|
||
target: pollId ? `${appUrl}/poll/${pollId}` : appUrl
|
||
},
|
||
{
|
||
title: '๐ View Results',
|
||
action: 'link',
|
||
target: pollId ? `${appUrl}/poll/${pollId}` : appUrl
|
||
},
|
||
{
|
||
title: '๐ Share',
|
||
action: 'link',
|
||
target: `https://warpcast.com/~/compose?text=${encodeURIComponent('Check out PollCelo - Daily prediction markets with cUSD rewards!')}&embeds[]=${encodeURIComponent(appUrl)}`
|
||
}
|
||
],
|
||
postUrl: `${appUrl}/api/frame/interact`,
|
||
state: { pollId, timestamp: Date.now() }
|
||
}
|
||
})
|
||
}
|
||
|
||
/**
|
||
* Validate frame interaction
|
||
*/
|
||
function validateInteraction(untrustedData: any): { valid: boolean; error?: string } {
|
||
if (!untrustedData.fid) {
|
||
return { valid: false, error: 'Missing user FID' }
|
||
}
|
||
|
||
if (!untrustedData.buttonIndex || untrustedData.buttonIndex < 1 || untrustedData.buttonIndex > 3) {
|
||
return { valid: false, error: 'Invalid button index' }
|
||
}
|
||
|
||
return { valid: true }
|
||
}
|