fix: exclude emails from URL gen

This commit is contained in:
Adhiraj Singh
2022-08-02 08:38:10 +05:30
parent 2549d10be9
commit a4208a4bf6
2 changed files with 14 additions and 4 deletions

View File

@@ -20,6 +20,7 @@ export const NOISE_WA_HEADER = Buffer.from(
) // last is "DICT_VERSION"
/** from: https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url */
export const URL_REGEX = /[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)?/gi
export const URL_EXCLUDE_REGEX = /.*@.*/
export const WA_CERT_DETAILS = {
SERIAL: 0,

View File

@@ -3,7 +3,7 @@ import axios from 'axios'
import { promises as fs } from 'fs'
import { Logger } from 'pino'
import { proto } from '../../WAProto'
import { MEDIA_KEYS, URL_REGEX, WA_DEFAULT_EPHEMERAL } from '../Defaults'
import { MEDIA_KEYS, URL_EXCLUDE_REGEX, URL_REGEX, WA_DEFAULT_EPHEMERAL } from '../Defaults'
import {
AnyMediaMessageContent,
AnyMessageContent,
@@ -57,11 +57,20 @@ const MessageTypeProto = {
const ButtonType = proto.Message.ButtonsMessage.HeaderType
/**
* Uses a regex to test whether the string contains a URL, and returns the URL if it does.
* @param text eg. hello https://google.com
* @returns the URL, eg. https://google.com
*/
export const extractUrlFromText = (text: string) => (
!URL_EXCLUDE_REGEX.test(text) ? text.match(URL_REGEX)?.[0] : undefined
)
export const generateLinkPreviewIfRequired = async(text: string, getUrlInfo: MessageGenerationOptions['getUrlInfo'], logger: MessageGenerationOptions['logger']) => {
const matchedUrls = text.match(URL_REGEX)
if(!!getUrlInfo && matchedUrls) {
const url = extractUrlFromText(text)
if(!!getUrlInfo && url) {
try {
const urlInfo = await getUrlInfo(matchedUrls[0])
const urlInfo = await getUrlInfo(url)
return urlInfo
} catch(error) { // ignore if fails
logger?.warn({ trace: error.stack }, 'url generation failed')