Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -640,12 +640,17 @@ export class BaileysStartupService extends ChannelStartupService {
}

// Fetch latest WhatsApp Web version automatically
const baileysVersion = await fetchLatestWaWebVersion({});
const baileysVersion = await fetchLatestWaWebVersion({}, this.cache);
const version = baileysVersion.version;

const log = `Baileys version: ${version.join('.')}`;
this.logger.info(log);

const error = baileysVersion?.error ?? null;
if (error) {
this.logger.error(`Fetch latest WaWeb version error: ${JSON.stringify({ error })}`);
}

this.logger.info(`Group Ignore: ${this.localSettings.groupsIgnore}`);

let options;
Expand Down
60 changes: 56 additions & 4 deletions src/utils/fetchLatestWaWebVersion.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import axios, { AxiosRequestConfig } from 'axios';
import { fetchLatestBaileysVersion, WAVersion } from 'baileys';

import { CacheService } from '../api/services/cache.service';
import { CacheEngine } from '../cache/cacheengine';
import { Baileys, configService } from '../config/env.config';

export const fetchLatestWaWebVersion = async (options: AxiosRequestConfig<{}>) => {
// Cache keys
const CACHE_KEY_WHATSAPP_WEB_VERSION = 'whatsapp_web_version';
const CACHE_KEY_BAILEYS_FALLBACK_VERSION = 'baileys_fallback_version';

// Cache TTL (1 hour in seconds)
const CACHE_TTL_SECONDS = 3600;

const MODULE_NAME = 'whatsapp-version';

export const fetchLatestWaWebVersion = async (options: AxiosRequestConfig<{}>, cache?: CacheService) => {
// Check if manual version is set via configuration
const baileysConfig = configService.get<Baileys>('BAILEYS');
const manualVersion = baileysConfig?.VERSION;
Expand All @@ -19,6 +30,22 @@ export const fetchLatestWaWebVersion = async (options: AxiosRequestConfig<{}>) =
}
}

let versionCache = cache || null;

if (!versionCache) {
// Cache estático para versões do WhatsApp Web e fallback do Baileys (fallback se não for passado via parâmetro)
const cacheEngine = new CacheEngine(configService, MODULE_NAME);
const engine = cacheEngine.getEngine();
const defaultVersionCache = new CacheService(engine);
versionCache = defaultVersionCache;
}

// Check cache for WhatsApp Web version
const cachedWaVersion = await versionCache.get(CACHE_KEY_WHATSAPP_WEB_VERSION);
if (cachedWaVersion) {
return cachedWaVersion;
}

try {
const { data } = await axios.get('https://web.whatsapp.com/sw.js', {
...options,
Expand All @@ -29,26 +56,51 @@ export const fetchLatestWaWebVersion = async (options: AxiosRequestConfig<{}>) =
const match = data.match(regex);

if (!match?.[1]) {
return {
// Check cache for Baileys fallback version
const cachedFallback = await versionCache.get(CACHE_KEY_BAILEYS_FALLBACK_VERSION);
if (cachedFallback) {
return cachedFallback;
}

// Fetch and cache Baileys fallback version
const fallbackVersion = {
version: (await fetchLatestBaileysVersion()).version as WAVersion,
isLatest: false,
error: {
message: 'Could not find client revision in the fetched content',
},
};

await versionCache.set(CACHE_KEY_BAILEYS_FALLBACK_VERSION, fallbackVersion, CACHE_TTL_SECONDS);
return fallbackVersion;
}

const clientRevision = match[1];

return {
const result = {
version: [2, 3000, +clientRevision] as WAVersion,
isLatest: true,
};

// Cache the successful result
await versionCache.set(CACHE_KEY_WHATSAPP_WEB_VERSION, result, CACHE_TTL_SECONDS);

return result;
} catch (error) {
return {
// Check cache for Baileys fallback version
const cachedFallback = await versionCache.get(CACHE_KEY_BAILEYS_FALLBACK_VERSION);
if (cachedFallback) {
return cachedFallback;
}

// Fetch and cache Baileys fallback version
const fallbackVersion = {
version: (await fetchLatestBaileysVersion()).version as WAVersion,
isLatest: false,
error,
};

await versionCache.set(CACHE_KEY_BAILEYS_FALLBACK_VERSION, fallbackVersion, CACHE_TTL_SECONDS);
return fallbackVersion;
}
};