mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-25 01:48:39 -06:00
Changed label update to chat instead of contacts
This commit is contained in:
parent
a2622cb38e
commit
3df4e8d2ba
@ -7,6 +7,7 @@ export class ChatRaw {
|
|||||||
id?: string;
|
id?: string;
|
||||||
owner: string;
|
owner: string;
|
||||||
lastMsgTimestamp?: number;
|
lastMsgTimestamp?: number;
|
||||||
|
labels?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
type ChatRawBoolean<T> = {
|
type ChatRawBoolean<T> = {
|
||||||
@ -18,6 +19,7 @@ const chatSchema = new Schema<ChatRaw>({
|
|||||||
_id: { type: String, _id: true },
|
_id: { type: String, _id: true },
|
||||||
id: { type: String, required: true, minlength: 1 },
|
id: { type: String, required: true, minlength: 1 },
|
||||||
owner: { 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');
|
export const ChatModel = dbserver?.model(ChatRaw.name, chatSchema, 'chats');
|
||||||
|
@ -8,7 +8,6 @@ export class ContactRaw {
|
|||||||
id?: string;
|
id?: string;
|
||||||
profilePictureUrl?: string;
|
profilePictureUrl?: string;
|
||||||
owner: string;
|
owner: string;
|
||||||
labels?: string[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ContactRawBoolean<T> = {
|
type ContactRawBoolean<T> = {
|
||||||
@ -22,7 +21,6 @@ const contactSchema = new Schema<ContactRaw>({
|
|||||||
id: { type: String, required: true, minlength: 1 },
|
id: { type: String, required: true, minlength: 1 },
|
||||||
profilePictureUrl: { type: String, minlength: 1 },
|
profilePictureUrl: { type: String, minlength: 1 },
|
||||||
owner: { type: String, required: true, minlength: 1 },
|
owner: { type: String, required: true, minlength: 1 },
|
||||||
labels: { type: [String], default: [] },
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export const ContactModel = dbserver?.model(ContactRaw.name, contactSchema, 'contacts');
|
export const ContactModel = dbserver?.model(ContactRaw.name, contactSchema, 'contacts');
|
||||||
|
@ -115,4 +115,63 @@ export class ChatRepository extends Repository {
|
|||||||
return { error: error?.toString() };
|
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');
|
this.logger.verbose('Sending data to webhook in event LABELS_ASSOCIATION');
|
||||||
|
|
||||||
// Atualiza labels no contato
|
// Atualiza labels nos chats
|
||||||
const contact = await this.repository.contact.find({
|
if (database.SAVE_DATA.CHATS) {
|
||||||
where: {
|
const chats = await this.repository.chat.find({
|
||||||
owner: this.instance.name,
|
where: {
|
||||||
id: data.association.chatId,
|
owner: this.instance.name,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
if (contact.length > 0) {
|
const chat = chats.find((c) => c.id === data.association.chatId);
|
||||||
let labels = [...contact[0].labels];
|
if (chat) {
|
||||||
if (data.type === 'remove') {
|
let labels = [...chat.labels];
|
||||||
labels = labels.filter((label) => label !== data.association.labelId);
|
if (data.type === 'remove') {
|
||||||
} else if (data.type === 'add') {
|
labels = labels.filter((label) => label !== data.association.labelId);
|
||||||
labels = [...labels, 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
|
// Envia dados para o webhook
|
||||||
this.sendDataWebhook(Events.LABELS_ASSOCIATION, {
|
this.sendDataWebhook(Events.LABELS_ASSOCIATION, {
|
||||||
instance: this.instance.name,
|
instance: this.instance.name,
|
||||||
type: data.type,
|
type: data.type,
|
||||||
jid: data.association.chatId,
|
chatId: data.association.chatId,
|
||||||
labelId: data.association.labelId,
|
labelId: data.association.labelId,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@ -2244,6 +2246,7 @@ export class WAStartupService {
|
|||||||
this.logger.verbose('Initializing event handler');
|
this.logger.verbose('Initializing event handler');
|
||||||
this.client.ev.process(async (events) => {
|
this.client.ev.process(async (events) => {
|
||||||
if (!this.endSession) {
|
if (!this.endSession) {
|
||||||
|
this.logger.verbose(`Event received: ${Object.keys(events).join(', ')}`);
|
||||||
const database = this.configService.get<Database>('DATABASE');
|
const database = this.configService.get<Database>('DATABASE');
|
||||||
const settings = await this.findSettings();
|
const settings = await this.findSettings();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user