[MM-35345]: fix unreads in threads and migration (#17938)

Summary
Fix unreads query for threads to check deleted option
Normalize unreads query to look for threads after given time
Fix migration to only run once, and increment timestamp by one so that threads are definitely marked as read.

Ticket Link
Fixes issues with https://mattermost.atlassian.net/browse/MM-35345
And possibly some issues with thread unreads.
Этот коммит содержится в:
Ashish Bhate
2021-07-16 19:26:16 +05:30
коммит произвёл GitHub
родитель 4ad0193d0a
Коммит f204c745cb
3 изменённых файлов: 45 добавлений и 8 удалений

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

@@ -130,7 +130,6 @@ func (s *SqlThreadStore) GetThreadsForUser(userId, teamId string, opts model.Get
model.Post
}
unreadRepliesQuery := "SELECT COUNT(Posts.Id) From Posts Where Posts.RootId=ThreadMemberships.PostId AND Posts.CreateAt >= ThreadMemberships.LastViewed"
fetchConditions := sq.And{
sq.Or{sq.Eq{"Channels.TeamId": teamId}, sq.Eq{"Channels.TeamId": ""}},
sq.Eq{"ThreadMemberships.UserId": userId},
@@ -159,7 +158,7 @@ func (s *SqlThreadStore) GetThreadsForUser(userId, teamId string, opts model.Get
LeftJoin("ThreadMemberships ON Posts.RootId = ThreadMemberships.PostId").
LeftJoin("Channels ON Posts.ChannelId = Channels.Id").
Where(fetchConditions).
Where("Posts.CreateAt >= ThreadMemberships.LastViewed").ToSql()
Where("Posts.CreateAt > ThreadMemberships.LastViewed").ToSql()
totalUnreadThreads, err := s.GetMaster().SelectInt(repliesQuery, repliesQueryArgs...)
totalUnreadThreadsChan <- store.StoreResult{Data: totalUnreadThreads, NErr: errors.Wrapf(err, "failed to get count unread on threads for user id=%s", userId)}
@@ -218,6 +217,24 @@ func (s *SqlThreadStore) GetThreadsForUser(userId, teamId string, opts model.Get
if opts.Unread {
newFetchConditions = sq.And{newFetchConditions, sq.Expr("ThreadMemberships.LastViewed < Threads.LastReplyAt")}
}
unreadRepliesFetchConditions := sq.And{
sq.Expr("Posts.RootId = ThreadMemberships.PostId"),
sq.Expr("Posts.CreateAt > ThreadMemberships.LastViewed"),
}
if !opts.Deleted {
unreadRepliesFetchConditions = sq.And{
unreadRepliesFetchConditions,
sq.Expr("Posts.DeleteAt = 0"),
}
}
unreadRepliesQuery, _ := sq.
Select("COUNT(Posts.Id)").
From("Posts").
Where(unreadRepliesFetchConditions).
MustSql()
var threads []*JoinedThread
query, args, _ := s.getQueryBuilder().
Select(`Threads.*,
@@ -358,7 +375,7 @@ func (s *SqlThreadStore) GetThreadForUser(teamId string, threadMembership *model
From("Posts").
Where(sq.And{
sq.Eq{"Posts.RootId": threadMembership.PostId},
sq.GtOrEq{"Posts.CreateAt": threadMembership.LastViewed},
sq.Gt{"Posts.CreateAt": threadMembership.LastViewed},
sq.Eq{"Posts.DeleteAt": 0},
}).MustSql()
@@ -651,8 +668,8 @@ func (s *SqlThreadStore) CollectThreadsWithNewerReplies(userId string, channelId
sq.Eq{"Threads.ChannelId": channelIds},
sq.Eq{"ChannelMembers.UserId": userId},
sq.Or{
sq.Expr("Threads.LastReplyAt >= ChannelMembers.LastViewedAt"),
sq.GtOrEq{"Threads.LastReplyAt": timestamp},
sq.Expr("Threads.LastReplyAt > ChannelMembers.LastViewedAt"),
sq.Gt{"Threads.LastReplyAt": timestamp},
},
}).
ToSql()