1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
/**
* @copyright Copyright (c) 2019 Marco Ambrosini <marcoambrosini@icloud.com>
*
* @author Marco Ambrosini <marcoambrosini@icloud.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import Vue from 'vue'
const state = {
messagesToBeReplied: {},
/**
* Cached last message input by conversation token
*/
currentMessageInput: {},
}
const getters = {
getMessageToBeReplied: (state) => (token) => {
if (state.messagesToBeReplied[token]) {
return state.messagesToBeReplied[token]
}
},
currentMessageInput: (state) => (token) => {
if (state.currentMessageInput[token]) {
return state.currentMessageInput[token]
}
},
}
const mutations = {
/**
* Add a message to be replied to the store. This message is generated when the
* reply button is clicked.
*
* @param {object} state current store state;
* @param {object} messageToBeReplied The message to be replied;
*/
addMessageToBeReplied(state, messageToBeReplied) {
Vue.set(state.messagesToBeReplied, [messageToBeReplied.token], messageToBeReplied)
},
/**
* Removes message to be replied from the store for the
* given conversation.
*
* @param {object} state current store state;
* @param {string} token The conversation token
*/
removeMessageToBeReplied(state, token) {
Vue.delete(state.messagesToBeReplied, token)
},
/**
* Sets the current message input for a given conversation
*
* @param {object} state Current store state;
* @param {object} data the wrapping object;
* @param {string} data.token The conversation token;
* @param {string} data.text Message text to set or null to clear it;
*/
setCurrentMessageInput(state, { token, text = null }) {
if (text !== null) {
Vue.set(state.currentMessageInput, token, text)
} else {
Vue.delete(state.currentMessageInput, token)
}
},
}
const actions = {
/**
* Add a message to be replied to the store. This message is generated when the
* reply button is clicked.
*
* @param {object} context default store context;
* @param {object} messageToBeReplied The message to be replied;
*/
addMessageToBeReplied(context, messageToBeReplied) {
context.commit('addMessageToBeReplied', messageToBeReplied)
},
/**
* Remove a message to be replied to the store. This is used either when the message
* has been replied to or the user finally decides to dismiss the reply operation.
*
* @param {object} context default store context;
* @param {object} token The token of the conversation whose message to be replied is
* being removed;
*/
removeMessageToBeReplied(context, token) {
context.commit('removeMessageToBeReplied', token)
},
/**
* Clears current messages from a deleted conversation
*
* @param {object} context default store context;
* @param {string} token the token of the conversation to be deleted;
*/
deleteMessages(context, token) {
context.commit('removeMessageToBeReplied', token)
context.commit('setCurrentMessageInput', { token, text: null })
},
/**
* Stores the current message input for a given conversation
*
* @param {object} context default store context;
* @param {object} data the wrapping object;
* @param {string} data.token the token of the conversation to be deleted;
* @param {string} data.text string to set or null to clear it;
*/
setCurrentMessageInput(context, { token, text }) {
context.commit('setCurrentMessageInput', { token, text })
},
}
export default { state, mutations, getters, actions }
|