Merge pull request #2154 from nextcloud/chore/noid/room

Add room database layer
This commit is contained in:
Marcel Hibbe 2022-07-01 13:08:47 +02:00 committed by GitHub
commit 97d6c1b527
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
56 changed files with 2058 additions and 124 deletions

View file

@ -33,6 +33,7 @@ apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.github.spotbugs'
apply plugin: 'io.gitlab.arturbosch.detekt'
apply plugin: "org.jlleitschuh.gradle.ktlint"
apply plugin: 'kotlinx-serialization'
android {
compileSdkVersion 31
@ -80,6 +81,12 @@ android {
disable 'VectorPath'
}
javaCompileOptions {
annotationProcessorOptions {
arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
testInstrumentationRunnerArgument "TEST_SERVER_URL", "${NC_TEST_SERVER_BASEURL}"
testInstrumentationRunnerArgument "TEST_SERVER_USERNAME", "${NC_TEST_SERVER_USERNAME}"
testInstrumentationRunnerArgument "TEST_SERVER_PASSWORD", "${NC_TEST_SERVER_PASSWORD}"
@ -153,10 +160,12 @@ ext {
butterknifeVersion = "10.2.3"
coilKtVersion = "2.1.0"
daggerVersion = "2.42"
lifecycleVersion = '2.2.0'
okhttpVersion = "4.10.0"
materialDialogsVersion = "3.3.0"
parcelerVersion = "1.1.13"
retrofit2Version = "2.9.0"
roomVersion = "2.2.5"
workVersion = "2.7.1"
markwonVersion = "4.6.2"
espressoVersion = "3.4.0"
@ -179,6 +188,9 @@ dependencies {
detektPlugins("io.gitlab.arturbosch.detekt:detekt-formatting:1.20.0")
implementation fileTree(include: ['*'], dir: 'libs')
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3"
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
@ -203,6 +215,7 @@ dependencies {
implementation "androidx.exifinterface:exifinterface:1.3.3"
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.2.0'
implementation 'androidx.biometric:biometric:1.1.0'
@ -236,6 +249,14 @@ dependencies {
implementation 'io.requery:requery-android:1.6.1'
implementation 'net.zetetic:android-database-sqlcipher:4.5.1'
kapt 'io.requery:requery-processor:1.6.1'
implementation "androidx.room:room-runtime:${roomVersion}"
implementation "androidx.room:room-rxjava2:${roomVersion}"
kapt "androidx.room:room-compiler:${roomVersion}"
implementation "androidx.room:room-ktx:${roomVersion}"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:${lifecycleVersion}"
implementation "org.parceler:parceler-api:$parcelerVersion"
implementation 'net.orange-box.storebox:storebox-lib:1.4.0'
implementation "com.jakewharton:butterknife:${butterknifeVersion}"

View file

@ -0,0 +1,138 @@
{
"formatVersion": 1,
"database": {
"version": 8,
"identityHash": "055a9d64f28216e2981bea2fb6cc4b28",
"entities": [
{
"tableName": "User",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `userId` TEXT, `username` TEXT, `baseUrl` TEXT, `token` TEXT, `displayName` TEXT, `pushConfigurationState` TEXT, `capabilities` TEXT, `clientCertificate` TEXT, `externalSignalingServer` TEXT, `current` INTEGER NOT NULL, `scheduledForDeletion` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "userId",
"columnName": "userId",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "username",
"columnName": "username",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "baseUrl",
"columnName": "baseUrl",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "token",
"columnName": "token",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "displayName",
"columnName": "displayName",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "pushConfigurationState",
"columnName": "pushConfigurationState",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "capabilities",
"columnName": "capabilities",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "clientCertificate",
"columnName": "clientCertificate",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "externalSignalingServer",
"columnName": "externalSignalingServer",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "current",
"columnName": "current",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "scheduledForDeletion",
"columnName": "scheduledForDeletion",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "ArbitraryStorage",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`accountIdentifier` INTEGER NOT NULL, `key` TEXT, `object` TEXT, `value` TEXT, PRIMARY KEY(`accountIdentifier`))",
"fields": [
{
"fieldPath": "accountIdentifier",
"columnName": "accountIdentifier",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "key",
"columnName": "key",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "storageObject",
"columnName": "object",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "value",
"columnName": "value",
"affinity": "TEXT",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"accountIdentifier"
],
"autoGenerate": false
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '055a9d64f28216e2981bea2fb6cc4b28')"
]
}
}

View file

@ -1,11 +1,8 @@
package com.nextcloud.talk.activities
import android.util.Log
import androidx.test.espresso.intent.rule.IntentsTestRule
import com.nextcloud.talk.models.database.UserEntity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import org.junit.Assert.assertTrue
import com.nextcloud.talk.users.UserManager
import org.junit.Assert.assertNotNull
import org.junit.Rule
import org.junit.Test
@ -20,40 +17,25 @@ class MainActivityTest {
@Test
fun login() {
val sut = activityRule.launchActivity(null)
sut.userUtils.createOrUpdateUser(
"test",
"test",
"http://server/nc",
"test",
null,
true,
"test",
null,
null,
null,
null
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
{ userEntity: UserEntity? -> Log.i("test", "stored: " + userEntity.toString()) },
{ throwable: Throwable? -> Log.e("test", "throwable") },
{ Log.d("test", "complete") }
)
try {
Thread.sleep(2000)
} catch (e: InterruptedException) {
e.printStackTrace()
}
val user = sut.userManager.createOrUpdateUser(
"test",
UserManager.UserAttributes(
null,
serverUrl = "http://server/nc",
currentUser = true,
userId = "test",
token = "test",
displayName = "Test Name",
pushConfigurationState = null,
capabilities = null,
certificateAlias = null,
externalSignalingServer = null
)
).blockingGet()
assertNotNull("Error creating user", user)
sut.runOnUiThread { sut.resetConversationsList() }
assertTrue(sut.userUtils.getIfUserWithUsernameAndServer("test", "http://server/nc"))
try {
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
}

View file

@ -29,8 +29,6 @@ import android.os.Bundle;
import com.nextcloud.talk.R;
import com.nextcloud.talk.activities.MainActivity;
import junit.framework.AssertionFailedError;
import org.junit.Test;
import java.util.Objects;
@ -52,7 +50,6 @@ import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static androidx.test.espresso.web.sugar.Web.onWebView;
import static androidx.test.espresso.web.webdriver.DriverAtoms.findElement;
import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
@ -143,10 +140,8 @@ public class LoginIT {
onView(withId(R.id.user_name)).check(matches(withText("User One")));
activityScenario.onActivity(activity -> {
assertEquals(loginName, Objects.requireNonNull(activity.userUtils.getCurrentUser()).getUserId());
assertEquals(loginName,
Objects.requireNonNull(activity.userManager.getCurrentUser().blockingGet()).getUserId());
});
}
}

View file

@ -46,9 +46,11 @@ import com.nextcloud.talk.controllers.ServerSelectionController
import com.nextcloud.talk.controllers.SettingsController
import com.nextcloud.talk.controllers.WebViewLoginController
import com.nextcloud.talk.controllers.base.providers.ActionBarProvider
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.databinding.ActivityMainBinding
import com.nextcloud.talk.models.database.UserEntity
import com.nextcloud.talk.models.json.conversations.RoomOverall
import com.nextcloud.talk.users.UserManager
import com.nextcloud.talk.utils.ApiUtils
import com.nextcloud.talk.utils.ConductorRemapping.remapChatController
import com.nextcloud.talk.utils.SecurityUtils
@ -57,8 +59,8 @@ import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ACTIVE_CONVERSATION
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_ID
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ROOM_TOKEN
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_USER_ENTITY
import com.nextcloud.talk.utils.database.user.UserUtils
import io.reactivex.Observer
import io.reactivex.SingleObserver
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
@ -72,9 +74,6 @@ import javax.inject.Inject
class MainActivity : BaseActivity(), ActionBarProvider {
lateinit var binding: ActivityMainBinding
@Inject
lateinit var userUtils: UserUtils
@Inject
lateinit var dataStore: ReactiveEntityStore<Persistable>
@ -84,6 +83,9 @@ class MainActivity : BaseActivity(), ActionBarProvider {
@Inject
lateinit var ncApi: NcApi
@Inject
lateinit var userManager: UserManager
private var router: Router? = null
@Suppress("Detekt.TooGenericExceptionCaught")
@ -114,11 +116,31 @@ class MainActivity : BaseActivity(), ActionBarProvider {
onNewIntent(intent)
} else if (!router!!.hasRootController()) {
if (hasDb) {
if (userUtils.anyUserExists()) {
if (!appPreferences.isDbRoomMigrated) {
appPreferences.isDbRoomMigrated = true
}
userManager.users.subscribe(object : SingleObserver<List<User>> {
override fun onSubscribe(d: Disposable) {
// unused atm
}
override fun onSuccess(users: List<User>) {
if (users.isNotEmpty()) {
runOnUiThread {
setDefaultRootController()
}
} else {
runOnUiThread {
launchLoginScreen()
}
}
}
override fun onError(e: Throwable) {
Log.e(TAG, "Error loading existing users", e)
}
})
} else {
launchLoginScreen()
}
@ -178,10 +200,24 @@ class MainActivity : BaseActivity(), ActionBarProvider {
}
fun resetConversationsList() {
if (userUtils.anyUserExists()) {
userManager.users.subscribe(object : SingleObserver<List<User>> {
override fun onSubscribe(d: Disposable) {
// unused atm
}
override fun onSuccess(users: List<User>) {
if (users.isNotEmpty()) {
runOnUiThread {
setDefaultRootController()
}
}
}
override fun onError(e: Throwable) {
Log.e(TAG, "Error loading existing users", e)
}
})
}
fun openSettings() {
router!!.pushController(
@ -218,7 +254,8 @@ class MainActivity : BaseActivity(), ActionBarProvider {
"vnd.android.cursor.item/vnd.com.nextcloud.talk2.chat" -> {
val user = userId.substringBeforeLast("@")
val baseUrl = userId.substringAfterLast("@")
if (userUtils.currentUser?.baseUrl?.endsWith(baseUrl) == true) {
if (userManager.currentUser.blockingGet()?.baseUrl?.endsWith(baseUrl) == true) {
startConversation(user)
} else {
Snackbar.make(
@ -234,14 +271,16 @@ class MainActivity : BaseActivity(), ActionBarProvider {
private fun startConversation(userId: String) {
val roomType = "1"
val currentUser = userUtils.currentUser ?: return
val currentUser = userManager.currentUser.blockingGet()
val apiVersion = ApiUtils.getConversationApiVersion(currentUser, intArrayOf(ApiUtils.APIv4, 1))
val credentials = ApiUtils.getCredentials(currentUser.username, currentUser.token)
val credentials = ApiUtils.getCredentials(currentUser?.username, currentUser?.token)
val retrofitBucket = ApiUtils.getRetrofitBucketForCreateRoom(
apiVersion, currentUser.baseUrl, roomType,
apiVersion, currentUser?.baseUrl, roomType,
null, userId, null
)
ncApi.createRoom(
credentials,
retrofitBucket.url, retrofitBucket.queryMap
@ -252,6 +291,7 @@ class MainActivity : BaseActivity(), ActionBarProvider {
override fun onSubscribe(d: Disposable) {
// unused atm
}
override fun onNext(roomOverall: RoomOverall) {
val bundle = Bundle()
bundle.putParcelable(KEY_USER_ENTITY, currentUser)
@ -263,7 +303,7 @@ class MainActivity : BaseActivity(), ActionBarProvider {
credentials,
ApiUtils.getUrlForRoom(
apiVersion,
currentUser.baseUrl,
currentUser?.baseUrl,
roomOverall.ocs!!.data!!.token
)
)
@ -273,13 +313,14 @@ class MainActivity : BaseActivity(), ActionBarProvider {
override fun onSubscribe(d: Disposable) {
// unused atm
}
override fun onNext(roomOverall: RoomOverall) {
bundle.putParcelable(
KEY_ACTIVE_CONVERSATION,
Parcels.wrap(roomOverall.ocs!!.data)
)
remapChatController(
router!!, currentUser.id,
router!!, currentUser!!.id!!,
roomOverall.ocs!!.data!!.token!!, bundle, true
)
}
@ -287,6 +328,7 @@ class MainActivity : BaseActivity(), ActionBarProvider {
override fun onError(e: Throwable) {
// unused atm
}
override fun onComplete() {
// unused atm
}
@ -296,6 +338,7 @@ class MainActivity : BaseActivity(), ActionBarProvider {
override fun onError(e: Throwable) {
// unused atm
}
override fun onComplete() {
// unused atm
}
@ -351,6 +394,6 @@ class MainActivity : BaseActivity(), ActionBarProvider {
}
companion object {
private val TAG = "MainActivity"
private const val TAG = "MainActivity"
}
}

View file

@ -351,7 +351,6 @@ public class ConversationItem extends AbstractFlexibleItem<ConversationItem.Conv
}
static class ConversationItemViewHolder extends FlexibleViewHolder {
ImageView userStatusImage;
RvItemConversationWithLastMessageBinding binding;

View file

@ -0,0 +1,39 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
*
* 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.nextcloud.talk.arbitrarystorage
import com.nextcloud.talk.data.storage.ArbitraryStoragesRepository
import com.nextcloud.talk.data.storage.model.ArbitraryStorage
import io.reactivex.Maybe
class ArbitraryStorageManager(private val arbitraryStoragesRepository: ArbitraryStoragesRepository) {
fun storeStorageSetting(accountIdentifier: Long, key: String?, value: String?, objectString: String?) {
arbitraryStoragesRepository.saveArbitraryStorage(ArbitraryStorage(accountIdentifier, key, objectString, value))
}
fun getStorageSetting(accountIdentifier: Long, key: String, objectString: String): Maybe<ArbitraryStorage> {
return arbitraryStoragesRepository.getStorageSetting(accountIdentifier, key, objectString)
}
suspend fun deleteAllEntriesForAccountIdentifier(accountIdentifier: Long) {
return arbitraryStoragesRepository.deleteArbitraryStorage(accountIdentifier)
}
}

View file

@ -884,8 +884,10 @@ public class ConversationsListController extends BaseController implements Flexi
public void onDestroy() {
super.onDestroy();
dispose(null);
if (searchViewDisposable != null && !searchViewDisposable.isDisposed()) {
searchViewDisposable.dispose();
}
}
public void onQueryTextChange(final String newText) {
if (!TextUtils.isEmpty(searchQuery)) {

View file

@ -69,6 +69,7 @@ import com.nextcloud.talk.application.NextcloudTalkApplication.Companion.setAppT
import com.nextcloud.talk.application.NextcloudTalkApplication.Companion.sharedApplication
import com.nextcloud.talk.controllers.base.NewBaseController
import com.nextcloud.talk.controllers.util.viewBinding
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.databinding.ControllerSettingsBinding
import com.nextcloud.talk.jobs.AccountRemovalWorker
import com.nextcloud.talk.jobs.ContactAddressBookWorker
@ -86,6 +87,8 @@ import com.nextcloud.talk.utils.NotificationUtils.getCallRingtoneUri
import com.nextcloud.talk.utils.NotificationUtils.getMessageRingtoneUri
import com.nextcloud.talk.utils.SecurityUtils
import com.nextcloud.talk.utils.bundle.BundleKeys.KEY_ARE_CALL_SOUNDS
import com.nextcloud.talk.utils.database.user.CapabilitiesUtilNew
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
import com.nextcloud.talk.utils.database.user.UserUtils
import com.nextcloud.talk.utils.preferences.MagicUserInputModule
import com.nextcloud.talk.utils.singletons.ApplicationWideMessageHolder
@ -100,7 +103,6 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import java.net.URI
import java.net.URISyntaxException
import java.util.ArrayList
import java.util.Arrays
import java.util.Locale
import javax.inject.Inject
@ -115,8 +117,11 @@ class SettingsController : NewBaseController(R.layout.controller_settings) {
@Inject
lateinit var userUtils: UserUtils
@Inject
lateinit var currentUserProvider: CurrentUserProviderNew
private var saveStateHandler: LovelySaveStateHandler? = null
private var currentUser: UserEntity? = null
private var currentUser: User? = null
private var credentials: String? = null
private var proxyTypeChangeListener: OnPreferenceValueChangedListener<String>? = null
private var proxyCredentialsChangeListener: OnPreferenceValueChangedListener<Boolean>? = null
@ -134,7 +139,7 @@ class SettingsController : NewBaseController(R.layout.controller_settings) {
resources!!.getString(R.string.nc_settings)
private fun getCurrentUser() {
currentUser = userUtils.currentUser
currentUser = currentUserProvider.currentUser.blockingGet()
credentials = ApiUtils.getCredentials(currentUser!!.username, currentUser!!.token)
}
@ -144,6 +149,7 @@ class SettingsController : NewBaseController(R.layout.controller_settings) {
sharedApplication!!.componentApplication.inject(this)
ViewCompat.setTransitionName((binding.avatarImage), "userAvatar.transitionTag")
getCurrentUser()
if (saveStateHandler == null) {
@ -184,7 +190,7 @@ class SettingsController : NewBaseController(R.layout.controller_settings) {
}
private fun setupPhoneBookIntegration() {
if (CapabilitiesUtil.isPhoneBookIntegrationAvailable(userUtils.currentUser)) {
if (CapabilitiesUtilNew.isPhoneBookIntegrationAvailable(currentUser!!)) {
binding.settingsPhoneBookIntegration.visibility = View.VISIBLE
} else {
binding.settingsPhoneBookIntegration.visibility = View.GONE
@ -424,7 +430,7 @@ class SettingsController : NewBaseController(R.layout.controller_settings) {
}
private fun removeCurrentAccount() {
val otherUserExists = userUtils.scheduleUserForDeletionWithId(currentUser!!.id)
val otherUserExists = userUtils.scheduleUserForDeletionWithId(currentUser!!.id!!)
val accountRemovalWork = OneTimeWorkRequest.Builder(AccountRemovalWorker::class.java).build()
WorkManager.getInstance().enqueue(accountRemovalWork)
if (otherUserExists && view != null) {
@ -456,7 +462,6 @@ class SettingsController : NewBaseController(R.layout.controller_settings) {
super.onAttach(view)
actionBar?.show()
dispose(null)
getCurrentUser()
binding.settingsVersion.setOnClickListener {
sendLogs()
@ -639,7 +644,7 @@ class SettingsController : NewBaseController(R.layout.controller_settings) {
private fun setupServerAgeWarning() {
when {
CapabilitiesUtil.isServerEOL(currentUser) -> {
CapabilitiesUtilNew.isServerEOL(currentUser!!) -> {
binding.serverAgeWarningText.setTextColor(ContextCompat.getColor((context)!!, R.color.nc_darkRed))
binding.serverAgeWarningText.setText(R.string.nc_settings_server_eol)
binding.serverAgeWarningIcon.setColorFilter(
@ -647,7 +652,7 @@ class SettingsController : NewBaseController(R.layout.controller_settings) {
PorterDuff.Mode.SRC_IN
)
}
CapabilitiesUtil.isServerAlmostEOL(currentUser) -> {
CapabilitiesUtilNew.isServerAlmostEOL(currentUser!!) -> {
binding.serverAgeWarningText.setTextColor(
ContextCompat.getColor((context)!!, R.color.nc_darkYellow)
)
@ -679,7 +684,7 @@ class SettingsController : NewBaseController(R.layout.controller_settings) {
if (CapabilitiesUtil.isReadStatusAvailable(userUtils.currentUser)) {
(binding.settingsReadPrivacy.findViewById<View>(R.id.mp_checkable) as Checkable).isChecked =
!CapabilitiesUtil.isReadStatusPrivate(currentUser)
!CapabilitiesUtilNew.isReadStatusPrivate(currentUser!!)
} else {
binding.settingsReadPrivacy.visibility = View.GONE
}

View file

@ -24,6 +24,7 @@ package com.nextcloud.talk.dagger.modules;
import android.content.Context;
import androidx.annotation.NonNull;
import com.nextcloud.talk.R;
import com.nextcloud.talk.data.source.local.TalkDatabase;
import com.nextcloud.talk.models.database.Models;
import com.nextcloud.talk.utils.preferences.AppPreferences;
import dagger.Module;
@ -35,6 +36,7 @@ import io.requery.reactivex.ReactiveSupport;
import io.requery.sql.Configuration;
import io.requery.sql.EntityDataStore;
import net.orange_box.storebox.StoreBox;
import net.sqlcipher.database.SQLiteDatabase;
import javax.inject.Singleton;
@ -44,7 +46,13 @@ public class DatabaseModule {
@Provides
@Singleton
public SqlCipherDatabaseSource provideSqlCipherDatabaseSource(@NonNull final Context context) {
public SqlCipherDatabaseSource provideSqlCipherDatabaseSource(
@NonNull final Context context,
final AppPreferences appPreferences) {
int version = DB_VERSION;
if (appPreferences.getIsDbRoomMigrated()) {
version++;
}
return new SqlCipherDatabaseSource(
context,
Models.DEFAULT,
@ -56,7 +64,14 @@ public class DatabaseModule {
.trim()
+ ".sqlite",
context.getString(R.string.nc_talk_database_encryption_key),
DB_VERSION);
version) {
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion < 7) {
super.onDowngrade(db, oldVersion, newVersion);
}
}
};
}
@Provides
@ -74,4 +89,10 @@ public class DatabaseModule {
preferences.removeLinkPreviews();
return preferences;
}
@Provides
@Singleton
public TalkDatabase provideTalkDatabase(@NonNull final Context context) {
return TalkDatabase.getInstance(context);
}
}

View file

@ -2,6 +2,8 @@
* Nextcloud Talk application
*
* @author Álvaro Brey
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
* Copyright (C) 2022 Álvaro Brey
* Copyright (C) 2022 Nextcloud GmbH
*
@ -22,6 +24,11 @@
package com.nextcloud.talk.dagger.modules
import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.data.source.local.TalkDatabase
import com.nextcloud.talk.data.storage.ArbitraryStoragesRepository
import com.nextcloud.talk.data.storage.ArbitraryStoragesRepositoryImpl
import com.nextcloud.talk.data.user.UsersRepository
import com.nextcloud.talk.data.user.UsersRepositoryImpl
import com.nextcloud.talk.remotefilebrowser.repositories.RemoteFileBrowserItemsRepository
import com.nextcloud.talk.remotefilebrowser.repositories.RemoteFileBrowserItemsRepositoryImpl
import com.nextcloud.talk.repositories.unifiedsearch.UnifiedSearchRepository
@ -50,4 +57,14 @@ class RepositoryModule {
RemoteFileBrowserItemsRepository {
return RemoteFileBrowserItemsRepositoryImpl(okHttpClient, userProvider)
}
@Provides
fun provideUsersRepository(database: TalkDatabase): UsersRepository {
return UsersRepositoryImpl(database.usersDao())
}
@Provides
fun provideArbitraryStoragesRepository(database: TalkDatabase): ArbitraryStoragesRepository {
return ArbitraryStoragesRepositoryImpl(database.arbitraryStoragesDao())
}
}

View file

@ -0,0 +1,95 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <infoi@andy-scherzinger.de>
*
* 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.nextcloud.talk.data.source.local
import android.util.Log
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
@Suppress("MagicNumber")
object Migrations {
val MIGRATION_6_8 = object : Migration(6, 8) {
override fun migrate(database: SupportSQLiteDatabase) {
Log.i("Migrations", "Migrating 6 to 8")
migrateToRoom(database)
}
}
val MIGRATION_7_8 = object : Migration(7, 8) {
override fun migrate(database: SupportSQLiteDatabase) {
Log.i("Migrations", "Migrating 7 to 8")
migrateToRoom(database)
}
}
fun migrateToRoom(database: SupportSQLiteDatabase) {
database.execSQL(
"CREATE TABLE User_new (" +
"id INTEGER NOT NULL, " +
"userId TEXT, " +
"username TEXT, " +
"baseUrl TEXT, " +
"token TEXT, " +
"displayName TEXT, " +
"pushConfigurationState TEXT, " +
"capabilities TEXT, " +
"clientCertificate TEXT, " +
"externalSignalingServer TEXT, " +
"current INTEGER NOT NULL, " +
"scheduledForDeletion INTEGER NOT NULL, " +
"PRIMARY KEY(id)" +
")"
)
database.execSQL(
"CREATE TABLE ArbitraryStorage_new (" +
"accountIdentifier INTEGER NOT NULL, " +
"\"key\" TEXT, " +
"object TEXT, " +
"value TEXT, " +
"PRIMARY KEY(accountIdentifier)" +
")"
)
// Copy the data
database.execSQL(
"INSERT INTO User_new (" +
"id, userId, username, baseUrl, token, displayName, pushConfigurationState, capabilities, " +
"clientCertificate, externalSignalingServer, current, scheduledForDeletion) " +
"SELECT " +
"id, userId, username, baseUrl, token, displayName, pushConfigurationState, capabilities, " +
"clientCertificate, externalSignalingServer, current, scheduledForDeletion " +
"FROM User"
)
database.execSQL(
"INSERT INTO ArbitraryStorage_new (" +
"accountIdentifier, \"key\", object, value) " +
"SELECT " +
"accountIdentifier, \"key\", object, value " +
"FROM ArbitraryStorage"
)
// Remove the old table
database.execSQL("DROP TABLE User")
database.execSQL("DROP TABLE ArbitraryStorage")
// Change the table name to the correct one
database.execSQL("ALTER TABLE User_new RENAME TO User")
database.execSQL("ALTER TABLE ArbitraryStorage_new RENAME TO ArbitraryStorage")
}
}

View file

@ -0,0 +1,99 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2020 Mario Danic <mario@lovelyhq.com>
*
* 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.nextcloud.talk.data.source.local
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.sqlite.db.SupportSQLiteDatabase
import com.nextcloud.talk.R
import com.nextcloud.talk.data.source.local.converters.CapabilitiesConverter
import com.nextcloud.talk.data.source.local.converters.ExternalSignalingServerConverter
import com.nextcloud.talk.data.source.local.converters.HashMapHashMapConverter
import com.nextcloud.talk.data.source.local.converters.PushConfigurationConverter
import com.nextcloud.talk.data.source.local.converters.SignalingSettingsConverter
import com.nextcloud.talk.data.storage.ArbitraryStoragesDao
import com.nextcloud.talk.data.storage.model.ArbitraryStorageEntity
import com.nextcloud.talk.data.user.UsersDao
import com.nextcloud.talk.data.user.model.UserEntity
import net.sqlcipher.database.SQLiteDatabase
import net.sqlcipher.database.SupportFactory
import java.util.Locale
@Database(
entities = [UserEntity::class, ArbitraryStorageEntity::class],
version = 8,
exportSchema = true
)
@TypeConverters(
PushConfigurationConverter::class,
CapabilitiesConverter::class,
ExternalSignalingServerConverter::class,
SignalingSettingsConverter::class,
HashMapHashMapConverter::class
)
abstract class TalkDatabase : RoomDatabase() {
abstract fun usersDao(): UsersDao
abstract fun arbitraryStoragesDao(): ArbitraryStoragesDao
companion object {
@Volatile
private var INSTANCE: TalkDatabase? = null
@JvmStatic
fun getInstance(context: Context): TalkDatabase =
INSTANCE ?: synchronized(this) {
INSTANCE ?: build(context).also { INSTANCE = it }
}
private fun build(context: Context): TalkDatabase {
val passCharArray = context.getString(R.string.nc_talk_database_encryption_key).toCharArray()
val passphrase: ByteArray = SQLiteDatabase.getBytes(passCharArray)
val factory = SupportFactory(passphrase)
val dbName = context
.resources
.getString(R.string.nc_app_product_name)
.lowercase(Locale.getDefault())
.replace(" ", "_")
.trim { it <= ' ' } +
".sqlite"
return Room
.databaseBuilder(context.applicationContext, TalkDatabase::class.java, dbName)
.openHelperFactory(factory)
.addMigrations(Migrations.MIGRATION_6_8, Migrations.MIGRATION_7_8)
.allowMainThreadQueries()
.addCallback(
object : RoomDatabase.Callback() {
override fun onOpen(db: SupportSQLiteDatabase) {
super.onOpen(db)
db.execSQL("PRAGMA defer_foreign_keys = 1")
}
})
.build()
}
}
}

View file

@ -0,0 +1,45 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2020 Mario Danic <mario@lovelyhq.com>
*
* 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.nextcloud.talk.data.source.local.converters
import androidx.room.TypeConverter
import com.bluelinelabs.logansquare.LoganSquare
import com.nextcloud.talk.models.json.capabilities.Capabilities
class CapabilitiesConverter {
@TypeConverter
fun fromCapabilitiesToString(capabilities: Capabilities?): String {
return if (capabilities == null) {
""
} else {
LoganSquare.serialize(capabilities)
}
}
@TypeConverter
fun fromStringToCapabilities(value: String): Capabilities? {
return if (value.isBlank()) {
null
} else {
return LoganSquare.parse(value, Capabilities::class.java)
}
}
}

View file

@ -0,0 +1,46 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
*
* 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.nextcloud.talk.data.source.local.converters
import androidx.room.TypeConverter
import com.bluelinelabs.logansquare.LoganSquare
import com.nextcloud.talk.models.ExternalSignalingServer
class ExternalSignalingServerConverter {
@TypeConverter
fun fromExternalSignalingServerToString(externalSignalingServer: ExternalSignalingServer?): String {
return if (externalSignalingServer == null) {
""
} else {
LoganSquare.serialize(externalSignalingServer)
}
}
@TypeConverter
fun fromStringToExternalSignalingServer(value: String): ExternalSignalingServer? {
return if (value.isBlank()) {
null
} else {
return LoganSquare.parse(value, ExternalSignalingServer::class.java)
}
}
}

View file

@ -0,0 +1,44 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2020 Mario Danic <mario@lovelyhq.com>
*
* 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.nextcloud.talk.data.source.local.converters
import androidx.room.TypeConverter
import com.bluelinelabs.logansquare.LoganSquare
class HashMapHashMapConverter {
@TypeConverter
fun fromDoubleHashMapToString(map: HashMap<String, HashMap<String, String>>?): String? {
return if (map == null) {
LoganSquare.serialize(hashMapOf<String, HashMap<String, String>>())
} else {
return LoganSquare.serialize(map)
}
}
@TypeConverter
fun fromStringToDoubleHashMap(value: String?): HashMap<String, HashMap<String, String>>? {
if (value.isNullOrEmpty()) {
return hashMapOf()
}
return LoganSquare.parseMap(value, HashMap::class.java) as HashMap<String, HashMap<String, String>>?
}
}

View file

@ -0,0 +1,48 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* @author Mario Danic
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
* Copyright (C) 2017-2020 Mario Danic <mario@lovelyhq.com>
*
* 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.nextcloud.talk.data.source.local.converters
import androidx.room.TypeConverter
import com.bluelinelabs.logansquare.LoganSquare
import com.nextcloud.talk.models.json.push.PushConfigurationState
class PushConfigurationConverter {
@TypeConverter
fun fromPushConfigurationToString(pushConfiguration: PushConfigurationState?): String {
return if (pushConfiguration == null) {
""
} else {
LoganSquare.serialize(pushConfiguration)
}
}
@TypeConverter
fun fromStringToPushConfiguration(value: String?): PushConfigurationState? {
return if (value.isNullOrBlank()) {
null
} else {
return LoganSquare.parse(value, PushConfigurationState::class.java)
}
}
}

View file

@ -0,0 +1,48 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* @author Mario Danic
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
* Copyright (C) 2017-2020 Mario Danic <mario@lovelyhq.com>
*
* 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.nextcloud.talk.data.source.local.converters
import androidx.room.TypeConverter
import com.bluelinelabs.logansquare.LoganSquare
import com.nextcloud.talk.models.json.signaling.settings.SignalingSettings
class SignalingSettingsConverter {
@TypeConverter
fun fromSignalingSettingsToString(signalingSettings: SignalingSettings?): String {
return if (signalingSettings == null) {
""
} else {
LoganSquare.serialize(signalingSettings)
}
}
@TypeConverter
fun fromStringToSignalingSettings(value: String): SignalingSettings? {
return if (value.isBlank()) {
null
} else {
return LoganSquare.parse(value, SignalingSettings::class.java)
}
}
}

View file

@ -0,0 +1,46 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <infoi@andy-scherzinger.de>
*
* model 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.
*
* model 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 model program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.data.storage
import com.nextcloud.talk.data.storage.model.ArbitraryStorage
import com.nextcloud.talk.data.storage.model.ArbitraryStorageEntity
object ArbitraryStorageMapper {
fun toModel(entity: ArbitraryStorageEntity?): ArbitraryStorage? {
return entity?.let {
ArbitraryStorage(
it.accountIdentifier,
it.key,
it.storageObject,
it.value
)
}
}
fun toEntity(model: ArbitraryStorage): ArbitraryStorageEntity {
return ArbitraryStorageEntity(
accountIdentifier = model.accountIdentifier,
key = model.key,
storageObject = model.storageObject,
value = model.value
)
}
}

View file

@ -0,0 +1,49 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <infoi@andy-scherzinger.de>
*
* 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.nextcloud.talk.data.storage
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.nextcloud.talk.data.storage.model.ArbitraryStorageEntity
import io.reactivex.Maybe
@Dao
abstract class ArbitraryStoragesDao {
@Query(
"SELECT * FROM ArbitraryStorage WHERE " +
"accountIdentifier = :accountIdentifier AND " +
"\"key\" = :key AND " +
"object = :objectString"
)
abstract fun getStorageSetting(
accountIdentifier: Long,
key: String,
objectString: String
): Maybe<ArbitraryStorageEntity>
@Query("DELETE FROM ArbitraryStorage WHERE accountIdentifier = :accountIdentifier")
abstract fun deleteArbitraryStorage(accountIdentifier: Long)
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun saveArbitraryStorage(arbitraryStorage: ArbitraryStorageEntity): Long
}

View file

@ -0,0 +1,30 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
*
* 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.nextcloud.talk.data.storage
import com.nextcloud.talk.data.storage.model.ArbitraryStorage
import io.reactivex.Maybe
interface ArbitraryStoragesRepository {
fun getStorageSetting(accountIdentifier: Long, key: String, objectString: String): Maybe<ArbitraryStorage>
fun deleteArbitraryStorage(accountIdentifier: Long)
fun saveArbitraryStorage(arbitraryStorage: ArbitraryStorage): Long
}

View file

@ -0,0 +1,45 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
*
* 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.nextcloud.talk.data.storage
import com.nextcloud.talk.data.storage.model.ArbitraryStorage
import io.reactivex.Maybe
class ArbitraryStoragesRepositoryImpl(private val arbitraryStoragesDao: ArbitraryStoragesDao) :
ArbitraryStoragesRepository {
override fun getStorageSetting(
accountIdentifier: Long,
key: String,
objectString: String
): Maybe<ArbitraryStorage> {
return arbitraryStoragesDao
.getStorageSetting(accountIdentifier, key, objectString)
.map { ArbitraryStorageMapper.toModel(it) }
}
override fun deleteArbitraryStorage(accountIdentifier: Long) {
arbitraryStoragesDao.deleteArbitraryStorage(accountIdentifier)
}
override fun saveArbitraryStorage(arbitraryStorage: ArbitraryStorage): Long {
return arbitraryStoragesDao.saveArbitraryStorage(ArbitraryStorageMapper.toEntity(arbitraryStorage))
}
}

View file

@ -0,0 +1,32 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <infoi@andy-scherzinger.de>
*
* 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.nextcloud.talk.data.storage.model
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
@Parcelize
data class ArbitraryStorage(
var accountIdentifier: Long = 0,
var key: String? = null,
var storageObject: String? = null,
var value: String? = null
) : Parcelable

View file

@ -0,0 +1,36 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <infoi@andy-scherzinger.de>
*
* 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.nextcloud.talk.data.storage.model
import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import kotlinx.android.parcel.Parcelize
@Parcelize
@Entity(tableName = "ArbitraryStorage")
data class ArbitraryStorageEntity(
@PrimaryKey @ColumnInfo(name = "accountIdentifier") var accountIdentifier: Long = 0,
@ColumnInfo(name = "key") var key: String? = null,
@ColumnInfo(name = "object") var storageObject: String? = null,
@ColumnInfo(name = "value") var value: String? = null
) : Parcelable

View file

@ -0,0 +1,69 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <infoi@andy-scherzinger.de>
*
* model 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.
*
* model 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 model program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.data.user
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.data.user.model.UserEntity
object UserMapper {
fun toModel(entities: List<UserEntity?>?): List<User> {
return entities?.map { user: UserEntity? ->
toModel(user)!!
} ?: emptyList()
}
fun toModel(entity: UserEntity?): User? {
return entity?.let {
User(
entity.id,
entity.userId,
entity.username,
entity.baseUrl,
entity.token,
entity.displayName,
entity.pushConfigurationState,
entity.capabilities,
entity.clientCertificate,
entity.externalSignalingServer,
entity.current,
entity.scheduledForDeletion
)
}
}
fun toEntity(model: User): UserEntity {
val userEntity = when (val id = model.id) {
null -> UserEntity(userId = model.userId, username = model.username, baseUrl = model.baseUrl)
else -> UserEntity(id, model.userId, model.username, model.baseUrl)
}
userEntity.apply {
token = model.token
displayName = model.displayName
pushConfigurationState = model.pushConfigurationState
capabilities = model.capabilities
clientCertificate = model.clientCertificate
externalSignalingServer = model.externalSignalingServer
current = model.current
scheduledForDeletion = model.scheduledForDeletion
}
return userEntity
}
}

View file

@ -0,0 +1,126 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
* Copyright (C) 2017-2020 Mario Danic <mario@lovelyhq.com>
*
* 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.nextcloud.talk.data.user
import android.util.Log
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import com.nextcloud.talk.data.user.model.UserEntity
import io.reactivex.Maybe
import io.reactivex.Single
import java.lang.Boolean.FALSE
import java.lang.Boolean.TRUE
@Dao
@Suppress("TooManyFunctions")
abstract class UsersDao {
// get active user
@Query("SELECT * FROM User where current = 1")
abstract fun getActiveUser(): Maybe<UserEntity>
@Query("SELECT * FROM User where current = 1")
abstract fun getActiveUserSynchronously(): UserEntity?
@Query("DELETE FROM User WHERE id = :id")
abstract fun deleteUserWithId(id: Long)
@Update
abstract fun updateUser(user: UserEntity): Int
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun saveUser(user: UserEntity): Long
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun saveUsers(vararg users: UserEntity): List<Long>
// get all users not scheduled for deletion
@Query("SELECT * FROM User where scheduledForDeletion != 1")
abstract fun getUsers(): Single<List<UserEntity>>
@Query("SELECT * FROM User where id = :id")
abstract fun getUserWithId(id: Long): Maybe<UserEntity>
@Query("SELECT * FROM User where id = :id AND scheduledForDeletion != 1")
abstract fun getUserWithIdNotScheduledForDeletion(id: Long): Maybe<UserEntity>
@Query("SELECT * FROM User where userId = :userId")
abstract fun getUserWithUserId(userId: String): Maybe<UserEntity>
@Query("SELECT * FROM User where userId != :userId")
abstract fun getUsersWithoutUserId(userId: Long): Single<List<UserEntity>>
@Query("SELECT * FROM User where scheduledForDeletion = 1")
abstract fun getUsersScheduledForDeletion(): Single<List<UserEntity>>
@Query("SELECT * FROM User where scheduledForDeletion = 0")
abstract fun getUsersNotScheduledForDeletion(): Single<List<UserEntity>>
@Query("SELECT * FROM User WHERE username = :username AND baseUrl = :server")
abstract fun getUserWithUsernameAndServer(username: String, server: String): Maybe<UserEntity>
@Transaction
@Suppress("Detekt.TooGenericExceptionCaught") // blockingGet() only throws RuntimeExceptions per rx docs
open fun setUserAsActiveWithId(id: Long): Boolean {
return try {
getUsers().blockingGet().forEach { user ->
user.current = user.id == id
updateUser(user)
}
true
} catch (e: RuntimeException) {
Log.e(TAG, "Error setting user active", e)
false
}
}
@Transaction
open fun markUserForDeletion(id: Long): Boolean {
getUserWithId(id).blockingGet()?.let { user ->
user.current = FALSE
updateUser(user)
}
return setAnyUserAsActive()
}
@Transaction
open fun setAnyUserAsActive(): Boolean {
val users = getUsers().blockingGet()
val result = users.firstOrNull()?.let { user ->
user.current = TRUE
updateUser(user)
TRUE
} ?: FALSE
return result
}
companion object {
const val TAG = "UsersDao"
}
}

View file

@ -0,0 +1,46 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
* Copyright (C) 2017-2020 Mario Danic <mario@lovelyhq.com>
*
* 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.nextcloud.talk.data.user
import com.nextcloud.talk.data.user.model.User
import io.reactivex.Maybe
import io.reactivex.Single
@Suppress("TooManyFunctions")
interface UsersRepository {
fun getActiveUser(): Maybe<User>
fun getUsers(): Single<List<User>>
fun getUserWithId(id: Long): Maybe<User>
fun getUserWithIdNotScheduledForDeletion(id: Long): Maybe<User>
fun getUserWithUserId(userId: String): Maybe<User>
fun getUsersWithoutUserId(userId: Long): Single<List<User>>
fun getUsersScheduledForDeletion(): Single<List<User>>
fun getUsersNotScheduledForDeletion(): Single<List<User>>
fun getUserWithUsernameAndServer(username: String, server: String): Maybe<User>
fun updateUser(user: User): Int
fun insertUser(user: User): Long
fun setUserAsActiveWithId(id: Long): Single<Boolean>
fun deleteUserWithId(id: Long)
fun setAnyUserAsActive(): Boolean
fun markUserForDeletion(id: Long): Boolean
}

View file

@ -0,0 +1,91 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
* Copyright (C) 2017-2020 Mario Danic <mario@lovelyhq.com>
*
* 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.nextcloud.talk.data.user
import com.nextcloud.talk.data.user.model.User
import io.reactivex.Maybe
import io.reactivex.Single
@Suppress("TooManyFunctions")
class UsersRepositoryImpl(private val usersDao: UsersDao) : UsersRepository {
override fun getActiveUser(): Maybe<User> {
return usersDao.getActiveUser().map { UserMapper.toModel(it) }
}
override fun getUsers(): Single<List<User>> {
return usersDao.getUsers().map { UserMapper.toModel(it) }
}
override fun getUserWithId(id: Long): Maybe<User> {
return usersDao.getUserWithId(id).map { UserMapper.toModel(it) }
}
override fun getUserWithIdNotScheduledForDeletion(id: Long): Maybe<User> {
return usersDao.getUserWithIdNotScheduledForDeletion(id).map { UserMapper.toModel(it) }
}
override fun getUserWithUserId(userId: String): Maybe<User> {
return usersDao.getUserWithUserId(userId).map { UserMapper.toModel(it) }
}
override fun getUsersWithoutUserId(userId: Long): Single<List<User>> {
return usersDao.getUsersWithoutUserId(userId).map { UserMapper.toModel(it) }
}
override fun getUsersScheduledForDeletion(): Single<List<User>> {
return usersDao.getUsersScheduledForDeletion().map { UserMapper.toModel(it) }
}
override fun getUsersNotScheduledForDeletion(): Single<List<User>> {
return usersDao.getUsersNotScheduledForDeletion().map { UserMapper.toModel(it) }
}
override fun getUserWithUsernameAndServer(username: String, server: String): Maybe<User> {
return usersDao.getUserWithUsernameAndServer(username, server).map { UserMapper.toModel(it) }
}
override fun updateUser(user: User): Int {
return usersDao.updateUser(UserMapper.toEntity(user))
}
override fun insertUser(user: User): Long {
return usersDao.saveUser(UserMapper.toEntity(user))
}
override fun setUserAsActiveWithId(id: Long): Single<Boolean> {
return Single.just(usersDao.setUserAsActiveWithId(id))
}
override fun deleteUserWithId(id: Long) {
usersDao.deleteUserWithId(id)
}
override fun setAnyUserAsActive(): Boolean {
return usersDao.setAnyUserAsActive()
}
override fun markUserForDeletion(id: Long): Boolean {
return usersDao.markUserForDeletion(id)
}
}

View file

@ -0,0 +1,72 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017-2020 Mario Danic <mario@lovelyhq.com>
*
* 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.nextcloud.talk.data.user.model
import android.os.Parcelable
import com.nextcloud.talk.models.ExternalSignalingServer
import com.nextcloud.talk.models.json.capabilities.Capabilities
import com.nextcloud.talk.models.json.push.PushConfigurationState
import com.nextcloud.talk.utils.ApiUtils
import kotlinx.android.parcel.Parcelize
import java.lang.Boolean.FALSE
@Parcelize
data class User(
var id: Long? = null,
var userId: String? = null,
var username: String? = null,
var baseUrl: String? = null,
var token: String? = null,
var displayName: String? = null,
var pushConfigurationState: PushConfigurationState? = null,
var capabilities: Capabilities? = null,
var clientCertificate: String? = null,
var externalSignalingServer: ExternalSignalingServer? = null,
var current: Boolean = FALSE,
var scheduledForDeletion: Boolean = FALSE,
) : Parcelable {
fun getMaxMessageLength(): Int {
return capabilities?.spreedCapability?.config?.get("chat")?.get("max-length")?.toInt()
?: DEFAULT_CHAT_MESSAGE_LENGTH
}
fun getAttachmentsConfig(key: String): Any? {
return capabilities?.spreedCapability?.config?.get("attachments")?.get(key)
}
fun canUserCreateGroupConversations(): Boolean {
val canCreateValue = capabilities?.spreedCapability?.config?.get("conversations")?.get("can-create")
canCreateValue?.let {
return it.toBoolean()
}
return true
}
fun getCredentials(): String = ApiUtils.getCredentials(username, token)
fun hasSpreedFeatureCapability(capabilityName: String): Boolean {
return capabilities?.spreedCapability?.features?.contains(capabilityName) ?: false
}
companion object {
const val DEFAULT_CHAT_MESSAGE_LENGTH: Int = 1000
}
}

View file

@ -0,0 +1,50 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* @author Mario Danic
* Copyright (C) 2022 Andy Scherzinger <infoi@andy-scherzinger.de>
* Copyright (C) 2017-2020 Mario Danic <mario@lovelyhq.com>
*
* 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.nextcloud.talk.data.user.model
import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.nextcloud.talk.models.ExternalSignalingServer
import com.nextcloud.talk.models.json.capabilities.Capabilities
import com.nextcloud.talk.models.json.push.PushConfigurationState
import kotlinx.android.parcel.Parcelize
import java.lang.Boolean.FALSE
@Parcelize
@Entity(tableName = "User")
data class UserEntity(
@PrimaryKey(autoGenerate = true) @ColumnInfo(name = "id") var id: Long = 0,
@ColumnInfo(name = "userId") var userId: String? = null,
@ColumnInfo(name = "username") var username: String? = null,
@ColumnInfo(name = "baseUrl") var baseUrl: String? = null,
@ColumnInfo(name = "token") var token: String? = null,
@ColumnInfo(name = "displayName") var displayName: String? = null,
@ColumnInfo(name = "pushConfigurationState") var pushConfigurationState: PushConfigurationState? = null,
@ColumnInfo(name = "capabilities") var capabilities: Capabilities? = null,
@ColumnInfo(name = "clientCertificate") var clientCertificate: String? = null,
@ColumnInfo(name = "externalSignalingServer") var externalSignalingServer: ExternalSignalingServer? = null,
@ColumnInfo(name = "current") var current: Boolean = FALSE,
@ColumnInfo(name = "scheduledForDeletion") var scheduledForDeletion: Boolean = FALSE,
) : Parcelable

View file

@ -27,6 +27,10 @@ import io.requery.Persistable;
import java.io.Serializable;
/**
* Legacy arbitrary storage entity, please migrate to {@link com.nextcloud.talk.data.storage.model.ArbitraryStorage}.
*/
@Deprecated
@Entity
public interface ArbitraryStorage extends Parcelable, Persistable, Serializable {
@Key

View file

@ -33,6 +33,10 @@ import java.util.Map;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* Deprecated, please migrate to {@link com.nextcloud.talk.utils.database.user.CapabilitiesUtilNew}.
*/
@Deprecated
public abstract class CapabilitiesUtil {
private static final String TAG = CapabilitiesUtil.class.getSimpleName();
@ -123,6 +127,7 @@ public abstract class CapabilitiesUtil {
return 1000;
}
@Deprecated
public static boolean isPhoneBookIntegrationAvailable(@Nullable UserEntity user) {
if (user != null && user.getCapabilities() != null) {
try {
@ -156,6 +161,7 @@ public abstract class CapabilitiesUtil {
return false;
}
@Deprecated
public static boolean isReadStatusPrivate(@Nullable UserEntity user) {
if (user != null && user.getCapabilities() != null) {
try {
@ -280,6 +286,7 @@ public abstract class CapabilitiesUtil {
return false;
}
@Deprecated
private static Capabilities parseUserCapabilities(@NonNull final UserEntity user) throws IOException {
return LoganSquare.parse(user.getCapabilities(), Capabilities.class);
}

View file

@ -30,6 +30,10 @@ import io.requery.Generated;
import io.requery.Key;
import io.requery.Persistable;
/**
* Legacy user entity, please migrate to {@link com.nextcloud.talk.data.user.model.User}.
*/
@Deprecated
@Entity
public interface User extends Parcelable, Persistable, Serializable {
String TAG = "UserEntity";

View file

@ -25,9 +25,11 @@ import android.os.Parcelable
import com.bluelinelabs.logansquare.annotation.JsonField
import com.bluelinelabs.logansquare.annotation.JsonObject
import kotlinx.android.parcel.Parcelize
import kotlinx.serialization.Serializable
@Parcelize
@JsonObject
@Serializable
data class NotificationsCapability(
@JsonField(name = ["ocs-endpoints"])
var features: List<String>?

View file

@ -25,9 +25,11 @@ import android.os.Parcelable
import com.bluelinelabs.logansquare.annotation.JsonField
import com.bluelinelabs.logansquare.annotation.JsonObject
import kotlinx.android.parcel.Parcelize
import kotlinx.serialization.Serializable
@Parcelize
@JsonObject
@Serializable
data class ProvisioningCapability(
@JsonField(name = ["AccountPropertyScopesVersion"])
var accountPropertyScopesVersion: Int?

View file

@ -25,9 +25,11 @@ import android.os.Parcelable
import com.bluelinelabs.logansquare.annotation.JsonField
import com.bluelinelabs.logansquare.annotation.JsonObject
import kotlinx.android.parcel.Parcelize
import kotlinx.serialization.Serializable
@Parcelize
@JsonObject
@Serializable
data class SpreedCapability(
@JsonField(name = ["features"])
var features: List<String>?,

View file

@ -25,9 +25,11 @@ import android.os.Parcelable
import com.bluelinelabs.logansquare.annotation.JsonField
import com.bluelinelabs.logansquare.annotation.JsonObject
import kotlinx.android.parcel.Parcelize
import kotlinx.serialization.Serializable
@Parcelize
@JsonObject
@Serializable
data class ThemingCapability(
@JsonField(name = ["name"])
var name: String?,

View file

@ -25,9 +25,11 @@ import android.os.Parcelable
import com.bluelinelabs.logansquare.annotation.JsonField
import com.bluelinelabs.logansquare.annotation.JsonObject
import kotlinx.android.parcel.Parcelize
import kotlinx.serialization.Serializable
@Parcelize
@JsonObject
@Serializable
data class UserStatusCapability(
@JsonField(name = ["enabled"])
var enabled: Boolean,

View file

@ -25,9 +25,11 @@ import android.os.Parcelable
import com.bluelinelabs.logansquare.annotation.JsonField
import com.bluelinelabs.logansquare.annotation.JsonObject
import kotlinx.android.parcel.Parcelize
import kotlinx.serialization.Serializable
@Parcelize
@JsonObject
@Serializable
data class IceServer(
@Deprecated("")
@JsonField(name = ["url"])

View file

@ -25,10 +25,12 @@ import android.os.Parcelable
import com.bluelinelabs.logansquare.annotation.JsonField
import com.bluelinelabs.logansquare.annotation.JsonObject
import kotlinx.android.parcel.Parcelize
import kotlinx.serialization.Serializable
@Parcelize
@JsonObject
data class Settings(
@Serializable
data class SignalingSettings(
@JsonField(name = ["stunservers"])
var stunServers: List<IceServer>? = null,
@JsonField(name = ["turnservers"])

View file

@ -33,7 +33,7 @@ data class SignalingSettingsOcs(
@JsonField(name = ["meta"])
var meta: GenericMeta?,
@JsonField(name = ["data"])
var settings: Settings? = null
var settings: SignalingSettings? = null
) : Parcelable {
// This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject'
constructor() : this(null, null)

View file

@ -0,0 +1,226 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <info@andy-scherzinger.de>
* Copyright (C) 2017 Mario Danic <mario@lovelyhq.com>
*
* 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.nextcloud.talk.users
import android.text.TextUtils
import com.bluelinelabs.logansquare.LoganSquare
import com.nextcloud.talk.data.user.UsersRepository
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.ExternalSignalingServer
import com.nextcloud.talk.models.json.capabilities.Capabilities
import com.nextcloud.talk.models.json.push.PushConfigurationState
import com.nextcloud.talk.utils.database.user.CurrentUserProviderNew
import io.reactivex.Maybe
import io.reactivex.Single
import java.lang.Boolean.TRUE
@Suppress("TooManyFunctions")
class UserManager internal constructor(private val userRepository: UsersRepository) : CurrentUserProviderNew {
val users: Single<List<User>>
get() = userRepository.getUsers()
val usersScheduledForDeletion: Single<List<User>>
get() = userRepository.getUsersScheduledForDeletion()
private fun setAnyUserAndSetAsActive(): Single<User> {
val results = userRepository.getUsersNotScheduledForDeletion()
return results.map { users ->
users
.firstOrNull()
?.apply {
current = true
}.also { user ->
userRepository.updateUser(user!!)
}
}
}
override val currentUser: Maybe<User>
get() {
return userRepository.getActiveUser()
}
fun deleteUser(internalId: Long) {
userRepository.deleteUserWithId(internalId)
}
fun deleteUserWithId(internalId: Long) {
userRepository.deleteUserWithId(internalId)
}
fun getUserById(userId: String): Maybe<User> {
return userRepository.getUserWithUserId(userId)
}
fun getUserWithId(id: Long): Maybe<User> {
return userRepository.getUserWithId(id)
}
fun disableAllUsersWithoutId(userId: Long): Single<Int> {
val results = userRepository.getUsersWithoutUserId(userId)
return results.map { users ->
var count = 0
if (users.isNotEmpty()) {
for (entity in users) {
entity.current = false
userRepository.updateUser(entity)
count++
}
}
count
}
}
fun checkIfUserIsScheduledForDeletion(username: String, server: String): Maybe<Boolean> {
return userRepository.getUserWithUsernameAndServer(username, server).map { it.scheduledForDeletion }
}
fun getUserWithInternalId(id: Long): Maybe<User> {
return userRepository.getUserWithIdNotScheduledForDeletion(id)
}
fun getIfUserWithUsernameAndServer(username: String, server: String): Maybe<Boolean> {
return userRepository.getUserWithUsernameAndServer(username, server).map { TRUE }
}
fun scheduleUserForDeletionWithId(id: Long): Single<Boolean> {
return userRepository.getUserWithId(id).map { user ->
user.scheduledForDeletion = true
user.current = false
userRepository.updateUser(user)
}
.toSingle()
.flatMap {
setAnyUserAndSetAsActive()
}.map { TRUE }
}
fun createOrUpdateUser(
username: String?,
userAttributes: UserAttributes,
): Maybe<User> {
val userMaybe: Maybe<User> = if (userAttributes.id != null) {
userRepository.getUserWithId(userAttributes.id)
} else if (username != null && userAttributes.serverUrl != null) {
userRepository.getUserWithUsernameAndServer(username, userAttributes.serverUrl)
} else {
Maybe.empty()
}
return userMaybe
.map { user: User? ->
when (user) {
null -> createUser(
username,
userAttributes
)
else -> {
updateUserData(
user,
userAttributes
)
user
}
}
}
.switchIfEmpty(Maybe.just(createUser(username, userAttributes)))
.map { user ->
userRepository.insertUser(user)
}
.flatMap { id ->
userRepository.getUserWithId(id)
}
}
fun getUserWithUsernameAndServer(username: String, server: String): Maybe<User> {
return userRepository.getUserWithUsernameAndServer(username, server)
}
private fun updateUserData(user: User, userAttributes: UserAttributes) {
user.userId = userAttributes.userId
user.token = userAttributes.token
user.displayName = userAttributes.displayName
if (userAttributes.pushConfigurationState != null) {
user.pushConfigurationState = LoganSquare
.parse(userAttributes.pushConfigurationState, PushConfigurationState::class.java)
}
if (userAttributes.capabilities != null) {
user.capabilities = LoganSquare
.parse(userAttributes.capabilities, Capabilities::class.java)
}
user.clientCertificate = userAttributes.certificateAlias
if (userAttributes.externalSignalingServer != null) {
user.externalSignalingServer = LoganSquare
.parse(userAttributes.externalSignalingServer, ExternalSignalingServer::class.java)
}
user.current = userAttributes.currentUser == true
}
private fun createUser(username: String?, userAttributes: UserAttributes): User {
val user = User()
user.baseUrl = userAttributes.serverUrl
user.username = username
user.token = userAttributes.token
if (!TextUtils.isEmpty(userAttributes.displayName)) {
user.displayName = userAttributes.displayName
}
if (userAttributes.pushConfigurationState != null) {
user.pushConfigurationState = LoganSquare
.parse(userAttributes.pushConfigurationState, PushConfigurationState::class.java)
}
if (!TextUtils.isEmpty(userAttributes.userId)) {
user.userId = userAttributes.userId
}
if (!TextUtils.isEmpty(userAttributes.capabilities)) {
user.capabilities = LoganSquare.parse(userAttributes.capabilities, Capabilities::class.java)
}
if (!TextUtils.isEmpty(userAttributes.certificateAlias)) {
user.clientCertificate = userAttributes.certificateAlias
}
if (!TextUtils.isEmpty(userAttributes.externalSignalingServer)) {
user.externalSignalingServer = LoganSquare
.parse(userAttributes.externalSignalingServer, ExternalSignalingServer::class.java)
}
user.current = true
return user
}
companion object {
const val TAG = "UserManager"
}
data class UserAttributes(
val id: Long?,
val serverUrl: String?,
val currentUser: Boolean?,
val userId: String?,
val token: String?,
val displayName: String?,
val pushConfigurationState: String?,
val capabilities: String?,
val certificateAlias: String?,
val externalSignalingServer: String?
)
}

View file

@ -28,6 +28,7 @@ import android.util.Log;
import com.nextcloud.talk.BuildConfig;
import com.nextcloud.talk.R;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.data.user.model.User;
import com.nextcloud.talk.models.RetrofitBucket;
import com.nextcloud.talk.models.database.CapabilitiesUtil;
import com.nextcloud.talk.models.database.UserEntity;
@ -123,7 +124,7 @@ public class ApiUtils {
return getConversationApiVersion(capabilities, versions);
}
public static int getConversationApiVersion(UserEntity user, int[] versions) throws NoSupportedApiException {
public static int getConversationApiVersion(User user, int[] versions) throws NoSupportedApiException {
boolean hasApiV4 = false;
for (int version : versions) {
hasApiV4 |= version == APIv4;
@ -135,18 +136,18 @@ public class ApiUtils {
}
for (int version : versions) {
if (CapabilitiesUtil.hasSpreedFeatureCapability(user, "conversation-v" + version)) {
if (user.hasSpreedFeatureCapability("conversation-v" + version)) {
return version;
}
// Fallback for old API versions
if ((version == APIv1 || version == APIv2)) {
if (CapabilitiesUtil.hasSpreedFeatureCapability(user, "conversation-v2")) {
if (user.hasSpreedFeatureCapability("conversation-v2")) {
return version;
}
if (version == APIv1 &&
CapabilitiesUtil.hasSpreedFeatureCapability(user, "mention-flag") &&
!CapabilitiesUtil.hasSpreedFeatureCapability(user, "conversation-v4")) {
user.hasSpreedFeatureCapability("mention-flag") &&
!user.hasSpreedFeatureCapability("conversation-v4")) {
return version;
}
}
@ -154,6 +155,11 @@ public class ApiUtils {
throw new NoSupportedApiException();
}
@Deprecated
public static int getConversationApiVersion(UserEntity user, int[] versions) throws NoSupportedApiException {
return getConversationApiVersion(LegacyUserEntityMapper.toModel(user), versions);
}
public static int getSignalingApiVersion(UserEntity user, int[] versions) throws NoSupportedApiException {
for (int version : versions) {
if (CapabilitiesUtil.hasSpreedFeatureCapability(user, "signaling-v" + version)) {

View file

@ -79,6 +79,7 @@ import com.facebook.widget.text.span.BetterImageSpan;
import com.google.android.material.chip.ChipDrawable;
import com.nextcloud.talk.R;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.data.user.model.User;
import com.nextcloud.talk.events.UserMentionClickEvent;
import com.nextcloud.talk.models.database.UserEntity;
import com.nextcloud.talk.utils.text.Spans;
@ -92,6 +93,7 @@ import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -489,9 +491,13 @@ public class DisplayUtils {
* @param color the color
* @return true if primaryColor is lighter than MAX_LIGHTNESS
*/
@SuppressWarnings("correctness")
public static boolean lightTheme(int color) {
float[] hsl = colorToHSL(color);
// spotbugs dislikes fixed index access
// which is enforced by having such an
// array from Android-API itself
return hsl[INDEX_LUMINATION] >= MAX_LIGHTNESS;
}
@ -565,7 +571,12 @@ public class DisplayUtils {
}
}
@Deprecated
public static void loadAvatarImage(UserEntity user, SimpleDraweeView avatarImageView, boolean deleteCache) {
loadAvatarImage(Objects.requireNonNull(LegacyUserEntityMapper.toModel(user)), avatarImageView, deleteCache);
}
public static void loadAvatarImage(User user, SimpleDraweeView avatarImageView, boolean deleteCache) {
String avatarId;
if (!TextUtils.isEmpty(user.getUserId())) {
avatarId = user.getUserId();

View file

@ -0,0 +1,56 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* Copyright (C) 2022 Andy Scherzinger <infoi@andy-scherzinger.de>
*
* model 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.
*
* model 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 model program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils
import com.bluelinelabs.logansquare.LoganSquare
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.ExternalSignalingServer
import com.nextcloud.talk.models.database.UserEntity
import com.nextcloud.talk.models.json.capabilities.Capabilities
import com.nextcloud.talk.models.json.push.PushConfigurationState
object LegacyUserEntityMapper {
fun toModel(entities: List<UserEntity?>?): List<User> {
return entities?.map { user: UserEntity? ->
toModel(user)!!
} ?: emptyList()
}
@JvmStatic
fun toModel(entity: UserEntity?): User? {
return entity?.let {
User(
entity.id,
entity.userId,
entity.username,
entity.baseUrl,
entity.token,
entity.displayName,
entity.pushConfigurationState?.let { LoganSquare.parse(it, PushConfigurationState::class.java) },
entity.capabilities?.let { LoganSquare.parse(it, Capabilities::class.java) },
entity.clientCertificate,
entity.externalSignalingServer?.let { LoganSquare.parse(it, ExternalSignalingServer::class.java) },
entity.current,
entity.scheduledForDeletion
)
}
}
}

View file

@ -21,7 +21,10 @@ package com.nextcloud.talk.utils.database.arbitrarystorage;
import autodagger.AutoInjector;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.arbitrarystorage.ArbitraryStorageManager;
import com.nextcloud.talk.dagger.modules.DatabaseModule;
import com.nextcloud.talk.data.storage.ArbitraryStoragesRepository;
import dagger.Module;
import dagger.Provides;
import io.requery.Persistable;
@ -41,4 +44,9 @@ public class ArbitraryStorageModule {
public ArbitraryStorageUtils provideArbitraryStorageUtils(ReactiveEntityStore<Persistable> dataStore) {
return new ArbitraryStorageUtils(dataStore);
}
@Provides
public ArbitraryStorageManager provideArbitraryStorageManager(ArbitraryStoragesRepository repository) {
return new ArbitraryStorageManager(repository);
}
}

View file

@ -20,8 +20,10 @@
package com.nextcloud.talk.utils.database.arbitrarystorage;
import androidx.annotation.Nullable;
import com.nextcloud.talk.models.database.ArbitraryStorage;
import com.nextcloud.talk.models.database.ArbitraryStorageEntity;
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import io.requery.Persistable;
@ -29,6 +31,10 @@ import io.requery.query.Result;
import io.requery.reactivex.ReactiveEntityStore;
import io.requery.reactivex.ReactiveScalar;
/**
* @deprecated use {@link com.nextcloud.talk.arbitrarystorage.ArbitraryStorageManager} instead.
*/
@Deprecated
public class ArbitraryStorageUtils {
private ReactiveEntityStore<Persistable> dataStore;

View file

@ -0,0 +1,154 @@
/*
* Nextcloud Talk application
*
* @author Andy Scherzinger
* @author Mario Danic
* Copyright (C) 2021 Andy Scherzinger (info@andy-scherzinger.de)
* Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
*
* 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.nextcloud.talk.utils.database.user
import com.nextcloud.talk.data.user.model.User
@Suppress("TooManyFunctions")
object CapabilitiesUtilNew {
fun hasNotificationsCapability(user: User, capabilityName: String): Boolean {
return user.capabilities?.spreedCapability?.features?.contains(capabilityName) == true
}
fun hasExternalCapability(user: User, capabilityName: String?): Boolean {
if (user.capabilities?.externalCapability?.containsKey("v1") == true) {
return user.capabilities!!.externalCapability!!["v1"]?.contains(capabilityName!!) == true
}
return false
}
fun isServerEOL(user: User): Boolean {
// Capability is available since Talk 4 => Nextcloud 14 => Autmn 2018
return !hasSpreedFeatureCapability(user, "no-ping")
}
fun isServerAlmostEOL(user: User): Boolean {
// Capability is available since Talk 8 => Nextcloud 18 => January 2020
return !hasSpreedFeatureCapability(user, "chat-replies")
}
fun canSetChatReadMarker(user: User): Boolean {
return hasSpreedFeatureCapability(user, "chat-read-marker")
}
fun hasSpreedFeatureCapability(user: User, capabilityName: String): Boolean {
if (user.capabilities?.spreedCapability?.features != null) {
return user.capabilities!!.spreedCapability!!.features!!.contains(capabilityName)
}
return false
}
fun getMessageMaxLength(user: User): Int {
val capabilities = user.capabilities!!
if (user.capabilities?.spreedCapability?.config?.containsKey("chat") == true) {
val chatConfigHashMap = user.capabilities!!.spreedCapability!!.config!!["chat"]
if (chatConfigHashMap?.containsKey("max-length") == true) {
val chatSize = chatConfigHashMap["max-length"]!!.toInt()
return if (chatSize > 0) {
chatSize
} else {
DEFAULT_CHAT_SIZE
}
}
}
return DEFAULT_CHAT_SIZE
}
fun isPhoneBookIntegrationAvailable(user: User): Boolean {
return user.capabilities?.spreedCapability?.features?.contains("phonebook-search") == true
}
fun isReadStatusAvailable(user: User): Boolean {
if (user.capabilities?.spreedCapability?.config?.containsKey("chat") == true) {
val map: Map<String, String>? = user.capabilities!!.spreedCapability!!.config!!["chat"]
return map != null && map.containsKey("read-privacy")
}
return false
}
fun isReadStatusPrivate(user: User): Boolean {
if (user.capabilities?.spreedCapability?.config?.containsKey("chat") == true) {
val map = user.capabilities!!.spreedCapability!!.config!!["chat"]
if (map?.containsKey("read-privacy") == true) {
return map["read-privacy"]!!.toInt() == 1
}
}
return false
}
fun isUserStatusAvailable(user: User): Boolean {
return user.capabilities?.userStatusCapability?.enabled == true &&
user.capabilities?.userStatusCapability?.supportsEmoji == true
}
fun getAttachmentFolder(user: User): String? {
if (user.capabilities?.spreedCapability?.config?.containsKey("attachments") == true) {
val map = user.capabilities!!.spreedCapability!!.config!!["attachments"]
if (map?.containsKey("folder") == true) {
return map["folder"]
}
}
return "/Talk"
}
fun getServerName(user: User): String? {
if (user.capabilities?.themingCapability != null) {
return user.capabilities!!.themingCapability!!.name
}
return ""
}
// TODO later avatar can also be checked via user fields, for now it is in Talk capability
fun isAvatarEndpointAvailable(user: User): Boolean {
return user.capabilities?.spreedCapability?.features?.contains("temp-user-avatar-api") == true
}
fun canEditScopes(user: User): Boolean {
return user.capabilities?.provisioningCapability?.accountPropertyScopesVersion != null &&
user.capabilities!!.provisioningCapability!!.accountPropertyScopesVersion!! > 1
}
fun isAbleToCall(user: User): Boolean {
if (user.capabilities != null) {
val capabilities = user.capabilities
return if (
capabilities?.spreedCapability?.config?.containsKey("call") == true &&
capabilities.spreedCapability!!.config!!["call"] != null &&
capabilities.spreedCapability!!.config!!["call"]!!.containsKey("enabled")
) {
java.lang.Boolean.parseBoolean(capabilities.spreedCapability!!.config!!["call"]!!["enabled"])
} else {
// older nextcloud versions without the capability can't disable the calls
true
}
}
return false
}
fun isUnifiedSearchAvailable(user: User): Boolean {
return hasSpreedFeatureCapability(user, "unified-search")
}
const val DEFAULT_CHAT_SIZE = 1000
}

View file

@ -0,0 +1,28 @@
/*
* Nextcloud Talk application
*
* @author Álvaro Brey
* Copyright (C) 2022 Álvaro Brey
* Copyright (C) 2022 Nextcloud 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 <https://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils.database.user
import com.nextcloud.talk.data.user.model.User
import io.reactivex.Maybe
interface CurrentUserProviderNew {
val currentUser: Maybe<User>
}

View file

@ -20,6 +20,8 @@
package com.nextcloud.talk.utils.database.user
import com.nextcloud.talk.dagger.modules.DatabaseModule
import com.nextcloud.talk.data.user.UsersRepository
import com.nextcloud.talk.users.UserManager
import dagger.Binds
import dagger.Module
import dagger.Provides
@ -32,10 +34,18 @@ abstract class UserModule {
@Binds
abstract fun bindCurrentUserProvider(userUtils: UserUtils): CurrentUserProvider
@Binds
abstract fun bindCurrentUserProviderNew(userManager: UserManager): CurrentUserProviderNew
companion object {
@Provides
fun provideUserUtils(dataStore: ReactiveEntityStore<Persistable?>?): UserUtils {
return UserUtils(dataStore)
}
@Provides
fun provideUserManager(userRepository: UsersRepository): UserManager {
return UserManager(userRepository)
}
}
}

View file

@ -35,6 +35,10 @@ import io.requery.Persistable;
import io.requery.query.Result;
import io.requery.reactivex.ReactiveEntityStore;
/**
* @deprecated use {@link com.nextcloud.talk.users.UserManager} instead.
*/
@Deprecated
public class UserUtils implements CurrentUserProvider {
private ReactiveEntityStore<Persistable> dataStore;
@ -83,7 +87,8 @@ public class UserUtils implements CurrentUserProvider {
}
@Override
public @Nullable UserEntity getCurrentUser() {
public @Nullable
UserEntity getCurrentUser() {
Result findUserQueryResult = dataStore.select(User.class).where(UserEntity.CURRENT.eq(Boolean.TRUE)
.and(UserEntity.SCHEDULED_FOR_DELETION.notEqual(Boolean.TRUE)))
.limit(1).get();

View file

@ -284,10 +284,17 @@ public interface AppPreferences {
@KeyByString("db_cypher_v4_upgrade")
@DefaultValue(R.bool.value_true)
boolean getIsDbCypherToUpgrade();
boolean isDbCypherToUpgrade();
@KeyByString("db_cypher_v4_upgrade")
void setIsDbCypherToUpgrade(boolean value);
void setDbCypherToUpgrade(boolean value);
@KeyByString("db_room_migrated")
@DefaultValue(R.bool.value_false)
boolean getIsDbRoomMigrated();
@KeyByString("db_room_migrated")
void setIsDbRoomMigrated(boolean value);
@KeyByResource(R.string.nc_settings_phone_book_integration_key)
@RegisterChangeListenerMethod

View file

@ -26,7 +26,6 @@ import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import autodagger.AutoInjector;
import com.nextcloud.talk.api.NcApi;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.models.database.ArbitraryStorageEntity;
@ -35,21 +34,20 @@ import com.nextcloud.talk.models.database.UserEntity;
import com.nextcloud.talk.models.json.generic.GenericOverall;
import com.nextcloud.talk.utils.ApiUtils;
import com.nextcloud.talk.utils.database.arbitrarystorage.ArbitraryStorageUtils;
import com.nextcloud.talk.utils.database.user.UserUtils;
import com.yarolegovich.mp.io.StorageModule;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
import javax.inject.Inject;
import autodagger.AutoInjector;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import javax.inject.Inject;
import java.util.Collections;
import java.util.Set;
@AutoInjector(NextcloudTalkApplication.class)
public class DatabaseStorageModule implements StorageModule {
private static final String TAG = "DatabaseStorageModule";

View file

@ -36,6 +36,7 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:7.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
classpath "org.jetbrains.kotlin:kotlin-serialization:${kotlinVersion}"
classpath 'gradle.plugin.com.github.spotbugs.snom:spotbugs-gradle-plugin:4.7.5'
classpath "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.20.0"
classpath "org.jlleitschuh.gradle:ktlint-gradle:10.3.0"

View file

@ -1 +1 @@
167
166

View file

@ -34,6 +34,16 @@
</Match>
<Match>
<Class name="~.*\$\$Parcelable.*" />
</Match>
<!-- Room code is autogenerated. Exclude it from Check. -->
<Match>
<Class name="~.*\.TalkDatabase_Impl.*" />
</Match>
<Match>
<Class name="~.*\.UsersDao_Impl.*" />
</Match>
<Match>
<Class name="~.*\.ArbitraryStoragesDao_Impl.*" />
</Match>
<!-- JSON/Data classes with generated accessor methods -->
<Match>