forked from mito-systems/sol-mem-gen
Store tweet after verification
This commit is contained in:
parent
7c3bf21e24
commit
6741d539af
@ -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<NextResponse> {
|
||||
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,
|
||||
};
|
||||
|
@ -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',
|
||||
|
@ -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<boolean> {
|
||||
const paymentRepository = AppDataSource.getRepository(Payment);
|
||||
@ -23,3 +19,11 @@ export async function verifySignatureInTweet(transactionSignature: string): Prom
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user