mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-22 20:12:02 -06:00
Merge pull request #411 from judsonjuniorr/feat/labels
Changed label update to chat instead of contacts
This commit is contained in:
commit
4bb81b9a41
@ -7,6 +7,7 @@ export class ChatRaw {
|
||||
id?: string;
|
||||
owner: string;
|
||||
lastMsgTimestamp?: number;
|
||||
labels?: string[];
|
||||
}
|
||||
|
||||
type ChatRawBoolean<T> = {
|
||||
@ -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');
|
||||
|
@ -8,7 +8,6 @@ export class ContactRaw {
|
||||
id?: string;
|
||||
profilePictureUrl?: string;
|
||||
owner: string;
|
||||
labels?: string[];
|
||||
}
|
||||
|
||||
type ContactRawBoolean<T> = {
|
||||
@ -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');
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
});
|
||||
},
|
||||
@ -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();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user