MM-15850/MM-15851 GetPosts Before/After sync by default (#11035)
Этот коммит содержится в:
@@ -581,98 +581,95 @@ func (s *SqlPostStore) GetPostsSince(channelId string, time int64, allowFromCach
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SqlPostStore) GetPostsBefore(channelId string, postId string, limit int, offset int) store.StoreChannel {
|
||||
func (s *SqlPostStore) GetPostsBefore(channelId string, postId string, limit int, offset int) (*model.PostList, *model.AppError) {
|
||||
return s.getPostsAround(channelId, postId, limit, offset, true)
|
||||
}
|
||||
|
||||
func (s *SqlPostStore) GetPostsAfter(channelId string, postId string, limit int, offset int) store.StoreChannel {
|
||||
func (s *SqlPostStore) GetPostsAfter(channelId string, postId string, limit int, offset int) (*model.PostList, *model.AppError) {
|
||||
return s.getPostsAround(channelId, postId, limit, offset, false)
|
||||
}
|
||||
|
||||
func (s *SqlPostStore) getPostsAround(channelId string, postId string, limit int, offset int, before bool) store.StoreChannel {
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
var direction string
|
||||
var sort string
|
||||
if before {
|
||||
direction = "<"
|
||||
sort = "DESC"
|
||||
} else {
|
||||
direction = ">"
|
||||
sort = "ASC"
|
||||
}
|
||||
func (s *SqlPostStore) getPostsAround(channelId string, postId string, limit int, offset int, before bool) (*model.PostList, *model.AppError) {
|
||||
var direction string
|
||||
var sort string
|
||||
if before {
|
||||
direction = "<"
|
||||
sort = "DESC"
|
||||
} else {
|
||||
direction = ">"
|
||||
sort = "ASC"
|
||||
}
|
||||
|
||||
var posts []*model.Post
|
||||
var parents []*model.Post
|
||||
_, err1 := s.GetReplica().Select(&posts,
|
||||
`SELECT
|
||||
*
|
||||
FROM
|
||||
Posts
|
||||
WHERE
|
||||
CreateAt `+direction+` (SELECT CreateAt FROM Posts WHERE Id = :PostId)
|
||||
AND ChannelId = :ChannelId
|
||||
AND DeleteAt = 0
|
||||
ORDER BY CreateAt `+sort+`
|
||||
LIMIT :Limit
|
||||
OFFSET :Offset`,
|
||||
map[string]interface{}{"ChannelId": channelId, "PostId": postId, "Limit": limit, "Offset": offset})
|
||||
_, err2 := s.GetReplica().Select(&parents,
|
||||
`SELECT
|
||||
q2.*
|
||||
FROM
|
||||
Posts q2
|
||||
INNER JOIN
|
||||
(SELECT DISTINCT
|
||||
q3.Id, q3.RootId
|
||||
FROM
|
||||
(SELECT
|
||||
Id, RootId
|
||||
FROM
|
||||
Posts
|
||||
WHERE
|
||||
CreateAt `+direction+` (SELECT CreateAt FROM Posts WHERE Id = :PostId)
|
||||
AND ChannelId = :ChannelId
|
||||
AND DeleteAt = 0
|
||||
ORDER BY CreateAt `+sort+`
|
||||
LIMIT :Limit OFFSET :Offset) q3 -- q3 contains the Id and RootId of every post in posts
|
||||
) q1 -- q1 is q3 with the duplicates removed
|
||||
ON q1.RootId = q2.Id -- This is the root post of a thread that appears in posts
|
||||
OR q1.Id = q2.RootId -- This is a comment on a post in posts
|
||||
OR (q2.RootId != '' AND q1.RootId = q2.RootId) -- This is a comment on a thread that appears in posts
|
||||
WHERE
|
||||
ChannelId = :ChannelId
|
||||
var posts []*model.Post
|
||||
var parents []*model.Post
|
||||
_, err := s.GetReplica().Select(&posts,
|
||||
`SELECT
|
||||
*
|
||||
FROM
|
||||
Posts
|
||||
WHERE
|
||||
CreateAt `+direction+` (SELECT CreateAt FROM Posts WHERE Id = :PostId)
|
||||
AND ChannelId = :ChannelId
|
||||
AND DeleteAt = 0
|
||||
ORDER BY CreateAt DESC`,
|
||||
map[string]interface{}{"ChannelId": channelId, "PostId": postId, "Limit": limit, "Offset": offset})
|
||||
ORDER BY CreateAt `+sort+`
|
||||
LIMIT :Limit
|
||||
OFFSET :Offset`,
|
||||
map[string]interface{}{"ChannelId": channelId, "PostId": postId, "Limit": limit, "Offset": offset})
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlPostStore.GetPostContext", "store.sql_post.get_posts_around.get.app_error", nil, "channelId="+channelId+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
_, err = s.GetReplica().Select(&parents,
|
||||
`SELECT
|
||||
q2.*
|
||||
FROM
|
||||
Posts q2
|
||||
INNER JOIN
|
||||
(SELECT DISTINCT
|
||||
q3.Id, q3.RootId
|
||||
FROM
|
||||
(SELECT
|
||||
Id, RootId
|
||||
FROM
|
||||
Posts
|
||||
WHERE
|
||||
CreateAt `+direction+` (SELECT CreateAt FROM Posts WHERE Id = :PostId)
|
||||
AND ChannelId = :ChannelId
|
||||
AND DeleteAt = 0
|
||||
ORDER BY CreateAt `+sort+`
|
||||
LIMIT :Limit OFFSET :Offset) q3 -- q3 contains the Id and RootId of every post in posts
|
||||
) q1 -- q1 is q3 with the duplicates removed
|
||||
ON q1.RootId = q2.Id -- This is the root post of a thread that appears in posts
|
||||
OR q1.Id = q2.RootId -- This is a comment on a post in posts
|
||||
OR (q2.RootId != '' AND q1.RootId = q2.RootId) -- This is a comment on a thread that appears in posts
|
||||
WHERE
|
||||
ChannelId = :ChannelId
|
||||
AND DeleteAt = 0
|
||||
ORDER BY CreateAt DESC`,
|
||||
map[string]interface{}{"ChannelId": channelId, "PostId": postId, "Limit": limit, "Offset": offset})
|
||||
|
||||
if err1 != nil {
|
||||
result.Err = model.NewAppError("SqlPostStore.GetPostContext", "store.sql_post.get_posts_around.get.app_error", nil, "channelId="+channelId+err1.Error(), http.StatusInternalServerError)
|
||||
} else if err2 != nil {
|
||||
result.Err = model.NewAppError("SqlPostStore.GetPostContext", "store.sql_post.get_posts_around.get_parent.app_error", nil, "channelId="+channelId+err2.Error(), http.StatusInternalServerError)
|
||||
} else {
|
||||
list := model.NewPostList()
|
||||
if err != nil {
|
||||
return nil, model.NewAppError("SqlPostStore.GetPostContext", "store.sql_post.get_posts_around.get_parent.app_error", nil, "channelId="+channelId+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
list := model.NewPostList()
|
||||
|
||||
// We need to flip the order if we selected backwards
|
||||
if before {
|
||||
for _, p := range posts {
|
||||
list.AddPost(p)
|
||||
list.AddOrder(p.Id)
|
||||
}
|
||||
} else {
|
||||
l := len(posts)
|
||||
for i := range posts {
|
||||
list.AddPost(posts[l-i-1])
|
||||
list.AddOrder(posts[l-i-1].Id)
|
||||
}
|
||||
}
|
||||
|
||||
for _, p := range parents {
|
||||
list.AddPost(p)
|
||||
}
|
||||
|
||||
result.Data = list
|
||||
// We need to flip the order if we selected backwards
|
||||
if before {
|
||||
for _, p := range posts {
|
||||
list.AddPost(p)
|
||||
list.AddOrder(p.Id)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
l := len(posts)
|
||||
for i := range posts {
|
||||
list.AddPost(posts[l-i-1])
|
||||
list.AddOrder(posts[l-i-1].Id)
|
||||
}
|
||||
}
|
||||
|
||||
for _, p := range parents {
|
||||
list.AddPost(p)
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
func (s *SqlPostStore) getRootPosts(channelId string, offset int, limit int) store.StoreChannel {
|
||||
|
||||
Ссылка в новой задаче
Block a user