Skip to content

Commit

Permalink
Revert "fix: inefficient method of extracting url from text (#741)" (#…
Browse files Browse the repository at this point in the history
…748)

* Revert "fix: inefficient method of extracting url from text (#741)"

This reverts commit f5c1aff.

* fix: link preview should follow redirects

* fix: allow link preview of URLs with @ sign

---------

Co-authored-by: Ezequiel Moraes <[email protected]>
  • Loading branch information
arthursimas1 and ezeksoft authored Apr 29, 2024
1 parent eb5eddd commit d91967a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/Defaults/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions src/Utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d91967a

Please sign in to comment.