diff --git a/app/src/main/java/com/nextcloud/talk/api/NcApi.java b/app/src/main/java/com/nextcloud/talk/api/NcApi.java index b0f5199fb0..38476a29a3 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApi.java +++ b/app/src/main/java/com/nextcloud/talk/api/NcApi.java @@ -309,6 +309,12 @@ Observable registerDeviceForNotificationsWithPushProxy(@Url String url, Observable unregisterDeviceForNotificationsWithProxy(@Url String url, @QueryMap Map fields); + @FormUrlEncoded + @PUT + Observable> setPassword2(@Header("Authorization") String authorization, + @Url String url, + @Field("password") String password); + @GET Observable getCapabilities(@Header("Authorization") String authorization, @Url String url); @@ -373,16 +379,6 @@ Observable getMentionAutocompleteSuggestions(@Header("Authorizat @Nullable @Query("limit") Integer limit, @QueryMap Map fields); - // Url is: /api/{apiVersion}/room/{token}/pin - @POST - Observable addConversationToFavorites(@Header("Authorization") String authorization, - @Url String url); - - // Url is: /api/{apiVersion}/room/{token}/favorites - @DELETE - Observable removeConversationFromFavorites(@Header("Authorization") String authorization, - @Url String url); - @GET Observable getNcNotification(@Header("Authorization") String authorization, @Url String url); @@ -393,6 +389,12 @@ Observable setNotificationLevel(@Header("Authorization") String @Url String url, @Field("level") int level); + @FormUrlEncoded + @PUT + Observable setConversationReadOnly(@Header("Authorization") String authorization, + @Url String url, + @Field("state") int state); + @FormUrlEncoded @POST Observable createRemoteShare(@Nullable @Header("Authorization") String authorization, @@ -472,6 +474,9 @@ Observable notificationCalls(@Header("Authorization") String aut @Url String url, @Field("level") Integer level); + @DELETE + Observable clearChatHistory(@Header("Authorization") String authorization, @Url String url); + @GET Observable hoverCard(@Header("Authorization") String authorization, @Url String url); @@ -573,12 +578,6 @@ Observable votePoll(@Header("Authorization") String authorization, Observable closePoll(@Header("Authorization") String authorization, @Url String url); - @FormUrlEncoded - @POST - Observable setMessageExpiration(@Header("Authorization") String authorization, - @Url String url, - @Field("seconds") Integer seconds); - @GET Observable getOpenGraph(@Header("Authorization") String authorization, @Url String url, diff --git a/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt b/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt index f47978d99a..1970d2eeda 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt +++ b/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt @@ -119,6 +119,26 @@ interface NcApiCoroutines { @DELETE suspend fun unarchiveConversation(@Header("Authorization") authorization: String, @Url url: String): GenericOverall + @POST + suspend fun addConversationToFavorites( + @Header("Authorization") authorization: String, + @Url url: String + ): GenericOverall + + @DELETE + suspend fun removeConversationFromFavorites( + @Header("Authorization") authorization: String, + @Url url: String + ): GenericOverall + + @FormUrlEncoded + @POST + suspend fun notificationCalls( + @Header("Authorization") authorization: String, + @Url url: String, + @Field("level") level: Int + ): GenericOverall + @POST suspend fun setReadStatusPrivacy( @Header("Authorization") authorization: String, @@ -143,4 +163,20 @@ interface NcApiCoroutines { @Url url: String, @Field("state") state: Int ): GenericOverall + + @FormUrlEncoded + @POST + suspend fun setNotificationLevel( + @Header("Authorization") authorization: String, + @Url url: String, + @Field("level") level: Int + ): GenericOverall + + @FormUrlEncoded + @POST + suspend fun setMessageExpiration( + @Header("Authorization") authorization: String, + @Url url: String, + @Field("seconds") seconds: Int + ): GenericOverall } diff --git a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt index da36a2d09c..b491094ab4 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt @@ -835,7 +835,9 @@ class ConversationInfoActivity : binding.lockConversation.setOnClickListener { val isLocked = binding.lockConversationSwitch.isChecked binding.lockConversationSwitch.isChecked = !isLocked - databaseStorageModule!!.saveBoolean("lock_switch", !isLocked) + lifecycleScope.launch { + databaseStorageModule!!.saveBoolean("lock_switch", !isLocked) + } val state = if (isLocked) 0 else 1 makeConversationReadOnly(conversationToken, state) } @@ -1014,7 +1016,9 @@ class ConversationInfoActivity : .setSimpleItems(resources.getStringArray(R.array.message_expiring_descriptions)) binding.conversationSettingsDropdown.setOnItemClickListener { _, _, position, _ -> val v: String = resources.getStringArray(R.array.message_expiring_values)[position] - databaseStorageModule!!.saveString("conversation_settings_dropdown", v) + lifecycleScope.launch { + databaseStorageModule!!.saveString("conversation_settings_dropdown", v) + } } binding.messageExpirationSettings.visibility = VISIBLE } else { @@ -1516,18 +1520,24 @@ class ConversationInfoActivity : binding.notificationSettingsView.notificationSettingsImportantConversation.setOnClickListener { val isChecked = binding.notificationSettingsView.importantConversationSwitch.isChecked binding.notificationSettingsView.importantConversationSwitch.isChecked = !isChecked - module.saveBoolean("important_conversation_switch", !isChecked) + lifecycleScope.launch { + module.saveBoolean("important_conversation_switch", !isChecked) + } } binding.notificationSettingsView.notificationSettingsCallNotifications.setOnClickListener { val isChecked = binding.notificationSettingsView.callNotificationsSwitch.isChecked binding.notificationSettingsView.callNotificationsSwitch.isChecked = !isChecked - module.saveBoolean("call_notifications_switch", !isChecked) + lifecycleScope.launch { + module.saveBoolean("call_notifications_switch", !isChecked) + } } binding.notificationSettingsView.conversationInfoMessageNotificationsDropdown .setOnItemClickListener { _, _, position, _ -> val value = resources.getStringArray(R.array.message_notification_levels_entry_values)[position] Log.i(TAG, "saved $value to module from $position") - module.saveString("conversation_info_message_notifications_dropdown", value) + lifecycleScope.launch { + module.saveString("conversation_info_message_notifications_dropdown", value) + } } binding.notificationSettingsView.importantConversationSwitch.isChecked = module diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/ConversationsListBottomDialog.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/ConversationsListBottomDialog.kt index 9dbd718413..6a33abb7fa 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/ConversationsListBottomDialog.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/ConversationsListBottomDialog.kt @@ -6,11 +6,13 @@ */ package com.nextcloud.talk.ui.dialog +import android.annotation.SuppressLint import android.content.Intent import android.os.Bundle import android.text.TextUtils import android.view.View import android.view.ViewGroup +import androidx.lifecycle.lifecycleScope import androidx.work.Data import androidx.work.OneTimeWorkRequest import androidx.work.WorkInfo @@ -21,6 +23,7 @@ import com.google.android.material.bottomsheet.BottomSheetDialog import com.nextcloud.talk.R import com.nextcloud.talk.activities.MainActivity import com.nextcloud.talk.api.NcApi +import com.nextcloud.talk.api.NcApiCoroutines import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.conversation.RenameConversationDialogFragment import com.nextcloud.talk.conversationlist.ConversationsListActivity @@ -42,6 +45,9 @@ import io.reactivex.Observer import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import javax.inject.Inject @AutoInjector(NextcloudTalkApplication::class) @@ -56,6 +62,9 @@ class ConversationsListBottomDialog( @Inject lateinit var ncApi: NcApi + @Inject + lateinit var ncApiCoroutines: NcApiCoroutines + @Inject lateinit var viewThemeUtils: ViewThemeUtils @@ -193,84 +202,56 @@ class ConversationsListBottomDialog( } } + @SuppressLint("StringFormatInvalid", "TooGenericExceptionCaught") private fun addConversationToFavorites() { val apiVersion = ApiUtils.getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1)) - ncApi.addConversationToFavorites( - credentials, - ApiUtils.getUrlForRoomFavorite( - apiVersion, - currentUser.baseUrl!!, - conversation.token - ) - ) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .retry(1) - .subscribe(object : Observer { - override fun onSubscribe(d: Disposable) { - // unused atm + val url = ApiUtils.getUrlForRoomFavorite(apiVersion, currentUser.baseUrl!!, conversation.token) + lifecycleScope.launch { + try { + withContext(Dispatchers.IO) { + ncApiCoroutines.addConversationToFavorites(credentials, url) } - - override fun onNext(genericOverall: GenericOverall) { - activity.fetchRooms() - activity.showSnackbar( - String.format( - context.resources.getString(R.string.added_to_favorites), - conversation.displayName - ) + activity.fetchRooms() + activity.showSnackbar( + String.format( + context.resources.getString(R.string.added_to_favorites), + conversation.displayName ) - dismiss() - } - - override fun onError(e: Throwable) { + ) + dismiss() + } catch (e: Exception) { + withContext(Dispatchers.Main) { activity.showSnackbar(context.resources.getString(R.string.nc_common_error_sorry)) dismiss() } - - override fun onComplete() { - // unused atm - } - }) + } + } } + @SuppressLint("StringFormatInvalid", "TooGenericExceptionCaught") private fun removeConversationFromFavorites() { val apiVersion = ApiUtils.getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1)) - ncApi.removeConversationFromFavorites( - credentials, - ApiUtils.getUrlForRoomFavorite( - apiVersion, - currentUser.baseUrl!!, - conversation.token - ) - ) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .retry(1) - .subscribe(object : Observer { - override fun onSubscribe(d: Disposable) { - // unused atm + val url = ApiUtils.getUrlForRoomFavorite(apiVersion, currentUser.baseUrl!!, conversation.token) + lifecycleScope.launch { + try { + withContext(Dispatchers.IO) { + ncApiCoroutines.removeConversationFromFavorites(credentials, url) } - - override fun onNext(genericOverall: GenericOverall) { - activity.fetchRooms() - activity.showSnackbar( - String.format( - context.resources.getString(R.string.removed_from_favorites), - conversation.displayName - ) + activity.fetchRooms() + activity.showSnackbar( + String.format( + context.resources.getString(R.string.removed_from_favorites), + conversation.displayName ) - dismiss() - } - - override fun onError(e: Throwable) { + ) + dismiss() + } catch (e: Exception) { + withContext(Dispatchers.Main) { activity.showSnackbar(context.resources.getString(R.string.nc_common_error_sorry)) dismiss() } - - override fun onComplete() { - // unused atm - } - }) + } + } } private fun markConversationAsUnread() { @@ -290,6 +271,7 @@ class ConversationsListBottomDialog( // unused atm } + @SuppressLint("StringFormatInvalid") override fun onNext(genericOverall: GenericOverall) { activity.fetchRooms() activity.showSnackbar( @@ -336,6 +318,7 @@ class ConversationsListBottomDialog( // unused atm } + @SuppressLint("StringFormatInvalid") override fun onNext(genericOverall: GenericOverall) { activity.fetchRooms() activity.showSnackbar( @@ -372,6 +355,7 @@ class ConversationsListBottomDialog( } } + @SuppressLint("StringFormatInvalid") private fun leaveConversation() { val dataBuilder = Data.Builder() dataBuilder.putString(KEY_ROOM_TOKEN, conversation.token) diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/preferencestorage/DatabaseStorageModule.java b/app/src/main/java/com/nextcloud/talk/utils/preferences/preferencestorage/DatabaseStorageModule.java deleted file mode 100644 index 77f8079817..0000000000 --- a/app/src/main/java/com/nextcloud/talk/utils/preferences/preferencestorage/DatabaseStorageModule.java +++ /dev/null @@ -1,242 +0,0 @@ -/* - * Nextcloud Talk - Android Client - * - * SPDX-FileCopyrightText: 2022 Andy Scherzinger - * SPDX-FileCopyrightText: 2021 Tim Krüger - * SPDX-FileCopyrightText: 2017-2018 Mario Danic - * SPDX-License-Identifier: GPL-3.0-or-later - */ -package com.nextcloud.talk.utils.preferences.preferencestorage; - -import android.text.TextUtils; -import android.util.Log; - -import com.nextcloud.talk.api.NcApi; -import com.nextcloud.talk.application.NextcloudTalkApplication; -import com.nextcloud.talk.arbitrarystorage.ArbitraryStorageManager; -import com.nextcloud.talk.data.storage.model.ArbitraryStorage; -import com.nextcloud.talk.data.user.model.User; -import com.nextcloud.talk.models.json.generic.GenericOverall; -import com.nextcloud.talk.utils.ApiUtils; -import com.nextcloud.talk.utils.SpreedFeatures; -import com.nextcloud.talk.utils.UserIdUtils; -import com.nextcloud.talk.utils.CapabilitiesUtil; - -import javax.inject.Inject; - -import autodagger.AutoInjector; -import io.reactivex.Observer; -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.annotations.NonNull; -import io.reactivex.disposables.Disposable; -import io.reactivex.schedulers.Schedulers; - -@AutoInjector(NextcloudTalkApplication.class) -public class DatabaseStorageModule { - private static final String TAG = "DatabaseStorageModule"; - @Inject - ArbitraryStorageManager arbitraryStorageManager; - - @Inject - NcApi ncApi; - - private int messageExpiration; - private final User conversationUser; - private final String conversationToken; - private final long accountIdentifier; - - private boolean lobbyValue; - - private String messageNotificationLevel; - - public DatabaseStorageModule(User conversationUser, String conversationToken) { - NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this); - - this.conversationUser = conversationUser; - this.accountIdentifier = UserIdUtils.INSTANCE.getIdForUser(conversationUser); - this.conversationToken = conversationToken; - } - - public void saveBoolean(String key, boolean value) { - if ("call_notifications_switch".equals(key)) { - int apiVersion = ApiUtils.getConversationApiVersion(conversationUser, new int[]{4}); - ncApi.notificationCalls(ApiUtils.getCredentials(conversationUser.getUsername(), - conversationUser.getToken()), - ApiUtils.getUrlForRoomNotificationCalls(apiVersion, - conversationUser.getBaseUrl(), - conversationToken), - value ? 1 : 0) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new Observer() { - @Override - public void onSubscribe(@NonNull Disposable d) { - // unused atm - } - - @Override - public void onNext(@NonNull GenericOverall genericOverall) { - Log.d(TAG, "Toggled notification calls"); - } - - @Override - public void onError(@NonNull Throwable e) { - Log.e(TAG, "Error when trying to toggle notification calls", e); - } - - @Override - public void onComplete() { - // unused atm - } - } - ); - } - - if (!"lobby_switch".equals(key)) { - arbitraryStorageManager.storeStorageSetting(accountIdentifier, - key, - Boolean.toString(value), - conversationToken); - } else { - lobbyValue = value; - } - } - - public void saveString(String key, String value) { - if ("conversation_settings_dropdown".equals(key)) { - int apiVersion = ApiUtils.getConversationApiVersion(conversationUser, new int[]{4}); - - String trimmedValue = value.replace("expire_", ""); - int valueInt = Integer.parseInt(trimmedValue); - - ncApi.setMessageExpiration( - ApiUtils.getCredentials( - conversationUser.getUsername(), - conversationUser.getToken()), - ApiUtils.getUrlForMessageExpiration( - apiVersion, - conversationUser.getBaseUrl(), - conversationToken), - valueInt) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe(new Observer() { - @Override - public void onSubscribe(@NonNull Disposable d) { - // unused atm - } - - @Override - public void onNext(@NonNull GenericOverall genericOverall) { - messageExpiration = valueInt; - } - - @Override - public void onError(@NonNull Throwable e) { - Log.e(TAG, "Error when trying to set message expiration", e); - } - - @Override - public void onComplete() { - // unused atm - } - }); - - } else if ("conversation_info_message_notifications_dropdown".equals(key)) { - if (CapabilitiesUtil.hasSpreedFeatureCapability( - conversationUser.getCapabilities().getSpreedCapability(), - SpreedFeatures.NOTIFICATION_LEVELS) - ) { - if (TextUtils.isEmpty(messageNotificationLevel) || !messageNotificationLevel.equals(value)) { - int intValue; - switch (value) { - case "never": - intValue = 3; - break; - case "mention": - intValue = 2; - break; - case "always": - intValue = 1; - break; - default: - intValue = 0; - } - - int apiVersion = ApiUtils.getConversationApiVersion(conversationUser, new int[]{ApiUtils.API_V4, 1}); - - ncApi.setNotificationLevel(ApiUtils.getCredentials(conversationUser.getUsername(), - conversationUser.getToken()), - ApiUtils.getUrlForRoomNotificationLevel(apiVersion, - conversationUser.getBaseUrl(), - conversationToken), - intValue) - .subscribeOn(Schedulers.io()) - .subscribe(new Observer() { - @Override - public void onSubscribe(Disposable d) { - // unused atm - } - - @Override - public void onNext(GenericOverall genericOverall) {messageNotificationLevel = value;} - - @Override - public void onError(Throwable e) { - // unused atm - } - - @Override - public void onComplete() { - // unused atm - } - }); - } else { - messageNotificationLevel = value; - } - } - } else { - arbitraryStorageManager.storeStorageSetting(accountIdentifier, key, value, conversationToken); - } - } - public boolean getBoolean(String key, boolean defaultVal) { - if ("lobby_switch".equals(key)) { - return lobbyValue; - } else { - return arbitraryStorageManager - .getStorageSetting(accountIdentifier, key, conversationToken) - .map(arbitraryStorage -> Boolean.parseBoolean(arbitraryStorage.getValue())) - .blockingGet(defaultVal); - } - } - - public String getString(String key, String defaultVal) { - if ("conversation_settings_dropdown".equals(key)) { - switch (messageExpiration) { - case 2419200: - return "expire_2419200"; - case 604800: - return "expire_604800"; - case 86400: - return "expire_86400"; - case 28800: - return "expire_28800"; - case 3600: - return "expire_3600"; - default: - return "expire_0"; - } - } else if ("conversation_info_message_notifications_dropdown".equals(key)) { - return messageNotificationLevel; - } else { - return arbitraryStorageManager - .getStorageSetting(accountIdentifier, key, conversationToken) - .map(ArbitraryStorage::getValue) - .blockingGet(defaultVal); - } - } - - public void setMessageExpiration(int messageExpiration) { - this.messageExpiration = messageExpiration; - } -} diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/preferencestorage/DatabaseStorageModule.kt b/app/src/main/java/com/nextcloud/talk/utils/preferences/preferencestorage/DatabaseStorageModule.kt new file mode 100644 index 0000000000..e6b376f87a --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/preferencestorage/DatabaseStorageModule.kt @@ -0,0 +1,210 @@ +/* + * Nextcloud Talk - Android Client + * + * SPDX-FileCopyrightText: 2022 Andy Scherzinger + * SPDX-FileCopyrightText: 2021 Tim Krüger + * SPDX-FileCopyrightText: 2017-2018 Mario Danic + * SPDX-License-Identifier: GPL-3.0-or-later + */ +package com.nextcloud.talk.utils.preferences.preferencestorage + +import android.annotation.SuppressLint +import android.text.TextUtils +import android.util.Log +import autodagger.AutoInjector +import com.nextcloud.talk.api.NcApi +import com.nextcloud.talk.api.NcApiCoroutines +import com.nextcloud.talk.application.NextcloudTalkApplication +import com.nextcloud.talk.application.NextcloudTalkApplication.Companion.sharedApplication +import com.nextcloud.talk.arbitrarystorage.ArbitraryStorageManager +import com.nextcloud.talk.data.storage.model.ArbitraryStorage +import com.nextcloud.talk.data.user.model.User +import com.nextcloud.talk.utils.ApiUtils +import com.nextcloud.talk.utils.ApiUtils.getConversationApiVersion +import com.nextcloud.talk.utils.ApiUtils.getCredentials +import com.nextcloud.talk.utils.ApiUtils.getUrlForMessageExpiration +import com.nextcloud.talk.utils.ApiUtils.getUrlForRoomNotificationCalls +import com.nextcloud.talk.utils.ApiUtils.getUrlForRoomNotificationLevel +import com.nextcloud.talk.utils.CapabilitiesUtil.hasSpreedFeatureCapability +import com.nextcloud.talk.utils.SpreedFeatures +import com.nextcloud.talk.utils.UserIdUtils.getIdForUser +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext +import javax.inject.Inject + +@AutoInjector(NextcloudTalkApplication::class) +class DatabaseStorageModule(conversationUser: User, conversationToken: String) { + + @JvmField + @Inject + var arbitraryStorageManager: ArbitraryStorageManager? = null + + @JvmField + @Inject + var ncApi: NcApi? = null + + @JvmField + @Inject + var ncApiCoroutines: NcApiCoroutines? = null + + private var messageExpiration = 0 + private val conversationUser: User + private val conversationToken: String + private val accountIdentifier: Long + + private var lobbyValue = false + + private var messageNotificationLevel: String? = null + + init { + sharedApplication!!.componentApplication.inject(this) + + this.conversationUser = conversationUser + this.accountIdentifier = getIdForUser(conversationUser) + this.conversationToken = conversationToken + } + + @SuppressLint("TooGenericExceptionCaught") + suspend fun saveBoolean(key: String, value: Boolean) { + if ("call_notifications_switch" == key) { + val apiVersion = getConversationApiVersion(conversationUser, intArrayOf(4)) + val url = getUrlForRoomNotificationCalls(apiVersion, conversationUser.baseUrl, conversationToken) + val credentials = getCredentials(conversationUser.username, conversationUser.token) + val notificationLevel = if (value) 1 else 0 + withContext(Dispatchers.IO) { + try { + ncApiCoroutines!!.notificationCalls(credentials!!, url, notificationLevel) + Log.d(TAG, "Toggled notification calls") + } catch (e: Exception) { + Log.e(TAG, "Error when trying to toggle notification calls", e) + } + } + } + if ("lobby_switch" != key) { + arbitraryStorageManager!!.storeStorageSetting( + accountIdentifier, + key, + value.toString(), + conversationToken + ) + } else { + lobbyValue = value + } + } + + @SuppressLint("TooGenericExceptionCaught") + suspend fun saveString(key: String, value: String) { + when (key) { + "conversation_settings_dropdown" -> { + try { + val apiVersion = getConversationApiVersion(conversationUser, intArrayOf(API_VERSION_4)) + val trimmedValue = value.replace("expire_", "") + val valueInt = trimmedValue.toInt() + withContext(Dispatchers.IO) { + ncApiCoroutines!!.setMessageExpiration( + getCredentials(conversationUser.username, conversationUser.token)!!, + getUrlForMessageExpiration( + apiVersion, + conversationUser.baseUrl, + conversationToken + ), + valueInt + ) + messageExpiration = valueInt + } + } catch (exception: Exception) { + Log.e(TAG, "Error when trying to set message expiration", exception) + } + } + "conversation_info_message_notifications_dropdown" -> { + try { + if (hasSpreedFeatureCapability( + conversationUser.capabilities!!.spreedCapability!!, + SpreedFeatures.NOTIFICATION_LEVELS + ) + ) { + if (TextUtils.isEmpty(messageNotificationLevel) || messageNotificationLevel != value) { + val intValue = when (value) { + "never" -> NOTIFICATION_NEVER + "mention" -> NOTIFICATION_MENTION + "always" -> NOTIFICATION_ALWAYS + else -> 0 + } + val apiVersion = getConversationApiVersion(conversationUser, intArrayOf(ApiUtils.API_V4, 1)) + withContext(Dispatchers.IO) { + ncApiCoroutines!!.setNotificationLevel( + getCredentials( + conversationUser.username, + conversationUser.token + )!!, + getUrlForRoomNotificationLevel( + apiVersion, + conversationUser.baseUrl, + conversationToken + ), + intValue + ) + messageNotificationLevel = value + } + } else { + messageNotificationLevel = value + } + } + } catch (exception: Exception) { + Log.e(TAG, "Error trying to set notification level", exception) + } + } + else -> { + arbitraryStorageManager!!.storeStorageSetting(accountIdentifier, key, value, conversationToken) + } + } + } + + fun getBoolean(key: String, defaultVal: Boolean): Boolean { + return if ("lobby_switch" == key) { + lobbyValue + } else { + arbitraryStorageManager!! + .getStorageSetting(accountIdentifier, key, conversationToken) + .map { arbitraryStorage: ArbitraryStorage -> arbitraryStorage.value.toBoolean() } + .blockingGet(defaultVal) + } + } + + fun getString(key: String, defaultVal: String): String? { + return if ("conversation_settings_dropdown" == key) { + when (messageExpiration) { + EXPIRE_4_WEEKS -> "expire_2419200" + EXPIRE_7_DAYS -> "expire_604800" + EXPIRE_1_DAY -> "expire_86400" + EXPIRE_8_HOURS -> "expire_28800" + EXPIRE_1_HOUR -> "expire_3600" + else -> "expire_0" + } + } else if ("conversation_info_message_notifications_dropdown" == key) { + messageNotificationLevel + } else { + arbitraryStorageManager!! + .getStorageSetting(accountIdentifier, key, conversationToken) + .map(ArbitraryStorage::value) + .blockingGet(defaultVal) + } + } + + fun setMessageExpiration(messageExpiration: Int) { + this.messageExpiration = messageExpiration + } + + companion object { + private const val TAG = "DatabaseStorageModule" + private const val EXPIRE_1_HOUR = 3600 + private const val EXPIRE_8_HOURS = 28800 + private const val EXPIRE_1_DAY = 86400 + private const val EXPIRE_7_DAYS = 604800 + private const val EXPIRE_4_WEEKS = 2419200 + private const val NOTIFICATION_NEVER = 3 + private const val NOTIFICATION_MENTION = 2 + private const val NOTIFICATION_ALWAYS = 1 + private const val API_VERSION_4 = 4 + } +}