Compare commits

..
33 Commits
Author SHA1 Message Date
devlikepro 900e3c23bc [core] 2024.12.5
Release / WEBJS - chrome - amd64 - chrome (push) Waiting to run
Release / WEBJS - chromium - amd64 - latest (push) Waiting to run
Release / WEBJS - chromium - linux/arm64 - arm (push) Waiting to run
Release / NOWEB - none - amd64 - noweb (push) Waiting to run
Release / NOWEB - none - linux/arm64 - noweb-arm (push) Waiting to run
2024-12-16 11:56:50 +07:00
devlikepro aaeffeb983 [core] Update libsignal 2024-12-16 11:56:39 +07:00
devlikepro 15600324be [core] Update NOWEB 2024-12-16 11:56:39 +07:00
devlikepro da4913edb5 [core] Use libsignal fork
Add logging
2024-12-16 11:56:39 +07:00
devlikepro e8cc1e4906 [core] 2024.12.4
Release / WEBJS - chrome - amd64 - chrome (push) Waiting to run
Release / WEBJS - chromium - amd64 - latest (push) Waiting to run
Release / WEBJS - chromium - linux/arm64 - arm (push) Waiting to run
Release / NOWEB - none - amd64 - noweb (push) Waiting to run
Release / NOWEB - none - linux/arm64 - noweb-arm (push) Waiting to run
2024-12-14 19:15:09 +07:00
devlikepro 801fc3f9fd [core] Join group via code
fix #688
2024-12-14 19:14:56 +07:00
devlikepro 703b0b4cde [core] Fix groups sort by
Release / WEBJS - chrome - amd64 - chrome (push) Waiting to run
Release / WEBJS - chromium - amd64 - latest (push) Waiting to run
Release / WEBJS - chromium - linux/arm64 - arm (push) Waiting to run
Release / NOWEB - none - amd64 - noweb (push) Waiting to run
Release / NOWEB - none - linux/arm64 - noweb-arm (push) Waiting to run
2024-12-09 11:30:39 +07:00
devlikepro d55895389a [core] 2024.12.3 2024-12-09 10:37:35 +07:00
devlikepro 56009e482b [core] Add dedicated POST /api/{session}/groups/refresh endpoint 2024-12-09 10:19:38 +07:00
devlikepro 767d1bb3e3 [core] Fix 'rate-overlimit' error for groups
fix #462
2024-12-08 18:23:39 +07:00
devlikepro 05132ac0f5 [core] Update NOWEB 1018710061 2024-12-08 18:23:39 +07:00
devlikepro fb372f8c28 [core] Set WHATSAPP_DEFAULT_ENGINE at build time
fix #414
2024-12-08 13:52:43 +07:00
devlikepro a2dc863b7a [core] 12.2
Release / amd64 - chrome - chrome (push) Waiting to run
Release / amd64 - chromium - latest (push) Waiting to run
Release / linux/arm64 - chromium - arm (push) Waiting to run
Release / amd64 - none - noweb (push) Waiting to run
Release / linux/arm64 - none - noweb-arm (push) Waiting to run
2024-12-07 00:15:49 +07:00
devlikepro 15671c6781 [core] NOWEB - potential fix for #671 2024-12-07 00:15:34 +07:00
devlikepro 6296115adb [core] Cache keys between restart 2024-12-07 00:15:34 +07:00
devlikepro 0a099a91d0 [core] 2024.12.1
Release / amd64 - chrome - chrome (push) Waiting to run
Release / amd64 - chromium - latest (push) Waiting to run
Release / linux/arm64 - chromium - arm (push) Waiting to run
Release / amd64 - none - noweb (push) Waiting to run
Release / linux/arm64 - none - noweb-arm (push) Waiting to run
2024-12-06 10:09:04 +07:00
devlikepro 5f060fe022 [core] Fix channel messages
fix #683
2024-12-06 10:09:03 +07:00
devlikepro a4bbe1cf7b [core] Fix GET channel by id
fix #682
2024-12-06 10:09:03 +07:00
devlikepro dce74ae7d5 [core] 2024.11.11
Release / amd64 - chrome - chrome (push) Waiting to run
Release / amd64 - chromium - latest (push) Waiting to run
Release / linux/arm64 - chromium - arm (push) Waiting to run
Release / amd64 - none - noweb (push) Waiting to run
Release / linux/arm64 - none - noweb-arm (push) Waiting to run
2024-12-01 16:23:50 +07:00
devlikepro 871c509493 [core] Up WEBJS
fix #664
2024-12-01 16:23:39 +07:00
devlikepro 9333470e3b [core] Fix 403 on media
fix #667
2024-12-01 15:56:40 +07:00
devlikepro 0c108f5232 [core] 2024.11.10
Release / amd64 - chrome - chrome (push) Waiting to run
Release / amd64 - chromium - latest (push) Waiting to run
Release / linux/arm64 - chromium - arm (push) Waiting to run
Release / amd64 - none - noweb (push) Waiting to run
Release / linux/arm64 - none - noweb-arm (push) Waiting to run
2024-12-01 15:37:21 +07:00
devlikepro 39d298c867 [core] WEBJS fix groups
fix #674 #670
2024-12-01 15:35:00 +07:00
devlikepro 376e804a28 [core] Up NOWEB 2024-12-01 15:16:47 +07:00
devlikepro 79e5c0589d [core] Up NOWEB
Release / amd64 - chrome - chrome (push) Waiting to run
Release / amd64 - chromium - latest (push) Waiting to run
Release / linux/arm64 - chromium - arm (push) Waiting to run
Release / amd64 - none - noweb (push) Waiting to run
Release / linux/arm64 - none - noweb-arm (push) Waiting to run
2024-11-29 14:51:16 +07:00
devlikepro f6679b7368 [core] 2024.11.9 2024-11-29 09:38:06 +07:00
devlikepro a3c0b86b67 [core] Add explicit placeholder cache 2024-11-29 09:37:49 +07:00
devlikepro c8f48dd62b [core] 11.8
Release / amd64 - chrome - chrome (push) Waiting to run
Release / amd64 - chromium - latest (push) Waiting to run
Release / linux/arm64 - chromium - arm (push) Waiting to run
Release / amd64 - none - noweb (push) Waiting to run
Release / linux/arm64 - none - noweb-arm (push) Waiting to run
2024-11-25 12:57:23 +07:00
devlikepro aed157697a [core] WEBJS - Fix QR code
fix #659
2024-11-25 12:56:51 +07:00
devlikepro 913b8606d9 [core] 2024.11.7
Release / amd64 - chrome - chrome (push) Waiting to run
Release / amd64 - chromium - latest (push) Waiting to run
Release / linux/arm64 - chromium - arm (push) Waiting to run
Release / amd64 - none - noweb (push) Waiting to run
Release / linux/arm64 - none - noweb-arm (push) Waiting to run
2024-11-24 14:06:33 +07:00
devlikepro 7cadbad36e [core] Add WebJSEngineConfigService.ts
fix #654 fix #653
2024-11-24 14:06:28 +07:00
devlikepro abb5bdffcc [core] 2024.11.6
Release / amd64 - chrome - chrome (push) Waiting to run
Release / amd64 - chromium - latest (push) Waiting to run
Release / linux/arm64 - chromium - arm (push) Waiting to run
Release / amd64 - none - noweb (push) Waiting to run
Release / linux/arm64 - none - noweb-arm (push) Waiting to run
2024-11-20 16:38:59 +07:00
devlikepro 6a44c15dd3 [core] fix undefined.get
fix #645
2024-11-20 16:38:43 +07:00
22 changed files with 487 additions and 39 deletions

