[MM-59361] custom groups & read only channels (#28892)

* added group mention, read only view and post event tracking

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Guillermo Vayá
2024-10-28 13:41:29 +01:00
коммит произвёл GitHub
родитель c64215f6c2
Коммит a532b70317
13 изменённых файлов: 309 добавлений и 12 удалений

Просмотреть файл

@@ -2059,6 +2059,42 @@ func (s *OpenTracingLayerChannelStore) InvalidatePinnedPostCount(channelID strin
}
func (s *OpenTracingLayerChannelStore) IsChannelReadOnlyScheme(schemeID string) (bool, error) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ChannelStore.IsChannelReadOnlyScheme")
s.Root.Store.SetContext(newCtx)
defer func() {
s.Root.Store.SetContext(origCtx)
}()
defer span.Finish()
result, err := s.ChannelStore.IsChannelReadOnlyScheme(schemeID)
if err != nil {
span.LogFields(spanlog.Error(err))
ext.Error.Set(span, true)
}
return result, err
}
func (s *OpenTracingLayerChannelStore) IsReadOnlyChannel(channelID string) (bool, error) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ChannelStore.IsReadOnlyChannel")
s.Root.Store.SetContext(newCtx)
defer func() {
s.Root.Store.SetContext(origCtx)
}()
defer span.Finish()
result, err := s.ChannelStore.IsReadOnlyChannel(channelID)
if err != nil {
span.LogFields(spanlog.Error(err))
ext.Error.Set(span, true)
}
return result, err
}
func (s *OpenTracingLayerChannelStore) MigrateChannelMembers(fromChannelID string, fromUserID string) (map[string]string, error) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "ChannelStore.MigrateChannelMembers")

Просмотреть файл

