Skip to content

Commit 0a925df

Browse files
committed
adjust in store files
1 parent db95de6 commit 0a925df

14 files changed

Lines changed: 54 additions & 34 deletions

File tree

.gitignore

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,6 @@ yarn-debug.log*
1111
yarn-error.log*
1212
lerna-debug.log*
1313

14-
/store/auth/apikey/*
15-
/store/auth/jwt/*
16-
/store/baileys/*
17-
/store/chats/*
18-
/store/contacts/*
19-
/store/message-up/*
20-
/store/messages/*
21-
/store/webhook/*
22-
2314
/docker-compose-data
2415

2516
# Package
@@ -41,5 +32,6 @@ lerna-debug.log*
4132
!/instances/.gitkeep
4233
/test/
4334
/src/env.yml
35+
/store
4436

4537
/temp/*

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
FROM node:16.18-alpine
22

3+
LABEL version="1.1.3" description="Api to control whatsapp features through http requests."
4+
LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes"
5+
LABEL contact="contato@agenciadgcode.com"
6+
37
RUN apk update && apk upgrade && \
48
apk add --no-cache git
59

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "evolution-api",
3-
"version": "1.2.0",
3+
"version": "1.1.3",
44
"description": "Rest api for communication with WhatsApp",
5-
"main": "index.js",
5+
"main": "./dist/src/main.js",
66
"scripts": {
77
"build": "tsc",
88
"start": "ts-node --files --transpile-only ./src/main.ts",

src/config/env.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ export class ConfigService {
160160
}
161161

162162
private envYaml(): Env {
163-
return load(readFileSync(join(SRC_DIR, 'env.yml'), { encoding: 'utf-8' })) as Env;
163+
return load(
164+
readFileSync(join(process.cwd(), 'src', 'env.yml'), { encoding: 'utf-8' }),
165+
) as Env;
164166
}
165167

166168
private envProcess(): Env {

src/whatsapp/repository/repository.manager.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { MessageUpRepository } from './messageUp.repository';
55
import { MongoClient } from 'mongodb';
66
import { WebhookRepository } from './webhook.repository';
77
import { AuthRepository } from './auth.repository';
8+
import { Auth, ConfigService, Database } from '../../config/env.config';
9+
import { execSync } from 'child_process';
10+
import { join } from 'path';
811

912
export class RepositoryBroker {
1013
constructor(
@@ -14,14 +17,34 @@ export class RepositoryBroker {
1417
public readonly messageUpdate: MessageUpRepository,
1518
public readonly webhook: WebhookRepository,
1619
public readonly auth: AuthRepository,
20+
private configService: ConfigService,
1721
dbServer?: MongoClient,
1822
) {
1923
this.dbClient = dbServer;
24+
this.__init_repo_without_db__();
2025
}
2126

2227
private dbClient?: MongoClient;
2328

2429
public get dbServer() {
2530
return this.dbClient;
2631
}
32+
33+
private __init_repo_without_db__() {
34+
if (!this.configService.get<Database>('DATABASE').ENABLED) {
35+
const storePath = join(process.cwd(), 'store');
36+
execSync(
37+
`mkdir -p ${join(
38+
storePath,
39+
'auth',
40+
this.configService.get<Auth>('AUTHENTICATION').TYPE,
41+
)}`,
42+
);
43+
execSync(`mkdir -p ${join(storePath, 'chats')}`);
44+
execSync(`mkdir -p ${join(storePath, 'contacts')}`);
45+
execSync(`mkdir -p ${join(storePath, 'messages')}`);
46+
execSync(`mkdir -p ${join(storePath, 'message-up')}`);
47+
execSync(`mkdir -p ${join(storePath, 'webhook')}`);
48+
}
49+
}
2750
}

src/whatsapp/services/whatsapp.service.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import makeWASocket, {
3030
WAMessageUpdate,
3131
WASocket,
3232
getAggregateVotesInPollMessage,
33-
makeInMemoryStore,
3433
} from '@whiskeysockets/baileys';
3534
import {
3635
Auth,
@@ -141,7 +140,6 @@ export class WAStartupService {
141140
private readonly userDevicesCache: CacheStore = new NodeCache();
142141
private endSession = false;
143142
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
144-
private store = makeInMemoryStore({ logger: P({ level: this.logBaileys }) });
145143

146144
public set instanceName(name: string) {
147145
if (!name) {
@@ -481,21 +479,30 @@ export class WAStartupService {
481479
if (full) {
482480
return webMessageInfo[0];
483481
}
482+
if (webMessageInfo[0].message?.pollCreationMessage) {
483+
const messageSecretBase64 =
484+
webMessageInfo[0].message?.messageContextInfo?.messageSecret;
485+
486+
if (typeof messageSecretBase64 === 'string') {
487+
const messageSecret = Buffer.from(messageSecretBase64, 'base64');
488+
489+
const msg = {
490+
messageContextInfo: {
491+
messageSecret,
492+
},
493+
pollCreationMessage: webMessageInfo[0].message?.pollCreationMessage,
494+
};
495+
496+
return msg;
497+
}
498+
}
499+
484500
return webMessageInfo[0].message;
485501
} catch (error) {
486502
return { conversation: '' };
487503
}
488504
}
489505

490-
private async getMessageStore(key: proto.IMessageKey) {
491-
if (this.store) {
492-
const msg = await this.store.loadMessage(key.remoteJid, key.id);
493-
return msg?.message || undefined;
494-
}
495-
496-
return proto.Message.fromObject({});
497-
}
498-
499506
private cleanStore() {
500507
const cleanStore = this.configService.get<CleanStoreConf>('CLEAN_STORE');
501508
const database = this.configService.get<Database>('DATABASE');
@@ -551,12 +558,6 @@ export class WAStartupService {
551558
const session = this.configService.get<ConfigSessionPhone>('CONFIG_SESSION_PHONE');
552559
const browser: WABrowserDescription = [session.CLIENT, session.NAME, release()];
553560

554-
this.store?.readFromFile(`${this.storePath}/baileys/store.json`);
555-
556-
setInterval(() => {
557-
this.store?.writeToFile(`${this.storePath}/baileys/store.json`);
558-
}, 10_000);
559-
560561
const socketConfig: UserFacingSocketConfig = {
561562
auth: {
562563
creds: this.instance.authState.state.creds,
@@ -574,7 +575,7 @@ export class WAStartupService {
574575
emitOwnEvents: false,
575576
msgRetryCounterCache: this.msgRetryCounterCache,
576577
getMessage: async (key) =>
577-
(await this.getMessageStore(key)) as Promise<proto.IMessage>,
578+
(await this.getMessage(key)) as Promise<proto.IMessage>,
578579
generateHighQualityLinkPreview: true,
579580
syncFullHistory: true,
580581
userDevicesCache: this.userDevicesCache,
@@ -607,8 +608,6 @@ export class WAStartupService {
607608

608609
this.client = makeWASocket(socketConfig);
609610

610-
this.store?.bind(this.client.ev);
611-
612611
this.eventHandler();
613612

614613
return this.client;
@@ -824,7 +823,7 @@ export class WAStartupService {
824823
) {
825824
let pollUpdates: any;
826825
if (update.pollUpdates) {
827-
const pollCreation = await this.getMessageStore(key);
826+
const pollCreation = await this.getMessage(key);
828827
if (pollCreation) {
829828
pollUpdates = getAggregateVotesInPollMessage({
830829
message: pollCreation as proto.IMessage,
@@ -1598,7 +1597,6 @@ export class WAStartupService {
15981597
);
15991598
const typeMessage = getContentType(msg.message);
16001599

1601-
// if for audioMessage converte para mp3
16021600
if (convertToMp4 && typeMessage === 'audioMessage') {
16031601
const number = msg.key.remoteJid.split('@')[0];
16041602
const convert = await this.processAudio(buffer.toString('base64'), number);

src/whatsapp/whatsapp.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const repository = new RepositoryBroker(
4747
messageUpdateRepository,
4848
webhookRepository,
4949
authRepository,
50+
configService,
5051
dbserver?.getClient(),
5152
);
5253

store/auth/apikey/.gitkeep

Whitespace-only changes.

store/auth/jwt/.gitkeep

Whitespace-only changes.

store/chats/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)