summaryrefslogtreecommitdiffstats
path: root/store/sql_post_store.go
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2015-11-04 07:51:19 -0500
committerJoram Wilander <jwawilander@gmail.com>2015-11-04 07:51:19 -0500
commit0cc985f6f4c439eb9a951d007628849d15a16087 (patch)
treedc9ef286cdfaa4a86a9ccab1d6a4ef3d3c0134ca /store/sql_post_store.go
parent87c4b4fbf44662b1ff70e245126921f24d0829a4 (diff)
parent188182a3a4499965dbbde254aed0ae5e60e71dc5 (diff)
downloadchat-0cc985f6f4c439eb9a951d007628849d15a16087.tar.gz
chat-0cc985f6f4c439eb9a951d007628849d15a16087.tar.bz2
chat-0cc985f6f4c439eb9a951d007628849d15a16087.zip
Merge pull request #1285 from mattermost/plt-824
PLT-824 Adding ability to fetch posts around a specifc post
Diffstat (limited to 'store/sql_post_store.go')
-rw-r--r--store/sql_post_store.go98
1 files changed, 98 insertions, 0 deletions
diff --git a/store/sql_post_store.go b/store/sql_post_store.go
index de8c4f356..fdae20f60 100644
--- a/store/sql_post_store.go
+++ b/store/sql_post_store.go
@@ -332,6 +332,104 @@ func (s SqlPostStore) GetPostsSince(channelId string, time int64) StoreChannel {
return storeChannel
}
+func (s SqlPostStore) GetPostsBefore(channelId string, postId string, numPosts int, offset int) StoreChannel {
+ return s.getPostsAround(channelId, postId, numPosts, offset, true)
+}
+
+func (s SqlPostStore) GetPostsAfter(channelId string, postId string, numPosts int, offset int) StoreChannel {
+ return s.getPostsAround(channelId, postId, numPosts, offset, false)
+}
+
+func (s SqlPostStore) getPostsAround(channelId string, postId string, numPosts int, offset int, before bool) StoreChannel {
+ storeChannel := make(StoreChannel)
+
+ go func() {
+ result := StoreResult{}
+
+ 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 :NumPosts
+ OFFSET :Offset)`,
+ map[string]interface{}{"ChannelId": channelId, "PostId": postId, "NumPosts": numPosts, "Offset": offset})
+ _, err2 := s.GetReplica().Select(&parents,
+ `(SELECT
+ *
+ FROM
+ Posts
+ WHERE
+ Id
+ IN
+ (SELECT * FROM (SELECT
+ RootId
+ FROM
+ Posts
+ WHERE
+ (CreateAt `+direction+` (SELECT CreateAt FROM Posts WHERE Id = :PostId)
+ AND ChannelId = :ChannelId
+ AND DeleteAt = 0)
+ ORDER BY CreateAt `+sort+`
+ LIMIT :NumPosts
+ OFFSET :Offset)
+ temp_tab))
+ ORDER BY CreateAt DESC`,
+ map[string]interface{}{"ChannelId": channelId, "PostId": postId, "NumPosts": numPosts, "Offset": offset})
+
+ if err1 != nil {
+ result.Err = model.NewAppError("SqlPostStore.GetPostContext", "We couldn't get the posts for the channel", "channelId="+channelId+err1.Error())
+ } else if err2 != nil {
+ result.Err = model.NewAppError("SqlPostStore.GetPostContext", "We couldn't get the parent posts for the channel", "channelId="+channelId+err2.Error())
+ } else {
+
+ list := &model.PostList{Order: make([]string, 0, len(posts))}
+
+ // 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
+ }
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
func (s SqlPostStore) getRootPosts(channelId string, offset int, limit int) StoreChannel {
storeChannel := make(StoreChannel)