summaryrefslogtreecommitdiffstats
path: root/src/components/LeftSidebar/NewGroupConversation/NewGroupConversation.vue
blob: 33460e4fbb5e730306d915412315b066bba7aec4 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<!--
  - @copyright Copyright (c) 2019 Marco Ambrosini <marcoambrosini@icloud.com>
  - @copyright Copyright (c) 2022 Informatyka Boguslawski sp. z o.o. sp.k., http://www.ib.pl/
  -
  - @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="wrapper">
		<NcButton slot="trigger"
			v-tooltip.bottom="t('spreed', 'Create a new group conversation')"
			type="tertiary"
			class="toggle"
			:aria-label="t('spreed', 'Create a new group conversation')"
			@click="showModal">
			<template #icon>
				<Plus :size="20" />
			</template>
		</NcButton>
		<!-- New group form -->
		<NcModal v-if="modal"
			:container="container"
			size="normal"
			@close="closeModal">
			<!-- Wrapper for content & navigation -->
			<div class="new-group-conversation talk-modal">
				<h2>{{ t('spreed', 'Create a new group conversation') }}</h2>
				<!-- Content -->
				<div class="new-group-conversation__content">
					<!-- First page -->
					<template v-if="page === 0">
						<SetConversationName v-model="conversationNameInput"
							@click-enter="handleEnter" />
						<NcCheckboxRadioSwitch :checked.sync="isPublic" v-if="publicRoomsEnabled">
							{{ t('spreed', 'Allow guests to join via link') }}
						</NcCheckboxRadioSwitch>
						<!-- Password protection -->
						<template v-if="isPublic">
							<NcCheckboxRadioSwitch :checked.sync="passwordProtect"
								@checked="handleCheckboxInput">
								{{ t('spreed', 'Password protect') }}
							</NcCheckboxRadioSwitch>
							<PasswordProtect v-if="passwordProtect"
								v-model="password" />
						</template>
						<ListableSettings v-model="listable" />
					</template>
					<!-- Second page -->
					<template v-if="page === 1">
						<SetContacts :conversation-name="conversationName" />
					</template>
					<!-- Third page -->
					<template v-if="page === 2">
						<Confirmation :conversation-name="conversationName"
							:error="error"
							:is-loading="isLoading"
							:success="success"
							:is-public="isPublic"
							:link-to-conversation="linkToConversation" />
					</template>
				</div>
				<!-- Navigation: different buttons with different actions and
				placement are rendered depending on the current page -->
				<div class="navigation">
					<!-- First page -->
					<NcButton v-if="page===0 && isPublic"
						:disabled="disabled"
						type="tertiary"
						@click="handleCreateConversation">
						{{ t('spreed', 'Create conversation') }}
					</NcButton>
					<NcButton v-if="page===0"
						type="primary"
						:disabled="disabled"
						class="navigation__button-right"
						@click="handleSetConversationName">
						{{ t('spreed', 'Add participants') }}
					</NcButton>
					<!-- Second page -->
					<NcButton v-if="page===1"
						type="tertiary"
						@click="handleClickBack">
						{{ t('spreed', 'Back') }}
					</NcButton>
					<NcButton v-if="page===1"
						type="primary"
						class="navigation__button-right"
						@click="handleCreateConversation">
						{{ t('spreed', 'Create conversation') }}
					</NcButton>
					<!-- Third page -->
					<NcButton v-if="page===2 && (error || isPublic)"
						type="primary"
						class="navigation__button-right"
						@click="closeModal">
						{{ t('spreed', 'Close') }}
					</NcButton>
				</div>
			</div>
		</NcModal>
	</div>
</template>

<script>

