MM-29987 Implement new collapsed threads API (#16091)

Этот коммит содержится в:
Eli Yukelzon
2020-11-08 10:36:46 +02:00
коммит произвёл GitHub
родитель 483441cea2
Коммит 45e340b5be
22 изменённых файлов: 1155 добавлений и 49 удалений

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

@@ -515,20 +515,17 @@ func (s *SqlPostStore) Delete(postId string, time int64, deleteByID string) erro
return errors.Wrap(err, "failed to update Posts")
}
return s.cleanupThreads(post.Id, post.RootId, post.UserId)
return s.cleanupThreads(post.Id, post.RootId, post.UserId, false)
}
func (s *SqlPostStore) permanentDelete(postId string) error {
var post model.Post
err := s.GetReplica().SelectOne(&post, "SELECT * FROM Posts WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": postId})
if err != nil && err != sql.ErrNoRows {
if err != sql.ErrNoRows {
return errors.Wrapf(err, "failed to get Post with id=%s", postId)
}
if err = s.cleanupThreads(post.Id, post.RootId, post.UserId); err != nil {
return errors.Wrapf(err, "failed to cleanup threads for Post with id=%s", postId)
}
return errors.Wrapf(err, "failed to get Post with id=%s", postId)
}
if err = s.cleanupThreads(post.Id, post.RootId, post.UserId, true); err != nil {
return errors.Wrapf(err, "failed to cleanup threads for Post with id=%s", postId)
}
if _, err = s.GetMaster().Exec("DELETE FROM Posts WHERE Id = :Id OR RootId = :RootId", map[string]interface{}{"Id": postId, "RootId": postId}); err != nil {
@@ -552,7 +549,7 @@ func (s *SqlPostStore) permanentDeleteAllCommentByUser(userId string) error {
}
for _, ids := range results {
if err = s.cleanupThreads(ids.Id, ids.RootId, userId); err != nil {
if err = s.cleanupThreads(ids.Id, ids.RootId, userId, true); err != nil {
return err
}
}
@@ -608,7 +605,7 @@ func (s *SqlPostStore) PermanentDeleteByChannel(channelId string) error {
}
for _, ids := range results {
if err = s.cleanupThreads(ids.Id, ids.RootId, ids.UserId); err != nil {
if err = s.cleanupThreads(ids.Id, ids.RootId, ids.UserId, true); err != nil {
return err
}
}
@@ -1921,7 +1918,16 @@ func (s *SqlPostStore) GetOldestEntityCreationTime() (int64, error) {
return oldest, nil
}
func (s *SqlPostStore) cleanupThreads(postId, rootId, userId string) error {
func (s *SqlPostStore) cleanupThreads(postId, rootId, userId string, permanent bool) error {
if permanent {
if _, err := s.GetMaster().Exec("DELETE FROM Threads WHERE PostId = :Id", map[string]interface{}{"Id": postId}); err != nil {
return errors.Wrap(err, "failed to delete Threads")
}
if _, err := s.GetMaster().Exec("DELETE FROM ThreadMemberships WHERE PostId = :Id", map[string]interface{}{"Id": postId}); err != nil {
return errors.Wrap(err, "failed to delete ThreadMemberships")
}
return nil
}
if len(rootId) > 0 {
thread, err := s.Thread().Get(rootId)
if err != nil {
@@ -1937,12 +1943,6 @@ func (s *SqlPostStore) cleanupThreads(postId, rootId, userId string) error {
}
}
}
if _, err := s.GetMaster().Exec("DELETE FROM Threads WHERE PostId = :Id", map[string]interface{}{"Id": postId}); err != nil {
return errors.Wrap(err, "failed to delete Threads")
}
if _, err := s.GetMaster().Exec("DELETE FROM ThreadMemberships WHERE PostId = :Id", map[string]interface{}{"Id": postId}); err != nil {
return errors.Wrap(err, "failed to delete ThreadMemberships")
}
return nil
}

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

@@ -108,6 +108,117 @@ func (s *SqlThreadStore) Get(id string) (*model.Thread, error) {
return &thread, nil
}
func (s *SqlThreadStore) GetThreadsForUser(userId string, opts model.GetUserThreadsOpts) (*model.Threads, error) {
type JoinedThread struct {
PostId string
ReplyCount int64
LastReplyAt int64
LastViewedAt int64
Participants model.StringArray
model.Post
}
var threads []*JoinedThread
fetchConditions := sq.And{
sq.Eq{"Posts.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)
}
var userIds []string
userIdMap := map[string]bool{}
for _, thread := range threads {
for _, participantId := range thread.Participants {
if _, ok := userIdMap[participantId]; !ok {
userIdMap[participantId] = true
userIds = append(userIds, participantId)
}
}
}
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 {
return nil, errors.Wrapf(err, "failed to get threads for user id=%s", userId)
}
} else {
for _, userId := range userIds {
users = append(users, &model.User{Id: userId})
}
}
result := &model.Threads{
Total: 0,
Threads: nil,
}
for _, thread := range threads {
var participants []*model.User
for _, participantId := range thread.Participants {
var participant *model.User
for _, u := range users {
if u.Id == participantId {
participant = u
break
}
}
if participant == nil {
return nil, errors.New("cannot find thread participant with id=" + participantId)
}
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,
})
}
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()
if _, err := s.GetMaster().Exec(query, args...); err != nil {
return errors.Wrapf(err, "failed to update thread read state for user id=%s", userId)
}
return nil
}
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()
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)
}
return nil
}
func (s *SqlThreadStore) Delete(threadId string) error {
query, args, _ := s.getQueryBuilder().Delete("Threads").Where(sq.Eq{"PostId": threadId}).ToSql()
if _, err := s.GetMaster().Exec(query, args...); err != nil {
@@ -162,12 +273,12 @@ func (s *SqlThreadStore) DeleteMembershipForUser(userId string, postId string) e
return nil
}
func (s *SqlThreadStore) CreateMembershipIfNeeded(userId, postId string) error {
func (s *SqlThreadStore) CreateMembershipIfNeeded(userId, postId string, following bool) error {
membership, err := s.GetMembershipForUser(userId, postId)
now := utils.MillisFromTime(time.Now())
if err == nil {
if !membership.Following {
membership.Following = true
if !membership.Following || membership.Following != following {
membership.Following = following
membership.LastUpdated = now
_, err = s.UpdateMembership(membership)
}
@@ -182,7 +293,7 @@ func (s *SqlThreadStore) CreateMembershipIfNeeded(userId, postId string) error {
_, err = s.SaveMembership(&model.ThreadMembership{
PostId: postId,
UserId: userId,
Following: true,
Following: following,
LastViewed: 0,
LastUpdated: now,
})