Skip to content

Commit

Permalink
Merge pull request #117 from Infomaniak/feat-save-email-token
Browse files Browse the repository at this point in the history
feat: Save EmailToken in AppSettings
  • Loading branch information
sirambd authored Dec 12, 2024
2 parents f9fe646 + 6095b2e commit f8d5969
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Infomaniak SwissTransfer - Multiplatform
* Copyright (C) 2024 Infomaniak Network SA
*
* 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.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings

interface EmailToken {
val email: String
val token: String
}
13 changes: 13 additions & 0 deletions STCore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ centralized access point to orchestrate transfer operations.
| Property | fileManager | A manager used to orchestrate Files operations. |
| Property | accountManager | A manager used to orchestrate Accounts operations. |
| Property | uploadManager | A manager used to orchestrate Uploads operations. |
| Property | emailTokensManager | A manager used to orchestrate EmailTokens operations. |
| Property | sharedApiUrlCreator | An utils to help use shared routes |

### Details of Properties and Methods
Expand Down Expand Up @@ -112,6 +113,18 @@ centralized access point to orchestrate transfer operations.
// Use the uploadManager to orchestrate Uploads
```

- **Type**: `EmailTokensManager`
- **Description**:
- `emailTokensManager` is a lazily initialized property that provides a manager to orchestrate all EmailTokens operations. It
uses `EmailTokensController` to do operations.

- **Usage Example**:
```kotlin
val core = SwissTransferInjection(userAgent = "user_agent")
val emailTokensManager = core.emailTokensManager
// Use the EmailTokensManager to orchestrate EmailTokens
```

#### Property: `sharedApiUrlCreator`

- **Type**: `SharedApiUrlCreator`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
package com.infomaniak.multiplatform_swisstransfer

import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider
import com.infomaniak.multiplatform_swisstransfer.database.controllers.AppSettingsController
import com.infomaniak.multiplatform_swisstransfer.database.controllers.FileController
import com.infomaniak.multiplatform_swisstransfer.database.controllers.TransferController
import com.infomaniak.multiplatform_swisstransfer.database.controllers.UploadController
import com.infomaniak.multiplatform_swisstransfer.database.controllers.*
import com.infomaniak.multiplatform_swisstransfer.managers.*
import com.infomaniak.multiplatform_swisstransfer.network.ApiClientProvider
import com.infomaniak.multiplatform_swisstransfer.network.repositories.TransferRepository
Expand All @@ -41,6 +38,7 @@ import com.infomaniak.multiplatform_swisstransfer.utils.EmailLanguageUtils
* @property appSettingsManager A manager used to orchestrate AppSettings operations.
* @property accountManager A manager used to orchestrate Accounts operations.
* @property uploadManager A manager used to orchestrate Uploads operations.
* @property emailTokensManager A manager used to orchestrate EmailTokens operations.
* @property sharedApiUrlCreator An utils to help use shared routes.
*/
class SwissTransferInjection(
Expand All @@ -54,6 +52,7 @@ class SwissTransferInjection(
private val transferRepository by lazy { TransferRepository(apiClientProvider) }

private val appSettingsController by lazy { AppSettingsController(realmProvider) }
private val emailTokensController by lazy { EmailTokensController(realmProvider) }
private val uploadController by lazy { UploadController(realmProvider) }
private val transferController by lazy { TransferController(realmProvider) }
private val fileController by lazy { FileController(realmProvider) }
Expand All @@ -69,6 +68,9 @@ class SwissTransferInjection(
/** A manager used to orchestrate AppSettings operations. */
val appSettingsManager by lazy { AppSettingsManager(appSettingsController) }

/** A manager used to orchestrate EmailTokens operations. */
val emailTokensManager by lazy { EmailTokensManager(emailTokensController) }

/** A manager used to orchestrate Accounts operations. */
val accountManager by lazy {
AccountManager(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Infomaniak SwissTransfer - Multiplatform
* Copyright (C) 2024 Infomaniak Network SA
*
* 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.infomaniak.multiplatform_swisstransfer.managers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.database.controllers.EmailTokensController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.withContext
import kotlin.coroutines.cancellation.CancellationException

class EmailTokensManager(private val emailTokensController: EmailTokensController) {

/**
* Get a token for a given email.
*
* @param email The email possibly associated with a token.
*
* @return A token which may be associated with the email.
*
* @throws RealmException If an error occurs during database access.
* @throws CancellationException If the operation is cancelled.
*/
@Throws(RealmException::class, CancellationException::class)
suspend fun getTokenForEmail(email: String): String? = withContext(Dispatchers.IO) {
return@withContext emailTokensController.getEmailTokenForEmail(email)?.token
}

/**
* Asynchronously sets the email and it's corresponding token.
*
* @param email The validated email.
* @param token The valid token associated to the email.
*
* @throws RealmException If an error occurs during database access.
* @throws CancellationException If the operation is cancelled.
*/
@Throws(RealmException::class, CancellationException::class)
suspend fun setEmailToken(email: String, token: String): Unit = withContext(Dispatchers.IO) {
emailTokensController.setEmailToken(email, token)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.infomaniak.multiplatform_swisstransfer.database

import com.infomaniak.multiplatform_swisstransfer.database.models.appSettings.AppSettingsDB
import com.infomaniak.multiplatform_swisstransfer.database.models.appSettings.EmailTokenDB
import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.ContainerDB
import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.FileDB
import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.TransferDB
Expand Down Expand Up @@ -58,7 +59,7 @@ class RealmProvider(private val loadDataInMemory: Boolean = false) {
}

private val realmAppSettingsConfiguration = RealmConfiguration
.Builder(schema = setOf(AppSettingsDB::class))
.Builder(schema = setOf(AppSettingsDB::class, EmailTokenDB::class))
.name("AppSettings.realm")
.deleteRealmDataIfNeeded()
.loadDataInMemoryIfNeeded()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Infomaniak SwissTransfer - Multiplatform
* Copyright (C) 2024 Infomaniak Network SA
*
* 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.infomaniak.multiplatform_swisstransfer.database.controllers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.EmailToken
import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider
import com.infomaniak.multiplatform_swisstransfer.database.models.appSettings.EmailTokenDB
import com.infomaniak.multiplatform_swisstransfer.database.utils.RealmUtils.runThrowingRealm
import io.realm.kotlin.UpdatePolicy
import io.realm.kotlin.ext.query
import kotlin.coroutines.cancellation.CancellationException

class EmailTokensController(private val realmProvider: RealmProvider) {
private val realm by lazy { realmProvider.realmAppSettings }

//region Get data
@Throws(RealmException::class)
fun getEmailTokenForEmail(email: String): EmailToken? = runThrowingRealm {
val query = "${EmailTokenDB::email.name} == '$email'"
return realm.query<EmailTokenDB>(query).first().find()
}
//endregion

//region Update data
@Throws(RealmException::class, CancellationException::class)
suspend fun setEmailToken(email: String, token: String) = runThrowingRealm {
realm.write {
val emailTokenDB = EmailTokenDB(email, token)
copyToRealm(emailTokenDB, UpdatePolicy.ALL)
}
}
//endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Infomaniak SwissTransfer - Multiplatform
* Copyright (C) 2024 Infomaniak Network SA
*
* 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.infomaniak.multiplatform_swisstransfer.database.models.appSettings

import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.EmailToken
import io.realm.kotlin.types.RealmObject
import io.realm.kotlin.types.annotations.PrimaryKey

class EmailTokenDB() : RealmObject, EmailToken {
@PrimaryKey
override var email: String = ""
override var token: String = ""

constructor(email: String, token: String) : this() {
this.email = email
this.token = token
}
}

0 comments on commit f8d5969

Please sign in to comment.