No files matched your search

+10 -1
View File
@@ -8,7 +8,9 @@ on:
jobs:
docker-build:
runs-on: ${{ matrix.runner }}
name: ${{ matrix.platform }} - ${{ matrix.browser }} - ${{ matrix.tag }}
name:
${{ matrix.engine }} - ${{ matrix.browser }} - ${{ matrix.platform }} -
${{ matrix.tag }}
strategy:
matrix:
include:
@@ -17,6 +19,7 @@ jobs:
tag: 'latest'
platform: 'amd64'
browser: 'chromium'
engine: 'WEBJS'
goss: 'goss-linux-amd64'
# Chromium - ARM
@@ -24,6 +27,7 @@ jobs:
tag: 'arm'
platform: 'linux/arm64'
browser: 'chromium'
engine: 'WEBJS'
goss: 'goss-linux-arm'
# Chrome - x86
@@ -31,6 +35,7 @@ jobs:
tag: 'chrome'
platform: 'amd64'
browser: 'chrome'
engine: 'WEBJS'
goss: 'goss-linux-amd64'
# Chrome - ARM (Chrome is not available for ARM)
@@ -45,6 +50,7 @@ jobs:
tag: 'noweb'
platform: 'amd64'
browser: 'none'
engine: 'NOWEB'
goss: 'goss-linux-amd64'
# No browser - ARM
@@ -52,6 +58,7 @@ jobs:
tag: 'noweb-arm'
platform: 'linux/arm64'
browser: 'none'
engine: 'NOWEB'
goss: 'goss-linux-arm'
steps:
@@ -85,6 +92,7 @@ jobs:
platforms: ${{ matrix.platform }}
build-args: |
USE_BROWSER=${{ matrix.browser }}
WHATSAPP_DEFAULT_ENGINE=${{ matrix.engine }}
push: false
load: true
tags: ${{ steps.meta.outputs.tags }}
@@ -116,6 +124,7 @@ jobs:
platforms: ${{ matrix.platform }}
build-args: |
USE_BROWSER=${{ matrix.browser }}
WHATSAPP_DEFAULT_ENGINE=${{ matrix.engine }}
push: ${{ github.ref_type == 'tag' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
+4
View File
@@ -44,6 +44,7 @@ ENV PUPPETEER_SKIP_DOWNLOAD=True
# https://github.com/devlikeapro/waha/issues/347
ENV NODE_OPTIONS="--max-old-space-size=16384"
ARG USE_BROWSER=chromium
ARG WHATSAPP_DEFAULT_ENGINE
RUN echo "USE_BROWSER=$USE_BROWSER"
@@ -99,6 +100,9 @@ RUN if [ "$USE_BROWSER" = "chrome" ]; then \
&& rm -rf /var/lib/apt/lists/*; \
fi
# Set the ENV for NOWEB docker image
ENV WHATSAPP_DEFAULT_ENGINE=$WHATSAPP_DEFAULT_ENGINE
# Attach sources, install packages
WORKDIR /app
COPY package.json ./
+4 -1
View File
@@ -5,7 +5,7 @@ build-chrome:
docker build . -t devlikeapro/waha-plus:chrome --build-arg USE_BROWSER=chrome
build-noweb:
docker build . -t devlikeapro/waha:noweb --build-arg USE_BROWSER=none
docker build . -t devlikeapro/waha:noweb --build-arg USE_BROWSER=none --build-arg WHATSAPP_DEFAULT_ENGINE=NOWEB
build-all: build build-chrome build-noweb
@@ -34,6 +34,9 @@ for-swagger:
up-noweb:
. ${NVM_DIR}/nvm.sh && nvm exec yarn up @adiwajshing/baileys@github:devlikeapro/Baileys#fork-master-channels-678
up-noweb-libsignal:
. ${NVM_DIR}/nvm.sh && nvm exec yarn up libsignal@github:devlikeapro/libsignal-node#fork-master
up-webjs:
. ${NVM_DIR}/nvm.sh && nvm exec yarn up whatsapp-web.js@github:devlikeapro/whatsapp-web.js#fork-main-channels
+3 -1
View File
@@ -59,6 +59,7 @@
"joi": "^17.13.3",
"knex": "^3.1.0",
"libphonenumber-js": "^1.10.36",
"libsignal": "github:devlikeapro/libsignal-node#fork-master",
"link-preview-js": "^3.0.4",
"lodash": "^4.17.21",
"mime-types": "^2.1.27",
@@ -88,7 +89,8 @@
"bufferutil": "^4.0.8",
"ws": "^8.18.0",
"puppeteer": "^23.6.0",
"whatwg-url": "13.0.0"
"whatwg-url": "13.0.0",
"libsignal": "github:devlikeapro/libsignal-node#fork-master"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
+45 -2
View File
@@ -6,6 +6,9 @@ import {
Param,
Post,
Put,
Query,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { GroupIdApiParam } from '@waha/nestjs/params/ChatIdApiParam';
@@ -19,6 +22,9 @@ import { WhatsappSession } from '../core/abc/session.abc';
import {
CreateGroupRequest,
DescriptionRequest,
GroupsPaginationParams,
JoinGroupRequest,
JoinGroupResponse,
ParticipantsRequest,
SettingsSecurityChangeInfo,
SubjectRequest,
@@ -40,11 +46,48 @@ export class GroupsController {
return session.createGroup(request);
}
@Get('join-info')
@SessionApiParam
@ApiOperation({ summary: 'Get info about the group before joining.' })
async joinInfoGroup(
@WorkingSessionParam session: WhatsappSession,
@Query() query: JoinGroupRequest,
): Promise<any> {
// https://chat.whatsapp.com/123 => 123
const code = query.code.split('/').pop();
return session.joinInfoGroup(code);
}
@Post('join')
@SessionApiParam
@ApiOperation({ summary: 'Join group via code' })
async joinGroup(
@WorkingSessionParam session: WhatsappSession,
@Body() request: JoinGroupRequest,
): Promise<JoinGroupResponse> {
// https://chat.whatsapp.com/123 => 123
const code = request.code.split('/').pop();
const id = await session.joinGroup(code);
return { id: id };
}
@Get('')
@SessionApiParam
@ApiOperation({ summary: 'Get all groups.' })
getGroups(@WorkingSessionParam session: WhatsappSession) {
return session.getGroups();
@UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
getGroups(
@WorkingSessionParam session: WhatsappSession,
@Query() pagination: GroupsPaginationParams,
) {
return session.getGroups(pagination);
}
@Post('refresh')
@SessionApiParam
@ApiOperation({ summary: 'Refresh groups from the server.' })
@UsePipes(new ValidationPipe({ transform: true, whitelist: true }))
async refreshGroups(@WorkingSessionParam session: WhatsappSession) {
return { success: await session.refreshGroups() };
}
@Get(':id')
+14 -1
View File
@@ -61,6 +61,7 @@ import {
} from '../../structures/enums.dto';
import {
CreateGroupRequest,
GroupsPaginationParams,
ParticipantsRequest,
SettingsSecurityChangeInfo,
} from '../../structures/groups.dto';
@@ -522,7 +523,19 @@ export abstract class WhatsappSession {
throw new NotImplementedByEngineError();
}
public getGroups() {
public joinGroup(code: string): Promise<string> {
throw new NotImplementedByEngineError();
}
public joinInfoGroup(code: string): Promise<any> {
throw new NotImplementedByEngineError();
}
public getGroups(pagination: PaginationParams) {
throw new NotImplementedByEngineError();
}
public refreshGroups(): Promise<boolean> {
throw new NotImplementedByEngineError();
}
+2
View File
@@ -10,6 +10,7 @@ import {
ServerDebugController,
} from '@waha/api/server.controller';
import { WebsocketGatewayCore } from '@waha/core/api/websocket.gateway.core';
import { WebJSEngineConfigService } from '@waha/core/config/WebJSEngineConfigService';
import { MediaLocalStorageModule } from '@waha/core/media/local/media.local.storage.module';
import { MediaLocalStorageConfig } from '@waha/core/media/local/MediaLocalStorageConfig';
import { BufferJsonReplacerInterceptor } from '@waha/nestjs/BufferJsonReplacerInterceptor';
@@ -146,6 +147,7 @@ const PROVIDERS = [
},
DashboardConfigServiceCore,
SwaggerConfigServiceCore,
WebJSEngineConfigService,
WhatsappConfigService,
EngineConfigService,
WebsocketGatewayCore,
@@ -0,0 +1,37 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { WebJSConfig } from '../../core/engines/webjs/session.webjs.core';
@Injectable()
export class WebJSEngineConfigService {
constructor(protected configService: ConfigService) {}
getConfig(): WebJSConfig {
let webVersion = this.configService.get<string>(
'WAHA_WEBJS_WEB_VERSION',
undefined,
);
if (webVersion === '2.2412.54-videofix') {
// Deprecated version
webVersion = undefined;
}
return {
webVersion: webVersion,
cacheType: this.getCacheType(),
};
}
getCacheType(): 'local' | 'none' {
const cacheType = this.configService
.get<string>('WAHA_WEBJS_CACHE_TYPE', 'none')
.toLowerCase();
if (cacheType != 'local' && cacheType != 'none') {
throw new Error(
'Invalid cache type, only "local" and "none" are allowed',
);
}
return cacheType;
}
}
+39 -11
View File
@@ -17,6 +17,7 @@ import makeWASocket, {
normalizeMessageContent,
PresenceData,
proto,
SocketConfig,
WAMessageContent,
WAMessageKey,
WAMessageUpdate,
@@ -194,6 +195,7 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
private autoRestartJob: SinglePeriodicJobRunner;
private msgRetryCounterCache: NodeCache;
private placeholderResendCache: NodeCache;
protected engineLogger: BaileysLogger;
private authNOWEBStore: any;
@@ -213,6 +215,10 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
stdTTL: 60 * 60, // 1 hour
useClones: false,
});
this.placeholderResendCache = new NodeCache({
stdTTL: 60 * 60, // 1 hour
useClones: false,
});
this.engineLogger = this.loggerBuilder.child({
name: 'NOWEBEngine',
@@ -252,7 +258,7 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
await this.sock?.logout();
}
getSocketConfig(agent, state): any {
getSocketConfig(agent, state): Partial<SocketConfig> {
const fullSyncEnabled = this.sessionConfig?.noweb?.store?.fullSync || false;
const browser = fullSyncEnabled
? Browsers.ubuntu('Desktop')
@@ -264,11 +270,7 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
return {
agent: agent,
fetchAgent: agent,
auth: {
creds: state.creds,
/** caching makes the store faster to send/recv messages */
keys: makeCacheableSignalKeyStore(state.keys, this.engineLogger),
},
auth: state,
printQRInTerminal: false,
browser: browser,
logger: this.engineLogger,
@@ -278,20 +280,30 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
getMessage: (key) => this.getMessage(key),
syncFullHistory: fullSyncEnabled,
msgRetryCounterCache: this.msgRetryCounterCache,
placeholderResendCache: this.placeholderResendCache,
markOnlineOnConnect: markOnlineOnConnect,
};
}
async makeSocket(): Promise<any> {
if (!this.authNOWEBStore) {
this.authNOWEBStore = await this.authFactory.buildAuth(
const store = await this.authFactory.buildAuth(
this.sessionStore,
this.name,
);
/** caching makes the store faster to send/recv messages */
store.state.keys = makeCacheableSignalKeyStore(
store.state.keys,
this.engineLogger,
);
this.authNOWEBStore = store;
}
const { state, saveCreds } = this.authNOWEBStore;
const agent = this.makeAgent();
const socketConfig = this.getSocketConfig(agent, state);
const socketConfig: SocketConfig = this.getSocketConfig(
agent,
state,
) as SocketConfig;
const sock = makeWASocket(socketConfig);
sock.ev.on('creds.update', saveCreds);
return sock;
@@ -1072,12 +1084,28 @@ export class WhatsappSessionNoWebCore extends WhatsappSession {
return this.sock.groupCreate(request.name, participants);
}
public async getGroups() {
return await this.sock.groupFetchAllParticipating();
public joinGroup(code: string) {
return this.sock.groupAcceptInvite(code);
}
public joinInfoGroup(code: string) {
return this.sock.groupGetInviteInfo(code);
}
public async getGroups(pagination: PaginationParams) {
const groups = await this.store.getGroups(pagination);
// return {id: group} mapping for backward compatability
return lodash.keyBy(groups, 'id');
}
public async refreshGroups(): Promise<boolean> {
this.store.resetGroupsCache();
await this.store.getGroups({});
return true;
}
public async getGroup(id) {
const groups = await this.sock.groupFetchAllParticipating();
const groups = await this.getGroups({});
return groups[id];
}
@@ -0,0 +1,14 @@
import { GroupMetadata } from '@adiwajshing/baileys';
import { PaginationParams } from '@waha/structures/pagination.dto';
export interface IGroupRepository {
getAll(pagination?: PaginationParams): Promise<GroupMetadata[]>;
getById(id: string): Promise<GroupMetadata | null>;
deleteAll(): Promise<void>;
deleteById(id: string): Promise<void>;
save(group: GroupMetadata): Promise<void>;
}
@@ -1,5 +1,6 @@
import { WAMessage } from '@adiwajshing/baileys';
import { LabelAssociation } from '@adiwajshing/baileys/lib/Types/LabelAssociation';
import { IGroupRepository } from '@waha/core/engines/noweb/store/IGroupRepository';
import { ILabelAssociationRepository } from '@waha/core/engines/noweb/store/ILabelAssociationsRepository';
import { ILabelsRepository } from '@waha/core/engines/noweb/store/ILabelsRepository';
@@ -16,6 +17,8 @@ export abstract class INowebStorage {
abstract getChatRepository(): IChatRepository;
abstract getGroupRepository(): IGroupRepository;
abstract getMessagesRepository(): IMessagesRepository;
abstract getLabelsRepository(): ILabelsRepository;
@@ -4,6 +4,7 @@ import {
Contact,
proto,
} from '@adiwajshing/baileys';
import { GroupMetadata } from '@adiwajshing/baileys/lib/Types/GroupMetadata';
import { Label } from '@adiwajshing/baileys/lib/Types/Label';
import { GetChatMessagesFilter } from '@waha/structures/chats.dto';
import { PaginationParams } from '@waha/structures/pagination.dto';
@@ -40,4 +41,8 @@ export interface INowebStore {
getChatsByLabelId(labelId: string): Promise<Chat[]>;
getChatLabels(chatId: string): Promise<Label[]>;
getGroups(pagination: PaginationParams): Promise<GroupMetadata[]>;
resetGroupsCache(): void;
}
@@ -1,8 +1,15 @@
import { Chat, Contact, makeInMemoryStore, proto } from '@adiwajshing/baileys';
import makeWASocket, {
Chat,
Contact,
GroupMetadata,
makeInMemoryStore,
proto,
} from '@adiwajshing/baileys';
import { Label } from '@adiwajshing/baileys/lib/Types/Label';
import { BadRequestException } from '@nestjs/common';
import { GetChatMessagesFilter } from '@waha/structures/chats.dto';
import { PaginationParams } from '@waha/structures/pagination.dto';
import { PaginatorInMemory } from '@waha/utils/Paginator';
import { INowebStore } from './INowebStore';
@@ -10,6 +17,8 @@ import { INowebStore } from './INowebStore';
const logger = require('pino')();
export class NowebInMemoryStore implements INowebStore {
private socket: ReturnType<typeof makeWASocket>;
private store: ReturnType<typeof makeInMemoryStore>;
errorMessage =
'Enable NOWEB store "config.noweb.store.enabled=True" and "config.noweb.store.full_sync=True" when starting a new session. ' +
@@ -33,6 +42,7 @@ export class NowebInMemoryStore implements INowebStore {
bind(ev: any, socket: any) {
this.store.bind(ev);
this.socket = socket;
}
loadMessage(jid: string, id: string): Promise<proto.IWebMessageInfo> {
@@ -78,4 +88,15 @@ export class NowebInMemoryStore implements INowebStore {
getChatLabels(chatId: string): Promise<Label[]> {
throw new BadRequestException(this.errorMessage);
}
async getGroups(pagination: PaginationParams): Promise<GroupMetadata[]> {
const response = await this.socket?.groupFetchAllParticipating();
const groups = Object.values(response);
const paginator = new PaginatorInMemory(pagination);
return paginator.apply(groups);
}
resetGroupsCache() {
return;
}
}
@@ -1,27 +1,34 @@
import {
import makeWASocket, {
areJidsSameUser,
BaileysEventEmitter,
Chat,
ChatUpdate,
Contact,
GroupParticipant,
isRealMessage,
jidNormalizedUser,
ParticipantAction,
proto,
updateMessageWithReaction,
updateMessageWithReceipt,
} from '@adiwajshing/baileys';
import { GroupMetadata } from '@adiwajshing/baileys/lib/Types/GroupMetadata';
import { Label } from '@adiwajshing/baileys/lib/Types/Label';
import {
LabelAssociation,
LabelAssociationType,
} from '@adiwajshing/baileys/lib/Types/LabelAssociation';
import { IGroupRepository } from '@waha/core/engines/noweb/store/IGroupRepository';
import { ILabelAssociationRepository } from '@waha/core/engines/noweb/store/ILabelAssociationsRepository';
import { ILabelsRepository } from '@waha/core/engines/noweb/store/ILabelsRepository';
import { GetChatMessagesFilter } from '@waha/structures/chats.dto';
import { PaginationParams, SortOrder } from '@waha/structures/pagination.dto';
import { DefaultMap } from '@waha/utils/DefaultMap';
import { sleep, waitUntil } from '@waha/utils/promiseTimeout';
import * as lodash from 'lodash';
import { toNumber } from 'lodash';
import { Logger } from 'pino';
import { toJID } from '../session.noweb.core';
import { IChatRepository } from './IChatRepository';
import { IContactRepository } from './IContactRepository';
import { IMessagesRepository } from './IMessagesRepository';
@@ -31,15 +38,27 @@ import { INowebStore } from './INowebStore';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const AsyncLock = require('async-lock');
type ms = number;
const HOUR: ms = 60 * 60 * 1000;
export class NowebPersistentStore implements INowebStore {
private socket: any;
private socket: ReturnType<typeof makeWASocket>;
private chatRepo: IChatRepository;
private groupRepo: IGroupRepository;
private contactRepo: IContactRepository;
private messagesRepo: IMessagesRepository;
private labelsRepo: ILabelsRepository;
private labelAssociationsRepo: ILabelAssociationRepository;
public presences: any;
private lock: any;
private groupsFetchLock: any = new AsyncLock({
maxPending: Infinity,
maxExecutionTime: 60_000,
});
private lastTimeGroupUpdate: Date = new Date(0);
private lastTimeGroupFetch: Date = new Date(0);
private GROUP_METADATA_CACHE_TIME = 24 * HOUR;
constructor(
private logger: Logger,
@@ -47,6 +66,7 @@ export class NowebPersistentStore implements INowebStore {
) {
this.socket = null;
this.chatRepo = storage.getChatRepository();
this.groupRepo = storage.getGroupRepository();
this.contactRepo = storage.getContactsRepository();
this.messagesRepo = storage.getMessagesRepository();
this.labelsRepo = storage.getLabelsRepository();
@@ -88,6 +108,19 @@ export class NowebPersistentStore implements INowebStore {
ev.on('chats.delete', (data) =>
this.withLock('chats', () => this.onChatDelete(data)),
);
// Groups
ev.on('groups.upsert', (data) =>
this.withLock('groups', () => this.onGroupUpsert(data)),
);
ev.on('groups.update', (data) =>
this.withLock('groups', () => this.onGroupUpdate(data)),
);
ev.on('group-participants.update', (data) =>
this.withLock(`group-${data.id}`, () =>
this.onGroupParticipantsUpdate(data),
),
);
// Contacts
ev.on('contacts.upsert', (data) =>
this.withLock('contacts', () => this.onContactsUpsert(data)),
@@ -195,7 +228,84 @@ export class NowebPersistentStore implements INowebStore {
chat.conversationTimestamp = toNumber(chat.conversationTimestamp);
await this.chatRepo.save(chat);
}
this.logger.info(`history sync - '${chats.length}' synced chats`);
this.logger.info(`store sync - '${chats.length}' synced chats`);
}
private async onGroupUpsert(groups: GroupMetadata[]) {
for (const group of groups) {
await this.groupRepo.save(group);
}
this.logger.info(`store sync - '${groups.length}' synced groups`);
}
private async onGroupUpdate(groups: Partial<GroupMetadata>[]) {
for (const update of groups) {
let group = await this.groupRepo.getById(update.id);
group = Object.assign(group || {}, update) as GroupMetadata;
await this.groupRepo.save(group);
}
this.logger.info(`store sync - '${groups.length}' updated groups`);
this.lastTimeGroupUpdate = new Date();
}
private async onGroupParticipantsUpdate(data) {
const id: string = data.id;
const participants: string[] = data.participants;
const action: ParticipantAction = data.action;
if (action == 'remove') {
// Remove the group if the current user is removed
const myJid = this.socket?.authState?.creds?.me?.id;
const participantsIncludesMe = lodash.find(participants, (p) =>
areJidsSameUser(p, myJid),
);
if (participantsIncludesMe) {
await this.groupRepo.deleteById(id);
return;
}
}
let group = await this.groupRepo.getById(id);
if (!group) {
group = { id: id, participants: [] } as GroupMetadata;
}
const participantsById = new DefaultMap<string, GroupParticipant>((key) => {
return { id: key, admin: null } as GroupParticipant;
});
for (const participant of group.participants) {
participantsById.set(participant.id, participant);
}
for (const participant of participants) {
this.participantUpdate(participantsById, participant, action);
}
group.participants = Array.from(participantsById.values());
await this.groupRepo.save(group);
}
private participantUpdate(
participantsById: DefaultMap<string, GroupParticipant>,
participant: string,
action: ParticipantAction,
) {
switch (action) {
case 'add':
// if there's no participant - add it (by id)
participantsById.get(participant);
break;
case 'remove':
// remove the participant (by id)
participantsById.delete(participant);
break;
case 'promote':
// set admin: admin
participantsById.get(participant).admin = 'admin';
break;
case 'demote':
participantsById.get(participant).admin = null;
break;
}
}
private async onChatUpdate(updates: ChatUpdate[]) {
@@ -344,6 +454,41 @@ export class NowebPersistentStore implements INowebStore {
return this.chatRepo.getAllWithMessages(pagination);
}
private shouldFetchGroup(): boolean {
const timePassed = new Date().getTime() - this.lastTimeGroupFetch.getTime();
return timePassed > this.GROUP_METADATA_CACHE_TIME;
}
private async fetchGroups() {
await this.groupsFetchLock.acquire('groups-fetch', async () => {
if (!this.shouldFetchGroup()) {
// Update has been done by another request
return;
}
const lastTimeGroupUpdate = this.lastTimeGroupUpdate;
await this.groupRepo.deleteAll();
await this.socket?.groupFetchAllParticipating();
// Wait until the groups update is done
await waitUntil(
async () => this.lastTimeGroupUpdate > lastTimeGroupUpdate,
100,
5_000,
);
this.lastTimeGroupFetch = new Date();
});
}
resetGroupsCache() {
this.lastTimeGroupFetch = new Date(0);
}
async getGroups(pagination: PaginationParams): Promise<GroupMetadata[]> {
if (this.shouldFetchGroup()) {
await this.fetchGroups();
}
return this.groupRepo.getAll(pagination);
}
getContactById(jid) {
return this.contactRepo.getById(jid);
}
+5
View File
@@ -18,6 +18,11 @@ export const NOWEB_STORE_SCHEMA = [
new Index('chats_conversationTimestamp_index', ['conversationTimestamp']),
],
),
new Schema(
'groups',
[new Field('id', 'TEXT'), new Field('data', 'TEXT')],
[new Index('groups_id_index', ['id'])],
),
new Schema(
'messages',
[
@@ -0,0 +1,16 @@
import { GroupMetadata } from '@adiwajshing/baileys/lib/Types/GroupMetadata';
import { IGroupRepository } from '@waha/core/engines/noweb/store/IGroupRepository';
import { KnexPaginator } from '@waha/utils/Paginator';
import { NOWEBSqlite3KVRepository } from './NOWEBSqlite3KVRepository';
class Paginator extends KnexPaginator {
indexes = ['id'];
}
export class Sqlite3GroupRepository
extends NOWEBSqlite3KVRepository<GroupMetadata>
implements IGroupRepository
{
protected Paginator = Paginator;
}
@@ -2,6 +2,7 @@ import { WAMessage } from '@adiwajshing/baileys';
import { LabelAssociation } from '@adiwajshing/baileys/lib/Types/LabelAssociation';
import { ILabelAssociationRepository } from '@waha/core/engines/noweb/store/ILabelAssociationsRepository';
import { ILabelsRepository } from '@waha/core/engines/noweb/store/ILabelsRepository';
import { Sqlite3GroupRepository } from '@waha/core/engines/noweb/store/sqlite3/Sqlite3GroupRepository';
import { Sqlite3LabelAssociationsRepository } from '@waha/core/engines/noweb/store/sqlite3/Sqlite3LabelAssociationsRepository';
import { Sqlite3LabelsRepository } from '@waha/core/engines/noweb/store/sqlite3/Sqlite3LabelsRepository';
import { Field, Index, Schema } from '@waha/core/storage/sqlite3/Schema';
@@ -62,6 +63,14 @@ export class Sqlite3Storage extends INowebStorage {
'CREATE INDEX IF NOT EXISTS chats_conversationTimestamp_index ON chats (conversationTimestamp)',
);
// Groups
this.db.exec(
'CREATE TABLE IF NOT EXISTS groups (id TEXT PRIMARY KEY, data TEXT)',
);
this.db.exec(
'CREATE UNIQUE INDEX IF NOT EXISTS groups_id_index ON groups (id)',
);
// Messages
this.db.exec(
'CREATE TABLE IF NOT EXISTS messages (jid TEXT, id TEXT, messageTimestamp INTEGER, data TEXT)',
@@ -119,6 +128,10 @@ export class Sqlite3Storage extends INowebStorage {
return new Sqlite3ChatRepository(this.db, this.getSchema('chats'));
}
getGroupRepository() {
return new Sqlite3GroupRepository(this.db, this.getSchema('groups'));
}
getLabelsRepository(): ILabelsRepository {
return new Sqlite3LabelsRepository(this.db, this.getSchema('labels'));
}
+36 -7
View File
@@ -55,6 +55,7 @@ import {
} from '@waha/structures/enums.dto';
import {
CreateGroupRequest,
GroupSortField,
ParticipantsRequest,
SettingsSecurityChangeInfo,
} from '@waha/structures/groups.dto';
@@ -75,6 +76,7 @@ import {
import { PaginatorInMemory } from '@waha/utils/Paginator';
import { sleep, waitUntil } from '@waha/utils/promiseTimeout';
import { SingleDelayedJobRunner } from '@waha/utils/SingleDelayedJobRunner';
import * as lodash from 'lodash';
import { fromEvent, merge, mergeMap, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import {
@@ -99,6 +101,7 @@ const QRCode = require('qrcode');
export interface WebJSConfig {
webVersion?: string;
cacheType: 'local' | 'none';
}
export class WhatsappSessionWebJSCore extends WhatsappSession {
@@ -143,7 +146,11 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
const path = this.getClassDirName();
const webVersion =
this.engineConfig?.webVersion || '2.3000.1018072227-alpha';
this.logger.info(`Using web version: '${webVersion}'`);
const cacheType = this.engineConfig?.cacheType || 'none';
this.logger.info(`Using cache type: '${cacheType}'`);
if (cacheType === 'local') {
this.logger.info(`Using web version: '${webVersion}'`);
}
return {
puppeteer: {
headless: true,
@@ -153,8 +160,7 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
},
webVersion: webVersion,
webVersionCache: {
// type: 'none',
type: 'local',
type: cacheType,
path: path,
strict: true,
},
@@ -756,6 +762,14 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
return this.whatsapp.createGroup(request.name, participantIds);
}
public joinGroup(code: string) {
return this.whatsapp.acceptInvite(code);
}
public joinInfoGroup(code: string) {
return this.whatsapp.getInviteInfo(code);
}
public async getInfoAdminsOnly(id): Promise<SettingsSecurityChangeInfo> {
const groupChat = (await this.whatsapp.getChatById(id)) as GroupChat;
return {
@@ -785,10 +799,25 @@ export class WhatsappSessionWebJSCore extends WhatsappSession {
return groupChat.setMessagesAdminsOnly(value);
}
public getGroups() {
return this.whatsapp
.getChats()
.then((chats) => chats.filter((chat) => chat.isGroup));
public async getGroups(pagination: PaginationParams) {
const chats = await this.whatsapp.getChats();
const groups = lodash.filter(chats, (chat) => chat.isGroup);
switch (pagination.sortBy) {
case GroupSortField.ID:
pagination.sortBy = 'id._serialized';
break;
case GroupSortField.SUBJECT:
pagination.sortBy = 'groupMetadata.subject';
break;
}
const paginator = new PaginatorInMemory(pagination);
return paginator.apply(groups);
}
public async refreshGroups(): Promise<boolean> {
return true;
}
public getGroup(id) {
+14 -1
View File
@@ -3,6 +3,7 @@ import {
NotFoundException,
UnprocessableEntityException,
} from '@nestjs/common';
import { WebJSEngineConfigService } from '@waha/core/config/WebJSEngineConfigService';
import { WebhookConductor } from '@waha/core/integrations/webhooks/WebhookConductor';
import { MediaStorageFactory } from '@waha/core/media/MediaStorageFactory';
import { DefaultMap } from '@waha/utils/DefaultMap';
@@ -11,7 +12,7 @@ import { promiseTimeout, sleep } from '@waha/utils/promiseTimeout';
import { complete } from '@waha/utils/reactive/complete';
import { SwitchObservable } from '@waha/utils/reactive/SwitchObservable';
import { PinoLogger } from 'nestjs-pino';
import { Observable } from 'rxjs';
import { Observable, retry, share } from 'rxjs';
import { map } from 'rxjs/operators';
import { WhatsappConfigService } from '../config.service';
@@ -69,6 +70,7 @@ export class SessionManagerCore extends SessionManager {
constructor(
config: WhatsappConfigService,
private engineConfigService: EngineConfigService,
private webjsEngineConfigService: WebJSEngineConfigService,
log: PinoLogger,
private mediaStorageFactory: MediaStorageFactory,
) {
@@ -77,6 +79,14 @@ export class SessionManagerCore extends SessionManager {
this.sessionConfig = null;
const engineName = this.engineConfigService.getDefaultEngineName();
this.EngineClass = this.getEngine(engineName);
this.events2 = new DefaultMap<WAHAEvents, SwitchObservable<any>>(
(key) =>
new SwitchObservable((obs$) => {
return obs$.pipe(retry(), share());
}),
);
this.store = new LocalStoreCore(engineName.toLowerCase());
this.sessionAuthRepository = new LocalSessionAuthRepository(this.store);
this.startPredefinedSessions();
@@ -167,6 +177,9 @@ export class SessionManagerCore extends SessionManager {
proxyConfig: proxyConfig,
sessionConfig: this.sessionConfig,
};
if (this.EngineClass === WhatsappSessionWebJSCore) {
sessionConfig.engineConfig = this.webjsEngineConfigService.getConfig();
}
await this.sessionAuthRepository.init(name);
// @ts-ignore
const session = new this.EngineClass(sessionConfig);
+43 -1
View File
@@ -1,5 +1,15 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsString } from 'class-validator';
import { BooleanString } from '@waha/nestjs/validation/BooleanString';
import { Transform } from 'class-transformer';
import {
IsArray,
IsBoolean,
IsEnum,
IsOptional,
IsString,
} from 'class-validator';
import { PaginationParams } from './pagination.dto';
/**
* Structures
@@ -46,3 +56,35 @@ export class CreateGroupRequest {
@IsArray()
participants: Array<Participant>;
}
export class JoinGroupRequest {
@ApiProperty({
description: 'Group code (123) or url (https://chat.whatsapp.com/123)',
example: 'https://chat.whatsapp.com/1234567890abcdef',
})
@IsString()
code: string;
}
export class JoinGroupResponse {
@ApiProperty({
description: 'Group ID',
example: '123@g.us',
})
id: string;
}
export enum GroupSortField {
ID = 'id',
SUBJECT = 'subject',
}
export class GroupsPaginationParams extends PaginationParams {
@ApiProperty({
description: 'Sort by field',
enum: GroupSortField,
})
@IsOptional()
@IsEnum(GroupSortField)
sortBy?: string;
}
+1 -1
View File
@@ -33,7 +33,7 @@ export function getEngineName(): string {
}
export const VERSION: WAHAEnvironment = {
version: '2024.11.5',
version: '2024.12.5',
engine: getEngineName(),
tier: getWAHAVersion(),
browser: getBrowserExecutablePath(),
+8 -7
View File
@@ -7,7 +7,7 @@ __metadata:
"@adiwajshing/baileys@github:devlikeapro/Baileys#fork-master-channels-678":
version: 6.7.8
resolution: "@adiwajshing/baileys@https://github.com/devlikeapro/Baileys.git#commit=e2d6246da89a4775a8482fbe8de2a42b8421516d"
resolution: "@adiwajshing/baileys@https://github.com/devlikeapro/Baileys.git#commit=04738f528321857efe837e3f93c73059878ecdcb"
dependencies:
"@adiwajshing/keyed-db": ^0.2.4
"@hapi/boom": ^9.1.3
@@ -39,7 +39,7 @@ __metadata:
optional: true
sharp:
optional: true
checksum: 43ac116fcc3eca176ff036a296c99d8be39bdcaafb788fb65305ee06d59060f0b8b8d5dee8596d9dca9794843dd76e90769e5acfa6d14ff55ad2a0f2616686e3
checksum: 0f7b2031eeb25137c65aee3151028f7a8637cfed0763c7426817415ec818ca71f55267b9de88511905bd266f4006301a26206dac9bdb1821d13f1a0cbf075e32
languageName: node
linkType: hard
@@ -8474,13 +8474,13 @@ __metadata:
languageName: node
linkType: hard
"libsignal@github:WhiskeySockets/libsignal-node":
"libsignal@github:devlikeapro/libsignal-node#fork-master":
version: 2.0.1
resolution: "libsignal@https://github.com/WhiskeySockets/libsignal-node.git#commit=83a3e3a3864511cb74df1b796373f0d49d071134"
resolution: "libsignal@https://github.com/devlikeapro/libsignal-node.git#commit=83bc9236ee3d96ae1747cf3ca2e505b10cf97250"
dependencies:
curve25519-js: ^0.0.4
protobufjs: 6.8.8
checksum: bc8a8c4d54395e6b3f7224e72fe0c447859eed688cc28da3a56d76dcd5bf1ea71b25b3ff0da96646b636f2bb679898ca38eff909cb83ef3e3b017fe81e06ed50
checksum: 3ee39bc7cbd123fa2c1f84ff53ab4e3dacfb5a00d10944960fd72156b24a5eb5af1fa5ca31b9f25b26aab107c712d8c295c85b0bed26ad6eba6d0e3f26f38927
languageName: node
linkType: hard
@@ -12272,6 +12272,7 @@ __metadata:
joi: ^17.13.3
knex: ^3.1.0
libphonenumber-js: ^1.10.36
libsignal: "github:devlikeapro/libsignal-node#fork-master"
link-preview-js: ^3.0.4
lodash: ^4.17.21
mime-types: ^2.1.27
@@ -12394,7 +12395,7 @@ __metadata:
"whatsapp-web.js@github:devlikeapro/whatsapp-web.js#fork-main-channels":
version: 1.26.0
resolution: "whatsapp-web.js@https://github.com/devlikeapro/whatsapp-web.js.git#commit=aa90f7378659a237f30cda6d368359a1f81904dc"
resolution: "whatsapp-web.js@https://github.com/devlikeapro/whatsapp-web.js.git#commit=73288b43a3a346520867ffcbf0de0867dd07b343"
dependencies:
"@pedroslopez/moduleraid": ^5.0.2
archiver: ^5.3.1
@@ -12412,7 +12413,7 @@ __metadata:
optional: true
unzipper:
optional: true
checksum: 7c914e9cdd4a25c967e057d0cb5019973f0556f6220320e4bcbc0666669801b92abf97c2cc783762ef462d0006123953e233136f457689ebac395260fe69a5fc
checksum: 2513fe75b213a8c78650c61abd1671a2f671ba4f5423fb949fda2d45cd2c24cf414dfdde5222adb6582cda264983f15a3701d0dfd8a6cf7918d70cfdfd5a7d58
languageName: node
linkType: hard