Skip to content

Commit

Permalink
Merge pull request #11165 from nextcloud/feat/11053/migrate-quote-rep…
Browse files Browse the repository at this point in the history
…ly-store

feat(Pinia): migrate quoteReplyStore (Vuex) to chatExtrasStore (Pinia)
  • Loading branch information
Antreesy authored Dec 13, 2023
2 parents 1d223a7 + 869e44c commit 89df16b
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 263 deletions.
12 changes: 10 additions & 2 deletions src/components/MessagesList/MessagesGroup/Message/Message.vue
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ import { useIsInCall } from '../../../../composables/useIsInCall.js'
import { ATTENDEE, CONVERSATION, PARTICIPANT } from '../../../../constants.js'
import participant from '../../../../mixins/participant.js'
import { EventBus } from '../../../../services/EventBus.js'
import { useChatExtrasStore } from '../../../../stores/chatExtras.js'
import { useGuestNameStore } from '../../../../stores/guestName.js'
import { getItemTypeFromMessage } from '../../../../utils/getItemTypeFromMessage.js'

Expand Down Expand Up @@ -467,8 +468,15 @@ export default {

setup() {
const isInCall = useIsInCall()
const chatExtrasStore = useChatExtrasStore()
const guestNameStore = useGuestNameStore()
return { isInCall, isTranslationAvailable, guestNameStore }

return {
isInCall,
isTranslationAvailable,
chatExtrasStore,
guestNameStore
}
},

expose: ['highlightMessage'],
Expand Down Expand Up @@ -881,7 +889,7 @@ export default {
},

handleReply() {
this.$store.dispatch('addMessageToBeReplied', {
this.chatExtrasStore.setParentIdToReply({
token: this.token,
id: this.id,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,6 @@ describe('MessageButtonsBar.vue', () => {

describe('reply action', () => {
test('replies to message', async () => {
const replyAction = jest.fn()
testStoreConfig.modules.quoteReplyStore.actions.addMessageToBeReplied = replyAction
store = new Store(testStoreConfig)

const wrapper = shallowMount(MessageButtonsBar, {
Expand Down
26 changes: 12 additions & 14 deletions src/components/NewMessage/NewMessage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@
</template>
</NcButton>
</div>
<div v-if="messageToBeReplied" class="new-message-form__quote">
<Quote is-new-message-quote v-bind="messageToBeReplied" />
<div v-if="parentMessage" class="new-message-form__quote">
<Quote is-new-message-quote v-bind="parentMessage" />
</div>
<NcRichContenteditable ref="richContenteditable"
v-shortkey.once="$options.disableKeyboardShortcuts ? null : ['c']"
Expand Down Expand Up @@ -331,8 +331,8 @@ export default {
}
},

messageToBeReplied() {
const parentId = this.$store.getters.getMessageToBeReplied(this.token)
parentMessage() {
const parentId = this.chatExtrasStore.getParentIdToReply(this.token)
return parentId && this.$store.getters.message(this.token, parentId)
},

Expand Down Expand Up @@ -403,14 +403,14 @@ export default {
},

text(newValue) {
this.$store.dispatch('setCurrentMessageInput', { token: this.token, text: newValue })
this.chatExtrasStore.setChatInput({ token: this.token, text: newValue })
},

token: {
immediate: true,
handler(token) {
if (token) {
this.text = this.$store.getters.currentMessageInput(token)
this.text = this.chatExtrasStore.getChatInput(token)
} else {
this.text = ''
}
Expand All @@ -426,7 +426,7 @@ export default {
EventBus.$on('upload-start', this.handleUploadSideEffects)
EventBus.$on('upload-discard', this.handleUploadSideEffects)
EventBus.$on('retry-message', this.handleRetryMessage)
this.text = this.$store.getters.currentMessageInput(this.token)
this.text = this.chatExtrasStore.getChatInput(this.token)

if (!this.$store.getters.areFileTemplatesInitialised) {
this.$store.dispatch('getFileTemplates')
Expand Down Expand Up @@ -484,7 +484,7 @@ export default {
}
this.$nextTick(() => {
// reset or fill main input in chat view from the store
this.text = this.$store.getters.currentMessageInput(this.token)
this.text = this.chatExtrasStore.getChatInput(this.token)
// refocus input as the user might want to type further
this.focusInput()
})
Expand Down Expand Up @@ -516,7 +516,7 @@ export default {

if (this.upload) {
// Clear input content from store
this.$store.dispatch('setCurrentMessageInput', { token: this.token, text: '' })
this.chatExtrasStore.setChatInput({ token: this.token, text: '' })

if (this.$store.getters.getInitialisedUploads(this.$store.getters.currentUploadId).length) {
// If dialog contains files to upload, delegate sending
Expand All @@ -539,7 +539,7 @@ export default {
// Scrolls the message list to the last added message
EventBus.$emit('smooth-scroll-chat-to-bottom')
// Also remove the message to be replied for this conversation
await this.$store.dispatch('removeMessageToBeReplied', this.token)
this.chatExtrasStore.removeParentIdToReply(this.token)

this.broadcast
? await this.broadcastMessage(temporaryMessage, options)
Expand Down Expand Up @@ -592,7 +592,7 @@ export default {

// Restore the parent/quote message
if (temporaryMessage.parent) {
this.$store.dispatch('addMessageToBeReplied', {
this.chatExtrasStore.setParentIdToReply({
token: this.token,
id: temporaryMessage.parent.id,
})
Expand Down Expand Up @@ -837,9 +837,7 @@ export default {
})
} else {
// Remove stored absence status
this.chatExtrasStore.resetUserAbsence({
token: this.token,
})
this.chatExtrasStore.removeUserAbsence(this.token)
}
}
},
Expand Down
14 changes: 8 additions & 6 deletions src/components/Quote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import FilePreview from './MessagesList/MessagesGroup/Message/MessagePart/FilePr

import { AVATAR } from '../constants.js'
import { EventBus } from '../services/EventBus.js'
import { useChatExtrasStore } from '../stores/chatExtras.js'

export default {
name: 'Quote',
Expand Down Expand Up @@ -143,7 +144,12 @@ export default {
},

setup() {
return { AVATAR }
const chatExtrasStore = useChatExtrasStore()

return {
AVATAR,
chatExtrasStore,
}
},

computed: {
Expand Down Expand Up @@ -243,12 +249,8 @@ export default {
},
},
methods: {
/**
* Stops the quote-reply operation by removing the MessageToBeReplied from
* the quoteReplyStore.
*/
handleAbortReply() {
this.$store.dispatch('removeMessageToBeReplied', this.token)
this.chatExtrasStore.removeParentIdToReply(this.token)
EventBus.$emit('focus-chat-input')
},

Expand Down
7 changes: 6 additions & 1 deletion src/store/conversationsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import {
stopCallRecording,
} from '../services/recordingService.js'
import { talkBroadcastChannel } from '../services/talkBroadcastChannel.js'
import { useChatExtrasStore } from '../stores/chatExtras.js'

const DUMMY_CONVERSATION = {
token: '',
Expand Down Expand Up @@ -317,10 +318,12 @@ const actions = {
* Delete a conversation from the store.
*
* @param {object} context default store context;
* @param {object} token the token of the conversation to be deleted;
* @param {string} token the token of the conversation to be deleted;
*/
deleteConversation(context, token) {
// FIXME: rename to deleteConversationsFromStore or a better name
const chatExtrasStore = useChatExtrasStore()
chatExtrasStore.purgeChatExtras(token)
context.dispatch('deleteMessages', token)
context.commit('deleteConversation', token)
},
Expand Down Expand Up @@ -431,6 +434,8 @@ const actions = {
async clearConversationHistory(context, { token }) {
try {
const response = await clearConversationHistory(token)
const chatExtrasStore = useChatExtrasStore()
chatExtrasStore.removeParentIdToReply(token)
context.dispatch('deleteMessages', token)
return response
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions src/store/conversationsStore.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createLocalVue } from '@vue/test-utils'
import flushPromises from 'flush-promises'
import { cloneDeep } from 'lodash'
import { createPinia, setActivePinia } from 'pinia'
import Vuex from 'vuex'

import { emit } from '@nextcloud/event-bus'
Expand Down Expand Up @@ -82,6 +83,7 @@ describe('conversationsStore', () => {
beforeEach(() => {
localVue = createLocalVue()
localVue.use(Vuex)
setActivePinia(createPinia())

testConversation = {
token: testToken,
Expand Down
4 changes: 3 additions & 1 deletion src/store/messagesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
addReactionToMessage,
removeReactionFromMessage,
} from '../services/messagesService.js'
import { useChatExtrasStore } from '../stores/chatExtras.js'
import { useGuestNameStore } from '../stores/guestName.js'
import { useSharedItemsStore } from '../stores/sharedItems.js'
import CancelableRequest from '../utils/cancelableRequest.js'
Expand Down Expand Up @@ -617,7 +618,8 @@ const actions = {
* @return {object} temporary message
*/
createTemporaryMessage(context, { text, token, uploadId, index, file, localUrl, isVoiceMessage }) {
const parentId = context.getters.getMessageToBeReplied(token)
const chatExtrasStore = useChatExtrasStore()
const parentId = chatExtrasStore.getParentIdToReply(token)
const parent = parentId && context.getters.message(token, parentId)
const date = new Date()
let tempId = 'temp-' + date.getTime()
Expand Down
11 changes: 4 additions & 7 deletions src/store/messagesStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
postNewMessage,
postRichObjectToConversation,
} from '../services/messagesService.js'
import { useChatExtrasStore } from '../stores/chatExtras.js'
import { useGuestNameStore } from '../stores/guestName.js'
import { generateOCSErrorResponse, generateOCSResponse } from '../test-helpers.js'
import CancelableRequest from '../utils/cancelableRequest.js'
Expand Down Expand Up @@ -318,23 +319,22 @@ describe('messagesStore', () => {

describe('temporary messages', () => {
let mockDate
let getMessageToBeRepliedMock
let getActorIdMock
let getActorTypeMock
let getDisplayNameMock
let chatExtraStore

beforeEach(() => {
mockDate = new Date('2020-01-01 20:00:00')
jest.spyOn(global, 'Date')
.mockImplementation(() => mockDate)

testStoreConfig = cloneDeep(messagesStore)
chatExtraStore = useChatExtrasStore()

getMessageToBeRepliedMock = jest.fn().mockReturnValue(() => undefined)
getActorIdMock = jest.fn().mockReturnValue(() => 'actor-id-1')
getActorTypeMock = jest.fn().mockReturnValue(() => ATTENDEE.ACTOR_TYPE.USERS)
getDisplayNameMock = jest.fn().mockReturnValue(() => 'actor-display-name-1')
testStoreConfig.getters.getMessageToBeReplied = getMessageToBeRepliedMock
testStoreConfig.getters.getActorId = getActorIdMock
testStoreConfig.getters.getActorType = getActorTypeMock
testStoreConfig.getters.getDisplayName = getDisplayNameMock
Expand All @@ -353,7 +353,6 @@ describe('messagesStore', () => {
localUrl: null,
})

expect(getMessageToBeRepliedMock).toHaveBeenCalled()
expect(getActorIdMock).toHaveBeenCalled()
expect(getActorTypeMock).toHaveBeenCalled()
expect(getDisplayNameMock).toHaveBeenCalled()
Expand Down Expand Up @@ -384,9 +383,7 @@ describe('messagesStore', () => {
}

store.dispatch('processMessage', parent)

getMessageToBeRepliedMock.mockReset()
getMessageToBeRepliedMock.mockReturnValue(() => (123))
chatExtraStore.setParentIdToReply({ token: TOKEN, id: 123 })

const temporaryMessage = await store.dispatch('createTemporaryMessage', {
text: 'blah',
Expand Down
Loading

0 comments on commit 89df16b

Please sign in to comment.