Skip to content

Commit

Permalink
add actions to storing and recovering fetched conversations from Brow…
Browse files Browse the repository at this point in the history
…serStorage

Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Aug 14, 2023
1 parent f522c26 commit 3263f12
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ export default {
EventBus.$on('should-refresh-conversations', this.debounceRefreshCurrentConversation)
}
// Restore last fetched conversations from browser storage,
// before updated ones come from server
this.$store.dispatch('getConversationsFromStorage')
if (this.$route.name === 'conversation') {
// Update current token in the token store
this.$store.dispatch('updateToken', this.$route.params.token)
Expand Down
42 changes: 42 additions & 0 deletions src/store/conversationsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
PARTICIPANT,
WEBINAR,
} from '../constants.js'
import BrowserStorage from '../services/BrowserStorage.js'
import {
makePublic,
makePrivate,
Expand Down Expand Up @@ -339,6 +340,47 @@ const actions = {
context.dispatch('updateConversationIfHasChanged', newConversation)
}
}

context.dispatch('putConversationsToStorage')
},

/**
* Restores conversations from BrowserStorage and add them to the store state
*
* @param {object} context default store context
*/
getConversationsFromStorage(context) {
const cachedConversations = BrowserStorage.getItem('cachedConversations_')
if (!cachedConversations?.length) {
return
}

const conversations = JSON.parse(cachedConversations)
console.debug(`${conversations.length} conversations have been restored from BrowserStorage`)

const currentConversations = context.state.conversations
for (const conversation of Object.values(conversations)) {
// Check if the conversation hasn't been added already
if (currentConversations[conversation.token] === undefined) {
context.dispatch('addConversation', conversation)
}
}
},

/**
* Save conversations to BrowserStorage from the store state
*
* @param {object} context default store context
*/
putConversationsToStorage(context) {
const conversations = context.getters.conversationsList
if (!conversations.length) {
return
}

const serializedConversations = JSON.stringify(conversations)
BrowserStorage.setItem('cachedConversations_', serializedConversations)
console.debug(`${conversations.length} chats were saved to BrowserStorage. Estimated object size: ${serializedConversations.length / 1000} kB`)
},

/**
Expand Down
62 changes: 62 additions & 0 deletions src/store/conversationsStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
PARTICIPANT,
ATTENDEE,
} from '../constants.js'
import BrowserStorage from '../services/BrowserStorage.js'
import {
makePublic,
makePrivate,
Expand Down Expand Up @@ -56,6 +57,11 @@ jest.mock('../services/conversationsService', () => ({

jest.mock('@nextcloud/event-bus')

jest.mock('../services/BrowserStorage.js', () => ({
getItem: jest.fn(),
setItem: jest.fn(),
}))

describe('conversationsStore', () => {
const testToken = 'XXTOKENXX'
const previousLastMessage = {
Expand Down Expand Up @@ -196,6 +202,31 @@ describe('conversationsStore', () => {
expect(deleteConversation).not.toHaveBeenCalled()
})

test('restores conversations cached in BrowserStorage', () => {
const testConversations = [
{
token: 'one_token',
attendeeId: 'attendee-id-1',
lastActivity: Date.parse('2023-02-01T00:00:00.000Z') / 1000,
},
{
token: 'another_token',
attendeeId: 'attendee-id-2',
lastActivity: Date.parse('2023-01-01T00:00:00.000Z') / 1000,
},
]

BrowserStorage.getItem.mockReturnValueOnce(
'[{"token":"one_token","attendeeId":"attendee-id-1","lastActivity":1675209600},{"token":"another_token","attendeeId":"attendee-id-2","lastActivity":1672531200}]'
)

store.dispatch('getConversationsFromStorage')

expect(BrowserStorage.getItem).toHaveBeenCalledWith('cachedConversations_')
expect(store.getters.conversationsList).toHaveLength(2)
expect(store.getters.conversationsList).toEqual(testConversations)
})

test('deletes conversation from server', async () => {
store.dispatch('addConversation', testConversation)

Expand Down Expand Up @@ -258,6 +289,37 @@ describe('conversationsStore', () => {
expect(store.getters.conversationsList).toStrictEqual(testConversations)
})

test('sets fetched conversations to BrowserStorage', async () => {
const testConversations = [
{
token: 'one_token',
attendeeId: 'attendee-id-1',
lastActivity: Date.parse('2023-02-01T00:00:00.000Z') / 1000,
},
{
token: 'another_token',
attendeeId: 'attendee-id-2',
lastActivity: Date.parse('2023-01-01T00:00:00.000Z') / 1000,
},
]

const response = {
data: {
ocs: {
data: testConversations,
},
},
}

fetchConversations.mockResolvedValue(response)

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

expect(BrowserStorage.setItem).toHaveBeenCalledWith('cachedConversations_',
'[{"token":"one_token","attendeeId":"attendee-id-1","lastActivity":1675209600},{"token":"another_token","attendeeId":"attendee-id-2","lastActivity":1672531200}]'
)
})

test('fetches all conversations and add new received conversations', async () => {
const oldConversation = {
token: 'tokenOne',
Expand Down

0 comments on commit 3263f12

Please sign in to comment.