Skip to content

Commit

Permalink
Refactor poll events (#5333)
Browse files Browse the repository at this point in the history
* Refactor logic to store poll on channel state

* Refactor Poll Events
  • Loading branch information
JcMinarro authored Jul 26, 2024
1 parent 508105e commit 20fdbdd
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 185 deletions.
94 changes: 43 additions & 51 deletions stream-chat-android-client/api/stream-chat-android-client.api

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ import io.getstream.chat.android.client.events.UserUpdatedEvent
import io.getstream.chat.android.client.events.VoteCastedEvent
import io.getstream.chat.android.client.events.VoteChangedEvent
import io.getstream.chat.android.client.events.VoteRemovedEvent
import io.getstream.chat.android.models.Message
import io.getstream.chat.android.models.Poll
import io.getstream.chat.android.client.extensions.cidToTypeAndId
import io.getstream.chat.android.models.UserId

internal fun ConnectedEvent.toDto(): UpstreamConnectedEventDto {
Expand Down Expand Up @@ -727,48 +726,49 @@ private fun UserUpdatedEventDto.toDomain(currentUserId: UserId?): UserUpdatedEve

private fun PollClosedEventDto.toDomain(currentUserId: UserId?): PollClosedEvent {
val newPoll = poll.toDomain(currentUserId)
val (channelType, channelId) = cid.cidToTypeAndId()
return PollClosedEvent(
type = type,
createdAt = created_at.date,
rawCreatedAt = created_at.rawDate,
cid = cid,
channelType = channel_type,
channelId = channel_id,
message = message.toDomain(currentUserId).enrichWithPoll(newPoll),
channelType = channelType,
channelId = channelId,
poll = newPoll,
)
}

private fun PollDeletedEventDto.toDomain(currentUserId: UserId?): PollDeletedEvent {
val newPoll = poll.toDomain(currentUserId)
val (channelType, channelId) = cid.cidToTypeAndId()
return PollDeletedEvent(
type = type,
createdAt = created_at.date,
rawCreatedAt = created_at.rawDate,
cid = cid,
channelType = channel_type,
channelId = channel_id,
message = message.toDomain(currentUserId).enrichWithPoll(newPoll),
channelType = channelType,
channelId = channelId,
poll = newPoll,
)
}

private fun PollUpdatedEventDto.toDomain(currentUserId: UserId?): PollUpdatedEvent {
val newPoll = poll.toDomain(currentUserId)
val (channelType, channelId) = cid.cidToTypeAndId()
return PollUpdatedEvent(
type = type,
createdAt = created_at.date,
rawCreatedAt = created_at.rawDate,
cid = cid,
channelType = channel_type,
channelId = channel_id,
message = message.toDomain(currentUserId).enrichWithPoll(newPoll),
channelType = channelType,
channelId = channelId,
poll = newPoll,
)
}

private fun VoteCastedEventDto.toDomain(currentUserId: UserId?): VoteCastedEvent {
val pollVote = poll_vote.toDomain(currentUserId)
val (channelType, channelId) = cid.cidToTypeAndId()
val newPoll = poll.toDomain(currentUserId)
.let { poll ->
pollVote.takeIf { it.user?.id == currentUserId }
Expand All @@ -784,16 +784,16 @@ private fun VoteCastedEventDto.toDomain(currentUserId: UserId?): VoteCastedEvent
createdAt = created_at.date,
rawCreatedAt = created_at.rawDate,
cid = cid,
channelType = channel_type,
channelId = channel_id,
message = message.toDomain(currentUserId).enrichWithPoll(newPoll),
channelType = channelType,
channelId = channelId,
poll = newPoll,
newVote = pollVote,
)
}

private fun VoteChangedEventDto.toDomain(currentUserId: UserId?): VoteChangedEvent {
val pollVote = poll_vote.toDomain(currentUserId)
val (channelType, channelId) = cid.cidToTypeAndId()
val newPoll = poll.toDomain(currentUserId)
.let { poll ->
pollVote.takeIf { it.user?.id == currentUserId }
Expand All @@ -809,24 +809,23 @@ private fun VoteChangedEventDto.toDomain(currentUserId: UserId?): VoteChangedEve
createdAt = created_at.date,
rawCreatedAt = created_at.rawDate,
cid = cid,
channelType = channel_type,
channelId = channel_id,
message = message.toDomain(currentUserId).enrichWithPoll(newPoll),
channelType = channelType,
channelId = channelId,
poll = newPoll,
newVote = pollVote,
)
}
private fun VoteRemovedEventDto.toDomain(currentUserId: UserId?): VoteRemovedEvent {
val removedVote = poll_vote.toDomain(currentUserId)
val (channelType, channelId) = cid.cidToTypeAndId()
val newPoll = poll.toDomain(currentUserId)
return VoteRemovedEvent(
type = type,
createdAt = created_at.date,
rawCreatedAt = created_at.rawDate,
cid = cid,
channelType = channel_type,
channelId = channel_id,
message = message.toDomain(currentUserId).enrichWithPoll(newPoll),
channelType = channelType,
channelId = channelId,
poll = newPoll,
removedVote = removedVote,
)
Expand Down Expand Up @@ -886,13 +885,3 @@ private fun UnknownEventDto.toDomain(currentUserId: UserId?): UnknownEvent {
rawData = rawData,
)
}

private fun Message.enrichWithPoll(newPoll: Poll): Message =
newPoll.takeUnless { it.updatedAt < poll?.updatedAt }
?.let {
copy(
poll = it,
updatedAt = listOfNotNull(updatedAt, it.updatedAt).maxBy { it.time },
)
}
?: this
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,6 @@ internal data class UserUpdatedEventDto(
internal data class PollUpdatedEventDto(
val type: String,
val cid: String,
val channel_type: String,
val channel_id: String,
val message: DownstreamMessageDto,
val poll: DownstreamPollDto,
val created_at: ExactDate,
) : ChatEventDto()
Expand All @@ -478,9 +475,6 @@ internal data class PollDeletedEventDto(
val type: String,
val cid: String,
val created_at: ExactDate,
val channel_type: String,
val channel_id: String,
val message: DownstreamMessageDto,
val poll: DownstreamPollDto,
) : ChatEventDto()

Expand All @@ -489,9 +483,6 @@ internal data class PollClosedEventDto(
val type: String,
val cid: String,
val created_at: ExactDate,
val channel_type: String,
val channel_id: String,
val message: DownstreamMessageDto,
val poll: DownstreamPollDto,
) : ChatEventDto()

Expand All @@ -500,9 +491,6 @@ internal data class VoteCastedEventDto(
val type: String,
val cid: String,
val created_at: ExactDate,
val channel_type: String,
val channel_id: String,
val message: DownstreamMessageDto,
val poll: DownstreamPollDto,
val poll_vote: DownstreamVoteDto,
) : ChatEventDto()
Expand All @@ -511,9 +499,6 @@ internal data class VoteCastedEventDto(
internal data class VoteChangedEventDto(
val type: String,
val cid: String,
val channel_type: String,
val channel_id: String,
val message: DownstreamMessageDto,
val poll: DownstreamPollDto,
val created_at: ExactDate,
val poll_vote: DownstreamVoteDto,
Expand All @@ -523,9 +508,6 @@ internal data class VoteChangedEventDto(
internal data class VoteRemovedEventDto(
val type: String,
val cid: String,
val channel_type: String,
val channel_id: String,
val message: DownstreamMessageDto,
val poll: DownstreamPollDto,
val created_at: ExactDate,
val poll_vote: DownstreamVoteDto,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ public sealed interface HasOwnUser {
public val me: User
}

public sealed interface HasPoll {
public val poll: Poll
}

/**
* Interface that marks a [ChatEvent] as having the information about watcher count.
*
Expand Down Expand Up @@ -668,9 +672,8 @@ public data class PollUpdatedEvent(
override val cid: String,
override val channelType: String,
override val channelId: String,
override val message: Message,
val poll: Poll,
) : CidEvent(), HasMessage
override val poll: Poll,
) : CidEvent(), HasPoll

/**
* Triggered when a poll is deleted.
Expand All @@ -682,9 +685,8 @@ public data class PollDeletedEvent(
override val cid: String,
override val channelType: String,
override val channelId: String,
override val message: Message,
val poll: Poll,
) : CidEvent(), HasMessage
override val poll: Poll,
) : CidEvent(), HasPoll

/**
* Triggered when a poll is closed.
Expand All @@ -696,9 +698,8 @@ public data class PollClosedEvent(
override val cid: String,
override val channelType: String,
override val channelId: String,
override val message: Message,
val poll: Poll,
) : CidEvent(), HasMessage
override val poll: Poll,
) : CidEvent(), HasPoll

/**
* Triggered when a vote is casted.
Expand All @@ -710,10 +711,9 @@ public data class VoteCastedEvent(
override val cid: String,
override val channelType: String,
override val channelId: String,
override val message: Message,
val poll: Poll,
override val poll: Poll,
val newVote: Vote,
) : CidEvent(), HasMessage
) : CidEvent(), HasPoll

/**
* Triggered when a vote is changed.
Expand All @@ -725,10 +725,9 @@ public data class VoteChangedEvent(
override val cid: String,
override val channelType: String,
override val channelId: String,
override val message: Message,
val poll: Poll,
override val poll: Poll,
val newVote: Vote,
) : CidEvent(), HasMessage
) : CidEvent(), HasPoll

/**
* Triggered when a vote is removed.
Expand All @@ -740,10 +739,9 @@ public data class VoteRemovedEvent(
override val cid: String,
override val channelType: String,
override val channelId: String,
override val message: Message,
val poll: Poll,
override val poll: Poll,
val removedVote: Vote,
) : CidEvent(), HasMessage
) : CidEvent(), HasPoll

/**
* Triggered when a user gets connected to the WS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,9 @@ import io.getstream.chat.android.client.events.MessageDeletedEvent
import io.getstream.chat.android.client.events.MessageUpdatedEvent
import io.getstream.chat.android.client.events.NewMessageEvent
import io.getstream.chat.android.client.events.NotificationMessageNewEvent
import io.getstream.chat.android.client.events.PollClosedEvent
import io.getstream.chat.android.client.events.PollDeletedEvent
import io.getstream.chat.android.client.events.PollUpdatedEvent
import io.getstream.chat.android.client.events.ReactionDeletedEvent
import io.getstream.chat.android.client.events.ReactionNewEvent
import io.getstream.chat.android.client.events.ReactionUpdateEvent
import io.getstream.chat.android.client.events.VoteCastedEvent
import io.getstream.chat.android.client.events.VoteChangedEvent
import io.getstream.chat.android.client.events.VoteRemovedEvent
import io.getstream.chat.android.client.extensions.enrichWithCid

public fun ChatEvent.enrichIfNeeded(): ChatEvent = when (this) {
Expand All @@ -46,11 +40,5 @@ public fun ChatEvent.enrichIfNeeded(): ChatEvent = when (this) {
is ChannelTruncatedEvent -> copy(message = message?.enrichWithCid(cid))
is ChannelUpdatedByUserEvent -> copy(message = message?.enrichWithCid(cid))
is NotificationMessageNewEvent -> copy(message = message.enrichWithCid(cid))
is PollUpdatedEvent -> copy(message = message.enrichWithCid(cid))
is PollDeletedEvent -> copy(message = message.enrichWithCid(cid))
is PollClosedEvent -> copy(message = message.enrichWithCid(cid))
is VoteCastedEvent -> copy(message = message.enrichWithCid(cid))
is VoteChangedEvent -> copy(message = message.enrichWithCid(cid))
is VoteRemovedEvent -> copy(message = message.enrichWithCid(cid))
else -> this
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ import io.getstream.chat.android.client.events.NotificationMarkUnreadEvent
import io.getstream.chat.android.client.events.NotificationMessageNewEvent
import io.getstream.chat.android.client.events.NotificationMutesUpdatedEvent
import io.getstream.chat.android.client.events.NotificationRemovedFromChannelEvent
import io.getstream.chat.android.client.events.PollClosedEvent
import io.getstream.chat.android.client.events.PollDeletedEvent
import io.getstream.chat.android.client.events.PollUpdatedEvent
import io.getstream.chat.android.client.events.ReactionDeletedEvent
import io.getstream.chat.android.client.events.ReactionNewEvent
import io.getstream.chat.android.client.events.ReactionUpdateEvent
Expand All @@ -65,9 +62,6 @@ import io.getstream.chat.android.client.events.UserPresenceChangedEvent
import io.getstream.chat.android.client.events.UserStartWatchingEvent
import io.getstream.chat.android.client.events.UserStopWatchingEvent
import io.getstream.chat.android.client.events.UserUpdatedEvent
import io.getstream.chat.android.client.events.VoteCastedEvent
import io.getstream.chat.android.client.events.VoteChangedEvent
import io.getstream.chat.android.client.events.VoteRemovedEvent
import io.getstream.chat.android.client.extensions.cidToTypeAndId
import io.getstream.chat.android.client.extensions.internal.addMember
import io.getstream.chat.android.client.extensions.internal.addMembership
Expand Down Expand Up @@ -672,24 +666,6 @@ internal class EventHandlerSequential(
is UserUpdatedEvent -> if (event.user.id == currentUserId) {
repos.insertCurrentUser(event.user)
}
is PollClosedEvent -> {
batch.addMessage(event.message)
}
is PollDeletedEvent -> {
batch.addMessage(event.message)
}
is PollUpdatedEvent -> {
batch.addMessage(event.message)
}
is VoteCastedEvent -> {
batch.addMessage(event.message)
}
is VoteChangedEvent -> {
batch.addMessage(event.message)
}
is VoteRemovedEvent -> {
batch.addMessage(event.message)
}
else -> Unit
}
}
Expand Down
Loading

0 comments on commit 20fdbdd

Please sign in to comment.