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 2d30d24658321bbaa32ebfc181dbdade685fd23d. * 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>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
e3ac469ec6
Коммит
5e076ea36f
@@ -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
|
return buildChannelModerations(channel.Type, memberRole, guestRole, higherScopedMemberRole, higherScopedGuestRole), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2981,23 +2989,20 @@ func (a *App) FillInChannelsProps(channelList *model.ChannelList) *model.AppErro
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) ClearChannelMembersCache(channelID string) {
|
func (a *App) forEachChannelMember(channelID string, f func(model.ChannelMember) error) error {
|
||||||
perPage := 100
|
perPage := 100
|
||||||
page := 0
|
page := 0
|
||||||
|
|
||||||
for {
|
for {
|
||||||
channelMembers, err := a.Srv().Store.Channel().GetMembers(channelID, page*perPage, perPage)
|
channelMembers, err := a.Srv().Store.Channel().GetMembers(channelID, page*perPage, perPage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.Log().Warn("error clearing cache for channel members", mlog.String("channel_id", channelID))
|
return err
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, channelMember := range *channelMembers {
|
for _, channelMember := range *channelMembers {
|
||||||
a.ClearSessionCacheForUser(channelMember.UserId)
|
if err = f(channelMember); err != nil {
|
||||||
|
return err
|
||||||
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CHANNEL_MEMBER_UPDATED, "", "", channelMember.UserId, nil)
|
}
|
||||||
message.Add("channelMember", channelMember.ToJson())
|
|
||||||
a.Publish(message)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
length := len(*(channelMembers))
|
length := len(*(channelMembers))
|
||||||
@@ -3007,6 +3012,21 @@ func (a *App) ClearChannelMembersCache(channelID string) {
|
|||||||
|
|
||||||
page++
|
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) {
|
func (a *App) GetMemberCountsByGroup(channelID string, includeTimezones bool) ([]*model.ChannelMemberCountByGroup, *model.AppError) {
|
||||||
|
|||||||
@@ -1517,6 +1517,9 @@ func TestPatchChannelModerationsForChannel(t *testing.T) {
|
|||||||
th.App.SetPhase2PermissionsMigrationStatus(true)
|
th.App.SetPhase2PermissionsMigrationStatus(true)
|
||||||
channel := th.BasicChannel
|
channel := th.BasicChannel
|
||||||
|
|
||||||
|
user := th.BasicUser
|
||||||
|
th.AddUserToChannel(user, channel)
|
||||||
|
|
||||||
createPosts := model.ChannelModeratedPermissions[0]
|
createPosts := model.ChannelModeratedPermissions[0]
|
||||||
createReactions := model.ChannelModeratedPermissions[1]
|
createReactions := model.ChannelModeratedPermissions[1]
|
||||||
manageMembers := model.ChannelModeratedPermissions[2]
|
manageMembers := model.ChannelModeratedPermissions[2]
|
||||||
@@ -1901,6 +1904,34 @@ func TestPatchChannelModerationsForChannel(t *testing.T) {
|
|||||||
assert.Contains(t, higherScopedGuestRole.Permissions, createPosts)
|
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
|
// TestMarkChannelsAsViewedPanic verifies that returning an error from a.GetUser
|
||||||
|
|||||||
@@ -343,6 +343,10 @@
|
|||||||
"id": "api.channel.move_channel.type.invalid",
|
"id": "api.channel.move_channel.type.invalid",
|
||||||
"translation": "Unable to move direct or group message channels"
|
"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",
|
"id": "api.channel.patch_channel_moderations.license.error",
|
||||||
"translation": "Your license does not support channel moderation"
|
"translation": "Your license does not support channel moderation"
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user