Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.getstream.feeds.android.client.api.model

import io.getstream.feeds.android.client.internal.model.addReaction
import io.getstream.feeds.android.client.internal.model.removeReaction
import io.getstream.feeds.android.client.internal.utils.upsert
import io.getstream.feeds.android.network.models.ActivityLocation
import io.getstream.feeds.android.network.models.ActivityResponse
Expand Down Expand Up @@ -276,28 +278,21 @@ internal fun ActivityData.deleteBookmark(
internal fun ActivityData.addReaction(
reaction: FeedsReactionData,
currentUserId: String,
): ActivityData {
val updatedLatestReactions = this.latestReactions.upsert(reaction, FeedsReactionData::id)
val reactionGroup =
this.reactionGroups[reaction.type]
?: ReactionGroupData(1, reaction.createdAt, reaction.createdAt)
val updatedReactionGroup = reactionGroup.increment(reaction.createdAt)
val updatedReactionGroups =
this.reactionGroups.toMutableMap().apply { this[reaction.type] = updatedReactionGroup }
val updatedReactionCount = updatedReactionGroups.values.sumOf(ReactionGroupData::count)
val updatedOwnReactions =
if (reaction.user.id == currentUserId) {
this.ownReactions.upsert(reaction, FeedsReactionData::id)
} else {
this.ownReactions
}
return this.copy(
latestReactions = updatedLatestReactions,
reactionGroups = updatedReactionGroups,
reactionCount = updatedReactionCount,
ownReactions = updatedOwnReactions,
)
}
): ActivityData =
addReaction(
ownReactions = ownReactions,
latestReactions = latestReactions,
reactionGroups = reactionGroups,
reaction = reaction,
currentUserId = currentUserId,
) { latestReactions, reactionGroups, reactionCount, ownReactions ->
copy(
latestReactions = latestReactions,
reactionGroups = reactionGroups,
reactionCount = reactionCount,
ownReactions = ownReactions,
)
}

/**
* Removes a reaction from the activity, updating the latest reactions, reaction groups, reaction
Expand All @@ -310,35 +305,18 @@ internal fun ActivityData.addReaction(
internal fun ActivityData.removeReaction(
reaction: FeedsReactionData,
currentUserId: String,
): ActivityData {
val updatedLatestReactions = this.latestReactions.filter { it.id != reaction.id }
val updatedOwnReactions =
if (reaction.user.id == currentUserId) {
this.ownReactions.filter { it.id != reaction.id }
} else {
this.ownReactions
}
val reactionGroup = this.reactionGroups[reaction.type]
if (reactionGroup == null) {
// If there is no reaction group for this type, just update latest and own reactions.
// Note: This is only a hypothetical case, as we should always have a reaction group.
return this.copy(
latestReactions = updatedLatestReactions,
ownReactions = updatedOwnReactions,
): ActivityData =
removeReaction(
ownReactions = ownReactions,
latestReactions = latestReactions,
reactionGroups = reactionGroups,
reaction = reaction,
currentUserId = currentUserId,
) { latestReactions, reactionGroups, reactionCount, ownReactions ->
copy(
latestReactions = latestReactions,
reactionGroups = reactionGroups ?: this.reactionGroups,
reactionCount = reactionCount ?: this.reactionCount,
ownReactions = ownReactions,
)
}
val updatedReactionGroup = reactionGroup.decrement(reaction.createdAt)
val updatedReactionGroups =
if (updatedReactionGroup.isEmpty) {
this.reactionGroups - reaction.type // Remove empty group
} else {
this.reactionGroups.toMutableMap().apply { this[reaction.type] = updatedReactionGroup }
}
val updatedReactionCount = updatedReactionGroups.values.sumOf(ReactionGroupData::count)
return this.copy(
latestReactions = updatedLatestReactions,
reactionGroups = updatedReactionGroups,
reactionCount = updatedReactionCount,
ownReactions = updatedOwnReactions,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@ import java.util.Date
*/
public data class FeedsReactionData(
val activityId: String,
val commentId: String?,
val createdAt: Date,
val custom: Map<String, Any?>?,
val type: String,
val updatedAt: Date,
val user: UserData,
) {

/** Unique identifier for the reaction, generated from the activity ID and user ID. */
/** Unique identifier for the reaction. */
public val id: String
get() = "${activityId}${user.id}"
get() = "${activityId}${commentId}${user.id}${type}"
}

