Store tweet after verification

This commit is contained in:
IshaVenikar 2025-02-05 18:20:09 +05:30
parent 7c3bf21e24
commit 6741d539af
4 changed files with 26 additions and 13 deletions

View File

@ -1,26 +1,35 @@
import { NextRequest, NextResponse } from 'next/server' import { NextRequest, NextResponse } from 'next/server';
import { verifySignatureInTweet } from '../../../utils/verifyTweet';
import { saveTweet, verifySignatureInTweet } from '../../../utils/verifyTweet';
import { initializeDataSource } from '../../../data-source';
export async function POST(req: NextRequest): Promise<NextResponse> { export async function POST(req: NextRequest): Promise<NextResponse> {
try { try {
await initializeDataSource();
const { tweetUrl } = await req.json(); const { tweetUrl } = await req.json();
const url = `https://publish.twitter.com/oembed?url=${tweetUrl}&maxwidth=600`; const url = `https://publish.twitter.com/oembed?url=${tweetUrl}&maxwidth=600`;
const response = await fetch(url); const response = await fetch(url);
const data = await response.json(); const data = await response.json();
const { handle, memeUrl, txHash } = extractData(data.html); const { handle, txHash } = extractData(data.html);
if (!handle || !memeUrl || !txHash) { if (!handle || !txHash) {
return NextResponse.json( return NextResponse.json(
{ error: 'Verification failed' }, { error: 'Verification failed' },
{ status: 500 } { status: 500 }
) )
} }
const isVerified = await verifySignatureInTweet(txHash); const isSigVerified = await verifySignatureInTweet(txHash);
const isHandleCorrect = handle === process.env.ACCOUNT_HANDLE; 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) { } catch (error) {
console.error('Error while verifying tweet:', error) console.error('Error while verifying tweet:', error)
return NextResponse.json( return NextResponse.json(
@ -40,7 +49,7 @@ const extractData = (tweet: string | object) => {
const handleMatch = decodedTweet.match(/@([A-Za-z0-9_]+)/); const handleMatch = decodedTweet.match(/@([A-Za-z0-9_]+)/);
return { return {
memeUrl: urlMatch ? urlMatch[1] : null, // memeUrl: urlMatch ? urlMatch[1] : null,
txHash: txHashMatch ? txHashMatch[1].trim() : null, txHash: txHashMatch ? txHashMatch[1].trim() : null,
handle: handleMatch ? handleMatch[1] : null, handle: handleMatch ? handleMatch[1] : null,
}; };

View File

@ -1,7 +1,7 @@
import { DataSource } from 'typeorm'; import { DataSource } from 'typeorm';
import { Payment } from './entity/Payment'; import { Payment } from './entity/Payment';
import { Tweet } from './entity/Tweets'; import { Tweet } from './entity/Tweet';
export const AppDataSource = new DataSource({ export const AppDataSource = new DataSource({
type: 'sqlite', type: 'sqlite',

View File

@ -1,10 +1,6 @@
import assert from 'assert';
import { AppDataSource } from '../data-source'; import { AppDataSource } from '../data-source';
import { Payment } from '../entity/Payment'; import { Payment } from '../entity/Payment';
import { Tweet } from '../entity/Tweets'; import { Tweet } from '../entity/Tweet';
assert(process.env.NEXT_PUBLIC_SOLANA_RPC_URL, 'SOLANA_RPC_URL is required');
export async function verifySignatureInTweet(transactionSignature: string): Promise<boolean> { export async function verifySignatureInTweet(transactionSignature: string): Promise<boolean> {
const paymentRepository = AppDataSource.getRepository(Payment); const paymentRepository = AppDataSource.getRepository(Payment);
@ -23,3 +19,11 @@ export async function verifySignatureInTweet(transactionSignature: string): Prom
return true; return true;
} }
export async function saveTweet(data: Partial<Tweet>): Promise<void> {
await AppDataSource.transaction(async (transactionalEntityManager) => {
const tweetRepository = transactionalEntityManager.getRepository(Tweet);
await tweetRepository.save(data);
});
}