Skip to content

Commit b7c98ae

Browse files
Merge pull request #8723 from nextcloud/perf/8710/update-conversation-list-from-notification-data
feat(performance): Update conversation data for chat messages and cal…
2 parents aed1975 + dcebb08 commit b7c98ae

File tree

2 files changed

+132
-2
lines changed

2 files changed

+132
-2
lines changed

src/App.vue

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ export default {
440440
}
441441
442442
subscribe('notifications:action:execute', this.interceptNotificationActions)
443+
subscribe('notifications:notification:received', this.interceptNotificationReceived)
443444
},
444445
445446
methods: {
@@ -476,6 +477,31 @@ export default {
476477
event.cancelAction = true
477478
},
478479
480+
/**
481+
* Intercept …
482+
*
483+
* @param {object} event The event object provided by the notifications app
484+
* @param {object} event.notification The notification object
485+
* @param {string} event.notification.app The app ID of the app providing the notification
486+
*/
487+
interceptNotificationReceived(event) {
488+
if (event.notification.app !== 'spreed') {
489+
return
490+
}
491+
492+
if (event.notification.objectType === 'chat') {
493+
this.$store.dispatch('updateConversationLastMessageFromNotification', {
494+
notification: event.notification,
495+
})
496+
}
497+
498+
if (event.notification.objectType === 'call') {
499+
this.$store.dispatch('updateCallStateFromNotification', {
500+
notification: event.notification,
501+
})
502+
}
503+
},
504+
479505
fixmeDelayedSetupOfGuestUsers() {
480506
// FIXME Refresh the data now that the user joined the conversation
481507
// The join request returns this data already, but it's lost in the signaling code

src/store/conversationsStore.js

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ const getters = {
9797
return conversation.objectType !== 'room'
9898
}),
9999
/**
100-
* Get a conversation providing it's token
100+
* Get a conversation providing its token
101101
*
102102
* @param {object} state state object
103103
* @return {Function} The callback function returning the conversation object
@@ -146,13 +146,16 @@ const mutations = {
146146
Vue.set(state.conversations[token], 'lastMessage', lastMessage)
147147
},
148148

149-
updateUnreadMessages(state, { token, unreadMessages, unreadMention }) {
149+
updateUnreadMessages(state, { token, unreadMessages, unreadMention, unreadMentionDirect }) {
150150
if (unreadMessages !== undefined) {
151151
Vue.set(state.conversations[token], 'unreadMessages', unreadMessages)
152152
}
153153
if (unreadMention !== undefined) {
154154
Vue.set(state.conversations[token], 'unreadMention', unreadMention)
155155
}
156+
if (unreadMentionDirect !== undefined) {
157+
Vue.set(state.conversations[token], 'unreadMentionDirect', unreadMentionDirect)
158+
}
156159
},
157160

158161
overwriteHasCallByChat(state, { token, hasCall }) {
@@ -477,6 +480,107 @@ const actions = {
477480
}
478481
},
479482

483+
async updateConversationLastMessageFromNotification({ getters, commit }, { notification }) {
484+
const [token, messageId] = notification.objectId.split('/')
485+
const conversation = { ...getters.conversation(token) }
486+
487+
if (!conversation) {
488+
// Conversation not loaded yet, skipping
489+
return
490+
}
491+
492+
const actor = notification.subjectRichParameters.user || notification.subjectRichParameters.guest || {
493+
type: 'guest',
494+
id: 'unknown',
495+
name: t('spreed', 'Guest'),
496+
}
497+
498+
const lastMessage = {
499+
token,
500+
id: parseInt(messageId, 10),
501+
actorType: actor.type + 's',
502+
actorId: actor.id,
503+
actorDisplayName: actor.name,
504+
message: notification.messageRich,
505+
messageParameters: notification.messageRichParameters,
506+
timestamp: (new Date(notification.datetime)).getTime() / 1000,
507+
508+
// Inaccurate but best effort from here on:
509+
expirationTimestamp: 0,
510+
isReplyable: true,
511+
messageType: 'comment',
512+
reactions: {},
513+
referenceId: '',
514+
systemMessage: '',
515+
}
516+
517+
const unreadCounterUpdate = {
518+
token,
519+
unreadMessages: conversation.unreadMessages,
520+
unreadMention: conversation.unreadMention,
521+
unreadMentionDirect: conversation.unreadMentionDirect,
522+
}
523+
524+
if (conversation.type === CONVERSATION.TYPE.ONE_TO_ONE) {
525+
unreadCounterUpdate.unreadMessages++
526+
unreadCounterUpdate.unreadMention++
527+
unreadCounterUpdate.unreadMentionDirect = true
528+
} else {
529+
unreadCounterUpdate.unreadMessages++
530+
Object.keys(notification.messageRichParameters).forEach(function(p) {
531+
const parameter = notification.messageRichParameters[p]
532+
if (parameter.type === 'user' && parameter.id === notification.user) {
533+
unreadCounterUpdate.unreadMention++
534+
unreadCounterUpdate.unreadMentionDirect = true
535+
} else if (parameter.type === 'call' && parameter.id === token) {
536+
unreadCounterUpdate.unreadMention++
537+
}
538+
})
539+
}
540+
conversation.lastActivity = lastMessage.timestamp
541+
542+
commit('addConversation', conversation)
543+
commit('updateConversationLastMessage', { token, lastMessage })
544+
commit('updateUnreadMessages', unreadCounterUpdate)
545+
},
546+
547+
async updateCallStateFromNotification({ getters, commit }, { notification }) {
548+
const token = notification.objectId
549+
const conversation = { ...getters.conversation(token) }
550+
551+
if (!conversation) {
552+
// Conversation not loaded yet, skipping
553+
return
554+
}
555+
556+
conversation.hasCall = true
557+
conversation.callFlag = PARTICIPANT.CALL_FLAG.WITH_VIDEO
558+
conversation.activeSince = (new Date(notification.datetime)).getTime() / 1000
559+
conversation.lastActivity = conversation.activeSince
560+
conversation.callStartTime = conversation.activeSince
561+
562+
// Inaccurate but best effort from here on:
563+
const lastMessage = {
564+
token,
565+
id: 'temp' + conversation.activeSince,
566+
actorType: 'guests',
567+
actorId: 'unknown',
568+
actorDisplayName: t('spreed', 'Guest'),
569+
message: notification.subjectRich,
570+
messageParameters: notification.subjectRichParameters,
571+
timestamp: conversation.activeSince,
572+
messageType: 'system',
573+
systemMessage: 'call_started',
574+
expirationTimestamp: 0,
575+
isReplyable: false,
576+
reactions: {},
577+
referenceId: '',
578+
}
579+
580+
commit('updateConversationLastMessage', { token, lastMessage })
581+
commit('addConversation', conversation)
582+
},
583+
480584
async updateConversationLastReadMessage({ commit }, { token, lastReadMessage }) {
481585
commit('updateConversationLastReadMessage', { token, lastReadMessage })
482586
},

0 commit comments

Comments
 (0)