/** Converts a [FeedsReactionResponse] to a [FeedsReactionData] model. */
internal fun FeedsReactionResponse.toModel(): FeedsReactionData =
FeedsReactionData(
activityId = activityId,
commentId = commentId,
createdAt = createdAt,
custom = custom,
type = type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package io.getstream.feeds.android.client.api.model

import io.getstream.feeds.android.client.api.state.query.CommentsSortDataFields
import io.getstream.feeds.android.client.internal.utils.upsert
import io.getstream.feeds.android.client.internal.model.addReaction
import io.getstream.feeds.android.client.internal.model.removeReaction
import io.getstream.feeds.android.client.internal.utils.upsertSorted
import io.getstream.feeds.android.network.models.Attachment
import io.getstream.feeds.android.network.models.RepliesMeta
Expand Down Expand Up @@ -169,38 +170,21 @@ public data class ThreadedCommentData(
internal fun ThreadedCommentData.addReaction(
reaction: FeedsReactionData,
currentUserId: String,
): ThreadedCommentData {
val ownReaction = reaction.user.id == currentUserId
val updatedOwnReactions =
if (ownReaction) {
ownReactions.upsert(reaction, FeedsReactionData::id)
} else {
ownReactions
}
val inserted = updatedOwnReactions.size > ownReactions.size
val updatedLatestReactions = latestReactions.upsert(reaction, FeedsReactionData::id)
val reactionGroup =
this.reactionGroups[reaction.type]
?: ReactionGroupData(count = 1, reaction.createdAt, reaction.createdAt)
// Increment the count if:
// 1. The reaction is from another user (not own reaction)
// 2. The reaction is from the current user, but it's a new reaction (not an update of existing)
val updatedReactionGroup =
if (!ownReaction || inserted) {
reactionGroup.increment(reaction.createdAt)
} else {
reactionGroup
}
val updatedReactionGroups =
this.reactionGroups.toMutableMap().apply { this[reaction.type] = updatedReactionGroup }
val updatedReactionCount = updatedReactionGroups.values.sumOf(ReactionGroupData::count)
return this.copy(
latestReactions = updatedLatestReactions,
reactionGroups = updatedReactionGroups,
reactionCount = updatedReactionCount,
ownReactions = updatedOwnReactions,
)
}
): ThreadedCommentData =
addReaction(
ownReactions = ownReactions,
latestReactions = latestReactions,
reactionGroups = reactionGroups,
reaction = reaction,
currentUserId = currentUserId,
) { latestReactions, reactionGroups, reactionCount, ownReactions ->
copy(
latestReactions = latestReactions,
reactionGroups = reactionGroups,
reactionCount = reactionCount,
ownReactions = ownReactions,
)
}

/**
* Removes a reaction from the comment, updating the latest reactions, reaction groups, reaction
Expand All @@ -213,48 +197,21 @@ internal fun ThreadedCommentData.addReaction(
internal fun ThreadedCommentData.removeReaction(
reaction: FeedsReactionData,
currentUserId: String,
): ThreadedCommentData {
val ownReaction = reaction.user.id == currentUserId
val updatedOwnReactions =
if (ownReaction) {
this.ownReactions.filter { it.id != reaction.id }
} else {
this.ownReactions
}
val removed = updatedOwnReactions.size < this.ownReactions.size
val updatedLatestReactions = this.latestReactions.filter { it.id != reaction.id }
val reactionGroup = this.reactionGroups[reaction.type]
if (reactionGroup == null) {
// If there is no reaction group for this type, just update latest and own reactions.
// Note: This is only a hypothetical case, as we should always have a reaction group.
return this.copy(
latestReactions = updatedLatestReactions,
ownReactions = updatedOwnReactions,
): ThreadedCommentData =
removeReaction(
ownReactions = ownReactions,
latestReactions = latestReactions,
reactionGroups = reactionGroups,
reaction = reaction,
currentUserId = currentUserId,
) { latestReactions, reactionGroups, reactionCount, ownReactions ->
copy(
latestReactions = latestReactions,
reactionGroups = reactionGroups ?: this.reactionGroups,
reactionCount = reactionCount ?: this.reactionCount,
ownReactions = ownReactions,
)
}
// Decrement the count if:
// 1. The reaction is from another user (not own reaction)
// 2. The reaction is from the current user, and it was already present
val updatedReactionGroup =
if (!ownReaction || removed) {
reactionGroup.decrement(reaction.createdAt)
} else {
reactionGroup
}
val updatedReactionGroups =
if (updatedReactionGroup.isEmpty) {
this.reactionGroups - reaction.type // Remove empty group
} else {
this.reactionGroups.toMutableMap().apply { this[reaction.type] = updatedReactionGroup }
}
val updatedReactionCount = updatedReactionGroups.values.sumOf(ReactionGroupData::count)
return this.copy(
latestReactions = updatedLatestReactions,
reactionGroups = updatedReactionGroups,
reactionCount = updatedReactionCount,
ownReactions = updatedOwnReactions,
)
}

/**
* Adds a reply to the comment, updating the replies list and reply count.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
*
* Licensed under the Stream License;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/GetStream/stream-feeds-android/blob/main/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.getstream.feeds.android.client.internal.model

import io.getstream.feeds.android.client.api.model.FeedsReactionData
import io.getstream.feeds.android.client.api.model.ReactionGroupData
import io.getstream.feeds.android.client.api.model.decrement
import io.getstream.feeds.android.client.api.model.increment
import io.getstream.feeds.android.client.api.model.isEmpty
import io.getstream.feeds.android.client.internal.utils.upsert

internal inline fun <T> addReaction(
ownReactions: List<FeedsReactionData>,
latestReactions: List<FeedsReactionData>,
reactionGroups: Map<String, ReactionGroupData>,
reaction: FeedsReactionData,
currentUserId: String,
update:
(
latestReactions: List<FeedsReactionData>,
reactionGroups: Map<String, ReactionGroupData>,
reactionCount: Int,
ownReactions: List<FeedsReactionData>,
) -> T,
): T {
val ownReaction = reaction.user.id == currentUserId
val updatedOwnReactions =
if (ownReaction) {
ownReactions.upsert(reaction, FeedsReactionData::id)
} else {
ownReactions
}
val updatedLatestReactions = latestReactions.upsert(reaction, FeedsReactionData::id)
val inserted =
updatedOwnReactions.size > ownReactions.size ||
updatedLatestReactions.size > latestReactions.size
val reactionGroup =
reactionGroups[reaction.type]
?: ReactionGroupData(count = 1, reaction.createdAt, reaction.createdAt)
// Increment the count if the reaction was inserted (not an update of existing)
val updatedReactionGroup =
if (inserted) {
reactionGroup.increment(reaction.createdAt)
} else {
reactionGroup
}
val updatedReactionGroups =
reactionGroups.toMutableMap().apply { this[reaction.type] = updatedReactionGroup }
val updatedReactionCount = updatedReactionGroups.values.sumOf(ReactionGroupData::count)
return update(
updatedLatestReactions,
updatedReactionGroups,
updatedReactionCount,
updatedOwnReactions,
)
}

internal inline fun <T> removeReaction(
ownReactions: List<FeedsReactionData>,
latestReactions: List<FeedsReactionData>,
reactionGroups: Map<String, ReactionGroupData>,
reaction: FeedsReactionData,
currentUserId: String,
update:
(
latestReactions: List<FeedsReactionData>,
reactionGroups: Map<String, ReactionGroupData>?,
reactionCount: Int?,
ownReactions: List<FeedsReactionData>,
) -> T,
): T {
val ownReaction = reaction.user.id == currentUserId
val updatedOwnReactions =
if (ownReaction) {
ownReactions.filter { it.id != reaction.id }
} else {
ownReactions
}
val updatedLatestReactions = latestReactions.filter { it.id != reaction.id }
val removed =
updatedOwnReactions.size < ownReactions.size ||
updatedLatestReactions.size < latestReactions.size
val reactionGroup = reactionGroups[reaction.type]
if (reactionGroup == null) {
// If there is no reaction group for this type, just update latest and own reactions.
// Note: This is only a hypothetical case, as we should always have a reaction group.
return update(updatedLatestReactions, null, null, updatedOwnReactions)
}
// Decrement the count if the reaction was actually removed
val updatedReactionGroup =
if (removed) {
reactionGroup.decrement(reaction.createdAt)
} else {
reactionGroup
}
val updatedReactionGroups =
if (updatedReactionGroup.isEmpty) {
reactionGroups - reaction.type // Remove empty group
} else {
reactionGroups.toMutableMap().apply { this[reaction.type] = updatedReactionGroup }
}
val updatedReactionCount = updatedReactionGroups.values.sumOf(ReactionGroupData::count)
return update(
updatedLatestReactions,
updatedReactionGroups,
updatedReactionCount,
updatedOwnReactions,
)
}
Loading