sol-mem-gen/src/app/api/tweet/route.ts
adwait fa168e5c61 Add feature to share generated memes on twitter and verify tweet (#10)
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>
2025-02-06 05:45:02 +00:00

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(/&#39;/g, "'").replace(/&quot;/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');
}