import { CONVERSATION } from '../../../constants.js'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import NcModal from '@nextcloud/vue/dist/Components/NcModal.js'
import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
import Plus from 'vue-material-design-icons/Plus.vue'
import SetContacts from './SetContacts/SetContacts.vue'
import SetConversationName from './SetConversationName/SetConversationName.vue'
import Confirmation from './Confirmation/Confirmation.vue'
import { addParticipant } from '../../../services/participantsService.js'
import {
	createPublicConversation,
	createPrivateConversation,
	setConversationPassword,
} from '../../../services/conversationsService.js'
import { generateUrl } from '@nextcloud/router'
import PasswordProtect from './PasswordProtect/PasswordProtect.vue'
import ListableSettings from '../../ConversationSettings/ListableSettings.vue'
import isInCall from '../../../mixins/isInCall.js'
import participant from '../../../mixins/participant.js'
import Tooltip from '@nextcloud/vue/dist/Directives/Tooltip.js'
import { EventBus } from '../../../services/EventBus.js'
import { loadState } from '@nextcloud/initial-state'

export default {

	name: 'NewGroupConversation',

	directives: {
		tooltip: Tooltip,
	},

	components: {
		NcModal,
		SetContacts,
		SetConversationName,
		NcButton,
		NcCheckboxRadioSwitch,
		Confirmation,
		PasswordProtect,
		ListableSettings,
		Plus,
	},

	mixins: [
		isInCall,
		participant,
	],

	data() {
		return {
			modal: false,
			page: 0,
			conversationNameInput: '',
			isPublic: false,
			isLoading: true,
			token: '',
			success: false,
			error: false,
			password: '',
			passwordProtect: false,
			listable: CONVERSATION.LISTABLE.NONE,
			publicRoomsEnabled: loadState('spreed', 'public_rooms_allowed'),
		}
	},

	computed: {
		container() {
			return this.$store.getters.getMainContainerSelector()
		},
		// Trims whitespaces from the input string
		conversationName() {
			return this.conversationNameInput.trim()
		},
		// Generates the link to the current conversation
		linkToConversation() {
			if (this.token !== '') {
				return window.location.protocol + '//' + window.location.host + generateUrl('/call/' + this.token)
			} else return ''
		},
		// Controls the disabled/enabled state of the first page's button.
		disabled() {
			return this.conversationName === '' || (this.passwordProtect && this.password === '')
		},
		selectedParticipants() {
			return this.$store.getters.selectedParticipants
		},
	},

	mounted() {
		EventBus.$on('new-group-conversation-dialog', this.showModalForItem)
	},

	destroyed() {
		EventBus.$off('new-group-conversation-dialog', this.showModalForItem)
	},

	methods: {
		showModal() {
			this.modal = true
		},

		showModalForItem(item) {
			if (item) {
				// Preload the conversation name from group selection
				this.conversationNameInput = item.label
				this.$store.dispatch('updateSelectedParticipants', item)
			}

			this.showModal()
		},
		/**
		 * Reinitialise the component to it's initial state. This is necessary
		 * because once the component is mounted it's data would persist even if
		 * the modal closes
		 */
		closeModal() {
			this.modal = false
			this.page = 0
			this.conversationNameInput = ''
			this.isPublic = false
			this.isLoading = true
			this.token = ''
			this.success = false
			this.error = false
			this.passwordProtect = false
			this.password = ''
			this.listable = CONVERSATION.LISTABLE.NONE
			this.$store.dispatch('purgeNewGroupConversationStore')
		},
		/** Switch to page 2 */
		handleSetConversationName() {
			this.page = 1
		},
		/** Switch to page 1 from page 2 */
		handleClickBack() {
			this.page = 0
		},
		/**
		 * Handles the creation of the group conversation, adds the seleced
		 * participants to it and routes to it
		 */
		async handleCreateConversation() {
			this.page = 2

			// TODO: move all operations to a single store action
			// and commit + addConversation only once at the very end
			if (this.isPublic) {
				try {
					await this.createPublicConversation()
					if (this.password && this.passwordProtect) {
						await setConversationPassword(this.token, this.password)
					}
				} catch (exception) {
					console.debug(exception)
					this.isLoading = false
					this.error = true
					// Stop the execution of the method on exceptions.
					return
				}
			} else {
				try {
					await this.createPrivateConversation()
				} catch (exception) {
					console.debug(exception)
					this.isLoading = false
					this.error = true
					// Stop the execution of the method on exceptions.
					return
				}
			}

			try {
				await this.$store.dispatch('setListable', {
					token: this.token,
					listable: this.listable,
				})
			} catch (exception) {
				console.debug(exception)
				this.isLoading = false
				this.error = true
				// Stop the execution of the method on exceptions.
				return
			}

			for (const participant of this.selectedParticipants) {
				try {
					await addParticipant(this.token, participant.id, participant.source)
				} catch (exception) {
					console.debug(exception)
					this.isLoading = false
					this.error = true
					// Stop the execution of the method on exceptions.
					return
				}
			}

			this.success = true

			if (!this.isInCall) {
				// Push the newly created conversation's route.
				this.pushNewRoute()
			}

			// Close the modal right away if the conversation is public.
			if (!this.isPublic) {
				this.closeModal()
			}
		},
		/**
		 * Creates a new private conversation, adds it to the store and sets
		 * the local token value to the newly created conversation's token
		 */
		async createPrivateConversation() {
			const response = await createPrivateConversation(this.conversationName)
			const conversation = response.data.ocs.data
			this.$store.dispatch('addConversation', conversation)
			this.token = conversation.token
		},
		/**
		 * Creates a new public conversation, adds it to the store and sets
		 * the local token value to the newly created conversation's token
		 */
		async createPublicConversation() {
			const response = await createPublicConversation(this.conversationName)
			const conversation = response.data.ocs.data
			this.$store.dispatch('addConversation', conversation)
			this.token = conversation.token
		},
		pushNewRoute() {
			this.$router.push({ name: 'conversation', params: { token: this.token } })
				.catch(err => console.debug(`Error while pushing the new conversation's route: ${err}`))
		},
		handleCheckboxInput(event) {
			this.passwordProtect = event.target.checked
			// Reinitialise the password value when unchecking the password-protect option.
			if (this.passwordProtect === false) {
				this.password = ''
			}
		},
		/** Handles the press of the enter key */
		handleEnter() {
			if (!this.disabled) {
				this.handleSetConversationName()
				this.page = 1
			}
		},
	},

}

