diff --git a/src/Defaults/index.ts b/src/Defaults/index.ts index 1ac91a8..23cbc59 100644 --- a/src/Defaults/index.ts +++ b/src/Defaults/index.ts @@ -37,6 +37,7 @@ const BASE_CONNECTION_CONFIG: CommonSocketConfig = { export const DEFAULT_CONNECTION_CONFIG: SocketConfig = { ...BASE_CONNECTION_CONFIG, waWebSocketUrl: 'wss://web.whatsapp.com/ws/chat', + linkPreviewImageThumbnailWidth: 192, getMessage: async() => undefined } diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index 6deb258..bb254d3 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -9,7 +9,7 @@ import { BinaryNode, BinaryNodeAttributes, getBinaryNodeChild, getBinaryNodeChil import { makeGroupsSocket } from './groups' export const makeMessagesSocket = (config: SocketConfig) => { - const { logger } = config + const { logger, linkPreviewImageThumbnailWidth } = config const sock = makeGroupsSocket(config) const { ev, @@ -485,7 +485,7 @@ export const makeMessagesSocket = (config: SocketConfig) => { logger, userJid, // multi-device does not have this yet - getUrlInfo: getUrlInfo, + getUrlInfo: text => getUrlInfo(text, { thumbnailWidth: linkPreviewImageThumbnailWidth }), upload: waUploadToServer, mediaCache: config.mediaCache, ...options, diff --git a/src/Types/index.ts b/src/Types/index.ts index d646b70..995841a 100644 --- a/src/Types/index.ts +++ b/src/Types/index.ts @@ -19,6 +19,8 @@ export type SocketConfig = CommonSocketConfig & { userDevicesCache?: NodeCache /** map to store the retry counts for failed messages */ msgRetryCounterMap?: { [msgId: string]: number } + /** width for link preview images */ + linkPreviewImageThumbnailWidth: number /** * fetch a message from your store * implement this so that messages failed to send (solves the "this message can take a while" issue) can be retried diff --git a/src/Utils/link-preview.ts b/src/Utils/link-preview.ts index ffd484b..f84116d 100644 --- a/src/Utils/link-preview.ts +++ b/src/Utils/link-preview.ts @@ -1,22 +1,29 @@ import { WAUrlInfo } from '../Types' import { extractImageThumb, getHttpStream } from './messages-media' -const THUMBNAIL_WIDTH_PX = 128 +const THUMBNAIL_WIDTH_PX = 192 /** Fetches an image and generates a thumbnail for it */ -const getCompressedJpegThumbnail = async(url: string) => { +const getCompressedJpegThumbnail = async(url: string, thumbnailWidth: number) => { const stream = await getHttpStream(url) - const result = await extractImageThumb(stream, THUMBNAIL_WIDTH_PX) + const result = await extractImageThumb(stream, thumbnailWidth) return result } +export type URLGenerationOptions = { + thumbnailWidth: number +} + /** * Given a piece of text, checks for any URL present, generates link preview for the same and returns it * Return undefined if the fetch failed or no URL was found * @param text the text containing URL * @returns the URL info required to generate link preview */ -export const getUrlInfo = async(text: string): Promise => { +export const getUrlInfo = async( + text: string, + opts: URLGenerationOptions = { thumbnailWidth: THUMBNAIL_WIDTH_PX } +): Promise => { try { const { getLinkPreview } = await import('link-preview-js') @@ -24,7 +31,9 @@ export const getUrlInfo = async(text: string): Promise => if(info && 'title' in info) { const [image] = info.images - const jpegThumbnail = image ? await getCompressedJpegThumbnail(image) : undefined + const jpegThumbnail = image + ? await getCompressedJpegThumbnail(image, opts.thumbnailWidth) + : undefined return { 'canonical-url': info.url,