2025-12-08 16:31:30 +03:00

327 lines
8.6 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 }
}