Skip to content

Commit

Permalink
Properly handle poll on offline layer
Browse files Browse the repository at this point in the history
  • Loading branch information
JcMinarro committed Jun 13, 2024
1 parent 18a1de9 commit 5116c8d
Showing 1 changed file with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private const val TAG_SOCKET = "Chat:SocketEvent"
* Processes events sequentially. That means a new event will not be processed
* until the previous event processing is not completed.
*/
@Suppress("LongParameterList", "TooManyFunctions")
@Suppress("LongParameterList", "TooManyFunctions", "LargeClass")
internal class EventHandlerSequential(
private val currentUserId: UserId,
private val subscribeForEvents: (ChatEventListener<ChatEvent>) -> Disposable,
Expand Down Expand Up @@ -682,13 +682,45 @@ internal class EventHandlerSequential(
batch.addMessage(event.message)
}
is VoteCastedEvent -> {
batch.addMessage(event.message)
val ownVotes = (
batch.getCurrentMessage(event.message.id)?.poll?.ownVotes?.associateBy { it.id }
?: emptyMap()
) +
listOfNotNull(event.newVote.takeIf { it.user?.id == currentUserId }).associateBy { it.id }
batch.addMessage(
event.message.copy(
poll = event.poll.copy(
ownVotes = ownVotes.values.toList(),
),
),
)
}
is VoteChangedEvent -> {
batch.addMessage(event.message)
val ownVotes = event.newVote.takeIf { it.user?.id == currentUserId }?.let { listOf(it) }
?: batch.getCurrentMessage(
event.message.id,
)?.poll?.ownVotes
batch.addMessage(
event.message.copy(
poll = event.poll.copy(
ownVotes = ownVotes ?: emptyList(),
),
),
)
}
is VoteRemovedEvent -> {
batch.addMessage(event.message)
val ownVotes =
(
batch.getCurrentMessage(event.message.id)?.poll?.ownVotes?.associateBy { it.id }
?: emptyMap()
) - event.removedVote.id
batch.addMessage(
event.message.copy(
poll = event.poll.copy(
ownVotes = ownVotes.values.toList(),
),
),
)
}
else -> Unit
}
Expand Down

0 comments on commit 5116c8d

Please sign in to comment.