Skip to content

Commit

Permalink
Utilize the modifiedSince parameter of the conversations API
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Feb 10, 2023
1 parent aeb4184 commit f112fb9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/components/LeftSidebar/LeftSidebar.spec.js
Expand Up @@ -76,7 +76,7 @@ describe('LeftSidebar.vue', () => {

// note: need a copy because the Vue modifies it when sorting
conversationsListMock = jest.fn()
fetchConversationsAction = jest.fn()
fetchConversationsAction = jest.fn().mockReturnValue({ headers: {} })
addConversationAction = jest.fn()
createOneToOneConversationAction = jest.fn()
const getUserIdMock = jest.fn().mockReturnValue('current-user')
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('LeftSidebar.vue', () => {

const wrapper = mountComponent()

expect(fetchConversationsAction).toHaveBeenCalledWith(expect.anything(), undefined)
expect(fetchConversationsAction).toHaveBeenCalledWith(expect.anything(), expect.anything())
expect(conversationsListMock).toHaveBeenCalled()

const appNavEl = wrapper.findComponent({ name: 'NcAppNavigation' })
Expand Down Expand Up @@ -194,7 +194,7 @@ describe('LeftSidebar.vue', () => {

expect(fetchConversationsAction).not.toHaveBeenCalled()

EventBus.$emit('should-refresh-conversations')
EventBus.$emit('should-refresh-conversations', {})

// note: debounce was short-circuited so no delay needed
expect(fetchConversationsAction).toHaveBeenCalled()
Expand Down Expand Up @@ -313,7 +313,7 @@ describe('LeftSidebar.vue', () => {

const wrapper = mountComponent()

expect(fetchConversationsAction).toHaveBeenCalledWith(expect.anything(), undefined)
expect(fetchConversationsAction).toHaveBeenCalledWith(expect.anything(), expect.anything())
expect(conversationsListMock).toHaveBeenCalled()

const appNavEl = wrapper.findComponent({ name: 'NcAppNavigation' })
Expand Down
29 changes: 26 additions & 3 deletions src/components/LeftSidebar/LeftSidebar.vue
Expand Up @@ -178,6 +178,8 @@ export default {
unreadNum: 0,
firstUnreadPos: 0,
preventFindingUnread: false,
roomListModifiedBefore: 0,
forceFullRoomListRefreshAfterXLoops: 0,
}
},
Expand Down Expand Up @@ -439,8 +441,10 @@ export default {
return conversation2.lastActivity - conversation1.lastActivity
},
async handleShouldRefreshConversations({ token, properties }) {
if (token && properties) {
async handleShouldRefreshConversations({ token = undefined, properties = undefined, all = undefined }) {
if (all === true) {
this.roomListModifiedBefore = 0
} else if (token && properties) {
await this.$store.dispatch('setConversationProperties', { token, properties })
}
Expand All @@ -455,13 +459,32 @@ export default {
async fetchConversations() {
this.isFetchingConversations = true
if (this.forceFullRoomListRefreshAfterXLoops === 0) {
this.roomListModifiedBefore = 0
this.forceFullRoomListRefreshAfterXLoops = 10
} else {
this.forceFullRoomListRefreshAfterXLoops--
}
/**
* Fetches the conversations from the server and then adds them one by one
* to the store.
*/
try {
await this.$store.dispatch('fetchConversations')
const response = await this.$store.dispatch('fetchConversations', {
modifiedSince: this.roomListModifiedBefore,
})
// We can only support this with the HPB as otherwise rooms,
// you are not currently active in, will not be removed anymore,
// as there is no signaling message about it when the internal
// signaling is used.
if (loadState('spreed', 'signaling_mode') !== 'internal') {
if (response?.headers && response.headers['x-nextcloud-talk-modified-before']) {
this.roomListModifiedBefore = response.headers['x-nextcloud-talk-modified-before']
}
}
this.initialisedConversations = true
/**
* Emits a global event that is used in App.vue to update the page title once the
Expand Down
18 changes: 15 additions & 3 deletions src/store/conversationsStore.js
Expand Up @@ -604,13 +604,25 @@ const actions = {
}
},

async fetchConversations({ dispatch }) {
async fetchConversations({ dispatch }, { modifiedSince }) {
try {
dispatch('clearMaintenanceMode')
modifiedSince = modifiedSince || 0

let options = {}
if (modifiedSince !== 0) {
options = {
params: {
modifiedSince,
},
}
}

const response = await fetchConversations()
const response = await fetchConversations(options)
dispatch('updateTalkVersionHash', response)
dispatch('purgeConversationsStore')
if (modifiedSince === 0) {
dispatch('purgeConversationsStore')
}
response.data.ocs.data.forEach(conversation => {
dispatch('addConversation', conversation)
})
Expand Down
6 changes: 3 additions & 3 deletions src/store/conversationsStore.spec.js
Expand Up @@ -259,9 +259,9 @@ describe('conversationsStore', () => {

fetchConversations.mockResolvedValue(response)

await store.dispatch('fetchConversations')
await store.dispatch('fetchConversations', {})

expect(fetchConversations).toHaveBeenCalledWith()
expect(fetchConversations).toHaveBeenCalledWith({})
expect(store.getters.conversationsList).toStrictEqual(testConversations)

expect(clearMaintenanceModeAction).toHaveBeenCalled()
Expand All @@ -281,7 +281,7 @@ describe('conversationsStore', () => {
const response = { status: 503 }
fetchConversations.mockRejectedValue({ response })

await expect(store.dispatch('fetchConversations')).rejects.toMatchObject({ response })
await expect(store.dispatch('fetchConversations', {})).rejects.toMatchObject({ response })

expect(checkMaintenanceModeAction).toHaveBeenCalledWith(expect.anything(), response)
})
Expand Down
4 changes: 4 additions & 0 deletions src/utils/signaling.js
Expand Up @@ -1302,6 +1302,10 @@ Signaling.Standalone.prototype.processRoomMessageEvent = function(data) {

Signaling.Standalone.prototype.processRoomListEvent = function(data) {
switch (data.event.type) {
case 'delete':
console.debug('Room list event', data)
EventBus.$emit('should-refresh-conversations', { all: true })
break
case 'update':
if (data.event.update.properties['participant-list']) {
console.debug('Room list event for participant list', data)
Expand Down

0 comments on commit f112fb9

Please sign in to comment.