Skip to content

Commit

Permalink
fix(messagesStore): update lastReadMessage if posted by same actor
Browse files Browse the repository at this point in the history
- text messages already covered in postNewMessage
- this fix is for file shares (and for text messages from another session)

Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Feb 24, 2025
1 parent bd54619 commit 62f0414
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 6 deletions.
19 changes: 16 additions & 3 deletions src/store/messagesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ const actions = {
let countNewMessages = 0
let hasNewMention = conversation.unreadMention
let lastMessage = null
let lastMessageFromSelf = null
// Process each messages and adds it to the store
response.data.ocs.data.forEach(message => {
if (message.actorType === ATTENDEE.ACTOR_TYPE.GUESTS) {
Expand All @@ -1117,11 +1118,16 @@ const actions = {
const guestNameStore = useGuestNameStore()
guestNameStore.addGuestName(message, { noUpdate: false })
}

const isMessageFromSelf = message.actorId === actorId && message.actorType === actorType

context.dispatch('processMessage', { token, message })
if (!lastMessage || message.id > lastMessage.id) {
if (!message.systemMessage) {
if (actorId !== message.actorId || actorType !== message.actorType) {
if (!isMessageFromSelf) {
countNewMessages++
} else {
lastMessageFromSelf = message
}

// parse mentions data to update "conversation.unreadMention",
Expand All @@ -1145,8 +1151,7 @@ const actions = {
}
if (message.systemMessage === 'call_ended_everyone'
&& conversation.type !== CONVERSATION.TYPE.ONE_TO_ONE
&& !(message.actorId === context.getters.getActorId()
&& message.actorType === context.getters.getActorType())) {
&& !isMessageFromSelf) {
const callViewStore = useCallViewStore()
callViewStore.setCallHasJustEnded(message.timestamp)

Expand Down Expand Up @@ -1189,6 +1194,14 @@ const actions = {
}
}

if (conversation && lastMessageFromSelf?.id > conversation.lastReadMessage) {
context.dispatch('updateLastReadMessage', {
token,
id: lastMessageFromSelf.id,
updateVisually: true,
})
}

context.commit('loadedMessagesOfConversation', { token })

return response
Expand Down
46 changes: 43 additions & 3 deletions src/store/messagesStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,7 @@ describe('messagesStore', () => {
describe('look for new messages', () => {
let updateLastCommonReadMessageAction
let updateConversationLastMessageAction
let updateLastReadMessageAction
let updateUnreadMessagesMutation
let addGuestNameAction
let cancelFunctionMocks
Expand All @@ -1144,11 +1145,11 @@ describe('messagesStore', () => {
const guestNameStore = useGuestNameStore()

conversationMock = jest.fn()
getActorIdMock = jest.fn()
getActorTypeMock = jest.fn()
getActorIdMock = jest.fn(() => 'actor-id-1')
getActorTypeMock = jest.fn(() => ATTENDEE.ACTOR_TYPE.USERS)
isActorUserMock = jest.fn()
isActorGuestMock = jest.fn()
getUserIdMock = jest.fn()
getUserIdMock = jest.fn(() => 'actor-id-1')
testStoreConfig.getters.conversation = jest.fn().mockReturnValue(conversationMock)
testStoreConfig.getters.getActorId = jest.fn().mockReturnValue(getActorIdMock)
testStoreConfig.getters.getActorType = jest.fn().mockReturnValue(getActorTypeMock)
Expand All @@ -1157,10 +1158,12 @@ describe('messagesStore', () => {
testStoreConfig.getters.getUserId = jest.fn().mockReturnValue(getUserIdMock)

updateConversationLastMessageAction = jest.fn()
updateLastReadMessageAction = jest.fn()
updateLastCommonReadMessageAction = jest.fn()
updateUnreadMessagesMutation = jest.fn()
addGuestNameAction = jest.fn()
testStoreConfig.actions.updateConversationLastMessage = updateConversationLastMessageAction
testStoreConfig.actions.updateLastReadMessage = updateLastReadMessageAction
testStoreConfig.actions.updateLastCommonReadMessage = updateLastCommonReadMessageAction
guestNameStore.addGuestName = addGuestNameAction
testStoreConfig.mutations.updateUnreadMessages = updateUnreadMessagesMutation
Expand Down Expand Up @@ -1273,6 +1276,43 @@ describe('messagesStore', () => {
expect(store.getters.getLastKnownMessageId(TOKEN)).toBe(null)
})

test('looks for new messages updates last read message if posted by same actor', async () => {
const messages = [{
id: 123,
token: TOKEN,
actorId: 'actor-id-1',
actorType: ATTENDEE.ACTOR_TYPE.USERS,

Check failure on line 1284 in src/store/messagesStore.spec.js

View workflow job for this annotation

GitHub Actions / NPM lint

'lookForNewMessages' is not defined
}, {
id: 124,
token: TOKEN,
actorType: ATTENDEE.ACTOR_TYPE.GUESTS,
}]
const response = generateOCSResponse({
payload: messages,
})
lookForNewMessages.mockResolvedValueOnce(response)

// smaller number to make it update
conversationMock.mockReturnValue({ lastReadMessage: 100, lastMessage: { id: 100 } })

await store.dispatch('lookForNewMessages', {
token: TOKEN,
requestId: 'request1',
lastKnownMessageId: 100,
requestOptions: {
dummyOption: true,
},
})

expect(updateConversationLastMessageAction)
.toHaveBeenCalledWith(expect.anything(), { token: TOKEN, lastMessage: messages[1] })
expect(updateLastReadMessageAction).toHaveBeenCalledWith(expect.anything(), {
token: TOKEN,
id: 123,
updateVisually: true,
})
})

test('does not look for new messages if lastKnownMessageId is falsy', async () => {
// Arrange: prepare cancelable request from previous call of the function
const cancelFunctionMock = jest.fn()
Expand Down

0 comments on commit 62f0414

Please sign in to comment.