Refactor out tweet text generation and regex logic

This commit is contained in:
IshaVenikar 2025-02-06 16:00:41 +05:30 committed by Adw8
parent 299e06f769
commit bfda39830a
3 changed files with 23 additions and 17 deletions

View File

@ -1,6 +1,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { processTweet, verifySignatureInTweet } from '../../../utils/verifyTweet';
import { extractData } from '../../../utils/tweetMessage';
export async function POST(req: NextRequest): Promise<NextResponse> {
try {
@ -41,19 +42,3 @@ export async function POST(req: NextRequest): Promise<NextResponse> {
)
}
}
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,
};
};

View File

@ -5,6 +5,8 @@ import BN from 'bn.js';
import Big from 'big.js';
import dynamic from 'next/dynamic'
import { generateTweetText } from '../utils/tweetMessage';
interface AIServiceCardProps {
title: string
description: string
@ -95,7 +97,7 @@ const AIServiceCard: React.FC<AIServiceCardProps> = ({
const cid = imageUrl.split("/image/")[1];
const memePageUrl = `${baseUrl}memes/${cid}`;
const tweetText = `Check out this meme that I generated! \n TX Hash: '${transactionSignature}' \n @${process.env.NEXT_PUBLIC_TWITTER_HANDLE} \n`;
const tweetText = generateTweetText(transactionSignature, process.env.NEXT_PUBLIC_TWITTER_HANDLE)
return `https://twitter.com/intent/tweet?text=${encodeURIComponent(tweetText)}&url=${encodeURIComponent(memePageUrl)}`;
};

19
src/utils/tweetMessage.ts Normal file
View File

@ -0,0 +1,19 @@
export const generateTweetText = (transactionSignature: string, handle: string | undefined) => {
return `Check out this meme that I generated! \n TX Hash: '${transactionSignature}' \n @${handle} \n`;
};
export 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,
};
};