mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-03 04:27:53 +00:00
fix: Remove duplicates message in WhatsApp Channel (#7003)
This commit is contained in:
@@ -93,7 +93,9 @@ import { mapGetters } from 'vuex';
|
|||||||
|
|
||||||
import ReplyBox from './ReplyBox';
|
import ReplyBox from './ReplyBox';
|
||||||
import Message from './Message';
|
import Message from './Message';
|
||||||
import conversationMixin from '../../../mixins/conversations';
|
import conversationMixin, {
|
||||||
|
filterDuplicateSourceMessages,
|
||||||
|
} from '../../../mixins/conversations';
|
||||||
import Banner from 'dashboard/components/ui/Banner.vue';
|
import Banner from 'dashboard/components/ui/Banner.vue';
|
||||||
import { getTypingUsersText } from '../../../helper/commons';
|
import { getTypingUsersText } from '../../../helper/commons';
|
||||||
import { BUS_EVENTS } from 'shared/constants/busEvents';
|
import { BUS_EVENTS } from 'shared/constants/busEvents';
|
||||||
@@ -171,24 +173,28 @@ export default {
|
|||||||
|
|
||||||
return '';
|
return '';
|
||||||
},
|
},
|
||||||
|
|
||||||
getMessages() {
|
getMessages() {
|
||||||
const [chat] = this.allConversations.filter(
|
const messages = this.currentChat.messages || [];
|
||||||
c => c.id === this.currentChat.id
|
if (this.isAWhatsAppChannel) {
|
||||||
);
|
return filterDuplicateSourceMessages(messages);
|
||||||
return chat;
|
}
|
||||||
|
return messages;
|
||||||
},
|
},
|
||||||
getReadMessages() {
|
getReadMessages() {
|
||||||
const chat = this.getMessages;
|
return this.readMessages(
|
||||||
return chat === undefined ? null : this.readMessages(chat);
|
this.getMessages,
|
||||||
|
this.currentChat.agent_last_seen_at
|
||||||
|
);
|
||||||
},
|
},
|
||||||
getUnReadMessages() {
|
getUnReadMessages() {
|
||||||
const chat = this.getMessages;
|
return this.unReadMessages(
|
||||||
return chat === undefined ? null : this.unReadMessages(chat);
|
this.getMessages,
|
||||||
|
this.currentChat.agent_last_seen_at
|
||||||
|
);
|
||||||
},
|
},
|
||||||
shouldShowSpinner() {
|
shouldShowSpinner() {
|
||||||
return (
|
return (
|
||||||
(this.getMessages && this.getMessages.dataFetched === undefined) ||
|
(this.currentChat && this.currentChat.dataFetched === undefined) ||
|
||||||
(!this.listLoadingStatus && this.isLoadingPrevious)
|
(!this.listLoadingStatus && this.isLoadingPrevious)
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
@@ -208,7 +214,7 @@ export default {
|
|||||||
|
|
||||||
selectedTweet() {
|
selectedTweet() {
|
||||||
if (this.selectedTweetId) {
|
if (this.selectedTweetId) {
|
||||||
const { messages = [] } = this.getMessages;
|
const { messages = [] } = this.currentChat;
|
||||||
const [selectedMessage] = messages.filter(
|
const [selectedMessage] = messages.filter(
|
||||||
message => message.id === this.selectedTweetId
|
message => message.id === this.selectedTweetId
|
||||||
);
|
);
|
||||||
@@ -360,7 +366,7 @@ export default {
|
|||||||
async fetchPreviousMessages(scrollTop = 0) {
|
async fetchPreviousMessages(scrollTop = 0) {
|
||||||
this.setScrollParams();
|
this.setScrollParams();
|
||||||
const shouldLoadMoreMessages =
|
const shouldLoadMoreMessages =
|
||||||
this.getMessages.dataFetched === true &&
|
this.currentChat.dataFetched === true &&
|
||||||
!this.listLoadingStatus &&
|
!this.listLoadingStatus &&
|
||||||
!this.isLoadingPrevious;
|
!this.isLoadingPrevious;
|
||||||
|
|
||||||
@@ -373,7 +379,7 @@ export default {
|
|||||||
try {
|
try {
|
||||||
await this.$store.dispatch('fetchPreviousMessages', {
|
await this.$store.dispatch('fetchPreviousMessages', {
|
||||||
conversationId: this.currentChat.id,
|
conversationId: this.currentChat.id,
|
||||||
before: this.getMessages.messages[0].id,
|
before: this.currentChat.messages[0].id,
|
||||||
});
|
});
|
||||||
const heightDifference =
|
const heightDifference =
|
||||||
this.conversationPanel.scrollHeight - this.heightBeforeLoad;
|
this.conversationPanel.scrollHeight - this.heightBeforeLoad;
|
||||||
|
|||||||
@@ -12,6 +12,26 @@ const getLastNonActivityMessage = (messageInStore, messageFromAPI) => {
|
|||||||
return messageInStore || messageFromAPI;
|
return messageInStore || messageFromAPI;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const filterDuplicateSourceMessages = (messages = []) => {
|
||||||
|
const messagesWithoutDuplicates = [];
|
||||||
|
// We cannot use Map or any short hand method as it returns the last message with the duplicate ID
|
||||||
|
// We should return the message with smaller id when there is a duplicate
|
||||||
|
messages.forEach(m1 => {
|
||||||
|
if (m1.source_id) {
|
||||||
|
if (
|
||||||
|
messagesWithoutDuplicates.findIndex(
|
||||||
|
m2 => m1.source_id === m2.source_id
|
||||||
|
) < 0
|
||||||
|
) {
|
||||||
|
messagesWithoutDuplicates.push(m1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
messagesWithoutDuplicates.push(m1);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return messagesWithoutDuplicates;
|
||||||
|
};
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
methods: {
|
methods: {
|
||||||
lastMessage(m) {
|
lastMessage(m) {
|
||||||
@@ -34,14 +54,14 @@ export default {
|
|||||||
lastNonActivityMessageFromAPI
|
lastNonActivityMessageFromAPI
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
readMessages(m) {
|
readMessages(messages, agentLastSeenAt) {
|
||||||
return m.messages.filter(
|
return messages.filter(
|
||||||
chat => chat.created_at * 1000 <= m.agent_last_seen_at * 1000
|
message => message.created_at * 1000 <= agentLastSeenAt * 1000
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
unReadMessages(m) {
|
unReadMessages(messages, agentLastSeenAt) {
|
||||||
return m.messages.filter(
|
return messages.filter(
|
||||||
chat => chat.created_at * 1000 > m.agent_last_seen_at * 1000
|
message => message.created_at * 1000 > agentLastSeenAt * 1000
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,17 +1,50 @@
|
|||||||
import conversationMixin from '../conversations';
|
import conversationMixin, {
|
||||||
|
filterDuplicateSourceMessages,
|
||||||
|
} from '../conversations';
|
||||||
import conversationFixture from './conversationFixtures';
|
import conversationFixture from './conversationFixtures';
|
||||||
import commonHelpers from '../../helper/commons';
|
import commonHelpers from '../../helper/commons';
|
||||||
commonHelpers();
|
commonHelpers();
|
||||||
|
|
||||||
|
describe('#filterDuplicateSourceMessages', () => {
|
||||||
|
it('returns messages without duplicate source_id and all messages without source_id', () => {
|
||||||
|
expect(
|
||||||
|
filterDuplicateSourceMessages([
|
||||||
|
{ source_id: null, id: 1 },
|
||||||
|
{ source_id: '', id: 2 },
|
||||||
|
{ id: 3 },
|
||||||
|
{ source_id: 'wa_1', id: 4 },
|
||||||
|
{ source_id: 'wa_1', id: 5 },
|
||||||
|
{ source_id: 'wa_1', id: 6 },
|
||||||
|
{ source_id: 'wa_2', id: 7 },
|
||||||
|
{ source_id: 'wa_2', id: 8 },
|
||||||
|
{ source_id: 'wa_3', id: 9 },
|
||||||
|
])
|
||||||
|
).toEqual([
|
||||||
|
{ source_id: null, id: 1 },
|
||||||
|
{ source_id: '', id: 2 },
|
||||||
|
{ id: 3 },
|
||||||
|
{ source_id: 'wa_1', id: 4 },
|
||||||
|
{ source_id: 'wa_2', id: 7 },
|
||||||
|
{ source_id: 'wa_3', id: 9 },
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('#conversationMixin', () => {
|
describe('#conversationMixin', () => {
|
||||||
it('should return read messages if conversation is passed', () => {
|
it('should return read messages if conversation is passed', () => {
|
||||||
expect(
|
expect(
|
||||||
conversationMixin.methods.readMessages(conversationFixture.conversation)
|
conversationMixin.methods.readMessages(
|
||||||
|
conversationFixture.conversation.messages,
|
||||||
|
conversationFixture.conversation.agent_last_seen_at
|
||||||
|
)
|
||||||
).toEqual(conversationFixture.readMessages);
|
).toEqual(conversationFixture.readMessages);
|
||||||
});
|
});
|
||||||
it('should return read messages if conversation is passed', () => {
|
it('should return read messages if conversation is passed', () => {
|
||||||
expect(
|
expect(
|
||||||
conversationMixin.methods.unReadMessages(conversationFixture.conversation)
|
conversationMixin.methods.unReadMessages(
|
||||||
|
conversationFixture.conversation.messages,
|
||||||
|
conversationFixture.conversation.agent_last_seen_at
|
||||||
|
)
|
||||||
).toEqual(conversationFixture.unReadMessages);
|
).toEqual(conversationFixture.unReadMessages);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user