summaryrefslogtreecommitdiffstats
path: root/store
diff options
context:
space:
mode:
authorGabin Aureche <gabin.aureche@live.fr>2017-03-13 13:25:08 +0100
committerGeorge Goldberg <george@gberg.me>2017-03-13 12:25:08 +0000
commitfe38d6d5bb36e18ddefbe490cc21f48f4f4c8d81 (patch)
treeb96d457cde64b7397f91028106e93a7f92a179bd /store
parent482a0fb5fc248b1ec61db35299dc3e6d963ad5ab (diff)
downloadchat-fe38d6d5bb36e18ddefbe490cc21f48f4f4c8d81.tar.gz
chat-fe38d6d5bb36e18ddefbe490cc21f48f4f4c8d81.tar.bz2
chat-fe38d6d5bb36e18ddefbe490cc21f48f4f4c8d81.zip
Add pinned posts (#4217)
Diffstat (limited to 'store')
-rw-r--r--store/sql_channel_store.go26
-rw-r--r--store/sql_channel_store_test.go43
-rw-r--r--store/sql_post_store.go1
-rw-r--r--store/store.go1
4 files changed, 71 insertions, 0 deletions
diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go
index ff1716957..27a00f484 100644
--- a/store/sql_channel_store.go
+++ b/store/sql_channel_store.go
@@ -321,6 +321,32 @@ func (s SqlChannelStore) Get(id string, allowFromCache bool) StoreChannel {
return s.get(id, false, allowFromCache)
}
+func (s SqlChannelStore) GetPinnedPosts(channelId string) StoreChannel {
+ storeChannel := make(StoreChannel, 1)
+
+ go func() {
+ result := StoreResult{}
+ pl := &model.PostList{}
+
+ var posts []*model.Post
+ if _, err := s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE IsPinned = true AND ChannelId = :ChannelId AND DeleteAt = 0 ORDER BY CreateAt ASC", map[string]interface{}{"ChannelId": channelId}); err != nil {
+ result.Err = model.NewLocAppError("SqlPostStore.GetPinnedPosts", "store.sql_channel.pinned_posts.app_error", nil, err.Error())
+ } else {
+ for _, post := range posts {
+ pl.AddPost(post)
+ pl.AddOrder(post.Id)
+ }
+ }
+
+ result.Data = pl
+
+ storeChannel <- result
+ close(storeChannel)
+ }()
+
+ return storeChannel
+}
+
func (s SqlChannelStore) GetFromMaster(id string) StoreChannel {
return s.get(id, true, false)
}
diff --git a/store/sql_channel_store_test.go b/store/sql_channel_store_test.go
index df9c76905..7b06dff8f 100644
--- a/store/sql_channel_store_test.go
+++ b/store/sql_channel_store_test.go
@@ -1493,3 +1493,46 @@ func TestChannelStoreAnalyticsDeletedTypeCount(t *testing.T) {
}
}
}
+
+func TestChannelStoreGetPinnedPosts(t *testing.T) {
+ Setup()
+
+ o1 := Must(store.Channel().Save(&model.Channel{
+ TeamId: model.NewId(),
+ DisplayName: "Name",
+ Name: "a" + model.NewId() + "b",
+ Type: model.CHANNEL_OPEN,
+ })).(*model.Channel)
+
+ p1 := Must(store.Post().Save(&model.Post{
+ UserId: model.NewId(),
+ ChannelId: o1.Id,
+ Message: "test",
+ IsPinned: true,
+ })).(*model.Post)
+
+ if r1 := <-store.Channel().GetPinnedPosts(o1.Id); r1.Err != nil {
+ t.Fatal(r1.Err)
+ } else if r1.Data.(*model.PostList).Posts[p1.Id] == nil {
+ t.Fatal("didn't return relevant pinned posts")
+ }
+
+ o2 := Must(store.Channel().Save(&model.Channel{
+ TeamId: model.NewId(),
+ DisplayName: "Name",
+ Name: "a" + model.NewId() + "b",
+ Type: model.CHANNEL_OPEN,
+ })).(*model.Channel)
+
+ Must(store.Post().Save(&model.Post{
+ UserId: model.NewId(),
+ ChannelId: o2.Id,
+ Message: "test",
+ }))
+
+ if r2 := <-store.Channel().GetPinnedPosts(o2.Id); r2.Err != nil {
+ t.Fatal(r2.Err)
+ } else if len(r2.Data.(*model.PostList).Posts) != 0 {
+ t.Fatal("wasn't supposed to return posts")
+ }
+}
diff --git a/store/sql_post_store.go b/store/sql_post_store.go
index eefd251e5..eb14a66a2 100644
--- a/store/sql_post_store.go
+++ b/store/sql_post_store.go
@@ -64,6 +64,7 @@ func (s SqlPostStore) CreateIndexesIfNotExists() {
s.CreateIndexIfNotExists("idx_posts_channel_id", "Posts", "ChannelId")
s.CreateIndexIfNotExists("idx_posts_root_id", "Posts", "RootId")
s.CreateIndexIfNotExists("idx_posts_user_id", "Posts", "UserId")
+ s.CreateIndexIfNotExists("idx_posts_is_pinned", "Posts", "IsPinned")
s.CreateFullTextIndexIfNotExists("idx_posts_message_txt", "Posts", "Message")
s.CreateFullTextIndexIfNotExists("idx_posts_hashtags_txt", "Posts", "Hashtags")
diff --git a/store/store.go b/store/store.go
index 7aa903f6f..330bc5716 100644
--- a/store/store.go
+++ b/store/store.go
@@ -118,6 +118,7 @@ type ChannelStore interface {
InvalidateMemberCount(channelId string)
GetMemberCountFromCache(channelId string) int64
GetMemberCount(channelId string, allowFromCache bool) StoreChannel
+ GetPinnedPosts(channelId string) StoreChannel
RemoveMember(channelId string, userId string) StoreChannel
PermanentDeleteMembersByUser(userId string) StoreChannel
PermanentDeleteMembersByChannel(channelId string) StoreChannel