Skip to content

Commit 6857dfb

Browse files
authored
Add more handling of comment events (#126)
* Handle CommentDeleted for CommentReactionList * Handle CommentDeleted for CommentReplyList * Handle CommentAdded for CommentList * Handle comment events for Activity
1 parent a399988 commit 6857dfb

18 files changed

+363
-29
lines changed

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/model/ActivityOperations.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ internal fun ActivityData.upsertComment(comment: CommentData): ActivityData {
112112
/**
113113
* Removes a comment from the activity, updating the comment count and the list of comments.
114114
*
115-
* @param comment The comment to be removed.
115+
* @param commentId The ID of the comment to be removed.
116116
* @return A new [ActivityData] instance with the updated comments and comment count.
117117
*/
118-
internal fun ActivityData.removeComment(comment: CommentData): ActivityData {
119-
val updatedComments = this.comments.filter { it.id != comment.id }
118+
internal fun ActivityData.removeComment(commentId: String): ActivityData {
119+
val updatedComments = this.comments.filter { it.id != commentId }
120120
return this.copy(comments = updatedComments, commentCount = max(0, this.commentCount - 1))
121121
}
122122

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/ActivityListStateImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ internal class ActivityListStateImpl(
128128
_activities.update { current ->
129129
current.map { activity ->
130130
if (activity.id == comment.objectId) {
131-
activity.removeComment(comment)
131+
activity.removeComment(comment.id)
132132
} else {
133133
activity
134134
}

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/ActivityStateImpl.kt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@ package io.getstream.feeds.android.client.internal.state
1717

1818
import io.getstream.feeds.android.client.api.model.ActivityData
1919
import io.getstream.feeds.android.client.api.model.BookmarkData
20+
import io.getstream.feeds.android.client.api.model.CommentData
2021
import io.getstream.feeds.android.client.api.model.FeedsReactionData
2122
import io.getstream.feeds.android.client.api.model.PollData
2223
import io.getstream.feeds.android.client.api.model.PollVoteData
2324
import io.getstream.feeds.android.client.api.model.ThreadedCommentData
2425
import io.getstream.feeds.android.client.api.state.ActivityCommentListState
2526
import io.getstream.feeds.android.client.api.state.ActivityState
2627
import io.getstream.feeds.android.client.internal.model.deleteBookmark
28+
import io.getstream.feeds.android.client.internal.model.removeComment
29+
import io.getstream.feeds.android.client.internal.model.removeCommentReaction
2730
import io.getstream.feeds.android.client.internal.model.removeReaction
2831
import io.getstream.feeds.android.client.internal.model.removeVote
2932
import io.getstream.feeds.android.client.internal.model.update
3033
import io.getstream.feeds.android.client.internal.model.upsertBookmark
34+
import io.getstream.feeds.android.client.internal.model.upsertComment
35+
import io.getstream.feeds.android.client.internal.model.upsertCommentReaction
3136
import io.getstream.feeds.android.client.internal.model.upsertReaction
3237
import io.getstream.feeds.android.client.internal.model.upsertVote
3338
import kotlinx.coroutines.flow.MutableStateFlow
@@ -94,6 +99,26 @@ internal class ActivityStateImpl(
9499
_activity.update { current -> current?.upsertBookmark(bookmark, currentUserId) }
95100
}
96101

102+
override fun onCommentRemoved(commentId: String) {
103+
_activity.update { current -> current?.removeComment(commentId) }
104+
}
105+
106+
override fun onCommentUpserted(comment: CommentData) {
107+
_activity.update { current -> current?.upsertComment(comment) }
108+
}
109+
110+
override fun onCommentReactionRemoved(comment: CommentData, reaction: FeedsReactionData) {
111+
_activity.update { current ->
112+
current?.removeCommentReaction(comment, reaction, currentUserId)
113+
}
114+
}
115+
116+
override fun onCommentReactionUpserted(comment: CommentData, reaction: FeedsReactionData) {
117+
_activity.update { current ->
118+
current?.upsertCommentReaction(comment, reaction, currentUserId)
119+
}
120+
}
121+
97122
override fun onPollDeleted(pollId: String) {
98123
updatePoll(pollId) { null }
99124
}
@@ -175,6 +200,36 @@ internal interface ActivityStateUpdates {
175200
*/
176201
fun onBookmarkUpserted(bookmark: BookmarkData)
177202

203+
/**
204+
* Called when a comment is removed from the activity.
205+
*
206+
* @param commentId The ID of the comment that was removed.
207+
*/
208+
fun onCommentRemoved(commentId: String)
209+
210+
/**
211+
* Called when a comment is added to or updated in the activity.
212+
*
213+
* @param comment The comment that was added or updated.
214+
*/
215+
fun onCommentUpserted(comment: CommentData)
216+
217+
/**
218+
* Called when a reaction is removed from a comment.
219+
*
220+
* @param comment The comment from which the reaction was removed.
221+
* @param reaction The reaction that was removed.
222+
*/
223+
fun onCommentReactionRemoved(comment: CommentData, reaction: FeedsReactionData)
224+
225+
/**
226+
* Called when a reaction is added to or updated in a comment.
227+
*
228+
* @param comment The comment to which the reaction was added or updated.
229+
* @param reaction The reaction that was added or updated.
230+
*/
231+
fun onCommentReactionUpserted(comment: CommentData, reaction: FeedsReactionData)
232+
178233
/**
179234
* Called when the associated poll is deleted.
180235
*

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/CommentListImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal class CommentListImpl(
4545

4646
private val _state: CommentListStateImpl = CommentListStateImpl(query, currentUserId)
4747

48-
private val eventHandler = CommentListEventHandler(_state)
48+
private val eventHandler = CommentListEventHandler(query.filter, _state)
4949

5050
init {
5151
subscriptionManager.subscribe(eventHandler)

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/CommentListStateImpl.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ internal class CommentListStateImpl(
7373
}
7474
}
7575

76-
override fun onCommentUpdated(comment: CommentData) {
76+
override fun onCommentUpserted(comment: CommentData) {
7777
_comments.update { current ->
7878
current.upsertSorted(
7979
element = comment,
@@ -120,11 +120,11 @@ internal interface CommentListStateUpdates {
120120
fun onQueryMoreComments(result: PaginationResult<CommentData>)
121121

122122
/**
123-
* Handles the update of a comment in the list.
123+
* Handles the addition or update of a comment in the list.
124124
*
125-
* @param comment The updated comment data.
125+
* @param comment The comment data.
126126
*/
127-
fun onCommentUpdated(comment: CommentData)
127+
fun onCommentUpserted(comment: CommentData)
128128

129129
/**
130130
* Handles the removal of a comment from the list.

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/CommentReactionListStateImpl.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ internal class CommentReactionListStateImpl(override val query: CommentReactions
5656
override val pagination: PaginationData?
5757
get() = _pagination
5858

59+
override fun onCommentRemoved() {
60+
_reactions.update { emptyList() }
61+
}
62+
5963
override fun onQueryMoreReactions(
6064
result: PaginationResult<FeedsReactionData>,
6165
queryConfig: CommentReactionsQueryConfig,
@@ -83,6 +87,9 @@ internal interface CommentReactionListMutableState :
8387

8488
internal interface CommentReactionListStateUpdates {
8589

90+
/** Handles the deletion of the parent comment. */
91+
fun onCommentRemoved()
92+
8693
/**
8794
* Handles the successful loading of reactions.
8895
*

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/CommentReplyListStateImpl.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ internal class CommentReplyListStateImpl(
7575
}
7676

7777
override fun onCommentRemoved(commentId: String) {
78+
if (commentId == query.commentId) {
79+
// If the deleted comment is the parent comment, we clear the entire state
80+
_replies.update { emptyList() }
81+
} else {
82+
onReplyRemoved(commentId)
83+
}
84+
}
85+
86+
private fun onReplyRemoved(commentId: String) {
7887
_replies.update { current ->
7988
val filteredTopLevel = current.filter { it.id != commentId }
8089
if (filteredTopLevel.size != current.size) {

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/FeedStateImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ internal class FeedStateImpl(
203203

204204
override fun onCommentRemoved(comment: CommentData) {
205205
updateActivitiesWhere({ it.id == comment.objectId }) { activity ->
206-
activity.removeComment(comment)
206+
activity.removeComment(comment.id)
207207
}
208208
}
209209

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/event/handler/ActivityEventHandler.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,36 @@ internal class ActivityEventHandler(
8484
state.onBookmarkUpserted(event.bookmark)
8585
}
8686

87+
is StateUpdateEvent.CommentAdded -> {
88+
if (fid.rawValue != event.fid || event.comment.objectId != activityId) return
89+
state.onCommentUpserted(event.comment)
90+
}
91+
92+
is StateUpdateEvent.CommentDeleted -> {
93+
if (fid.rawValue != event.fid || event.comment.objectId != activityId) return
94+
state.onCommentRemoved(event.comment.id)
95+
}
96+
97+
is StateUpdateEvent.CommentUpdated -> {
98+
if (fid.rawValue != event.fid || event.comment.objectId != activityId) return
99+
state.onCommentUpserted(event.comment)
100+
}
101+
102+
is StateUpdateEvent.CommentReactionAdded -> {
103+
if (fid.rawValue != event.fid || event.comment.objectId != activityId) return
104+
state.onCommentReactionUpserted(event.comment, event.reaction)
105+
}
106+
107+
is StateUpdateEvent.CommentReactionDeleted -> {
108+
if (fid.rawValue != event.fid || event.comment.objectId != activityId) return
109+
state.onCommentReactionRemoved(event.comment, event.reaction)
110+
}
111+
112+
is StateUpdateEvent.CommentReactionUpdated -> {
113+
if (fid.rawValue != event.fid || event.comment.objectId != activityId) return
114+
state.onCommentReactionUpserted(event.comment, event.reaction)
115+
}
116+
87117
is StateUpdateEvent.PollDeleted -> {
88118
if (event.fid != fid.rawValue) return
89119
state.onPollDeleted(event.pollId)

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/state/event/handler/CommentListEventHandler.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,27 @@
1515
*/
1616
package io.getstream.feeds.android.client.internal.state.event.handler
1717

18+
import io.getstream.feeds.android.client.api.state.query.CommentsFilter
1819
import io.getstream.feeds.android.client.internal.state.CommentListStateUpdates
1920
import io.getstream.feeds.android.client.internal.state.event.StateUpdateEvent
21+
import io.getstream.feeds.android.client.internal.state.query.matches
2022
import io.getstream.feeds.android.client.internal.subscribe.StateUpdateEventListener
2123

22-
internal class CommentListEventHandler(private val state: CommentListStateUpdates) :
23-
StateUpdateEventListener {
24+
internal class CommentListEventHandler(
25+
private val filter: CommentsFilter?,
26+
private val state: CommentListStateUpdates,
27+
) : StateUpdateEventListener {
2428

2529
override fun onEvent(event: StateUpdateEvent) {
2630
when (event) {
31+
is StateUpdateEvent.CommentAdded -> {
32+
if (event.comment matches filter) {
33+
state.onCommentUpserted(event.comment)
34+
}
35+
}
36+
2737
is StateUpdateEvent.CommentDeleted -> state.onCommentRemoved(event.comment.id)
28-
is StateUpdateEvent.CommentUpdated -> state.onCommentUpdated(event.comment)
38+
is StateUpdateEvent.CommentUpdated -> state.onCommentUpserted(event.comment)
2939
is StateUpdateEvent.CommentReactionAdded ->
3040
state.onCommentReactionUpserted(event.comment, event.reaction)
3141

@@ -34,6 +44,7 @@ internal class CommentListEventHandler(private val state: CommentListStateUpdate
3444

3545
is StateUpdateEvent.CommentReactionUpdated ->
3646
state.onCommentReactionUpserted(event.comment, event.reaction)
47+
3748
else -> Unit
3849
}
3950
}

0 commit comments

Comments
 (0)