Skip to content

Commit

Permalink
fix(frontend): Make frontend compatible with absence of lastMessage
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Nov 29, 2024
1 parent 54f89df commit 48df4e5
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('Conversation.vue', () => {
})

test('displays nothing when there is no last chat message', () => {
item.lastMessage = {}
delete item.lastMessage
testConversationLabel(item, 'No messages')
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ export default {
type: 0,
displayName: '',
isFavorite: false,
lastMessage: {},
notificationLevel: PARTICIPANT.NOTIFY.DEFAULT,
notificationCalls: PARTICIPANT.NOTIFY_CALLS.ON,
canDeleteConversation: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export default {
displayName: '',
isFavorite: false,
notificationLevel: 0,
lastMessage: {},
}
},
},
Expand Down
5 changes: 0 additions & 5 deletions src/components/LeftSidebar/LeftSidebar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,36 +225,31 @@ describe('LeftSidebar.vue', () => {
isFavorite: false,
name: 'one',
displayName: 'the searched one by display name',
lastMessage: {},
}, {
id: 200,
token: 't200',
lastActivity: 80,
isFavorite: false,
name: 'searched by name',
displayName: 'another one',
lastMessage: {},
}, {
id: 300,
token: 't300',
lastActivity: 120,
isFavorite: true,
name: 'excluded',
displayName: 'excluded from results',
lastMessage: {},
}]

listedResults = [{
id: 1000,
name: 'listed one searched',
displayName: 'listed one searched',
lastMessage: {},
token: 'listed-token-1',
}, {
id: 1001,
name: 'listed two searched',
displayName: 'listed two searched',
lastMessage: {},
token: 'listed-token-2',
}]
usersResults = [{
Expand Down
2 changes: 1 addition & 1 deletion src/composables/useConversationInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function useConversationInfo({
})

const hasLastMessage = computed(() => {
return !!Object.keys(Object(item.value?.lastMessage)).length
return !!item.value?.lastMessage && !!Object.keys(Object(item.value?.lastMessage)).length
})

/**
Expand Down
13 changes: 10 additions & 3 deletions src/store/conversationsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ const actions = {
updateConversationIfHasChanged(context, conversation) {
const oldConversation = context.state.conversations[conversation.token]

// Update conversation if the number of attributes differ
// (e.g. lastMessage was added or removed, because we don't keep lastMessage as an empty object)
if (Object.keys(oldConversation).length !== Object.keys(conversation).length) {
context.commit('updateConversation', conversation)
return true
}

// Update 1-1 conversation, if its status was changed
if (conversation.type === CONVERSATION.TYPE.ONE_TO_ONE
&& (oldConversation.status !== conversation.status
Expand All @@ -336,7 +343,7 @@ const actions = {
return true
}

// Check if any property were changed (no properties except status-related supposed to be added or deleted)
// Check if any property were changed (no properties except status-related and lastMessage supposed to be added or deleted)
for (const key of Object.keys(conversation)) {
// "lastMessage" is the only property with non-primitive (object) value and cannot be compared by ===
// If "lastMessage" was actually changed, it is already checked by "lastActivity"
Expand Down Expand Up @@ -760,8 +767,8 @@ const actions = {
}

const conversation = Object.assign({}, getters.conversations[token])
if (conversation.lastMessage.id === parseInt(messageId, 10)
|| conversation.lastMessage.timestamp >= Date.parse(notification.datetime) / 1000) {
if (conversation.lastMessage?.id === parseInt(messageId, 10)
|| conversation.lastMessage?.timestamp >= Date.parse(notification.datetime) / 1000) {
// Already updated from other source, skipping
return
}
Expand Down
10 changes: 5 additions & 5 deletions src/store/messagesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const getters = {
return false
}

return getters.getLastKnownMessageId(token) < conversation.lastMessage.id
return getters.getLastKnownMessageId(token) < conversation.lastMessage?.id
},

isMessagesListPopulated: (state) => (token) => {
Expand Down Expand Up @@ -552,7 +552,7 @@ const actions = {

if (message.systemMessage === 'message_edited' || message.systemMessage === 'message_deleted') {
// update conversation lastMessage, if it was edited or deleted
if (message.parent.id === context.getters.conversation(token).lastMessage.id) {
if (message.parent.id === context.getters.conversation(token).lastMessage?.id) {
context.dispatch('updateConversationLastMessage', { token, lastMessage: message.parent })
}
// Check existing messages for having a deleted / edited message as parent, and update them
Expand Down Expand Up @@ -1118,7 +1118,7 @@ const actions = {

// Overwrite the conversation.hasCall property so people can join
// after seeing the message in the chat.
if (conversation && conversation.lastMessage && message.id > conversation.lastMessage.id) {
if (conversation?.lastMessage && message.id > conversation.lastMessage.id) {
if (['call_started', 'call_ended', 'call_ended_everyone', 'call_missed'].includes(message.systemMessage)) {
context.dispatch('overwriteHasCallByChat', {
token,
Expand Down Expand Up @@ -1150,7 +1150,7 @@ const actions = {
id: parseInt(response.headers['x-chat-last-given'], 10),
})

if (conversation && conversation.lastMessage && lastMessage.id > conversation.lastMessage.id) {
if (conversation?.lastMessage && lastMessage.id > conversation.lastMessage.id) {
context.dispatch('updateConversationLastMessage', {
token,
lastMessage,
Expand Down Expand Up @@ -1237,7 +1237,7 @@ const actions = {

// update lastMessage and lastReadMessage
// do it conditionally because there could have been more messages appearing concurrently
if (conversation && conversation.lastMessage && message.id > conversation.lastMessage.id) {
if (conversation?.lastMessage && message.id > conversation.lastMessage.id) {
context.dispatch('updateConversationLastMessage', {
token,
lastMessage: message,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/getMessageIcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ const iconSvgTemplate = (path: string) => {
}

export const getMessageIcon = (lastMessage: Conversation['lastMessage']): string => {
if (Array.isArray(lastMessage)) {
if (!lastMessage || Array.isArray(lastMessage)) {
return ''
}
const file = lastMessage?.messageParameters?.file
const file = lastMessage.messageParameters?.file
if (file) {
if (file.mimetype?.startsWith('video')) {
return iconSvgTemplate(mdiMovie) // Media - Videos
Expand Down

0 comments on commit 48df4e5

Please sign in to comment.