Skip to content

Commit

Permalink
fix: Adjusts in settings with options always_online, read_messages an…
Browse files Browse the repository at this point in the history
…d read_status
  • Loading branch information
DavidsonGomes committed Jul 25, 2023
1 parent 183efd4 commit a12231a
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.4.3 (homolog)

### Fixed

* Adjusts in settings with options always_online, read_messages and read_status

# 1.4.2 (2023-07-24 20:52)

### Fixed
Expand Down
25 changes: 20 additions & 5 deletions src/validate/validate.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ export const readMessageSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
readMessages: {
read_messages: {
type: 'array',
minItems: 1,
uniqueItems: true,
Expand All @@ -470,7 +470,7 @@ export const readMessageSchema: JSONSchema7 = {
},
},
},
required: ['readMessages'],
required: ['read_messages'],
};

export const privacySettingsSchema: JSONSchema7 = {
Expand Down Expand Up @@ -884,7 +884,22 @@ export const settingsSchema: JSONSchema7 = {
reject_call: { type: 'boolean', enum: [true, false] },
msg_call: { type: 'string' },
groups_ignore: { type: 'boolean', enum: [true, false] },
},
required: ['reject_call'],
...isNotEmpty('reject_call'),
always_online: { type: 'boolean', enum: [true, false] },
read_messages: { type: 'boolean', enum: [true, false] },
read_status: { type: 'boolean', enum: [true, false] },
},
required: [
'reject_call',
'groups_ignore',
'always_online',
'read_messages',
'read_status',
],
...isNotEmpty(
'reject_call',
'groups_ignore',
'always_online',
'read_messages',
'read_status',
),
};
2 changes: 1 addition & 1 deletion src/whatsapp/dto/chat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Key {
remoteJid: string;
}
export class ReadMessageDto {
readMessages: Key[];
read_messages: Key[];
}

class LastMessage {
Expand Down
3 changes: 3 additions & 0 deletions src/whatsapp/dto/settings.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ export class SettingsDto {
reject_call?: boolean;
msg_call?: string;
groups_ignore?: boolean;
always_online?: boolean;
read_messages?: boolean;
read_status?: boolean;
}
6 changes: 6 additions & 0 deletions src/whatsapp/models/settings.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ export class SettingsRaw {
reject_call?: boolean;
msg_call?: string;
groups_ignore?: boolean;
always_online?: boolean;
read_messages?: boolean;
read_status?: boolean;
}

const settingsSchema = new Schema<SettingsRaw>({
_id: { type: String, _id: true },
reject_call: { type: Boolean, required: true },
msg_call: { type: String, required: true },
groups_ignore: { type: Boolean, required: true },
always_online: { type: Boolean, required: true },
read_messages: { type: Boolean, required: true },
read_status: { type: Boolean, required: true },
});

export const SettingsModel = dbserver?.model(
Expand Down
29 changes: 27 additions & 2 deletions src/whatsapp/services/whatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,15 @@ export class WAStartupService {
this.localSettings.groups_ignore = data?.groups_ignore;
this.logger.verbose(`Settings groups_ignore: ${this.localSettings.groups_ignore}`);

this.localSettings.always_online = data?.always_online;
this.logger.verbose(`Settings always_online: ${this.localSettings.always_online}`);

this.localSettings.read_messages = data?.read_messages;
this.logger.verbose(`Settings read_messages: ${this.localSettings.read_messages}`);

this.localSettings.read_status = data?.read_status;
this.logger.verbose(`Settings read_status: ${this.localSettings.read_status}`);

this.logger.verbose('Settings loaded');
}

Expand All @@ -371,8 +380,13 @@ export class WAStartupService {
this.logger.verbose(`Settings reject_call: ${data.reject_call}`);
this.logger.verbose(`Settings msg_call: ${data.msg_call}`);
this.logger.verbose(`Settings groups_ignore: ${data.groups_ignore}`);
this.logger.verbose(`Settings always_online: ${data.always_online}`);
this.logger.verbose(`Settings read_messages: ${data.read_messages}`);
this.logger.verbose(`Settings read_status: ${data.read_status}`);
Object.assign(this.localSettings, data);
this.logger.verbose('Settings set');

this.client?.ws?.close();
}

public async findSettings() {
Expand All @@ -387,6 +401,9 @@ export class WAStartupService {
this.logger.verbose(`Settings url: ${data.reject_call}`);
this.logger.verbose(`Settings msg_call: ${data.msg_call}`);
this.logger.verbose(`Settings groups_ignore: ${data.groups_ignore}`);
this.logger.verbose(`Settings always_online: ${data.always_online}`);
this.logger.verbose(`Settings read_messages: ${data.read_messages}`);
this.logger.verbose(`Settings read_status: ${data.read_status}`);
return data;
}

Expand Down Expand Up @@ -847,6 +864,7 @@ export class WAStartupService {
printQRInTerminal: false,
browser,
version,
markOnlineOnConnect: this.localSettings.always_online,
connectTimeoutMs: 60_000,
qrTimeout: 40_000,
defaultQueryTimeoutMs: undefined,
Expand Down Expand Up @@ -1143,6 +1161,14 @@ export class WAStartupService {
source: getDevice(received.key.id),
};

if (this.localSettings.read_messages && received.key.id !== 'status@broadcast') {
await this.client.readMessages([received.key]);
}

if (this.localSettings.read_status && received.key.id === 'status@broadcast') {
await this.client.readMessages([received.key]);
}

this.logger.log(messageRaw);

this.logger.verbose('Sending data to webhook in event MESSAGES_UPSERT');
Expand Down Expand Up @@ -2400,7 +2426,7 @@ export class WAStartupService {
this.logger.verbose('Marking message as read');
try {
const keys: proto.IMessageKey[] = [];
data.readMessages.forEach((read) => {
data.read_messages.forEach((read) => {
if (isJidGroup(read.remoteJid) || isJidUser(read.remoteJid)) {
keys.push({
remoteJid: read.remoteJid,
Expand Down Expand Up @@ -2640,7 +2666,6 @@ export class WAStartupService {
await this.client.updateGroupsAddPrivacy(settings.privacySettings.groupadd);
this.logger.verbose('Groups add privacy updated');

// reinicia a instancia
this.client?.ws?.close();

return {
Expand Down
3 changes: 3 additions & 0 deletions src/whatsapp/types/wa.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export declare namespace wa {
reject_call?: boolean;
msg_call?: string;
groups_ignore?: boolean;
always_online?: boolean;
read_messages?: boolean;
read_status?: boolean;
};

export type StateConnection = {
Expand Down

0 comments on commit a12231a

Please sign in to comment.