Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed label update to chat instead of contacts #411

Merged
merged 1 commit into from
Feb 9, 2024
Merged
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
2 changes: 2 additions & 0 deletions src/whatsapp/models/chat.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class ChatRaw {
id?: string;
owner: string;
lastMsgTimestamp?: number;
labels?: string[];
}

type ChatRawBoolean<T> = {
Expand All @@ -18,6 +19,7 @@ const chatSchema = new Schema<ChatRaw>({
_id: { type: String, _id: true },
id: { type: String, required: true, minlength: 1 },
owner: { type: String, required: true, minlength: 1 },
labels: { type: [String], default: [] },
});

export const ChatModel = dbserver?.model(ChatRaw.name, chatSchema, 'chats');
Expand Down
2 changes: 0 additions & 2 deletions src/whatsapp/models/contact.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export class ContactRaw {
id?: string;
profilePictureUrl?: string;
owner: string;
labels?: string[];
}

type ContactRawBoolean<T> = {
Expand All @@ -22,7 +21,6 @@ const contactSchema = new Schema<ContactRaw>({
id: { type: String, required: true, minlength: 1 },
profilePictureUrl: { type: String, minlength: 1 },
owner: { type: String, required: true, minlength: 1 },
labels: { type: [String], default: [] },
});

export const ContactModel = dbserver?.model(ContactRaw.name, contactSchema, 'contacts');
Expand Down
59 changes: 59 additions & 0 deletions src/whatsapp/repository/chat.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,63 @@ export class ChatRepository extends Repository {
return { error: error?.toString() };
}
}

public async update(data: ChatRaw[], instanceName: string, saveDb = false): Promise<IInsert> {
try {
this.logger.verbose('updating chats');

if (data.length === 0) {
this.logger.verbose('no chats to update');
return;
}

if (this.dbSettings.ENABLED && saveDb) {
this.logger.verbose('updating chats in db');

const chats = data.map((chat) => {
return {
updateOne: {
filter: { id: chat.id },
update: { ...chat },
upsert: true,
},
};
});

const { nModified } = await this.chatModel.bulkWrite(chats);

this.logger.verbose('chats updated in db: ' + nModified + ' chats');
return { insertCount: nModified };
}

this.logger.verbose('updating chats in store');

const store = this.configService.get<StoreConf>('STORE');

if (store.CONTACTS) {
this.logger.verbose('updating chats in store');
data.forEach((chat) => {
this.writeStore({
path: join(this.storePath, 'chats', instanceName),
fileName: chat.id,
data: chat,
});
this.logger.verbose(
'chats updated in store in path: ' + join(this.storePath, 'chats', instanceName) + '/' + chat.id,
);
});

this.logger.verbose('chats updated in store: ' + data.length + ' chats');

return { insertCount: data.length };
}

this.logger.verbose('chats not updated');
return { insertCount: 0 };
} catch (error) {
return error;
} finally {
data = undefined;
}
}
}
41 changes: 22 additions & 19 deletions src/whatsapp/services/whatsapp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2209,32 +2209,34 @@ export class WAStartupService {
) => {
this.logger.verbose('Sending data to webhook in event LABELS_ASSOCIATION');

// Atualiza labels no contato
const contact = await this.repository.contact.find({
where: {
owner: this.instance.name,
id: data.association.chatId,
},
});
if (contact.length > 0) {
let labels = [...contact[0].labels];
if (data.type === 'remove') {
labels = labels.filter((label) => label !== data.association.labelId);
} else if (data.type === 'add') {
labels = [...labels, data.association.labelId];
// Atualiza labels nos chats
if (database.SAVE_DATA.CHATS) {
const chats = await this.repository.chat.find({
where: {
owner: this.instance.name,
},
});
const chat = chats.find((c) => c.id === data.association.chatId);
if (chat) {
let labels = [...chat.labels];
if (data.type === 'remove') {
labels = labels.filter((label) => label !== data.association.labelId);
} else if (data.type === 'add') {
labels = [...labels, data.association.labelId];
}
await this.repository.chat.update(
[{ id: chat.id, owner: this.instance.name, labels }],
this.instance.name,
database.SAVE_DATA.CHATS,
);
}
await this.repository.contact.update(
[{ ...contact[0], labels }],
this.instance.name,
database.SAVE_DATA.CONTACTS,
);
}

// Envia dados para o webhook
this.sendDataWebhook(Events.LABELS_ASSOCIATION, {
instance: this.instance.name,
type: data.type,
jid: data.association.chatId,
chatId: data.association.chatId,
labelId: data.association.labelId,
});
},
Expand All @@ -2244,6 +2246,7 @@ export class WAStartupService {
this.logger.verbose('Initializing event handler');
this.client.ev.process(async (events) => {
if (!this.endSession) {
this.logger.verbose(`Event received: ${Object.keys(events).join(', ')}`);
const database = this.configService.get<Database>('DATABASE');
const settings = await this.findSettings();

Expand Down