[MM-16339] Migrate "Channel.GetPinnedPosts" to Sync by default (#11280)

* Migrate "Channel.GetPinnedPosts" to Sync by default

* remove unnecessary error checking

* fixing shadowed err variable

* fixing shadowed err variable

* fixing gofmt
This commit is contained in:
Adzim Zul Fahmi
2019-06-28 13:53:48 +07:00
committed by Jesús Espino
parent 86e0c8567c
commit 73e47567bc
5 changed files with 33 additions and 32 deletions

View File

@@ -2008,11 +2008,7 @@ func (a *App) postChannelMoveMessage(user *model.User, channel *model.Channel, p
}
func (a *App) GetPinnedPosts(channelId string) (*model.PostList, *model.AppError) {
result := <-a.Srv.Store.Channel().GetPinnedPosts(channelId)
if result.Err != nil {
return nil, result.Err
}
return result.Data.(*model.PostList), nil
return a.Srv.Store.Channel().GetPinnedPosts(channelId)
}
func (a *App) ToggleMuteChannel(channelId string, userId string) *model.ChannelMember {

View File

@@ -710,22 +710,18 @@ func (s SqlChannelStore) Get(id string, allowFromCache bool) (*model.Channel, *m
return s.get(id, false, allowFromCache)
}
func (s SqlChannelStore) GetPinnedPosts(channelId string) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
pl := model.NewPostList()
func (s SqlChannelStore) GetPinnedPosts(channelId string) (*model.PostList, *model.AppError) {
pl := model.NewPostList()
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.NewAppError("SqlPostStore.GetPinnedPosts", "store.sql_channel.pinned_posts.app_error", nil, err.Error(), http.StatusInternalServerError)
} else {
for _, post := range posts {
pl.AddPost(post)
pl.AddOrder(post.Id)
}
}
result.Data = pl
})
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 {
return nil, model.NewAppError("SqlPostStore.GetPinnedPosts", "store.sql_channel.pinned_posts.app_error", nil, err.Error(), http.StatusInternalServerError)
}
for _, post := range posts {
pl.AddPost(post)
pl.AddOrder(post.Id)
}
return pl, nil
}
func (s SqlChannelStore) GetFromMaster(id string) (*model.Channel, *model.AppError) {

View File

@@ -172,7 +172,7 @@ type ChannelStore interface {
InvalidateMemberCount(channelId string)
GetMemberCountFromCache(channelId string) int64
GetMemberCount(channelId string, allowFromCache bool) (int64, *model.AppError)
GetPinnedPosts(channelId string) StoreChannel
GetPinnedPosts(channelId string) (*model.PostList, *model.AppError)
RemoveMember(channelId string, userId string) *model.AppError
PermanentDeleteMembersByUser(userId string) *model.AppError
PermanentDeleteMembersByChannel(channelId string) *model.AppError

View File

@@ -2974,9 +2974,9 @@ func testChannelStoreGetPinnedPosts(t *testing.T, ss store.Store) {
IsPinned: true,
})
if r1 := <-ss.Channel().GetPinnedPosts(o1.Id); r1.Err != nil {
t.Fatal(r1.Err)
} else if r1.Data.(*model.PostList).Posts[p1.Id] == nil {
if pl, errGet := ss.Channel().GetPinnedPosts(o1.Id); errGet != nil {
t.Fatal(errGet)
} else if pl.Posts[p1.Id] == nil {
t.Fatal("didn't return relevant pinned posts")
}
@@ -2997,9 +2997,9 @@ func testChannelStoreGetPinnedPosts(t *testing.T, ss store.Store) {
})
require.Nil(t, err)
if r2 := <-ss.Channel().GetPinnedPosts(o2.Id); r2.Err != nil {
t.Fatal(r2.Err)
} else if len(r2.Data.(*model.PostList).Posts) != 0 {
if pl, errGet := ss.Channel().GetPinnedPosts(o2.Id); errGet != nil {
t.Fatal(errGet)
} else if len(pl.Posts) != 0 {
t.Fatal("wasn't supposed to return posts")
}
}

View File

@@ -939,19 +939,28 @@ func (_m *ChannelStore) GetMoreChannels(teamId string, userId string, offset int
}
// GetPinnedPosts provides a mock function with given fields: channelId
func (_m *ChannelStore) GetPinnedPosts(channelId string) store.StoreChannel {
func (_m *ChannelStore) GetPinnedPosts(channelId string) (*model.PostList, *model.AppError) {
ret := _m.Called(channelId)
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok {
var r0 *model.PostList
if rf, ok := ret.Get(0).(func(string) *model.PostList); ok {
r0 = rf(channelId)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
r0 = ret.Get(0).(*model.PostList)
}
}
return r0
var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
r1 = rf(channelId)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
}
// GetPublicChannelsByIdsForTeam provides a mock function with given fields: teamId, channelIds