Skip to content

Commit

Permalink
Cherry pick of #20380 (release-6.6) (#20434) (#20436)
Browse files Browse the repository at this point in the history
Automatic Merge
  • Loading branch information
koox00 committed Jun 10, 2022
1 parent aa4f857 commit f2f46f8
Show file tree
Hide file tree
Showing 18 changed files with 231 additions and 230 deletions.
6 changes: 3 additions & 3 deletions app/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ func TestAddChannelMemberNoUserRequestor(t *testing.T) {
}
assert.Equal(t, groupUserIds, channelMemberHistoryUserIds)

postList, nErr := th.App.Srv().Store.Post().GetPosts(model.GetPostsOptions{ChannelId: channel.Id, Page: 0, PerPage: 1}, false)
postList, nErr := th.App.Srv().Store.Post().GetPosts(model.GetPostsOptions{ChannelId: channel.Id, Page: 0, PerPage: 1}, false, map[string]bool{})
require.NoError(t, nErr)

if assert.Len(t, postList.Order, 1) {
Expand Down Expand Up @@ -2032,8 +2032,8 @@ func TestMarkChannelAsUnreadFromPostPanic(t *testing.T) {

mockPostStore := mocks.PostStore{}
mockPostStore.On("GetMaxPostSize").Return(65535, nil)
mockPostStore.On("Get", context.Background(), "postID", false, false, false, "userID").Return(&model.PostList{}, nil)
mockPostStore.On("GetPostsAfter", mock.AnythingOfType("model.GetPostsOptions")).Return(&model.PostList{}, nil)
mockPostStore.On("Get", context.Background(), "postID", false, false, false, "userID", map[string]bool{"email": true, "fullname": true}).Return(&model.PostList{}, nil)
mockPostStore.On("GetPostsAfter", mock.AnythingOfType("model.GetPostsOptions"), map[string]bool{"email": true, "fullname": true}).Return(&model.PostList{}, nil)
mockPostStore.On("GetSingle", "postID", false).Return(&model.Post{
Id: "postID",
RootId: "rootID",
Expand Down
2 changes: 1 addition & 1 deletion app/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func (a *App) MigrateFilenamesToFileInfos(post *model.Post) []*model.FileInfo {
fileMigrationLock.Lock()
defer fileMigrationLock.Unlock()

result, nErr := a.Srv().Store.Post().Get(context.Background(), post.Id, false, false, false, "")
result, nErr := a.Srv().Store.Post().Get(context.Background(), post.Id, false, false, false, "", a.Config().GetSanitizeOptions())
if nErr != nil {
mlog.Error("Unable to get post when migrating post to use FileInfos", mlog.Err(nErr), mlog.String("post_id", post.Id))
return []*model.FileInfo{}
Expand Down
23 changes: 12 additions & 11 deletions app/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (a *App) CreatePost(c *request.Context, post *model.Post, channel *model.Ch
if post.RootId != "" {
pchan = make(chan store.StoreResult, 1)
go func() {
r, pErr := a.Srv().Store.Post().Get(sqlstore.WithMaster(context.Background()), post.RootId, false, false, false, "")
r, pErr := a.Srv().Store.Post().Get(sqlstore.WithMaster(context.Background()), post.RootId, false, false, false, "", a.Config().GetSanitizeOptions())
pchan <- store.StoreResult{Data: r, NErr: pErr}
close(pchan)
}()
Expand Down Expand Up @@ -559,7 +559,7 @@ func (a *App) DeleteEphemeralPost(userID, postID string) {
func (a *App) UpdatePost(c *request.Context, post *model.Post, safeUpdate bool) (*model.Post, *model.AppError) {
post.SanitizeProps()

postLists, nErr := a.Srv().Store.Post().Get(context.Background(), post.Id, false, false, false, "")
postLists, nErr := a.Srv().Store.Post().Get(context.Background(), post.Id, false, false, false, "", a.Config().GetSanitizeOptions())
if nErr != nil {
var nfErr *store.ErrNotFound
var invErr *store.ErrInvalidInput
Expand Down Expand Up @@ -784,7 +784,7 @@ func (a *App) PatchPost(c *request.Context, postID string, patch *model.PostPatc
}

func (a *App) GetPostsPage(options model.GetPostsOptions) (*model.PostList, *model.AppError) {
postList, err := a.Srv().Store.Post().GetPosts(options, false)
postList, err := a.Srv().Store.Post().GetPosts(options, false, a.Config().GetSanitizeOptions())
if err != nil {
var invErr *store.ErrInvalidInput
switch {
Expand All @@ -799,7 +799,7 @@ func (a *App) GetPostsPage(options model.GetPostsOptions) (*model.PostList, *mod
}

func (a *App) GetPosts(channelID string, offset int, limit int) (*model.PostList, *model.AppError) {
postList, err := a.Srv().Store.Post().GetPosts(model.GetPostsOptions{ChannelId: channelID, Page: offset, PerPage: limit}, true)
postList, err := a.Srv().Store.Post().GetPosts(model.GetPostsOptions{ChannelId: channelID, Page: offset, PerPage: limit}, true, a.Config().GetSanitizeOptions())
if err != nil {
var invErr *store.ErrInvalidInput
switch {
Expand All @@ -818,7 +818,7 @@ func (a *App) GetPostsEtag(channelID string, collapsedThreads bool) string {
}

func (a *App) GetPostsSince(options model.GetPostsSinceOptions) (*model.PostList, *model.AppError) {
postList, err := a.Srv().Store.Post().GetPostsSince(options, true)
postList, err := a.Srv().Store.Post().GetPostsSince(options, true, a.Config().GetSanitizeOptions())
if err != nil {
return nil, model.NewAppError("GetPostsSince", "app.post.get_posts_since.app_error", nil, err.Error(), http.StatusInternalServerError)
}
Expand All @@ -842,7 +842,7 @@ func (a *App) GetSinglePost(postID string) (*model.Post, *model.AppError) {
}

func (a *App) GetPostThread(postID string, skipFetchThreads, collapsedThreads, collapsedThreadsExtended bool, userID string) (*model.PostList, *model.AppError) {
posts, err := a.Srv().Store.Post().Get(context.Background(), postID, skipFetchThreads, collapsedThreads, collapsedThreadsExtended, userID)
posts, err := a.Srv().Store.Post().Get(context.Background(), postID, skipFetchThreads, collapsedThreads, collapsedThreadsExtended, userID, a.Config().GetSanitizeOptions())
if err != nil {
var nfErr *store.ErrNotFound
var invErr *store.ErrInvalidInput
Expand Down Expand Up @@ -887,7 +887,7 @@ func (a *App) GetFlaggedPostsForChannel(userID, channelID string, offset int, li
}

func (a *App) GetPermalinkPost(c *request.Context, postID string, userID string) (*model.PostList, *model.AppError) {
list, nErr := a.Srv().Store.Post().Get(context.Background(), postID, false, false, false, userID)
list, nErr := a.Srv().Store.Post().Get(context.Background(), postID, false, false, false, userID, a.Config().GetSanitizeOptions())
if nErr != nil {
var nfErr *store.ErrNotFound
var invErr *store.ErrInvalidInput
Expand Down Expand Up @@ -919,7 +919,7 @@ func (a *App) GetPermalinkPost(c *request.Context, postID string, userID string)
}

func (a *App) GetPostsBeforePost(options model.GetPostsOptions) (*model.PostList, *model.AppError) {
postList, err := a.Srv().Store.Post().GetPostsBefore(options)
postList, err := a.Srv().Store.Post().GetPostsBefore(options, a.Config().GetSanitizeOptions())
if err != nil {
var invErr *store.ErrInvalidInput
switch {
Expand All @@ -934,7 +934,7 @@ func (a *App) GetPostsBeforePost(options model.GetPostsOptions) (*model.PostList
}

func (a *App) GetPostsAfterPost(options model.GetPostsOptions) (*model.PostList, *model.AppError) {
postList, err := a.Srv().Store.Post().GetPostsAfter(options)
postList, err := a.Srv().Store.Post().GetPostsAfter(options, a.Config().GetSanitizeOptions())
if err != nil {
var invErr *store.ErrInvalidInput
switch {
Expand All @@ -951,10 +951,11 @@ func (a *App) GetPostsAfterPost(options model.GetPostsOptions) (*model.PostList,
func (a *App) GetPostsAroundPost(before bool, options model.GetPostsOptions) (*model.PostList, *model.AppError) {
var postList *model.PostList
var err error
sanitize := a.Config().GetSanitizeOptions()
if before {
postList, err = a.Srv().Store.Post().GetPostsBefore(options)
postList, err = a.Srv().Store.Post().GetPostsBefore(options, sanitize)
} else {
postList, err = a.Srv().Store.Post().GetPostsAfter(options)
postList, err = a.Srv().Store.Post().GetPostsAfter(options, sanitize)
}

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion services/sharedchannel/permalink.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (scs *Service) processPermalinkToRemote(p *model.Post) string {
// Extract the postID (This is simple enough not to warrant full-blown URL parsing.)
lastSlash := strings.LastIndexByte(msg, '/')
postID := msg[lastSlash+1:]
postList, err := scs.server.GetStore().Post().Get(context.Background(), postID, true, false, false, "")
postList, err := scs.server.GetStore().Post().Get(context.Background(), postID, true, false, false, "", map[string]bool{})
if err != nil {
scs.server.GetLogger().Log(mlog.LvlSharedChannelServiceWarn, "Unable to get post during replacing permalinks", mlog.Err(err))
return msg
Expand Down
2 changes: 1 addition & 1 deletion services/sharedchannel/permalink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestProcessPermalinkToRemote(t *testing.T) {
utils.TranslationsPreInit()

pl := &model.PostList{}
mockPostStore.On("Get", context.Background(), "postID", true, false, false, "").Return(pl, nil)
mockPostStore.On("Get", context.Background(), "postID", true, false, false, "", map[string]bool{}).Return(pl, nil)

mockStore.On("Post").Return(&mockPostStore)

Expand Down
8 changes: 4 additions & 4 deletions store/localcachelayer/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ func getMockStore() *mocks.Store {
fakePosts := &model.PostList{}
fakeOptions := model.GetPostsOptions{ChannelId: "123", PerPage: 30}
mockPostStore := mocks.PostStore{}
mockPostStore.On("GetPosts", fakeOptions, true).Return(fakePosts, nil)
mockPostStore.On("GetPosts", fakeOptions, false).Return(fakePosts, nil)
mockPostStore.On("GetPosts", fakeOptions, true, map[string]bool{}).Return(fakePosts, nil)
mockPostStore.On("GetPosts", fakeOptions, false, map[string]bool{}).Return(fakePosts, nil)
mockPostStore.On("InvalidateLastPostTimeCache", "12360")

mockPostStoreOptions := model.GetPostsSinceOptions{
Expand All @@ -119,8 +119,8 @@ func getMockStore() *mocks.Store {
mockPostStore.On("InvalidateLastPostTimeCache", "channelId")
mockPostStore.On("GetEtag", "channelId", true, false).Return(mockPostStoreEtagResult)
mockPostStore.On("GetEtag", "channelId", false, false).Return(mockPostStoreEtagResult)
mockPostStore.On("GetPostsSince", mockPostStoreOptions, true).Return(model.NewPostList(), nil)
mockPostStore.On("GetPostsSince", mockPostStoreOptions, false).Return(model.NewPostList(), nil)
mockPostStore.On("GetPostsSince", mockPostStoreOptions, true, map[string]bool{}).Return(model.NewPostList(), nil)
mockPostStore.On("GetPostsSince", mockPostStoreOptions, false, map[string]bool{}).Return(model.NewPostList(), nil)
mockStore.On("Post").Return(&mockPostStore)

fakeTermsOfService := model.TermsOfService{Id: "123", CreateAt: 11111, UserId: "321", Text: "Terms of service test"}
Expand Down
10 changes: 5 additions & 5 deletions store/localcachelayer/post_layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s LocalCachePostStore) GetEtag(channelId string, allowFromCache, collapsed
return result
}

func (s LocalCachePostStore) GetPostsSince(options model.GetPostsSinceOptions, allowFromCache bool) (*model.PostList, error) {
func (s LocalCachePostStore) GetPostsSince(options model.GetPostsSinceOptions, allowFromCache bool, sanitizeOptions map[string]bool) (*model.PostList, error) {
if allowFromCache {
// If the last post in the channel's time is less than or equal to the time we are getting posts since,
// we can safely return no posts.
Expand All @@ -90,7 +90,7 @@ func (s LocalCachePostStore) GetPostsSince(options model.GetPostsSinceOptions, a
}
}

list, err := s.PostStore.GetPostsSince(options, allowFromCache)
list, err := s.PostStore.GetPostsSince(options, allowFromCache, sanitizeOptions)

latestUpdate := options.Time
if err == nil {
Expand All @@ -105,9 +105,9 @@ func (s LocalCachePostStore) GetPostsSince(options model.GetPostsSinceOptions, a
return list, err
}

func (s LocalCachePostStore) GetPosts(options model.GetPostsOptions, allowFromCache bool) (*model.PostList, error) {
func (s LocalCachePostStore) GetPosts(options model.GetPostsOptions, allowFromCache bool, sanitizeOptions map[string]bool) (*model.PostList, error) {
if !allowFromCache {
return s.PostStore.GetPosts(options, allowFromCache)
return s.PostStore.GetPosts(options, allowFromCache, sanitizeOptions)
}

offset := options.PerPage * options.Page
Expand All @@ -119,7 +119,7 @@ func (s LocalCachePostStore) GetPosts(options model.GetPostsOptions, allowFromCa
}
}

list, err := s.PostStore.GetPosts(options, false)
list, err := s.PostStore.GetPosts(options, false, sanitizeOptions)
if err != nil {
return nil, err
}
Expand Down
28 changes: 14 additions & 14 deletions store/localcachelayer/post_layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ func TestPostStoreLastPostTimeCache(t *testing.T) {

expectedResult := model.NewPostList()

list, err := cachedStore.Post().GetPostsSince(fakeOptions, true)
list, err := cachedStore.Post().GetPostsSince(fakeOptions, true, map[string]bool{})
require.NoError(t, err)
assert.Equal(t, list, expectedResult)
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1)

list, err = cachedStore.Post().GetPostsSince(fakeOptions, true)
list, err = cachedStore.Post().GetPostsSince(fakeOptions, true, map[string]bool{})
require.NoError(t, err)
assert.Equal(t, list, expectedResult)
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1)
Expand All @@ -108,9 +108,9 @@ func TestPostStoreLastPostTimeCache(t *testing.T) {
cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
require.NoError(t, err)

cachedStore.Post().GetPostsSince(fakeOptions, true)
cachedStore.Post().GetPostsSince(fakeOptions, true, map[string]bool{})
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1)
cachedStore.Post().GetPostsSince(fakeOptions, false)
cachedStore.Post().GetPostsSince(fakeOptions, false, map[string]bool{})
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 2)
})

Expand All @@ -120,10 +120,10 @@ func TestPostStoreLastPostTimeCache(t *testing.T) {
cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
require.NoError(t, err)

cachedStore.Post().GetPostsSince(fakeOptions, true)
cachedStore.Post().GetPostsSince(fakeOptions, true, map[string]bool{})
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1)
cachedStore.Post().InvalidateLastPostTimeCache(channelId)
cachedStore.Post().GetPostsSince(fakeOptions, true)
cachedStore.Post().GetPostsSince(fakeOptions, true, map[string]bool{})
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 2)
})

Expand All @@ -133,10 +133,10 @@ func TestPostStoreLastPostTimeCache(t *testing.T) {
cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
require.NoError(t, err)

cachedStore.Post().GetPostsSince(fakeOptions, true)
cachedStore.Post().GetPostsSince(fakeOptions, true, map[string]bool{})
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 1)
cachedStore.Post().ClearCaches()
cachedStore.Post().GetPostsSince(fakeOptions, true)
cachedStore.Post().GetPostsSince(fakeOptions, true, map[string]bool{})
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPostsSince", 2)
})
}
Expand All @@ -151,12 +151,12 @@ func TestPostStoreCache(t *testing.T) {
cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
require.NoError(t, err)

gotPosts, err := cachedStore.Post().GetPosts(fakeOptions, true)
gotPosts, err := cachedStore.Post().GetPosts(fakeOptions, true, map[string]bool{})
require.NoError(t, err)
assert.Equal(t, fakePosts, gotPosts)
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1)

_, _ = cachedStore.Post().GetPosts(fakeOptions, true)
_, _ = cachedStore.Post().GetPosts(fakeOptions, true, map[string]bool{})
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1)
})

Expand All @@ -166,12 +166,12 @@ func TestPostStoreCache(t *testing.T) {
cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
require.NoError(t, err)

gotPosts, err := cachedStore.Post().GetPosts(fakeOptions, true)
gotPosts, err := cachedStore.Post().GetPosts(fakeOptions, true, map[string]bool{})
require.NoError(t, err)
assert.Equal(t, fakePosts, gotPosts)
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1)

_, _ = cachedStore.Post().GetPosts(fakeOptions, false)
_, _ = cachedStore.Post().GetPosts(fakeOptions, false, map[string]bool{})
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 2)
})

Expand All @@ -181,14 +181,14 @@ func TestPostStoreCache(t *testing.T) {
cachedStore, err := NewLocalCacheLayer(mockStore, nil, nil, mockCacheProvider)
require.NoError(t, err)

gotPosts, err := cachedStore.Post().GetPosts(fakeOptions, true)
gotPosts, err := cachedStore.Post().GetPosts(fakeOptions, true, map[string]bool{})
require.NoError(t, err)
assert.Equal(t, fakePosts, gotPosts)
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1)

cachedStore.Post().InvalidateLastPostTimeCache("12360")

_, _ = cachedStore.Post().GetPosts(fakeOptions, true)
_, _ = cachedStore.Post().GetPosts(fakeOptions, true, map[string]bool{})
mockStore.Post().(*mocks.PostStore).AssertNumberOfCalls(t, "GetPosts", 1)

})
Expand Down

0 comments on commit f2f46f8

Please sign in to comment.