MM-33402: Invalidated the LRU cache for channel members after moderat… (#17171)

* MM-33402: Invalidated the LRU cache for channel members after moderation changes.

* MM-33402: Handles cache invalidation error.

* Revert "MM-33402: Handles cache invalidation error."

This reverts commit 2d30d24658.

* MM-33402: Un-exports method.

* MM-33402: Removes log warning from previous code use.

* MM-33402: Handles possible (store.ChannelStore).GetMembers error.

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
This commit is contained in:
Martin Kraft
2021-03-26 14:09:32 -04:00
committed by GitHub
parent e3ac469ec6
commit 5e076ea36f
3 changed files with 63 additions and 8 deletions

View File

@@ -1029,6 +1029,14 @@ func (a *App) PatchChannelModerationsForChannel(channel *model.Channel, channelM
}
}
cErr := a.forEachChannelMember(channel.Id, func(channelMember model.ChannelMember) error {
a.Srv().Store.Channel().InvalidateAllChannelMembersForUser(channelMember.UserId)
return nil
})
if cErr != nil {
return nil, model.NewAppError("PatchChannelModerationsForChannel", "api.channel.patch_channel_moderations.cache_invalidation.error", nil, cErr.Error(), http.StatusInternalServerError)
}
return buildChannelModerations(channel.Type, memberRole, guestRole, higherScopedMemberRole, higherScopedGuestRole), nil
}
@@ -2981,23 +2989,20 @@ func (a *App) FillInChannelsProps(channelList *model.ChannelList) *model.AppErro
return nil
}
func (a *App) ClearChannelMembersCache(channelID string) {
func (a *App) forEachChannelMember(channelID string, f func(model.ChannelMember) error) error {
perPage := 100
page := 0
for {
channelMembers, err := a.Srv().Store.Channel().GetMembers(channelID, page*perPage, perPage)
if err != nil {
a.Log().Warn("error clearing cache for channel members", mlog.String("channel_id", channelID))
break
return err
}
for _, channelMember := range *channelMembers {
a.ClearSessionCacheForUser(channelMember.UserId)
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_MEMBER_UPDATED, "", "", channelMember.UserId, nil)
message.Add("channelMember", channelMember.ToJson())
a.Publish(message)
if err = f(channelMember); err != nil {
return err
}
}
length := len(*(channelMembers))
@@ -3007,6 +3012,21 @@ func (a *App) ClearChannelMembersCache(channelID string) {
page++
}
return nil
}
func (a *App) ClearChannelMembersCache(channelID string) {
clearSessionCache := func(channelMember model.ChannelMember) error {
a.ClearSessionCacheForUser(channelMember.UserId)
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_MEMBER_UPDATED, "", "", channelMember.UserId, nil)
message.Add("channelMember", channelMember.ToJson())
a.Publish(message)
return nil
}
if err := a.forEachChannelMember(channelID, clearSessionCache); err != nil {
a.Log().Warn("error clearing cache for channel members", mlog.String("channel_id", channelID))
}
}
func (a *App) GetMemberCountsByGroup(channelID string, includeTimezones bool) ([]*model.ChannelMemberCountByGroup, *model.AppError) {

View File

@@ -1517,6 +1517,9 @@ func TestPatchChannelModerationsForChannel(t *testing.T) {
th.App.SetPhase2PermissionsMigrationStatus(true)
channel := th.BasicChannel
user := th.BasicUser
th.AddUserToChannel(user, channel)
createPosts := model.ChannelModeratedPermissions[0]
createReactions := model.ChannelModeratedPermissions[1]
manageMembers := model.ChannelModeratedPermissions[2]
@@ -1901,6 +1904,34 @@ func TestPatchChannelModerationsForChannel(t *testing.T) {
assert.Contains(t, higherScopedGuestRole.Permissions, createPosts)
})
t.Run("Updates the authorization to create post", func(t *testing.T) {
addCreatePosts := []*model.ChannelModerationPatch{
{
Name: &createPosts,
Roles: &model.ChannelModeratedRolesPatch{
Members: model.NewBool(true),
},
},
}
removeCreatePosts := []*model.ChannelModerationPatch{
{
Name: &createPosts,
Roles: &model.ChannelModeratedRolesPatch{
Members: model.NewBool(false),
},
},
}
mockSession := model.Session{UserId: user.Id}
_, err := th.App.PatchChannelModerationsForChannel(channel.DeepCopy(), addCreatePosts)
require.Nil(t, err)
require.True(t, th.App.SessionHasPermissionToChannel(mockSession, channel.Id, model.PERMISSION_CREATE_POST))
_, err = th.App.PatchChannelModerationsForChannel(channel.DeepCopy(), removeCreatePosts)
require.Nil(t, err)
require.False(t, th.App.SessionHasPermissionToChannel(mockSession, channel.Id, model.PERMISSION_CREATE_POST))
})
}
// TestMarkChannelsAsViewedPanic verifies that returning an error from a.GetUser

View File

@@ -343,6 +343,10 @@
"id": "api.channel.move_channel.type.invalid",
"translation": "Unable to move direct or group message channels"
},
{
"id": "api.channel.patch_channel_moderations.cache_invalidation.error",
"translation": "Error invalidating cache"
},
{
"id": "api.channel.patch_channel_moderations.license.error",
"translation": "Your license does not support channel moderation"