diff --git a/src/app/api/tweet/route.ts b/src/app/api/tweet/route.ts index b44d0ae..30d299d 100644 --- a/src/app/api/tweet/route.ts +++ b/src/app/api/tweet/route.ts @@ -1,26 +1,35 @@ -import { NextRequest, NextResponse } from 'next/server' -import { verifySignatureInTweet } from '../../../utils/verifyTweet'; +import { NextRequest, NextResponse } from 'next/server'; + +import { saveTweet, verifySignatureInTweet } from '../../../utils/verifyTweet'; +import { initializeDataSource } from '../../../data-source'; export async function POST(req: NextRequest): Promise { try { + await initializeDataSource(); + const { tweetUrl } = await req.json(); const url = `https://publish.twitter.com/oembed?url=${tweetUrl}&maxwidth=600`; const response = await fetch(url); const data = await response.json(); - const { handle, memeUrl, txHash } = extractData(data.html); - if (!handle || !memeUrl || !txHash) { + const { handle, txHash } = extractData(data.html); + if (!handle || !txHash) { return NextResponse.json( { error: 'Verification failed' }, { status: 500 } ) } - const isVerified = await verifySignatureInTweet(txHash); + const isSigVerified = await verifySignatureInTweet(txHash); const isHandleCorrect = handle === process.env.ACCOUNT_HANDLE; - return NextResponse.json({isVerified: isVerified && isHandleCorrect}) + const isVerified = isSigVerified && isHandleCorrect; + if (isVerified) { + saveTweet({transactionSignature: txHash}) + } + + return NextResponse.json({ isVerified }) } catch (error) { console.error('Error while verifying tweet:', error) return NextResponse.json( @@ -40,7 +49,7 @@ const extractData = (tweet: string | object) => { const handleMatch = decodedTweet.match(/@([A-Za-z0-9_]+)/); return { - memeUrl: urlMatch ? urlMatch[1] : null, + // memeUrl: urlMatch ? urlMatch[1] : null, txHash: txHashMatch ? txHashMatch[1].trim() : null, handle: handleMatch ? handleMatch[1] : null, }; diff --git a/src/data-source.ts b/src/data-source.ts index 4b30ab1..d81e654 100644 --- a/src/data-source.ts +++ b/src/data-source.ts @@ -1,7 +1,7 @@ import { DataSource } from 'typeorm'; import { Payment } from './entity/Payment'; -import { Tweet } from './entity/Tweets'; +import { Tweet } from './entity/Tweet'; export const AppDataSource = new DataSource({ type: 'sqlite', diff --git a/src/entity/Tweets.ts b/src/entity/Tweet.ts similarity index 100% rename from src/entity/Tweets.ts rename to src/entity/Tweet.ts diff --git a/src/utils/verifyTweet.ts b/src/utils/verifyTweet.ts index eca1fbb..25216ae 100644 --- a/src/utils/verifyTweet.ts +++ b/src/utils/verifyTweet.ts @@ -1,10 +1,6 @@ -import assert from 'assert'; - import { AppDataSource } from '../data-source'; import { Payment } from '../entity/Payment'; -import { Tweet } from '../entity/Tweets'; - -assert(process.env.NEXT_PUBLIC_SOLANA_RPC_URL, 'SOLANA_RPC_URL is required'); +import { Tweet } from '../entity/Tweet'; export async function verifySignatureInTweet(transactionSignature: string): Promise { const paymentRepository = AppDataSource.getRepository(Payment); @@ -23,3 +19,11 @@ export async function verifySignatureInTweet(transactionSignature: string): Prom return true; } + +export async function saveTweet(data: Partial): Promise { + await AppDataSource.transaction(async (transactionalEntityManager) => { + const tweetRepository = transactionalEntityManager.getRepository(Tweet); + + await tweetRepository.save(data); + }); +}