mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 01:41:24 -06:00
fix: receive medias on chatwoot
This commit is contained in:
parent
5401ecd2c4
commit
34769e2293
@ -933,9 +933,11 @@ export class ChatwootService {
|
|||||||
) {
|
) {
|
||||||
if (sourceId && this.isImportHistoryAvailable()) {
|
if (sourceId && this.isImportHistoryAvailable()) {
|
||||||
const messageAlreadySaved = await chatwootImport.getExistingSourceIds([sourceId]);
|
const messageAlreadySaved = await chatwootImport.getExistingSourceIds([sourceId]);
|
||||||
if (messageAlreadySaved.size > 0) {
|
if (messageAlreadySaved) {
|
||||||
this.logger.warn('Message already saved on chatwoot');
|
if (messageAlreadySaved.size > 0) {
|
||||||
return null;
|
this.logger.warn('Message already saved on chatwoot');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const data = new FormData();
|
const data = new FormData();
|
||||||
@ -2442,57 +2444,61 @@ export class ChatwootService {
|
|||||||
chatwootConfig: ChatwootDto,
|
chatwootConfig: ChatwootDto,
|
||||||
prepareMessage: (message: any) => any,
|
prepareMessage: (message: any) => any,
|
||||||
) {
|
) {
|
||||||
if (!this.isImportHistoryAvailable()) {
|
try {
|
||||||
return;
|
if (!this.isImportHistoryAvailable()) {
|
||||||
}
|
return;
|
||||||
if (!this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
|
}
|
||||||
return;
|
if (!this.configService.get<Database>('DATABASE').SAVE_DATA.MESSAGE_UPDATE) {
|
||||||
}
|
return;
|
||||||
|
|
||||||
const inbox = await this.getInbox(instance);
|
|
||||||
|
|
||||||
const sqlMessages = `select * from messages m
|
|
||||||
where account_id = ${chatwootConfig.accountId}
|
|
||||||
and inbox_id = ${inbox.id}
|
|
||||||
and created_at >= now() - interval '6h'
|
|
||||||
order by created_at desc`;
|
|
||||||
|
|
||||||
const messagesData = (await this.pgClient.query(sqlMessages))?.rows;
|
|
||||||
const ids: string[] = messagesData
|
|
||||||
.filter((message) => !!message.source_id)
|
|
||||||
.map((message) => message.source_id.replace('WAID:', ''));
|
|
||||||
|
|
||||||
const savedMessages = await this.prismaRepository.message.findMany({
|
|
||||||
where: {
|
|
||||||
Instance: { name: instance.instanceName },
|
|
||||||
messageTimestamp: { gte: dayjs().subtract(6, 'hours').unix() },
|
|
||||||
AND: ids.map((id) => ({ key: { path: ['id'], not: id } })),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const filteredMessages = savedMessages.filter(
|
|
||||||
(msg: any) => !chatwootImport.isIgnorePhoneNumber(msg.key?.remoteJid),
|
|
||||||
);
|
|
||||||
const messagesRaw: any[] = [];
|
|
||||||
for (const m of filteredMessages) {
|
|
||||||
if (!m.message || !m.key || !m.messageTimestamp) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Long.isLong(m?.messageTimestamp)) {
|
const inbox = await this.getInbox(instance);
|
||||||
m.messageTimestamp = m.messageTimestamp?.toNumber();
|
|
||||||
|
const sqlMessages = `select * from messages m
|
||||||
|
where account_id = ${chatwootConfig.accountId}
|
||||||
|
and inbox_id = ${inbox.id}
|
||||||
|
and created_at >= now() - interval '6h'
|
||||||
|
order by created_at desc`;
|
||||||
|
|
||||||
|
const messagesData = (await this.pgClient.query(sqlMessages))?.rows;
|
||||||
|
const ids: string[] = messagesData
|
||||||
|
.filter((message) => !!message.source_id)
|
||||||
|
.map((message) => message.source_id.replace('WAID:', ''));
|
||||||
|
|
||||||
|
const savedMessages = await this.prismaRepository.message.findMany({
|
||||||
|
where: {
|
||||||
|
Instance: { name: instance.instanceName },
|
||||||
|
messageTimestamp: { gte: dayjs().subtract(6, 'hours').unix() },
|
||||||
|
AND: ids.map((id) => ({ key: { path: ['id'], not: id } })),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const filteredMessages = savedMessages.filter(
|
||||||
|
(msg: any) => !chatwootImport.isIgnorePhoneNumber(msg.key?.remoteJid),
|
||||||
|
);
|
||||||
|
const messagesRaw: any[] = [];
|
||||||
|
for (const m of filteredMessages) {
|
||||||
|
if (!m.message || !m.key || !m.messageTimestamp) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Long.isLong(m?.messageTimestamp)) {
|
||||||
|
m.messageTimestamp = m.messageTimestamp?.toNumber();
|
||||||
|
}
|
||||||
|
|
||||||
|
messagesRaw.push(prepareMessage(m as any));
|
||||||
}
|
}
|
||||||
|
|
||||||
messagesRaw.push(prepareMessage(m as any));
|
this.addHistoryMessages(
|
||||||
|
instance,
|
||||||
|
messagesRaw.filter((msg) => !chatwootImport.isIgnorePhoneNumber(msg.key?.remoteJid)),
|
||||||
|
);
|
||||||
|
|
||||||
|
await chatwootImport.importHistoryMessages(instance, this, inbox, this.provider);
|
||||||
|
const waInstance = this.waMonitor.waInstances[instance.instanceName];
|
||||||
|
waInstance.clearCacheChatwoot();
|
||||||
|
} catch (error) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.addHistoryMessages(
|
|
||||||
instance,
|
|
||||||
messagesRaw.filter((msg) => !chatwootImport.isIgnorePhoneNumber(msg.key?.remoteJid)),
|
|
||||||
);
|
|
||||||
|
|
||||||
await chatwootImport.importHistoryMessages(instance, this, inbox, this.provider);
|
|
||||||
const waInstance = this.waMonitor.waInstances[instance.instanceName];
|
|
||||||
waInstance.clearCacheChatwoot();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -170,22 +170,26 @@ class ChatwootImport {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async getExistingSourceIds(sourceIds: string[]): Promise<Set<string>> {
|
public async getExistingSourceIds(sourceIds: string[]): Promise<Set<string>> {
|
||||||
const existingSourceIdsSet = new Set<string>();
|
try {
|
||||||
|
const existingSourceIdsSet = new Set<string>();
|
||||||
|
|
||||||
|
if (sourceIds.length === 0) {
|
||||||
|
return existingSourceIdsSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
const formattedSourceIds = sourceIds.map((sourceId) => `WAID:${sourceId.replace('WAID:', '')}`); // Make sure the sourceId is always formatted as WAID:1234567890
|
||||||
|
const query = 'SELECT source_id FROM messages WHERE source_id = ANY($1)';
|
||||||
|
const pgClient = postgresClient.getChatwootConnection();
|
||||||
|
const result = await pgClient.query(query, [formattedSourceIds]);
|
||||||
|
|
||||||
|
for (const row of result.rows) {
|
||||||
|
existingSourceIdsSet.add(row.source_id);
|
||||||
|
}
|
||||||
|
|
||||||
if (sourceIds.length === 0) {
|
|
||||||
return existingSourceIdsSet;
|
return existingSourceIdsSet;
|
||||||
|
} catch (error) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const formattedSourceIds = sourceIds.map((sourceId) => `WAID:${sourceId.replace('WAID:', '')}`); // Make sure the sourceId is always formatted as WAID:1234567890
|
|
||||||
const query = 'SELECT source_id FROM messages WHERE source_id = ANY($1)';
|
|
||||||
const pgClient = postgresClient.getChatwootConnection();
|
|
||||||
const result = await pgClient.query(query, [formattedSourceIds]);
|
|
||||||
|
|
||||||
for (const row of result.rows) {
|
|
||||||
existingSourceIdsSet.add(row.source_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
return existingSourceIdsSet;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async importHistoryMessages(
|
public async importHistoryMessages(
|
||||||
|
Loading…
Reference in New Issue
Block a user