Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lock room #4428

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/src/main/java/com/nextcloud/talk/api/NcApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ Observable<GenericOverall> setNotificationLevel(@Header("Authorization") String

@FormUrlEncoded
@PUT
Observable<GenericOverall> setReadOnlyState(@Header("Authorization") String authorization,
Observable<GenericOverall> setConversationReadOnly(@Header("Authorization") String authorization,
@Url String url,
@Field("state") int state);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,18 @@ class ConversationInfoActivity :
else -> {}
}
}

viewModel.getConversationReadOnlyState.observe(this) { state ->
when (state) {
is ConversationInfoViewModel.SetConversationReadOnlySuccessState -> {
}
is ConversationInfoViewModel.SetConversationReadOnlyErrorState -> {
Snackbar.make(binding.root, R.string.conversation_read_only_failed, Snackbar.LENGTH_LONG).show()
}
else -> {
}
}
}
}

private fun setupActionBar() {
Expand Down Expand Up @@ -658,6 +670,7 @@ class ConversationInfoActivity :
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)
}

WorkInfo.State.FAILED -> {
val errorType = workInfo.outputData.getString("error_type")
if (errorType == LeaveConversationWorker.ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT) {
Expand All @@ -674,6 +687,7 @@ class ConversationInfoActivity :
).show()
}
}

else -> {
}
}
Expand Down Expand Up @@ -825,6 +839,20 @@ class ConversationInfoActivity :
binding.archiveConversationText.text = resources.getString(R.string.archive_conversation)
binding.archiveConversationTextHint.text = resources.getString(R.string.archive_hint)
}
if (ConversationUtils.isConversationReadOnlyAvailable(conversationCopy, spreedCapabilities)) {
binding.lockConversation.visibility = VISIBLE
binding.lockConversationSwitch.isChecked = databaseStorageModule!!.getBoolean("lock_switch", false)

binding.lockConversation.setOnClickListener {
val isLocked = binding.lockConversationSwitch.isChecked
binding.lockConversationSwitch.isChecked = !isLocked
databaseStorageModule!!.saveBoolean("lock_switch", !isLocked)
val state = if (isLocked) 0 else 1
makeConversationReadOnly(conversationUser, conversationToken, state)
}
} else {
binding.lockConversation.visibility = GONE
}

if (!isDestroyed) {
binding.dangerZoneOptions.visibility = VISIBLE
Expand Down Expand Up @@ -898,6 +926,10 @@ class ConversationInfoActivity :
}
}

private fun makeConversationReadOnly(conversationUser: User, roomToken: String, state: Int) {
viewModel.setConversationReadOnly(conversationUser, roomToken, state)
}

private fun initRecordingConsentOption() {
fun hide() {
binding.recordingConsentView.recordingConsentSettingsCategory.visibility = GONE
Expand Down Expand Up @@ -1295,7 +1327,7 @@ class ConversationInfoActivity :
}
}

@SuppressLint("CheckResult")
@SuppressLint("CheckResult", "StringFormatInvalid")
override fun onItemClick(view: View?, position: Int): Boolean {
if (!ConversationUtils.canModerate(conversation!!, spreedCapabilities)) {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ class ConversationInfoViewModel @Inject constructor(
val getUnBanActorState: LiveData<ViewState>
get() = _getUnBanActorState

object SetConversationReadOnlySuccessState : ViewState
object SetConversationReadOnlyErrorState : ViewState

private val _getConversationReadOnlyState: MutableLiveData<ViewState> = MutableLiveData()
val getConversationReadOnlyState: LiveData<ViewState>
get() = _getConversationReadOnlyState

object GetRoomStartState : ViewState
object GetRoomErrorState : ViewState
open class GetRoomSuccessState(val conversationModel: ConversationModel) : ViewState
Expand Down Expand Up @@ -178,6 +185,30 @@ class ConversationInfoViewModel @Inject constructor(
})
}

fun setConversationReadOnly(user: User, token: String, state: Int) {
val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1))
val url = ApiUtils.getUrlForConversationReadOnly(apiVersion, user.baseUrl!!, token)
conversationsRepository.setConversationReadOnly(user.getCredentials(), url, state)
.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(object : Observer<GenericOverall> {
override fun onSubscribe(p0: Disposable) {
}

override fun onError(error: Throwable) {
_getConversationReadOnlyState.value = SetConversationReadOnlyErrorState
}

override fun onComplete() {
// unused atm
}

override fun onNext(p0: GenericOverall) {
_getConversationReadOnlyState.value = SetConversationReadOnlySuccessState
}
})
}

