Skip to content

Commit

Permalink
feat: Automatically handles the authorEmailToken inside KMP
Browse files Browse the repository at this point in the history
  • Loading branch information
LunarX committed Dec 20, 2024
1 parent 508128f commit b3411b4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ class SwissTransferInjection(
}

/** A manager used to orchestrate Uploads operations. */
val uploadManager by lazy { UploadManager(uploadController, uploadRepository, transferManager, emailLanguageUtils) }
val uploadManager by lazy {
UploadManager(
uploadController,
uploadRepository,
transferManager,
emailLanguageUtils,
emailTokensManager,
)
}

/** An utils to help use shared routes */
val sharedApiUrlCreator by lazy { SharedApiUrlCreator(environment, transferController, uploadController) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.infomaniak.multiplatform_swisstransfer.managers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.database.controllers.EmailTokensController
import com.infomaniak.multiplatform_swisstransfer.network.models.upload.response.AuthorEmailToken
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.withContext
Expand All @@ -45,13 +46,13 @@ class EmailTokensManager(private val emailTokensController: EmailTokensControlle
* Asynchronously sets the email and it's corresponding token.
*
* @param email The validated email.
* @param token The valid token associated to the email.
* @param authorEmailToken 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)
suspend fun setEmailToken(email: String, authorEmailToken: AuthorEmailToken): Unit = withContext(Dispatchers.IO) {
emailTokensController.setEmailToken(email, authorEmailToken.token)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ package com.infomaniak.multiplatform_swisstransfer.managers

import com.infomaniak.multiplatform_swisstransfer.common.exceptions.RealmException
import com.infomaniak.multiplatform_swisstransfer.common.exceptions.UnknownException
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadFileSession
import com.infomaniak.multiplatform_swisstransfer.common.interfaces.upload.UploadSession
import com.infomaniak.multiplatform_swisstransfer.common.models.TransferDirection
import com.infomaniak.multiplatform_swisstransfer.common.models.TransferStatus
import com.infomaniak.multiplatform_swisstransfer.common.models.*
import com.infomaniak.multiplatform_swisstransfer.data.NewUploadSession
import com.infomaniak.multiplatform_swisstransfer.database.controllers.UploadController
import com.infomaniak.multiplatform_swisstransfer.exceptions.NotFoundException
Expand Down Expand Up @@ -56,6 +56,7 @@ class UploadManager(
private val uploadRepository: UploadRepository,
private val transferManager: TransferManager,
private val emailLanguageUtils: EmailLanguageUtils,
private val emailTokensManager: EmailTokensManager,
) {

val lastUploadFlow get() = uploadController.getLastUploadFlow().flowOn(Dispatchers.IO)
Expand Down Expand Up @@ -108,6 +109,60 @@ class UploadManager(
uploadController.insertAndGet(newUploadSession)
}

/**
* Stores the email address token in DB for future uses and updates the current uploadSession stored in DB with this new
* email address token.
*
* @param authorEmail The email to which the [authorEmailToken] is associated.
* @param authorEmailToken The token returned by the API that proves that the user owns [authorEmail].
*
* @throws RealmException If an error occurs during database access.
* @throws CancellationException If the operation is cancelled.
* @throws NotFoundException If we can't find any upload to update.
*/
@Throws(RealmException::class, CancellationException::class, NotFoundException::class)
suspend fun updateAuthorEmailToken(authorEmail: String, authorEmailToken: AuthorEmailToken) {
emailTokensManager.setEmailToken(authorEmail, authorEmailToken)

runCatching {
uploadController.updateLastUploadSessionAuthorEmailToken(authorEmailToken.token)
}.onFailure {
when (it) {
is NoSuchElementException -> throw NotFoundException(it.message ?: "")
else -> throw it
}
}
}

/**
* Instantiate a [NewUploadSession] and automatically fills in the author's email token with the one associated with
* [authorEmail] from the data base.
*
* @throws RealmException If an error occurs during database access.
* @throws CancellationException If the operation is cancelled.
*/
@Throws(RealmException::class, CancellationException::class)
suspend fun generateNewUploadSession(
duration: ValidityPeriod,
authorEmail: String,
password: String,
message: String,
numberOfDownload: DownloadLimit,
language: EmailLanguage,
recipientsEmails: Set<String>,
files: List<UploadFileSession>,
): NewUploadSession = NewUploadSession(
duration = duration,
authorEmail = authorEmail,
authorEmailToken = emailTokensManager.getTokenForEmail(authorEmail),
password = password,
message = message,
numberOfDownload = numberOfDownload,
language = language,
recipientsEmails = recipientsEmails,
files = files,
)

/**
* Initializes an upload session and update it in database with the remote data.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class UploadController(private val realmProvider: RealmProvider) {

@Throws(RealmException::class)
fun getLastUpload(): UploadSession? = runThrowingRealm {
return getUploadsQuery().first().find()
return realm.getLastUploadQuery().find()
}

@Throws(RealmException::class)
Expand Down Expand Up @@ -97,6 +97,14 @@ class UploadController(private val realmProvider: RealmProvider) {
}
}

@Throws(RealmException::class, CancellationException::class, NoSuchElementException::class)
suspend fun updateLastUploadSessionAuthorEmailToken(authorToken: String) {
realm.write {
val lastUpload = getLastUploadQuery().find() ?: throw NoSuchElementException("No uploadSession found in DB")
lastUpload.authorEmailToken = authorToken
}
}

@Throws(RealmException::class, CancellationException::class)
suspend fun removeData() = runThrowingRealm {
realm.write { deleteAll() }
Expand All @@ -116,6 +124,10 @@ class UploadController(private val realmProvider: RealmProvider) {
private fun TypedRealm.getUploadSessionQuery(uuid: String): RealmSingleQuery<UploadSessionDB> {
return query<UploadSessionDB>("${UploadSessionDB::uuid.name} == '$uuid'").first()
}

fun TypedRealm.getLastUploadQuery(): RealmSingleQuery<UploadSessionDB> {
return query<UploadSessionDB>().first()
}
//endregion
}
}

0 comments on commit b3411b4

Please sign in to comment.