Skip to content

Commit

Permalink
Add accounts to DB; reference from services; make AccountRepository t…
Browse files Browse the repository at this point in the history
…rue SSOT for accounts
  • Loading branch information
rfc2822 committed Dec 30, 2024
1 parent 77a795d commit ea25201
Show file tree
Hide file tree
Showing 21 changed files with 926 additions and 87 deletions.
723 changes: 723 additions & 0 deletions app/schemas/at.bitfire.davdroid.db.AppDatabase/17.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class DavCollectionRepositoryTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)

@Inject
lateinit var accountRepository: AccountRepository

@Inject
lateinit var accountSettingsFactory: AccountSettings.Factory

Expand Down Expand Up @@ -65,6 +68,7 @@ class DavCollectionRepositoryTest {
)
val testObserver = mockk<DavCollectionRepository.OnChangeListener>(relaxed = true)
val collectionRepository = DavCollectionRepository(
accountRepository,
accountSettingsFactory,
context,
db,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.content.ContentProviderClient
import android.content.ContentUris
import android.content.Context
import android.provider.ContactsContract
import at.bitfire.davdroid.repository.AccountRepository
import at.bitfire.davdroid.repository.DavCollectionRepository
import at.bitfire.davdroid.repository.DavServiceRepository
import at.bitfire.davdroid.settings.AccountSettings
Expand All @@ -28,6 +29,7 @@ class LocalTestAddressBook @AssistedInject constructor(
@Assisted account: Account,
@Assisted provider: ContentProviderClient,
@Assisted override val groupMethod: GroupMethod,
accountRepository: AccountRepository,
accountSettingsFactory: AccountSettings.Factory,
collectionRepository: DavCollectionRepository,
@ApplicationContext context: Context,
Expand All @@ -38,6 +40,7 @@ class LocalTestAddressBook @AssistedInject constructor(
account = account,
_addressBookAccount = ACCOUNT,
provider = provider,
accountRepository = accountRepository,
accountSettingsFactory = accountSettingsFactory,
collectionRepository = collectionRepository,
context = context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.hilt.work.HiltWorkerFactory
import androidx.work.WorkInfo
import androidx.work.WorkManager
import at.bitfire.davdroid.TestUtils
import at.bitfire.davdroid.repository.AccountRepository
import at.bitfire.davdroid.repository.DavCollectionRepository
import at.bitfire.davdroid.repository.DavServiceRepository
import at.bitfire.davdroid.settings.AccountSettings
Expand Down Expand Up @@ -49,6 +50,9 @@ class SyncAdapterServicesTest {

lateinit var account: Account

@Inject
lateinit var accountRepository: AccountRepository

@Inject
lateinit var accountSettingsFactory: AccountSettings.Factory

Expand Down Expand Up @@ -98,6 +102,7 @@ class SyncAdapterServicesTest {
syncWorkerManager: SyncWorkerManager
): SyncAdapterService.SyncAdapter =
SyncAdapterService.SyncAdapter(
accountRepository = accountRepository,
accountSettingsFactory = accountSettingsFactory,
collectionRepository = collectionRepository,
serviceRepository = serviceRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ class PeriodicSyncWorkerTest {
// Run PeriodicSyncWorker as TestWorker
val inputData = workDataOf(
BaseSyncWorker.INPUT_DATA_TYPE to SyncDataType.EVENTS.toString(),
BaseSyncWorker.INPUT_ACCOUNT_NAME to invalidAccount.name,
BaseSyncWorker.INPUT_ACCOUNT_TYPE to invalidAccount.type
BaseSyncWorker.INPUT_ACCOUNT_NAME to invalidAccount.name
)

// mock WorkManager to observe cancellation call
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/kotlin/at/bitfire/davdroid/db/Account.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
*/

package at.bitfire.davdroid.db

import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey

/**
* Represents an account, which again has services (CalDAV/CardDAV).
*/
@Entity(
tableName = "account",
indices = [
Index("name", unique = true)
]
)
data class Account(
@PrimaryKey(autoGenerate = true)
val id: Long = 0L,

val name: String
)
25 changes: 25 additions & 0 deletions app/src/main/kotlin/at/bitfire/davdroid/db/AccountDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright © All Contributors. See LICENSE and AUTHORS in the root directory for details.
*/

package at.bitfire.davdroid.db

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query

@Dao
interface AccountDao {

@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insertOrIgnore(account: Account)

@Query("DELETE FROM account WHERE name=:name")
fun deleteByName(name: String)

@Query("UPDATE account SET name=:newName WHERE name=:oldName")
fun rename(oldName: String, newName: String)


}
7 changes: 5 additions & 2 deletions app/src/main/kotlin/at/bitfire/davdroid/db/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,23 @@ import javax.inject.Singleton

@Suppress("ClassName")
@Database(entities = [
Account::class,
Service::class,
HomeSet::class,
Collection::class,
Principal::class,
SyncStats::class,
WebDavDocument::class,
WebDavMount::class
], exportSchema = true, version = 16, autoMigrations = [
], exportSchema = true, version = 17, autoMigrations = [
AutoMigration(from = 9, to = 10),
AutoMigration(from = 10, to = 11),
AutoMigration(from = 11, to = 12, spec = AppDatabase.AutoMigration11_12::class),
AutoMigration(from = 12, to = 13),
AutoMigration(from = 13, to = 14),
AutoMigration(from = 14, to = 15),
AutoMigration(from = 15, to = 16, spec = AppDatabase.AutoMigration15_16::class)
AutoMigration(from = 15, to = 16, spec = AppDatabase.AutoMigration15_16::class),
AutoMigration(from = 16, to = 17)
])
@TypeConverters(Converters::class)
abstract class AppDatabase: RoomDatabase() {
Expand Down Expand Up @@ -257,6 +259,7 @@ abstract class AppDatabase: RoomDatabase() {

// DAOs

abstract fun accountDao(): AccountDao
abstract fun serviceDao(): ServiceDao
abstract fun homeSetDao(): HomeSetDao
abstract fun collectionDao(): CollectionDao
Expand Down
23 changes: 17 additions & 6 deletions app/src/main/kotlin/at/bitfire/davdroid/db/Service.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

package at.bitfire.davdroid.db

import androidx.annotation.StringDef
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
import okhttp3.HttpUrl
Expand All @@ -14,22 +16,31 @@ import okhttp3.HttpUrl
*
* Services represent accounts and are unique. They are of type CardDAV or CalDAV and may have an associated principal.
*/
@Entity(tableName = "service",
indices = [
// only one service per type and account
Index("accountName", "type", unique = true)
])
@Entity(
tableName = "service",
foreignKeys = [
ForeignKey(entity = Account::class, parentColumns = ["name"], childColumns = ["accountName"], onDelete = ForeignKey.CASCADE, onUpdate = ForeignKey.CASCADE)
],
indices = [
// only one service per type and account
Index("accountName", "type", unique = true)
]
)
data class Service(
@PrimaryKey(autoGenerate = true)
var id: Long,

var accountName: String,
var type: String,
@ServiceTypeDef var type: String,

var principal: HttpUrl?
) {

companion object {

@StringDef(TYPE_CALDAV, TYPE_CARDDAV)
annotation class ServiceTypeDef

const val TYPE_CALDAV = "caldav"
const val TYPE_CARDDAV = "carddav"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import at.bitfire.dav4jvm.XmlUtils
import at.bitfire.dav4jvm.XmlUtils.insertTag
import at.bitfire.dav4jvm.exception.DavException
import at.bitfire.dav4jvm.property.push.NS_WEBDAV_PUSH
import at.bitfire.davdroid.R
import at.bitfire.davdroid.db.Collection
import at.bitfire.davdroid.network.HttpClient
import at.bitfire.davdroid.repository.AccountRepository
import at.bitfire.davdroid.repository.DavCollectionRepository
import at.bitfire.davdroid.repository.DavServiceRepository
import at.bitfire.davdroid.repository.PreferenceRepository
Expand Down Expand Up @@ -47,6 +47,7 @@ import java.util.logging.Logger
class PushRegistrationWorker @AssistedInject constructor(
@Assisted context: Context,
@Assisted workerParameters: WorkerParameters,
private val accountRepository: AccountRepository,
private val accountSettingsFactory: AccountSettings.Factory,
private val collectionRepository: DavCollectionRepository,
private val logger: Logger,
Expand Down Expand Up @@ -136,7 +137,7 @@ class PushRegistrationWorker @AssistedInject constructor(
// no existing subscription or expiring soon
logger.info("Registering push for ${collection.url}")
serviceRepository.get(collection.serviceId)?.let { service ->
val account = Account(service.accountName, applicationContext.getString(R.string.account_type))
val account = accountRepository.fromName(service.accountName)
try {
registerPushSubscription(collection, account, endpoint)
} catch (e: DavException) {
Expand Down Expand Up @@ -182,7 +183,7 @@ class PushRegistrationWorker @AssistedInject constructor(
logger.info("Unregistering push for ${collection.url}")
collection.pushSubscription?.toHttpUrlOrNull()?.let { url ->
serviceRepository.get(collection.serviceId)?.let { service ->
val account = Account(service.accountName, applicationContext.getString(R.string.account_type))
val account = accountRepository.fromName(service.accountName)
unregisterPushSubscription(collection, account, url)
}
}
Expand Down
Loading

0 comments on commit ea25201

Please sign in to comment.