fun unbanActor(user: User, token: String, banId: Int) {
val url = ApiUtils.getUrlForUnban(user.baseUrl!!, token, banId)
chatNetworkDataSource.unbanActor(user.getCredentials(), url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ interface ConversationsRepository {
suspend fun archiveConversation(credentials: String, url: String): GenericOverall

suspend fun unarchiveConversation(credentials: String, url: String): GenericOverall

fun setConversationReadOnly(credentials: String, url: String, state: Int): Observable<GenericOverall>
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ class ConversationsRepositoryImpl(
return coroutineApi.unarchiveConversation(credentials, url)
}

override fun setConversationReadOnly(credentials: String, url: String, state: Int): Observable<GenericOverall> {
return api.setConversationReadOnly(credentials, url, state)
}

private fun apiVersion(): Int {
return ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4))
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/nextcloud/talk/utils/ApiUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ object ApiUtils {
return getUrlForRoom(version, baseUrl, token) + "/password"
}

fun getUrlForRoomReadOnlyState(version: Int, baseUrl: String?, token: String?): String {
fun getUrlForConversationReadOnly(version: Int, baseUrl: String?, token: String?): String {
return getUrlForRoom(version, baseUrl, token) + "/read-only"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ object ConversationUtils {
!isNoteToSelfConversation(conversation)
}

fun isConversationReadOnlyAvailable(
conversation: ConversationModel,
spreedCapabilities: SpreedCapability
): Boolean {
return CapabilitiesUtil.hasSpreedFeatureCapability(spreedCapabilities, SpreedFeatures.READ_ONLY_ROOMS) &&
canModerate(conversation, spreedCapabilities)
}

fun isLobbyViewApplicable(conversation: ConversationModel, spreedCapabilities: SpreedCapability): Boolean {
return !canModerate(conversation, spreedCapabilities) &&
(
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/res/layout/activity_conversation_info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,51 @@

</LinearLayout>

<LinearLayout
android:id="@+id/lock_conversation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="@dimen/standard_margin"
android:paddingEnd="@dimen/standard_margin"
android:orientation="horizontal"
android:background="?android:attr/selectableItemBackground">

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingTop="@dimen/standard_half_margin"
android:paddingBottom="@dimen/standard_half_margin"
android:layout_weight="1"
android:orientation="horizontal">

<ImageView
android:layout_width="24dp"
android:layout_height="40dp"
android:layout_marginEnd="@dimen/standard_margin"
android:contentDescription="@null"
android:src="@drawable/ic_lock_white_24px"
app:tint="@color/grey_600" />

<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="@string/lock_conversation"
android:textSize="@dimen/headline_text_size" />

</LinearLayout>

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/lock_conversation_switch"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_marginStart="@dimen/standard_margin"
android:layout_marginEnd="1dp"
android:clickable="false"
android:focusable="true" />
</LinearLayout>

<LinearLayout
android:id="@+id/participants"
android:layout_width="match_parent"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ How to translate with transifex:
<string name="nc_new_mention">Unread mentions</string>
<string name="conversations">Conversations</string>
<string name="openConversations">Open conversations</string>
<string name="lock_conversation">Lock conversation</string>
<string name="error_loading_chats">There was a problem loading your chats</string>
<string name="close">Close</string>
<string name="close_icon">Close Icon</string>
Expand Down Expand Up @@ -823,4 +824,5 @@ How to translate with transifex:
<string name="archived">Archived</string>
<string name="archive_hint">Once a conversation is archived, it will be hidden by default. Select the filter \'Archived\' to view archived conversations. Direct mentions will still be received.</string>
<string name="unarchive_hint">Once a conversation is unarchived, it will be shown by default again.</string>
<string name="conversation_read_only_failed">Failed to set conversation Read-only</string>
</resources>
Loading