@@ -2252,6 +2252,48 @@ func (s *RetryLayerChannelStore) InvalidatePinnedPostCount(channelID string) {
}
func (s *RetryLayerChannelStore) IsChannelReadOnlyScheme(schemeID string) (bool, error) {
tries := 0
for {
result, err := s.ChannelStore.IsChannelReadOnlyScheme(schemeID)
if err == nil {
return result, nil
}
if !isRepeatableError(err) {
return result, err
}
tries++
if tries >= 3 {
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
return result, err
}
timepkg.Sleep(100 * timepkg.Millisecond)
}
}
func (s *RetryLayerChannelStore) IsReadOnlyChannel(channelID string) (bool, error) {
tries := 0
for {
result, err := s.ChannelStore.IsReadOnlyChannel(channelID)
if err == nil {
return result, nil
}
if !isRepeatableError(err) {
return result, err
}
tries++
if tries >= 3 {
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
return result, err
}
timepkg.Sleep(100 * timepkg.Millisecond)
}
}
func (s *RetryLayerChannelStore) MigrateChannelMembers(fromChannelID string, fromUserID string) (map[string]string, error) {
tries := 0

Просмотреть файл

@@ -17,6 +17,7 @@ import (
"github.com/pkg/errors"
"github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/public/shared/request"
"github.com/mattermost/mattermost/server/v8/channels/store"
"github.com/mattermost/mattermost/server/v8/einterfaces"
@@ -4322,3 +4323,40 @@ func (s SqlChannelStore) GetTeamForChannel(channelID string) (*model.Team, error
}
return &team, nil
}
func (s SqlChannelStore) IsReadOnlyChannel(channelID string) (bool, error) {
query := s.getQueryBuilder().Select("schemeid").From("channels").Where(sq.Eq{"id": channelID}).Limit(1)
squery, args, err := query.ToSql()
if err != nil {
return false, err
}
// we look for schemeID to look for a custom scheme, if there is none chances are it is a writeable
// there might be in effect a custom scheme for the user that doesn't allow to create posts, but that wouldn't
// be a readonly channel but a readonly user
var schemaId string
err = s.GetReplicaX().Get(&schemaId, squery, args...)
if err != nil {
return false, nil
}
if schemaId == "" {
return false, nil
}
return s.IsChannelReadOnlyScheme(schemaId)
}
func (s SqlChannelStore) IsChannelReadOnlyScheme(schemeID string) (bool, error) {
query := s.getQueryBuilder().Select("roles.permissions").From("roles").InnerJoin("schemes ON roles.name = schemes.defaultchanneluserrole").Where(sq.Eq{"schemes.id": schemeID}).Limit(1)
squery, args, err := query.ToSql()
if err != nil {
mlog.Err(err)
return false, err
}
var permissions string
err = s.GetReplicaX().Get(&permissions, squery, args...)
if err != nil {
mlog.Err(err)
return false, err
}
permissionList := strings.Split(permissions, " ")
return slices.Index(permissionList, model.PermissionCreatePost.Id) == -1, nil
}

Просмотреть файл

@@ -310,6 +310,8 @@ type ChannelStore interface {
SetShared(channelId string, shared bool) error
// GetTeamForChannel returns the team for a given channelID.
GetTeamForChannel(channelID string) (*model.Team, error)
IsReadOnlyChannel(channelID string) (bool, error)
IsChannelReadOnlyScheme(schemeID string) (bool, error)
}
type ChannelMemberHistoryStore interface {

Просмотреть файл

@@ -2199,6 +2199,62 @@ func (_m *ChannelStore) InvalidatePinnedPostCount(channelID string) {
_m.Called(channelID)
}
// IsChannelReadOnlyScheme provides a mock function with given fields: schemeID
func (_m *ChannelStore) IsChannelReadOnlyScheme(schemeID string) (bool, error) {
ret := _m.Called(schemeID)
if len(ret) == 0 {
panic("no return value specified for IsChannelReadOnlyScheme")
}
var r0 bool
var r1 error
if rf, ok := ret.Get(0).(func(string) (bool, error)); ok {
return rf(schemeID)
}
if rf, ok := ret.Get(0).(func(string) bool); ok {
r0 = rf(schemeID)
} else {
r0 = ret.Get(0).(bool)
}
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(schemeID)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// IsReadOnlyChannel provides a mock function with given fields: channelID
func (_m *ChannelStore) IsReadOnlyChannel(channelID string) (bool, error) {
ret := _m.Called(channelID)
if len(ret) == 0 {
panic("no return value specified for IsReadOnlyChannel")
}
var r0 bool
var r1 error
if rf, ok := ret.Get(0).(func(string) (bool, error)); ok {
return rf(channelID)
}
if rf, ok := ret.Get(0).(func(string) bool); ok {
r0 = rf(channelID)
} else {
r0 = ret.Get(0).(bool)
}
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(channelID)
} else {
r1 = ret.Error(1)
}
return r0, r1
}
// MigrateChannelMembers provides a mock function with given fields: fromChannelID, fromUserID
func (_m *ChannelStore) MigrateChannelMembers(fromChannelID string, fromUserID string) (map[string]string, error) {
ret := _m.Called(fromChannelID, fromUserID)

Просмотреть файл

@@ -1918,6 +1918,38 @@ func (s *TimerLayerChannelStore) InvalidatePinnedPostCount(channelID string) {
}
}
func (s *TimerLayerChannelStore) IsChannelReadOnlyScheme(schemeID string) (bool, error) {
start := time.Now()
result, err := s.ChannelStore.IsChannelReadOnlyScheme(schemeID)
elapsed := float64(time.Since(start)) / float64(time.Second)
if s.Root.Metrics != nil {
success := "false"
if err == nil {
success = "true"
}
s.Root.Metrics.ObserveStoreMethodDuration("ChannelStore.IsChannelReadOnlyScheme", success, elapsed)
}
return result, err
}
func (s *TimerLayerChannelStore) IsReadOnlyChannel(channelID string) (bool, error) {
start := time.Now()
result, err := s.ChannelStore.IsReadOnlyChannel(channelID)
elapsed := float64(time.Since(start)) / float64(time.Second)
if s.Root.Metrics != nil {
success := "false"
if err == nil {
success = "true"
}
s.Root.Metrics.ObserveStoreMethodDuration("ChannelStore.IsReadOnlyChannel", success, elapsed)
}
return result, err
}
func (s *TimerLayerChannelStore) MigrateChannelMembers(fromChannelID string, fromUserID string) (map[string]string, error) {
start := time.Now()