Skip to content

Commit

Permalink
fix: modified some parts in helper func
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishek-01k committed Oct 3, 2024
1 parent e454e13 commit d53f65a
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions packages/uiweb/src/lib/components/chat/helpers/twitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ export const checkTwitterUrl = (message: string): TwitterFeedReturnType => {
const URL_REGEX = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/)?([\w#!:.?+=&%@!-]+)/;
const messageContent = typeof message === 'string' ? message.split(' ') : [];

for (let i = 0; i < messageContent?.length; i++) {
if (
(URL_REGEX.test(messageContent[i]) && messageContent[i].toLowerCase().includes('twitter')) ||
messageContent[i].toLowerCase().includes('x')
) {
// Extracting tweetId
const wordArray = messageContent[i].split('?')[0].split('/'); // split url at '?' and take first element and split at '/'
if (wordArray?.length >= 6) {
tweetId = wordArray[wordArray?.length - 1];
messageContent?.forEach((message) => {
if (isTweet) return; // Exit the iteration if the tweet was already found

const lowerCaseMessage = message.toLowerCase();

// Check if the message contains a Twitter URL or the letter 'x'
if (URL_REGEX.test(message) && (lowerCaseMessage.includes('twitter') || lowerCaseMessage.includes('x'))) {
// Extract tweetId by splitting the URL before the '?' and then splitting by '/'
const urlParts = message.split('?')[0].split('/');

// Ensure the URL has at least 6 parts to extract the tweetId
if (urlParts.length >= 6) {
tweetId = urlParts[urlParts.length - 1];
isTweet = true;
break;
} else {
isTweet = false;
break;
}
}
}
});

return { tweetId, isTweet };
};

0 comments on commit d53f65a

Please sign in to comment.