Skip to content

Commit

Permalink
fix(openfeedback): use correct id to check if the comment is upvoted …
Browse files Browse the repository at this point in the history
…by user
  • Loading branch information
GerardPaligot committed Nov 7, 2023
1 parent 1cfca99 commit 7e45770
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,11 +15,13 @@ import kotlin.random.Random

fun convertToUiSessionFeedback(
project: Project,
userVotes: List<String>,
userVotes: List<UserVote>,
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,
Expand All @@ -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
Expand All @@ -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)
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +23,7 @@ class OpenFeedbackRepository(
fun project(projectId: String): Flow<Project> = firestore.project(projectId)

@OptIn(ExperimentalCoroutinesApi::class)
fun userVotes(projectId: String, sessionId: String): Flow<List<String>> =
fun userVotes(projectId: String, sessionId: String): Flow<List<UserVote>> =
flow { emit(auth.firebaseUser()) }
.flatMapConcat {
if (it != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Long>,
val comments: Map<String, Comment>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,18 +25,19 @@ class OpenFeedbackFirestore(
querySnapshot.toObject(Project::class.java)!!
}

fun userVotes(projectId: String, userId: String, sessionId: String): Flow<List<String>> =
fun userVotes(projectId: String, userId: String, sessionId: String): Flow<List<UserVote>> =
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<SessionVotes> =
Expand Down Expand Up @@ -164,6 +166,7 @@ class OpenFeedbackFirestore(
.whereEqualTo("userId", userId)
.whereEqualTo("talkId", talkId)
.whereEqualTo("voteItemId", voteItemId)
.whereEqualTo("voteId", voteId)
.get()
.await()
if (querySnapshot.isEmpty) {
Expand Down

0 comments on commit 7e45770

Please sign in to comment.