Skip to content

Commit

Permalink
fix voting in boosted polls on user profiles (#4890)
Browse files Browse the repository at this point in the history
Without this fix, the vote goes through but the poll in the app doesn't
update.
Fixed by using `updateStatusByActionableId`, which automatically handles
boosted & regular posts. Also handle poll votes the same way as in the
home timeline, by listening to the `PollVoteEvent`.
  • Loading branch information
connyduck authored Jan 23, 2025
1 parent 77dbac2 commit 170358f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import com.keylesspalace.tusky.db.AppDatabase
import com.keylesspalace.tusky.db.entity.HomeTimelineData
import com.keylesspalace.tusky.db.entity.HomeTimelineEntity
import com.keylesspalace.tusky.entity.Filter
import com.keylesspalace.tusky.entity.Poll
import com.keylesspalace.tusky.network.FilterModel
import com.keylesspalace.tusky.network.MastodonApi
import com.keylesspalace.tusky.usecase.TimelineCases
Expand Down Expand Up @@ -111,10 +110,6 @@ class CachedTimelineViewModel @Inject constructor(
}
.flowOn(Dispatchers.Default)

override fun updatePoll(newPoll: Poll, status: StatusViewData.Concrete) {
// handled by CacheUpdater
}

override fun changeExpanded(expanded: Boolean, status: StatusViewData.Concrete) {
viewModelScope.launch {
db.timelineStatusDao()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.keylesspalace.tusky.appstore.DomainMuteEvent
import com.keylesspalace.tusky.appstore.Event
import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.MuteEvent
import com.keylesspalace.tusky.appstore.PollVoteEvent
import com.keylesspalace.tusky.appstore.StatusChangedEvent
import com.keylesspalace.tusky.appstore.StatusDeletedEvent
import com.keylesspalace.tusky.appstore.UnfollowEvent
Expand Down Expand Up @@ -116,6 +117,7 @@ class NetworkTimelineViewModel @Inject constructor(
private fun handleEvent(event: Event) {
when (event) {
is StatusChangedEvent -> handleStatusChangedEvent(event.status)
is PollVoteEvent -> handlePollVote(event.statusId, event.poll)
is UnfollowEvent -> {
if (kind == Kind.HOME) {
val id = event.accountId
Expand Down Expand Up @@ -148,12 +150,6 @@ class NetworkTimelineViewModel @Inject constructor(
}
}

override fun updatePoll(newPoll: Poll, status: StatusViewData.Concrete) {
status.copy(
status = status.status.copy(poll = newPoll)
).update()
}

override fun changeExpanded(expanded: Boolean, status: StatusViewData.Concrete) {
status.copy(
isExpanded = expanded
Expand Down Expand Up @@ -297,6 +293,12 @@ class NetworkTimelineViewModel @Inject constructor(
updateStatusByActionableId(status.id) { status }
}

private fun handlePollVote(statusId: String, poll: Poll) {
updateStatusByActionableId(statusId) { status ->
status.copy(poll = poll)
}
}

override fun fullReload() {
nextKey = statusData.firstOrNull { it is StatusViewData.Concrete }?.asStatusOrNull()?.id
statusData.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import com.keylesspalace.tusky.components.preference.PreferencesFragment.Reading
import com.keylesspalace.tusky.components.timeline.util.ifExpected
import com.keylesspalace.tusky.db.AccountManager
import com.keylesspalace.tusky.entity.Filter
import com.keylesspalace.tusky.entity.Poll
import com.keylesspalace.tusky.network.FilterModel
import com.keylesspalace.tusky.settings.PrefKeys
import com.keylesspalace.tusky.usecase.TimelineCases
Expand Down Expand Up @@ -145,9 +144,6 @@ abstract class TimelineViewModel(
return@launch
}

val votedPoll = poll.votedCopy(choices)
updatePoll(votedPoll, status)

try {
timelineCases.voteInPoll(status.actionableId, poll.id, choices).getOrThrow()
} catch (t: Exception) {
Expand All @@ -157,8 +153,6 @@ abstract class TimelineViewModel(
}
}

abstract fun updatePoll(newPoll: Poll, status: StatusViewData.Concrete)

abstract fun changeExpanded(expanded: Boolean, status: StatusViewData.Concrete)

abstract fun changeContentShowing(isShowing: Boolean, status: StatusViewData.Concrete)
Expand Down

0 comments on commit 170358f

Please sign in to comment.