From 995da613e4d7bfcae850636b22a56aa0560b81e0 Mon Sep 17 00:00:00 2001 From: Gerard Paligot Date: Tue, 7 Nov 2023 21:50:28 +0100 Subject: [PATCH] fix(openfeedback): use correct id to check if the comment is upvoted by user --- .../android/viewmodels/mappers/UiMappers.kt | 9 ++++++--- .../android/OpenFeedbackRepository.kt | 3 ++- .../io/openfeedback/android/model/Model.kt | 5 +++++ .../android/sources/OpenFeedbackFirestore.kt | 19 +++++++++++-------- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/openfeedback-viewmodel/src/main/java/io/openfeedback/android/viewmodels/mappers/UiMappers.kt b/openfeedback-viewmodel/src/main/java/io/openfeedback/android/viewmodels/mappers/UiMappers.kt index b8adf28..8252711 100644 --- a/openfeedback-viewmodel/src/main/java/io/openfeedback/android/viewmodels/mappers/UiMappers.kt +++ b/openfeedback-viewmodel/src/main/java/io/openfeedback/android/viewmodels/mappers/UiMappers.kt @@ -2,6 +2,7 @@ package io.openfeedback.android.viewmodels.mappers import io.openfeedback.android.model.Project import io.openfeedback.android.model.SessionVotes +import io.openfeedback.android.model.UserVote import io.openfeedback.android.viewmodels.models.UIComment import io.openfeedback.android.viewmodels.models.UIDot import io.openfeedback.android.viewmodels.models.UISessionFeedback @@ -14,11 +15,13 @@ import kotlin.random.Random fun convertToUiSessionFeedback( project: Project, - userVotes: List, + userVotes: List, totalVotes: SessionVotes, locale: Locale ): UISessionFeedback { val formatter = SimpleDateFormat("dd MMMM yyyy, hh:mm", locale) + val userUpVoteIds = userVotes.filter { it.voteId != null }.map { it.voteId!! } + val userVoteIds = userVotes.map { it.voteItemId } return UISessionFeedback( commentValue = "", commentVoteItemId = project.voteItems.find { it.type == "text" }?.id, @@ -30,7 +33,7 @@ fun convertToUiSessionFeedback( createdAt = formatter.format(commentItem.value.createdAt.toDate()), upVotes = commentItem.value.plus.toInt(), dots = dots(commentItem.value.plus.toInt(), project.chipColors), - votedByUser = userVotes.contains(commentItem.value.voteItemId) + votedByUser = userUpVoteIds.contains(commentItem.value.id) ) }, voteItem = project.voteItems @@ -41,7 +44,7 @@ fun convertToUiSessionFeedback( id = voteItem.id, text = voteItem.localizedName(locale.language), dots = dots(count, project.chipColors), - votedByUser = userVotes.contains(voteItem.id) + votedByUser = userVoteIds.contains(voteItem.id) ) } ) diff --git a/openfeedback/src/main/java/io/openfeedback/android/OpenFeedbackRepository.kt b/openfeedback/src/main/java/io/openfeedback/android/OpenFeedbackRepository.kt index c51bf89..097be63 100644 --- a/openfeedback/src/main/java/io/openfeedback/android/OpenFeedbackRepository.kt +++ b/openfeedback/src/main/java/io/openfeedback/android/OpenFeedbackRepository.kt @@ -3,6 +3,7 @@ package io.openfeedback.android import io.openfeedback.android.caches.OptimisticVoteCaching import io.openfeedback.android.model.Project import io.openfeedback.android.model.SessionVotes +import io.openfeedback.android.model.UserVote import io.openfeedback.android.model.VoteStatus import io.openfeedback.android.sources.OpenFeedbackAuth import io.openfeedback.android.sources.OpenFeedbackFirestore @@ -22,7 +23,7 @@ class OpenFeedbackRepository( fun project(projectId: String): Flow = firestore.project(projectId) @OptIn(ExperimentalCoroutinesApi::class) - fun userVotes(projectId: String, sessionId: String): Flow> = + fun userVotes(projectId: String, sessionId: String): Flow> = flow { emit(auth.firebaseUser()) } .flatMapConcat { if (it != null) { diff --git a/openfeedback/src/main/java/io/openfeedback/android/model/Model.kt b/openfeedback/src/main/java/io/openfeedback/android/model/Model.kt index 73820cb..85f0586 100644 --- a/openfeedback/src/main/java/io/openfeedback/android/model/Model.kt +++ b/openfeedback/src/main/java/io/openfeedback/android/model/Model.kt @@ -24,6 +24,11 @@ enum class VoteStatus(val value: String) { Deleted("deleted") } +data class UserVote( + val voteItemId: String, + val voteId: String? +) + data class SessionVotes( val votes: Map, val comments: Map diff --git a/openfeedback/src/main/java/io/openfeedback/android/sources/OpenFeedbackFirestore.kt b/openfeedback/src/main/java/io/openfeedback/android/sources/OpenFeedbackFirestore.kt index a92b6e5..14915a5 100644 --- a/openfeedback/src/main/java/io/openfeedback/android/sources/OpenFeedbackFirestore.kt +++ b/openfeedback/src/main/java/io/openfeedback/android/sources/OpenFeedbackFirestore.kt @@ -7,6 +7,7 @@ import com.google.firebase.firestore.FirebaseFirestoreSettings import io.openfeedback.android.mappers.convertToModel import io.openfeedback.android.model.Project import io.openfeedback.android.model.SessionVotes +import io.openfeedback.android.model.UserVote import io.openfeedback.android.model.VoteStatus import io.openfeedback.android.toFlow import kotlinx.coroutines.flow.Flow @@ -24,18 +25,19 @@ class OpenFeedbackFirestore( querySnapshot.toObject(Project::class.java)!! } - fun userVotes(projectId: String, userId: String, sessionId: String): Flow> = + fun userVotes(projectId: String, userId: String, sessionId: String): Flow> = firestore.collection("projects/$projectId/userVotes") .whereEqualTo("userId", userId) + .whereEqualTo("status", VoteStatus.Active.value) + .whereEqualTo("talkId", sessionId) .toFlow() .map { querySnapshot -> - querySnapshot - .filter { - it.data["status"] == VoteStatus.Active.value - && it.data["talkId"] == sessionId - && it.data["userId"] == userId - } - .map { it.data["voteItemId"] as String } + querySnapshot.map { + UserVote( + voteItemId = it.data["voteItemId"] as String, + voteId = it.data["voteId"] as String? + ) + } } fun sessionVotes(projectId: String, sessionId: String): Flow = @@ -164,6 +166,7 @@ class OpenFeedbackFirestore( .whereEqualTo("userId", userId) .whereEqualTo("talkId", talkId) .whereEqualTo("voteItemId", voteItemId) + .whereEqualTo("voteId", voteId) .get() .await() if (querySnapshot.isEmpty) {