MM-41565,MM-42473: fixes incorrect unread thread count (#19962)

* MM-41565,MM-42473: fixes incorrect unread thread count

The infamous "-1 unread threads" bug, was due to incorrectly counting
previous unread mentions and replies.

When we calculate the previous counts, we didn't take into account if
a thread was newly followed one or not. So when a user gets added to a
thread via a mention receives a WS event with the previous count values
calculated by subtracting "1" from the current count values.
In the result the webapp subtracts the previous values for the thread
from the total values of all threads for the user.

This is wrong since the user just followed the thread those previous values
should be 0. Which results into showing a negative value of unread
threads to the user.

This commit fixes this issue by zeroing the previous count values when
the user was not following the thread previously.

* Adds test, and addresses comments

* Changes test's description

* Removes unneeded assertion

* Empty commit just to re-build
Этот коммит содержится в:
Kyriakos Z
2022-04-15 19:47:04 +03:00
коммит произвёл GitHub
родитель ed3b3b57de
Коммит b15cd9d29f
2 изменённых файлов: 58 добавлений и 7 удалений

Просмотреть файл

@@ -6116,6 +6116,50 @@ func TestThreadSocketEvents(t *testing.T) {
require.Truef(t, caught, "User should have received %s event", model.WebsocketEventThreadUpdated)
}
})
t.Run("Listen for thread updated event after create post when not previously following the thread", func(t *testing.T) {
rpost2 := &model.Post{ChannelId: th.BasicChannel.Id, UserId: th.BasicUser2.Id, Message: "root post"}
var appErr *model.AppError
rpost2, appErr = th.App.CreatePostAsUser(th.Context, rpost2, th.Context.Session().Id, false)
require.Nil(t, appErr)
reply1 := &model.Post{ChannelId: th.BasicChannel.Id, UserId: th.BasicUser2.Id, Message: "reply 1", RootId: rpost2.Id}
reply2 := &model.Post{ChannelId: th.BasicChannel.Id, UserId: th.BasicUser2.Id, Message: "reply 2", RootId: rpost2.Id}
reply3 := &model.Post{ChannelId: th.BasicChannel.Id, UserId: th.BasicUser2.Id, Message: "mention @" + th.BasicUser.Username, RootId: rpost2.Id}
_, appErr = th.App.CreatePostAsUser(th.Context, reply1, th.Context.Session().Id, false)
require.Nil(t, appErr)
_, appErr = th.App.CreatePostAsUser(th.Context, reply2, th.Context.Session().Id, false)
require.Nil(t, appErr)
_, appErr = th.App.CreatePostAsUser(th.Context, reply3, th.Context.Session().Id, false)
require.Nil(t, appErr)
count := 0
func() {
for {
select {
case ev := <-userWSClient.EventChannel:
if ev.EventType() == model.WebsocketEventThreadUpdated {
count++
data := ev.GetData()
var thread model.ThreadResponse
jsonErr := json.Unmarshal([]byte(data["thread"].(string)), &thread)
require.NoError(t, jsonErr)
require.Equal(t, int64(0), int64(data["previous_unread_replies"].(float64)))
require.Equal(t, int64(0), int64(data["previous_unread_mentions"].(float64)))
require.Equal(t, int64(3), thread.UnreadReplies)
require.Equal(t, int64(1), thread.UnreadMentions)
}
case <-time.After(1 * time.Second):
return
}
}
}()
require.Equalf(t, 1, count, "User should have received 1 %s event", model.WebsocketEventThreadUpdated)
})
}
func TestFollowThreads(t *testing.T) {