</script>

<style lang="scss" scoped>
.toggle {
	height: 44px;
	width: 44px;
	padding: 0;
	margin: 0 0 0 4px;
	display: flex;
	align-items: center;
	justify-content: center;
}

.new-group-conversation {
	/** This next 2 rules are pretty hacky, with the modal component somehow
	the margin applied to the content is added to the total modal width,
	so here we subtract it to the width and height of the content.
	*/
	height: 100%;
	padding: 20px;
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	position: relative;
	&__content {
		/**
		 * Top: 30px line height header + 12px margin
		 * Bottom: 44px buttons + 12 px margin
		 * Total: 98px
		 */
		height: calc(100% - 98px);
		display: flex;
		flex-direction: column;
		gap: 0.5rem;
	}
}

/** Size full in the modal component doesn't have border radius, this adds
it back */
::v-deep .modal-container {
	border-radius: var(--border-radius-large) !important;
	height: 700px;
}

.navigation {
	display: flex;
	justify-content: space-between;
	flex: 0 0 40px;
	height: 50px;
	box-shadow: 0 -10px 5px var(--color-main-background);
	z-index: 1;
	width: 100%;

	&__button-right {
		margin-left:auto;
	}
}

::v-deep .app-settings-section__hint {
	color: var(--color-text-lighter);
	padding: 8px 0;
}

::v-deep .app-settings-subsection {
	&:first-child {
		margin-top: 0;
	}
}

</style>