From 5396c530bf630f3906f456348d6437df31771246 Mon Sep 17 00:00:00 2001 From: Kyriakos Z <3829551+koox00@users.noreply.github.com> Date: Wed, 8 Jun 2022 21:01:58 +0300 Subject: [PATCH] MM-42581: fixes unread threads on user channel add (#20181) * MM-42581: fixes unread threads on user channel add Currently when we are adding a user to a channel we don't send previous values for unread replies and mentions. This is resulting the thread to not be marked as unread in the UI, since we rely on the previous values for that. This commit fixes the issue by returning previous unread values of 0. * Adds test Co-authored-by: Mattermod --- api4/channel_test.go | 41 +++++++++++++++++++++++++++++++++++------ app/user.go | 2 ++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/api4/channel_test.go b/api4/channel_test.go index 8a94d20177..38dc0e8cd9 100644 --- a/api4/channel_test.go +++ b/api4/channel_test.go @@ -3077,9 +3077,14 @@ func TestAddChannelMemberFromThread(t *testing.T) { _, _, err := th.SystemAdminClient.AddTeamMember(team.Id, user3.Id) require.NoError(t, err) + wsClient, err2 := th.CreateWebSocketClient() + require.NoError(t, err2) + defer wsClient.Close() + wsClient.Listen() + publicChannel := th.CreatePublicChannel() - _, resp, err := th.Client.AddChannelMember(publicChannel.Id, user.Id) + _, resp, err := th.Client.AddChannelMember(publicChannel.Id, user3.Id) require.NoError(t, err) CheckCreatedStatus(t, resp) _, resp, err = th.Client.AddChannelMember(publicChannel.Id, user2.Id) @@ -3089,7 +3094,7 @@ func TestAddChannelMemberFromThread(t *testing.T) { post := &model.Post{ ChannelId: publicChannel.Id, Message: "A root post", - UserId: user.Id, + UserId: user3.Id, } rpost, _, err := th.SystemAdminClient.CreatePost(post) require.NoError(t, err) @@ -3097,7 +3102,7 @@ func TestAddChannelMemberFromThread(t *testing.T) { _, _, err = th.SystemAdminClient.CreatePost( &model.Post{ ChannelId: publicChannel.Id, - Message: "A reply post with mention @" + user3.Username, + Message: "A reply post with mention @" + user.Username, UserId: user2.Id, RootId: rpost.Id, }) @@ -3106,22 +3111,46 @@ func TestAddChannelMemberFromThread(t *testing.T) { _, _, err = th.SystemAdminClient.CreatePost( &model.Post{ ChannelId: publicChannel.Id, - Message: "Another reply post with mention @" + user3.Username, + Message: "Another reply post with mention @" + user.Username, UserId: user2.Id, RootId: rpost.Id, }) require.NoError(t, err) // Simulate adding a user to a channel from a thread - _, _, err = th.SystemAdminClient.AddChannelMemberWithRootId(publicChannel.Id, user3.Id, rpost.Id) + _, _, err = th.SystemAdminClient.AddChannelMemberWithRootId(publicChannel.Id, user.Id, rpost.Id) require.NoError(t, err) // Threadmembership should exist for added user - ut, _, err := th.SystemAdminClient.GetUserThread(user3.Id, team.Id, rpost.Id, false) + ut, _, err := th.Client.GetUserThread(user.Id, team.Id, rpost.Id, false) require.NoError(t, err) // Should have two mentions. There might be a race condition // here between the "added user to the channel" message and the GetUserThread call require.LessOrEqual(t, int64(2), ut.UnreadMentions) + + var caught bool + func() { + for { + select { + case ev := <-wsClient.EventChannel: + if ev.EventType() == model.WebsocketEventThreadUpdated { + caught = true + var thread model.ThreadResponse + data := ev.GetData() + jsonErr := json.Unmarshal([]byte(data["thread"].(string)), &thread) + + require.NoError(t, jsonErr) + require.EqualValues(t, int64(2), thread.UnreadReplies) + require.EqualValues(t, int64(2), thread.UnreadMentions) + require.EqualValues(t, float64(0), data["previous_unread_replies"]) + require.EqualValues(t, float64(0), data["previous_unread_mentions"]) + } + case <-time.After(1 * time.Second): + return + } + } + }() + require.Truef(t, caught, "User should have received %s event", model.WebsocketEventThreadUpdated) } func TestAddChannelMemberAddMyself(t *testing.T) { diff --git a/app/user.go b/app/user.go index 9bd21cce07..408ce457a6 100644 --- a/app/user.go +++ b/app/user.go @@ -2489,6 +2489,8 @@ func (a *App) UpdateThreadFollowForUserFromChannelAdd(userID, teamID, threadID s mlog.Warn("Failed to encode thread to JSON") } message.Add("thread", string(payload)) + message.Add("previous_unread_replies", int64(0)) + message.Add("previous_unread_mentions", int64(0)) a.Publish(message) return nil