From d91967aa1c025f96336dbe8e390ae38ed6845566 Mon Sep 17 00:00:00 2001 From: arthur simas Date: Mon, 29 Apr 2024 04:44:42 -0300 Subject: [PATCH] Revert "fix: inefficient method of extracting url from text (#741)" (#748) * Revert "fix: inefficient method of extracting url from text (#741)" This reverts commit f5c1affc4d97c00e01cdbff6e2575a18ddf896fc. * fix: link preview should follow redirects * fix: allow link preview of URLs with @ sign --------- Co-authored-by: Ezequiel Moraes --- src/Defaults/index.ts | 1 - src/Utils/messages.ts | 6 ++---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index c21e2bb5766..564581f0cd7 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -40,7 +40,6 @@ export const PROTOCOL_VERSION = [5, 2] export const MOBILE_NOISE_HEADER = Buffer.concat([Buffer.from('WA'), Buffer.from(PROTOCOL_VERSION)]) /** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */ export const URL_REGEX = /(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/ -export const URL_EXCLUDE_REGEX = /[\w-\.]+@([\w-]+\.)+[\w-]{2,4}/ export const WA_CERT_DETAILS = { SERIAL: 0, diff --git a/src/Utils/messages.ts b/src/Utils/messages.ts index 42d477a8234..8681d4b8ad0 100644 --- a/src/Utils/messages.ts +++ b/src/Utils/messages.ts @@ -4,7 +4,7 @@ import { randomBytes } from 'crypto' import { promises as fs } from 'fs' import { Logger } from 'pino' import { proto } from '../../WAProto' -import { MEDIA_KEYS, URL_EXCLUDE_REGEX, URL_REGEX, WA_DEFAULT_EPHEMERAL } from '../Defaults' +import { MEDIA_KEYS, URL_REGEX, WA_DEFAULT_EPHEMERAL } from '../Defaults' import { AnyMediaMessageContent, AnyMessageContent, @@ -68,9 +68,7 @@ const ButtonType = proto.Message.ButtonsMessage.HeaderType * @param text eg. hello https://google.com * @returns the URL, eg. https://google.com */ -export const extractUrlFromText = (text: string) => ( - text.split(' ').find(word => URL_REGEX.test(word) && !URL_EXCLUDE_REGEX.test(word)) || undefined -) +export const extractUrlFromText = (text: string) => text.match(URL_REGEX)?.[0] export const generateLinkPreviewIfRequired = async(text: string, getUrlInfo: MessageGenerationOptions['getUrlInfo'], logger: MessageGenerationOptions['logger']) => { const url = extractUrlFromText(text)