-
Notifications
You must be signed in to change notification settings - Fork 20
/
EditGuestAccessViewModel.kt
261 lines (231 loc) · 10.9 KB
/
EditGuestAccessViewModel.kt
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
/*
* Wire
* Copyright (C) 2023 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
*
*/
package com.wire.android.ui.home.conversations.details.editguestaccess
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wire.android.BuildConfig
import com.wire.android.ui.home.conversations.details.participants.usecase.ObserveParticipantsForConversationUseCase
import com.wire.android.ui.navArgs
import com.wire.android.util.dispatchers.DispatcherProvider
import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.id.QualifiedID
import com.wire.kalium.logic.feature.conversation.ObserveConversationDetailsUseCase
import com.wire.kalium.logic.feature.conversation.UpdateConversationAccessRoleUseCase
import com.wire.kalium.logic.feature.conversation.guestroomlink.CanCreatePasswordProtectedLinksUseCase
import com.wire.kalium.logic.feature.conversation.guestroomlink.GenerateGuestRoomLinkResult
import com.wire.kalium.logic.feature.conversation.guestroomlink.GenerateGuestRoomLinkUseCase
import com.wire.kalium.logic.feature.conversation.guestroomlink.ObserveGuestRoomLinkUseCase
import com.wire.kalium.logic.feature.conversation.guestroomlink.RevokeGuestRoomLinkResult
import com.wire.kalium.logic.feature.conversation.guestroomlink.RevokeGuestRoomLinkUseCase
import com.wire.kalium.logic.feature.user.guestroomlink.ObserveGuestRoomLinkFeatureFlagUseCase
import com.wire.kalium.logic.functional.onSuccess
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
@HiltViewModel
@Suppress("LongParameterList", "TooManyFunctions")
class EditGuestAccessViewModel @Inject constructor(
private val dispatcher: DispatcherProvider,
private val updateConversationAccessRole: UpdateConversationAccessRoleUseCase,
private val observeConversationDetails: ObserveConversationDetailsUseCase,
private val observeConversationMembers: ObserveParticipantsForConversationUseCase,
private val generateGuestRoomLink: GenerateGuestRoomLinkUseCase,
private val revokeGuestRoomLink: RevokeGuestRoomLinkUseCase,
private val observeGuestRoomLink: ObserveGuestRoomLinkUseCase,
private val observeGuestRoomLinkFeatureFlag: ObserveGuestRoomLinkFeatureFlagUseCase,
private val canCreatePasswordProtectedLinks: CanCreatePasswordProtectedLinksUseCase,
savedStateHandle: SavedStateHandle
) : ViewModel() {
private val editGuestAccessNavArgs: EditGuestAccessNavArgs = savedStateHandle.navArgs()
val conversationId: QualifiedID = editGuestAccessNavArgs.conversationId
private val accessParams = editGuestAccessNavArgs.editGuessAccessParams
var editGuestAccessState by mutableStateOf(
EditGuestAccessState(
isGuestAccessAllowed = accessParams.isGuestAccessAllowed,
isServicesAccessAllowed = accessParams.isServicesAllowed,
isUpdatingGuestAccessAllowed = accessParams.isUpdatingGuestAccessAllowed
)
)
init {
observeConversationDetails()
startObservingGuestRoomLink()
observeGuestRoomLinkFeature()
checkIfUserCanCreatePasswordProtectedLinks()
}
private fun checkIfUserCanCreatePasswordProtectedLinks() {
viewModelScope.launch {
val canCreatePasswordProtectedLinks = when {
!BuildConfig.IS_PASSWORD_PROTECTED_GUEST_LINK_ENABLED -> false
else -> canCreatePasswordProtectedLinks()
}
editGuestAccessState = editGuestAccessState.copy(
isPasswordProtectedLinksAllowed = canCreatePasswordProtectedLinks
)
}
}
private fun observeGuestRoomLinkFeature() {
viewModelScope.launch {
observeGuestRoomLinkFeatureFlag().collect {
it.isGuestRoomLinkEnabled?.let { isEnabled ->
editGuestAccessState = editGuestAccessState.copy(isGuestRoomLinkFeatureEnabled = isEnabled)
}
}
}
}
private fun observeConversationDetails() {
viewModelScope.launch {
val conversationDetailsFlow = observeConversationDetails(conversationId)
.filterIsInstance<ObserveConversationDetailsUseCase.Result.Success>()
.map { it.conversationDetails }
.distinctUntilChanged()
.flowOn(dispatcher.io())
.shareIn(this, SharingStarted.WhileSubscribed(), 1)
val isSelfAdminFlow = observeConversationMembers(conversationId)
.map { it.isSelfAnAdmin }
.distinctUntilChanged()
combine(
conversationDetailsFlow,
isSelfAdminFlow
) { conversationDetails, isSelfAnAdmin ->
val isGuestAllowed =
conversationDetails.conversation.isGuestAllowed() || conversationDetails.conversation.isNonTeamMemberAllowed()
editGuestAccessState = editGuestAccessState.copy(
isGuestAccessAllowed = isGuestAllowed,
isServicesAccessAllowed = conversationDetails.conversation.isServicesAllowed(),
isUpdatingGuestAccessAllowed = isSelfAnAdmin
)
}
}
}
private fun updateState(newState: EditGuestAccessState) {
editGuestAccessState = newState
}
fun updateGuestAccess(shouldEnableGuestAccess: Boolean) {
updateState(editGuestAccessState.copy(isUpdatingGuestAccess = true, isGuestAccessAllowed = shouldEnableGuestAccess))
when (shouldEnableGuestAccess) {
true -> updateGuestRemoteRequest(true)
false -> updateState(editGuestAccessState.copy(shouldShowGuestAccessChangeConfirmationDialog = true))
}
}
private fun updateGuestRemoteRequest(shouldEnableGuestAccess: Boolean) {
val newAccessRoles = Conversation
.accessRolesFor(
guestAllowed = shouldEnableGuestAccess,
servicesAllowed = editGuestAccessState.isServicesAccessAllowed,
nonTeamMembersAllowed = shouldEnableGuestAccess // Guest access controls non-team member access
)
val newAccess = Conversation
.accessFor(shouldEnableGuestAccess)
viewModelScope.launch {
withContext(dispatcher.io()) {
updateConversationAccessRole(
conversationId = conversationId,
accessRoles = newAccessRoles,
access = newAccess
)
}.also {
when (it) {
is UpdateConversationAccessRoleUseCase.Result.Failure -> updateState(
editGuestAccessState.copy(
isGuestAccessAllowed = !shouldEnableGuestAccess
)
)
is UpdateConversationAccessRoleUseCase.Result.Success -> Unit
}
updateState(editGuestAccessState.copy(isUpdatingGuestAccess = false))
}
}
}
fun onRequestGuestRoomLink() {
viewModelScope.launch {
safeCreateGuestLink(null)
}
}
private suspend fun safeCreateGuestLink(password: String?) {
editGuestAccessState = editGuestAccessState.copy(isGeneratingGuestRoomLink = true)
generateGuestRoomLink(conversationId, password).also {
editGuestAccessState = editGuestAccessState.copy(isGeneratingGuestRoomLink = false)
if (it is GenerateGuestRoomLinkResult.Failure) {
editGuestAccessState = editGuestAccessState.copy(isFailedToGenerateGuestRoomLink = true)
}
}
}
fun onGenerateGuestRoomFailureDialogDismiss() {
editGuestAccessState = editGuestAccessState.copy(isFailedToGenerateGuestRoomLink = false)
}
fun onGuestDialogDismiss() {
updateState(
editGuestAccessState.copy(
shouldShowGuestAccessChangeConfirmationDialog = false,
isUpdatingGuestAccess = false,
isGuestAccessAllowed = !editGuestAccessState.isGuestAccessAllowed
)
)
}
fun onGuestDialogConfirm() {
updateState(editGuestAccessState.copy(shouldShowGuestAccessChangeConfirmationDialog = false, isUpdatingGuestAccess = true))
updateGuestRemoteRequest(false)
}
fun onRevokeGuestRoomLink() {
editGuestAccessState = editGuestAccessState.copy(shouldShowRevokeLinkConfirmationDialog = true)
}
fun onRevokeDialogDismiss() {
editGuestAccessState = editGuestAccessState.copy(shouldShowRevokeLinkConfirmationDialog = false)
}
fun removeGuestLink() {
viewModelScope.launch {
updateState(editGuestAccessState.copy(shouldShowRevokeLinkConfirmationDialog = false, isRevokingLink = true))
revokeGuestRoomLink(conversationId).also {
if (it is RevokeGuestRoomLinkResult.Failure) {
updateState(editGuestAccessState.copy(isFailedToRevokeGuestRoomLink = true))
}
}
updateState(editGuestAccessState.copy(isRevokingLink = false))
}
}
private fun startObservingGuestRoomLink() {
viewModelScope.launch {
observeGuestRoomLink(conversationId).collect {
it.onSuccess {
editGuestAccessState =
editGuestAccessState.copy(link = it?.link, isLinkPasswordProtected = it?.isPasswordProtected ?: false)
}
}
}
}
fun onRevokeGuestRoomFailureDialogDismiss() {
editGuestAccessState = editGuestAccessState.copy(isFailedToRevokeGuestRoomLink = false)
}
fun shouldDisableGenerateGuestLinkButton() = !editGuestAccessState.isGuestRoomLinkFeatureEnabled ||
!editGuestAccessState.isGuestAccessAllowed ||
editGuestAccessState.isGeneratingGuestRoomLink
}