HH-569: guard replayed frontend realtime messages (#144)
* fix(HH-569): guard replayed realtime messages * fix(HH-569): gate dashboard message side effects --------- Co-authored-by: Rogee <rogee@ipao.vip>
This commit is contained in:
@@ -173,13 +173,24 @@ export const actions = {
|
||||
commit('clearConversations');
|
||||
},
|
||||
|
||||
addOrUpdateMessage: async ({ commit }, data) => {
|
||||
addOrUpdateMessage: async ({ commit, state }, data) => {
|
||||
const { id, content_attributes } = data;
|
||||
const currentMessage = state?.conversations?.[id];
|
||||
const isNew = !currentMessage;
|
||||
if (
|
||||
currentMessage?.updated_at &&
|
||||
data.updated_at &&
|
||||
data.updated_at <= currentMessage.updated_at
|
||||
) {
|
||||
return { applied: false, isNew };
|
||||
}
|
||||
|
||||
if (content_attributes && content_attributes.deleted) {
|
||||
commit('deleteMessage', id);
|
||||
return;
|
||||
return { applied: true, isNew };
|
||||
}
|
||||
commit('pushMessageToConversation', data);
|
||||
return { applied: true, isNew };
|
||||
},
|
||||
|
||||
toggleAgentTyping({ commit }, data) {
|
||||
|
||||
@@ -82,6 +82,38 @@ describe('#actions', () => {
|
||||
message_type: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it('ignores an older realtime update', async () => {
|
||||
const data = {
|
||||
id: 1,
|
||||
updated_at: '2026-08-24T01:00:00Z',
|
||||
content_attributes: {},
|
||||
};
|
||||
const state = {
|
||||
conversations: {
|
||||
1: { id: 1, updated_at: '2026-08-24T02:00:00Z' },
|
||||
},
|
||||
};
|
||||
|
||||
const result = await actions.addOrUpdateMessage({ commit, state }, data);
|
||||
|
||||
expect(result).toEqual({ applied: false, isNew: false });
|
||||
expect(commit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('ignores a replayed message with the same id and version', async () => {
|
||||
const data = {
|
||||
id: 1,
|
||||
updated_at: '2026-08-24T02:00:00Z',
|
||||
content_attributes: {},
|
||||
};
|
||||
const state = { conversations: { 1: data } };
|
||||
|
||||
const result = await actions.addOrUpdateMessage({ commit, state }, data);
|
||||
|
||||
expect(result).toEqual({ applied: false, isNew: false });
|
||||
expect(commit).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#toggleAgentTyping', () => {
|
||||
|
||||
Reference in New Issue
Block a user