MM-30558 - Add unreadReplies and unreadMentions to thread membership (#16304)

Этот коммит содержится в:
Eli Yukelzon
2020-12-06 10:02:53 +02:00
коммит произвёл GitHub
родитель cd9185fa23
Коммит 86e228b6c6
18 изменённых файлов: 653 добавлений и 187 удалений

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

@@ -108,44 +108,119 @@ func (s *SqlThreadStore) Get(id string) (*model.Thread, error) {
return &thread, nil
}
func (s *SqlThreadStore) GetThreadsForUser(userId string, opts model.GetUserThreadsOpts) (*model.Threads, error) {
func (s *SqlThreadStore) GetThreadsForUser(userId, teamId string, opts model.GetUserThreadsOpts) (*model.Threads, error) {
type JoinedThread struct {
PostId string
ReplyCount int64
LastReplyAt int64
LastViewedAt int64
Participants model.StringArray
PostId string
ReplyCount int64
LastReplyAt int64
LastViewedAt int64
UnreadReplies int64
UnreadMentions int64
Participants model.StringArray
model.Post
}
var threads []*JoinedThread
unreadRepliesQuery := "SELECT COUNT(Posts.Id) From Posts Where Posts.RootId=ThreadMemberships.PostId AND Posts.UpdateAt >= ThreadMemberships.LastViewed AND Posts.DeleteAt=0"
fetchConditions := sq.And{
sq.Or{sq.Eq{"Channels.TeamId": teamId}, sq.Eq{"Channels.TeamId": ""}},
sq.Eq{"ThreadMemberships.UserId": userId},
sq.Eq{"ThreadMemberships.Following": true},
}
if !opts.Deleted {
fetchConditions = sq.And{fetchConditions, sq.Eq{"Posts.DeleteAt": 0}}
}
if opts.Since > 0 {
fetchConditions = sq.And{fetchConditions, sq.GtOrEq{"Threads.LastReplyAt": opts.Since}}
}
pageSize := uint64(30)
if opts.PageSize == 0 {
pageSize = opts.PageSize
}
query, args, _ := s.getQueryBuilder().
Select("Threads.*, Posts.*, ThreadMemberships.LastViewed as LastViewedAt").
From("Threads").
LeftJoin("Posts ON Posts.Id = Threads.PostId").
LeftJoin("ThreadMemberships ON ThreadMemberships.PostId = Threads.PostId").
OrderBy("Threads.LastReplyAt DESC").
Offset(pageSize * opts.Page).
Limit(pageSize).
Where(fetchConditions).ToSql()
_, err := s.GetReplica().Select(&threads, query, args...)
if err != nil {
return nil, errors.Wrapf(err, "failed to get threads for user id=%s", userId)
totalUnreadRepliesChan := make(chan store.StoreResult, 1)
totalCountChan := make(chan store.StoreResult, 1)
totalUnreadMentionsChan := make(chan store.StoreResult, 1)
threadsChan := make(chan store.StoreResult, 1)
go func() {
repliesQuery, repliesQueryArgs, _ := s.getQueryBuilder().
Select("COUNT(Posts.Id)").
From("Posts").
LeftJoin("ThreadMemberships ON Posts.RootId = ThreadMemberships.PostId").
LeftJoin("Channels ON Posts.ChannelId = Channels.Id").
Where(fetchConditions).
Where("Posts.UpdateAt >= ThreadMemberships.LastViewed").ToSql()
totalUnreadReplies, err := s.GetMaster().SelectInt(repliesQuery, repliesQueryArgs...)
totalUnreadRepliesChan <- store.StoreResult{Data: totalUnreadReplies, NErr: errors.Wrapf(err, "failed to get count replies on threads for user id=%s", userId)}
close(totalUnreadRepliesChan)
}()
go func() {
threadsQuery, threadsQueryArgs, _ := s.getQueryBuilder().
Select("COUNT(ThreadMemberships.PostId)").
LeftJoin("Threads ON Threads.PostId = ThreadMemberships.PostId").
LeftJoin("Channels ON Threads.ChannelId = Channels.Id").
From("ThreadMemberships").
Where(fetchConditions).ToSql()
totalCount, err := s.GetMaster().SelectInt(threadsQuery, threadsQueryArgs...)
totalCountChan <- store.StoreResult{Data: totalCount, NErr: err}
close(totalCountChan)
}()
go func() {
mentionsQuery, mentionsQueryArgs, _ := s.getQueryBuilder().
Select("COALESCE(SUM(ThreadMemberships.UnreadMentions),0)").
From("ThreadMemberships").
LeftJoin("Threads ON Threads.PostId = ThreadMemberships.PostId").
LeftJoin("Channels ON Threads.ChannelId = Channels.Id").
Where(fetchConditions).ToSql()
totalUnreadMentions, err := s.GetMaster().SelectInt(mentionsQuery, mentionsQueryArgs...)
totalUnreadMentionsChan <- store.StoreResult{Data: totalUnreadMentions, NErr: err}
close(totalUnreadMentionsChan)
}()
go func() {
newFetchConditions := fetchConditions
if !opts.Deleted {
newFetchConditions = sq.And{fetchConditions, sq.Eq{"Posts.DeleteAt": 0}}
}
if opts.Since > 0 {
newFetchConditions = sq.And{newFetchConditions, sq.GtOrEq{"Threads.LastReplyAt": opts.Since}}
}
var threads []*JoinedThread
query, args, _ := s.getQueryBuilder().
Select("Threads.*, Posts.*, ThreadMemberships.LastViewed as LastViewedAt, ThreadMemberships.UnreadMentions as UnreadMentions").
From("Threads").
Column(sq.Alias(sq.Expr(unreadRepliesQuery), "UnreadReplies")).
LeftJoin("Posts ON Posts.Id = Threads.PostId").
LeftJoin("Channels ON Posts.ChannelId = Channels.Id").
LeftJoin("ThreadMemberships ON ThreadMemberships.PostId = Threads.PostId").
Where(newFetchConditions).
OrderBy("Threads.LastReplyAt DESC").
Offset(pageSize * opts.Page).
Limit(pageSize).ToSql()
_, err := s.GetReplica().Select(&threads, query, args...)
threadsChan <- store.StoreResult{Data: threads, NErr: err}
close(threadsChan)
}()
threadsResult := <-threadsChan
if threadsResult.NErr != nil {
return nil, threadsResult.NErr
}
threads := threadsResult.Data.([]*JoinedThread)
totalUnreadMentionsResult := <-totalUnreadMentionsChan
if totalUnreadMentionsResult.NErr != nil {
return nil, totalUnreadMentionsResult.NErr
}
totalUnreadMentions := totalUnreadMentionsResult.Data.(int64)
totalCountResult := <-totalCountChan
if totalCountResult.NErr != nil {
return nil, totalCountResult.NErr
}
totalCount := totalCountResult.Data.(int64)
totalUnreadRepliesResult := <-totalUnreadRepliesChan
if totalUnreadRepliesResult.NErr != nil {
return nil, totalUnreadRepliesResult.NErr
}
totalUnreadReplies := totalUnreadRepliesResult.Data.(int64)
var userIds []string
userIdMap := map[string]bool{}
@@ -159,9 +234,8 @@ func (s *SqlThreadStore) GetThreadsForUser(userId string, opts model.GetUserThre
}
var users []*model.User
if opts.Extended {
query, args, _ = s.getQueryBuilder().Select("*").From("Users").Where(sq.Eq{"Id": userIds}).ToSql()
_, err = s.GetReplica().Select(&users, query, args...)
if err != nil {
query, args, _ := s.getQueryBuilder().Select("*").From("Users").Where(sq.Eq{"Id": userIds}).ToSql()
if _, err := s.GetReplica().Select(&users, query, args...); err != nil {
return nil, errors.Wrapf(err, "failed to get threads for user id=%s", userId)
}
} else {
@@ -171,8 +245,10 @@ func (s *SqlThreadStore) GetThreadsForUser(userId string, opts model.GetUserThre
}
result := &model.Threads{
Total: 0,
Threads: nil,
Total: totalCount,
Threads: nil,
TotalUnreadMentions: totalUnreadMentions,
TotalUnreadReplies: totalUnreadReplies,
}
for _, thread := range threads {
@@ -191,20 +267,37 @@ func (s *SqlThreadStore) GetThreadsForUser(userId string, opts model.GetUserThre
participants = append(participants, participant)
}
result.Threads = append(result.Threads, &model.ThreadResponse{
PostId: thread.PostId,
ReplyCount: thread.ReplyCount,
LastReplyAt: thread.LastReplyAt,
LastViewedAt: thread.LastViewedAt,
Participants: participants,
Post: &thread.Post,
PostId: thread.PostId,
ReplyCount: thread.ReplyCount,
LastReplyAt: thread.LastReplyAt,
LastViewedAt: thread.LastViewedAt,
UnreadReplies: thread.UnreadReplies,
UnreadMentions: thread.UnreadMentions,
Participants: participants,
Post: &thread.Post,
})
}
return result, nil
}
func (s *SqlThreadStore) MarkAllAsRead(userId string, timestamp int64) error {
query, args, _ := s.getQueryBuilder().Update("ThreadMemberships").Where(sq.Eq{"UserId": userId}).Set("LastViewed", timestamp).ToSql()
func (s *SqlThreadStore) MarkAllAsRead(userId, teamId string) error {
memberships, err := s.GetMembershipsForUser(userId, teamId)
if err != nil {
return err
}
var membershipIds []string
for _, m := range memberships {
membershipIds = append(membershipIds, m.PostId)
}
timestamp := model.GetMillis()
query, args, _ := s.getQueryBuilder().
Update("ThreadMemberships").
Where(sq.Eq{"PostId": membershipIds}).
Where(sq.Eq{"UserId": userId}).
Set("LastViewed", timestamp).
Set("UnreadMentions", 0).
ToSql()
if _, err := s.GetMaster().Exec(query, args...); err != nil {
return errors.Wrapf(err, "failed to update thread read state for user id=%s", userId)
}
@@ -212,7 +305,11 @@ func (s *SqlThreadStore) MarkAllAsRead(userId string, timestamp int64) error {
}
func (s *SqlThreadStore) MarkAsRead(userId, threadId string, timestamp int64) error {
query, args, _ := s.getQueryBuilder().Update("ThreadMemberships").Where(sq.Eq{"UserId": userId}, sq.Eq{"PostId": threadId}).Set("LastViewed", timestamp).ToSql()
query, args, _ := s.getQueryBuilder().
Update("ThreadMemberships").
Where(sq.Eq{"UserId": userId}, sq.Eq{"PostId": threadId}).
Set("LastViewed", timestamp).
ToSql()
if _, err := s.GetMaster().Exec(query, args...); err != nil {
return errors.Wrapf(err, "failed to update thread read state for user id=%s thread_id=%v", userId, threadId)
}
@@ -244,9 +341,19 @@ func (s *SqlThreadStore) UpdateMembership(membership *model.ThreadMembership) (*
return membership, nil
}
func (s *SqlThreadStore) GetMembershipsForUser(userId string) ([]*model.ThreadMembership, error) {
func (s *SqlThreadStore) GetMembershipsForUser(userId, teamId string) ([]*model.ThreadMembership, error) {
var memberships []*model.ThreadMembership
_, err := s.GetReplica().Select(&memberships, "SELECT * from ThreadMemberships WHERE UserId = :UserId", map[string]interface{}{"UserId": userId})
query, args, _ := s.getQueryBuilder().
Select("ThreadMemberships.*").
Join("Threads ON Threads.PostId = ThreadMemberships.PostId").
Join("Channels ON Threads.ChannelId = Channels.Id").
From("ThreadMemberships").
Where(sq.Or{sq.Eq{"Channels.TeamId": teamId}, sq.Eq{"Channels.TeamId": ""}}).
Where(sq.Eq{"ThreadMemberships.UserId": userId}).
ToSql()
_, err := s.GetReplica().Select(&memberships, query, args...)
if err != nil {
return nil, errors.Wrapf(err, "failed to get thread membership with userid=%s", userId)
}