summaryrefslogtreecommitdiffstats
path: root/src/components/MessagesList/MessagesGroup/Message/MessagePart/Forwarder.vue
blob: f672ece3918c430e418d49057098a1af3113433b (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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!--
  - @copyright Copyright (c) 2021 Marco Ambrosini <marcoambrosini@icloud.com>
  -
  - @author Marco Ambrosini <marcoambrosini@icloud.com>
  -
  - @license GNU AGPL version 3 or any later version
  -
  - 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/>.
-->

<template>
	<div class="forwarder">
		<!-- First step of the flow: selection of the room to which forward the
		message to -->
		<RoomSelector v-if="!showForwardedConfirmation"
			:container="container"
			:show-postable-only="true"
			:dialog-title="dialogTitle"
			:dialog-subtitle="dialogSubtitle"
			@select="setSelectedConversationToken"
			@close="handleClose" />

		<!-- Second step of the flow: confirmation modal that gives the user
		the possibility to direclty route to the conversation to which the
		message has been forwarded -->
		<Modal v-else
			@close="handleClose">
			<div class="forwarder">
				<EmptyContent icon="icon-checkmark" class="forwarded-confirmation__emptycontent">
					<template #desc>
						{{ t('spreed', 'The message has been forwarded to {selectedConversationName}', { selectedConversationName }) }}
					</template>
				</EmptyContent>
				<div class="forwarded-confirmation__navigation">
					<Button type="tertiary" @click="handleClose">
						{{ t('spreed', 'Dismiss') }}
					</Button>
					<Button type="primary" @click="openConversation">
						{{ t('spreed', 'Go to conversation') }}
					</Button>
				</div>
			</div>
		</Modal>
	</div>
</template>

<script>
import RoomSelector from '../../../../../views/RoomSelector.vue'
import EmptyContent from '@nextcloud/vue/dist/Components/EmptyContent'
import Modal from '@nextcloud/vue/dist/Components/Modal'
import { showError } from '@nextcloud/dialogs'
import cloneDeep from 'lodash/cloneDeep'
import Button from '@nextcloud/vue/dist/Components/Button'

export default {
	name: 'Forwarder',

	components: {
		RoomSelector,
		EmptyContent,
		Modal,
		Button,
	},

	props: {
		/**
		 * The message to be forwarded
		 */
		messageObject: {
			type: Object,
			required: true,
		},
	},

	data() {
		return {
			selectedConversationToken: null,
			showForwardedConfirmation: false,
			forwardedMessageID: '',
		}
	},

	computed: {
		container() {
			return this.$store.getters.getMainContainerSelector()
		},

		dialogTitle() {
			return t('spreed', 'Forward message')
		},

		dialogSubtitle() {
			return t('spreed', 'Choose a conversation to forward the selected message.')
		},

		selectedConversationName() {
			return this.$store.getters?.conversation(this.selectedConversationToken).name
		},

		/**
		 * Object containing all the mentions in the message that will be forwarded
		 *
		 * @return {object} mentions.
		 */
		mentions() {
			const mentions = {}
			for (const key in this.messageObject.messageParameters) {
				if (key.startsWith('mention')) {
					mentions[key] = this.messageObject.messageParameters[key]
				}
			}
			return mentions
		},
	},

	methods: {
		async setSelectedConversationToken(token) {
			this.selectedConversationToken = token
			const messageToBeForwarded = cloneDeep(this.messageObject)
			// Overwrite the selected conversation token
			messageToBeForwarded.token = token

			if (messageToBeForwarded.parent) {
				delete messageToBeForwarded.parent
			}

			if (messageToBeForwarded.message === '{object}' && messageToBeForwarded.messageParameters.object) {
				const richObject = messageToBeForwarded.messageParameters.object
				try {
					const response = await this.$store.dispatch('forwardRichObject', {
						token,
						richObject: {
							objectId: richObject.id,
							objectType: richObject.type,
							metaData: JSON.stringify(richObject),
							referenceId: '',
						},
					})
					this.showForwardedConfirmation = true
					this.forwardedMessageID = response.data.ocs.data.id
				} catch (error) {
					console.error('Error while forwarding message', error)
					showError(t('spreed', 'Error while forwarding message'))
				}
				return
			}

			// If there are mentions in the message to be forwarded, replace them in the message
			// text.
			if (this.mentions !== {}) {
				for (const mention in this.mentions) {
					messageToBeForwarded.message = messageToBeForwarded.message.replace(`{${mention}}`, '@' + this.mentions[mention].name)
				}
			}
			try {
				const response = await this.$store.dispatch('forwardMessage', { messageToBeForwarded })
				this.showForwardedConfirmation = true
				this.forwardedMessageID = response.data.ocs.data.id
			} catch (error) {
				console.error('Error while forwarding message', error)
				showError(t('spreed', 'Error while forwarding message'))
			}

		},

		openConversation() {

			this.$router.push({
				name: 'conversation',
				hash: `#message_${this.forwardedMessageID}`,
				params: {
					token: `${this.selectedConversationToken}`,
				},
			})
				.catch(err => console.debug(`Error while pushing the new conversation's route: ${err}`))
			this.showForwardedConfirmation = false
			this.forwardedMessageID = ''
		},

		handleClose() {
			this.$emit('close')
		},
	},
}
</script>

<style lang="scss" scoped>
.forwarded-confirmation {
	&__emptycontent {
		width: 100%;
		text-align: center;
		margin-top: 15vh !important;
	}
	&__navigation {
		display: flex;
		justify-content: space-between;
		padding: 12px;
		button {
			height: 44px;
		}
	}
}

.forwarder {
	padding: 20px;
}

</style>