Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat: Save EmailToken in AppSettings #117

Merged
merged 7 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
PhilippeWeidmann marked this conversation as resolved.
Show resolved Hide resolved
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,54 @@
/*
* 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.common.interfaces.appSettings.EmailToken
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.
*/
fun getEmailTokenForEmail(email: String): EmailToken? {
PhilippeWeidmann marked this conversation as resolved.
Show resolved Hide resolved
PhilippeWeidmann marked this conversation as resolved.
Show resolved Hide resolved
return emailTokensController.getEmailTokenForEmail(email)
}

/**
* 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
Expand Up @@ -19,9 +19,14 @@ package com.infomaniak.multiplatform_swisstransfer.database.controllers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.AppSettings
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.appSettings.EmailToken
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.transfers.File
import com.infomaniak.multiplatform_swisstransfer.common.models.*
import com.infomaniak.multiplatform_swisstransfer.database.RealmProvider
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.FileDB
import com.infomaniak.multiplatform_swisstransfer.database.models.transfers.TransferDB
import com.infomaniak.multiplatform_swisstransfer.database.utils.RealmUtils.runThrowingRealm
import io.realm.kotlin.UpdatePolicy
import io.realm.kotlin.ext.query
Expand Down Expand Up @@ -56,6 +61,7 @@ class AppSettingsController(private val realmProvider: RealmProvider) {
fun getAppSettings(): AppSettings? = runThrowingRealm {
return appSettingsQuery.find()
}

PhilippeWeidmann marked this conversation as resolved.
Show resolved Hide resolved
//endregion

//region Update data
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
}
}
Loading