forked from mito-systems/sol-mem-gen
Part of https://www.notion.so/Option-to-post-paid-for-memes-to-twitter-x-18ca6b22d4728051804ef4f55065d5ba - Generate dynamic page with required img and meta tags - Add button to share generated meme to twitter Co-authored-by: Adw8 <adwaitgharpure@gmail.com> Co-authored-by: IshaVenikar <ishavenikar7@gmail.com> Co-authored-by: AdityaSalunkhe21 <adityasalunkhe2204@gmail.com> Co-authored-by: Nabarun <nabarun@deepstacksoft.com> Reviewed-on: #10 Co-authored-by: adwait <adwait@noreply.git.vdb.to> Co-committed-by: adwait <adwait@noreply.git.vdb.to>
67 lines
2.1 KiB
TypeScript
67 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
import { saveTweet, verifySignatureInTweet } from '../../../utils/verifyTweet';
|
|
|
|
export async function POST(req: NextRequest): Promise<NextResponse> {
|
|
try {
|
|
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, txSignature, memeUrl } = extractData(data.html);
|
|
|
|
if (!handle || !txSignature) {
|
|
return NextResponse.json(
|
|
{ error: 'Could not extract tweet data' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
|
|
const isSigVerified = await verifySignatureInTweet(txSignature);
|
|
const isHandleCorrect = handle === process.env.NEXT_PUBLIC_ACCOUNT_HANDLE;
|
|
|
|
// TODO: Verify dynamic page URL in tweet
|
|
const isVerified = isSigVerified && isHandleCorrect;
|
|
if (!isVerified) {
|
|
throw new Error('Tweet is not valid');
|
|
}
|
|
|
|
const { isFourthUser } = await saveTweet({ transactionSignature: txSignature, url: memeUrl });
|
|
if (isFourthUser) {
|
|
createTokenLockForRecipient();
|
|
}
|
|
|
|
return NextResponse.json({ success: isVerified, message: 'Tweet verified' })
|
|
} catch (error) {
|
|
console.error('Error while verifying tweet:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to verify tweet' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
const extractData = (tweet: string | object) => {
|
|
const tweetText = typeof tweet === 'string' ? tweet : JSON.stringify(tweet);
|
|
|
|
const decodedTweet = tweetText.replace(/'/g, "'").replace(/"/g, '"');
|
|
|
|
const urlMatch = decodedTweet.match(/<a href="(https:\/\/t.co\/[^"]+)">/);
|
|
const txSignatureMatch = decodedTweet.match(/TX Hash: '([^']+)'/);
|
|
const handleMatch = decodedTweet.match(/@([A-Za-z0-9_]+)/);
|
|
|
|
return {
|
|
memeUrl: urlMatch ? urlMatch[1] : null,
|
|
txSignature: txSignatureMatch ? txSignatureMatch[1].trim() : null,
|
|
handle: handleMatch ? handleMatch[1] : null,
|
|
};
|
|
};
|
|
|
|
// TODO: Implement function to create lock for a recipient
|
|
const createTokenLockForRecipient = () => {
|
|
console.log('Lock created');
|
|
}
|