Welcome to mirror list, hosted at ThFree Co, Russian Federation.

reactionsStore.js « store « src - github.com/nextcloud/spreed.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 821e6c60aba73898b2edf9a45a849c9e08b04edc (plain)
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
/**
 * @copyright Copyright (c) 2022 Marco Ambrosini <marcoambrosini@pm.me>
 *
 * @author Marco Ambrosini <marcoambrosini@pm.me>
 *
 * @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'
import {
	getReactionsDetails,
} from '../services/messagesService'

const state = {
	/**
	 * Structure:
	 * reactions.token.messageId
	 */
	reactions: {},
}

const getters = {
	reactions: (state) => (token, messageId) => {
		if (state.reactions?.[token]?.[messageId]) {
			return state.reactions[token][messageId]
		} else {
			return undefined
		}
	},

	// Checks if a user has already reacted to a message with a particular reaction
	userHasReacted: (state) => (actorId, token, messageId, reaction) => {
		if (!state?.reactions?.[token]?.[messageId]?.[reaction]) {
			return false
		}
		return state?.reactions?.[token]?.[messageId]?.[reaction].filter(item => {
			return item.actorId === actorId
		}).length !== 0
	},
}

const mutations = {
	addReactions(state, { token, messageId, reactions }) {
		if (!state.reactions[token]) {
			Vue.set(state.reactions, token, {})

		}
		Vue.set(state.reactions[token], messageId, reactions)
	},
}

const actions = {
	/**
	 * Updates reactions for a given message.
	 *
	 * @param {*} context The context object
	 * @param {*} param1 conversation token, message id
	 */
	async updateReactions(context, { token, messageId, reactionsDetails }) {
		context.commit('addReactions', {
			token,
			messageId,
			reactions: reactionsDetails,
		})
	},

	/**
	 * Gets the full reactions array for a given message.
	 *
	 * @param {*} context the context object
	 * @param {*} param1 conversation token, message id
	 */
	async getReactions(context, { token, messageId }) {
		console.debug('getting reactions details')
		try {
			const response = await getReactionsDetails(token, messageId)
			context.commit('addReactions', {
				token,
				messageId,
				reactions: response.data.ocs.data,
			})

			return response
		} catch (error) {
			console.debug(error)
		}
	},
}

export default { state, mutations, getters, actions }