[MM-45564] - Notify Admin v2 (#20777)
* [MM-45564] - Notify Admin v2 * add dummy data * update dummy data * add store methods * experiment with recurring task * complete saving of the notification * make improvements * make improvements * add store layer tests * fix lint * update store layer tests * add app layer unit tests * add store layers * add app layer tests * fix lint * fix lint * fix tests * fix tests lint * fix lint * fix lint * fix retry layer test * add notifications manual trigger * filter notifications based on current plan * add test case * temp change * feedback impl * fix translations * change job scheduler * refactor job * fix store layer tests * extract i18n * fix lint * fix translations * fix translations * add license statement for new file * feedback impl-2 * fix lint * update make file * add intl ids * improve * fix lint * feedback impl * move code and rename files * fix lint * feedback impl * add config for trigger notifications api * fix tests * tmp change * undo temp changes Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
c11ad8995f
Коммит
bd42f0cd8c
@@ -33,6 +33,7 @@ type OpenTracingLayer struct {
|
||||
JobStore store.JobStore
|
||||
LicenseStore store.LicenseStore
|
||||
LinkMetadataStore store.LinkMetadataStore
|
||||
NotifyAdminStore store.NotifyAdminStore
|
||||
OAuthStore store.OAuthStore
|
||||
PluginStore store.PluginStore
|
||||
PostStore store.PostStore
|
||||
@@ -114,6 +115,10 @@ func (s *OpenTracingLayer) LinkMetadata() store.LinkMetadataStore {
|
||||
return s.LinkMetadataStore
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayer) NotifyAdmin() store.NotifyAdminStore {
|
||||
return s.NotifyAdminStore
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayer) OAuth() store.OAuthStore {
|
||||
return s.OAuthStore
|
||||
}
|
||||
@@ -276,6 +281,11 @@ type OpenTracingLayerLinkMetadataStore struct {
|
||||
Root *OpenTracingLayer
|
||||
}
|
||||
|
||||
type OpenTracingLayerNotifyAdminStore struct {
|
||||
store.NotifyAdminStore
|
||||
Root *OpenTracingLayer
|
||||
}
|
||||
|
||||
type OpenTracingLayerOAuthStore struct {
|
||||
store.OAuthStore
|
||||
Root *OpenTracingLayer
|
||||
@@ -4970,6 +4980,78 @@ func (s *OpenTracingLayerLinkMetadataStore) Save(linkMetadata *model.LinkMetadat
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerNotifyAdminStore) DeleteBefore(trial bool, now int64) error {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "NotifyAdminStore.DeleteBefore")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
err := s.NotifyAdminStore.DeleteBefore(trial, now)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerNotifyAdminStore) Get(trial bool) ([]*model.NotifyAdminData, error) {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "NotifyAdminStore.Get")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
result, err := s.NotifyAdminStore.Get(trial)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerNotifyAdminStore) GetDataByUserIdAndFeature(userId string, feature model.MattermostPaidFeature) ([]*model.NotifyAdminData, error) {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "NotifyAdminStore.GetDataByUserIdAndFeature")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
result, err := s.NotifyAdminStore.GetDataByUserIdAndFeature(userId, feature)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerNotifyAdminStore) Save(data *model.NotifyAdminData) (*model.NotifyAdminData, error) {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "NotifyAdminStore.Save")
|
||||
s.Root.Store.SetContext(newCtx)
|
||||
defer func() {
|
||||
s.Root.Store.SetContext(origCtx)
|
||||
}()
|
||||
|
||||
defer span.Finish()
|
||||
result, err := s.NotifyAdminStore.Save(data)
|
||||
if err != nil {
|
||||
span.LogFields(spanlog.Error(err))
|
||||
ext.Error.Set(span, true)
|
||||
}
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *OpenTracingLayerOAuthStore) DeleteApp(id string) error {
|
||||
origCtx := s.Root.Store.Context()
|
||||
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "OAuthStore.DeleteApp")
|
||||
@@ -12333,6 +12415,7 @@ func New(childStore store.Store, ctx context.Context) *OpenTracingLayer {
|
||||
newStore.JobStore = &OpenTracingLayerJobStore{JobStore: childStore.Job(), Root: &newStore}
|
||||
newStore.LicenseStore = &OpenTracingLayerLicenseStore{LicenseStore: childStore.License(), Root: &newStore}
|
||||
newStore.LinkMetadataStore = &OpenTracingLayerLinkMetadataStore{LinkMetadataStore: childStore.LinkMetadata(), Root: &newStore}
|
||||
newStore.NotifyAdminStore = &OpenTracingLayerNotifyAdminStore{NotifyAdminStore: childStore.NotifyAdmin(), Root: &newStore}
|
||||
newStore.OAuthStore = &OpenTracingLayerOAuthStore{OAuthStore: childStore.OAuth(), Root: &newStore}
|
||||
newStore.PluginStore = &OpenTracingLayerPluginStore{PluginStore: childStore.Plugin(), Root: &newStore}
|
||||
newStore.PostStore = &OpenTracingLayerPostStore{PostStore: childStore.Post(), Root: &newStore}
|
||||
|
||||
@@ -36,6 +36,7 @@ type RetryLayer struct {
|
||||
JobStore store.JobStore
|
||||
LicenseStore store.LicenseStore
|
||||
LinkMetadataStore store.LinkMetadataStore
|
||||
NotifyAdminStore store.NotifyAdminStore
|
||||
OAuthStore store.OAuthStore
|
||||
PluginStore store.PluginStore
|
||||
PostStore store.PostStore
|
||||
@@ -117,6 +118,10 @@ func (s *RetryLayer) LinkMetadata() store.LinkMetadataStore {
|
||||
return s.LinkMetadataStore
|
||||
}
|
||||
|
||||
func (s *RetryLayer) NotifyAdmin() store.NotifyAdminStore {
|
||||
return s.NotifyAdminStore
|
||||
}
|
||||
|
||||
func (s *RetryLayer) OAuth() store.OAuthStore {
|
||||
return s.OAuthStore
|
||||
}
|
||||
@@ -279,6 +284,11 @@ type RetryLayerLinkMetadataStore struct {
|
||||
Root *RetryLayer
|
||||
}
|
||||
|
||||
type RetryLayerNotifyAdminStore struct {
|
||||
store.NotifyAdminStore
|
||||
Root *RetryLayer
|
||||
}
|
||||
|
||||
type RetryLayerOAuthStore struct {
|
||||
store.OAuthStore
|
||||
Root *RetryLayer
|
||||
@@ -5633,6 +5643,90 @@ func (s *RetryLayerLinkMetadataStore) Save(linkMetadata *model.LinkMetadata) (*m
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerNotifyAdminStore) DeleteBefore(trial bool, now int64) error {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
err := s.NotifyAdminStore.DeleteBefore(trial, now)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if !isRepeatableError(err) {
|
||||
return err
|
||||
}
|
||||
tries++
|
||||
if tries >= 3 {
|
||||
err = errors.Wrap(err, "giving up after 3 consecutive repeatable transaction failures")
|
||||
return err
|
||||
}
|
||||
timepkg.Sleep(100 * timepkg.Millisecond)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *RetryLayerNotifyAdminStore) Get(trial bool) ([]*model.NotifyAdminData, error) {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
result, err := s.NotifyAdminStore.Get(trial)
|
||||
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 *RetryLayerNotifyAdminStore) GetDataByUserIdAndFeature(userId string, feature model.MattermostPaidFeature) ([]*model.NotifyAdminData, error) {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
result, err := s.NotifyAdminStore.GetDataByUserIdAndFeature(userId, feature)
|
||||
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 *RetryLayerNotifyAdminStore) Save(data *model.NotifyAdminData) (*model.NotifyAdminData, error) {
|
||||
|
||||
tries := 0
|
||||
for {
|
||||
result, err := s.NotifyAdminStore.Save(data)
|
||||
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 *RetryLayerOAuthStore) DeleteApp(id string) error {
|
||||
|
||||
tries := 0
|
||||
@@ -14058,6 +14152,7 @@ func New(childStore store.Store) *RetryLayer {
|
||||
newStore.JobStore = &RetryLayerJobStore{JobStore: childStore.Job(), Root: &newStore}
|
||||
newStore.LicenseStore = &RetryLayerLicenseStore{LicenseStore: childStore.License(), Root: &newStore}
|
||||
newStore.LinkMetadataStore = &RetryLayerLinkMetadataStore{LinkMetadataStore: childStore.LinkMetadata(), Root: &newStore}
|
||||
newStore.NotifyAdminStore = &RetryLayerNotifyAdminStore{NotifyAdminStore: childStore.NotifyAdmin(), Root: &newStore}
|
||||
newStore.OAuthStore = &RetryLayerOAuthStore{OAuthStore: childStore.OAuth(), Root: &newStore}
|
||||
newStore.PluginStore = &RetryLayerPluginStore{PluginStore: childStore.Plugin(), Root: &newStore}
|
||||
newStore.PostStore = &RetryLayerPostStore{PostStore: childStore.Post(), Root: &newStore}
|
||||
|
||||
@@ -53,6 +53,7 @@ func genStore() *mocks.Store {
|
||||
mock.On("UserAccessToken").Return(&mocks.UserAccessTokenStore{})
|
||||
mock.On("UserTermsOfService").Return(&mocks.UserTermsOfServiceStore{})
|
||||
mock.On("Webhook").Return(&mocks.WebhookStore{})
|
||||
mock.On("NotifyAdmin").Return(&mocks.NotifyAdminStore{})
|
||||
return mock
|
||||
}
|
||||
|
||||
|
||||
87
store/sqlstore/notify_admin_store.go
Обычный файл
87
store/sqlstore/notify_admin_store.go
Обычный файл
@@ -0,0 +1,87 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
"github.com/mattermost/mattermost-server/v6/store"
|
||||
sq "github.com/mattermost/squirrel"
|
||||
)
|
||||
|
||||
type SqlNotifyAdminStore struct {
|
||||
*SqlStore
|
||||
}
|
||||
|
||||
func newSqlNotifyAdminStore(sqlStore *SqlStore) store.NotifyAdminStore {
|
||||
return &SqlNotifyAdminStore{sqlStore}
|
||||
}
|
||||
|
||||
func (s SqlNotifyAdminStore) insert(data *model.NotifyAdminData) (sql.Result, error) {
|
||||
query := `INSERT INTO NotifyAdmin (UserId, CreateAt, RequiredPlan, RequiredFeature, Trial) VALUES (:UserId, :CreateAt, :RequiredPlan, :RequiredFeature, :Trial)`
|
||||
return s.GetMasterX().NamedExec(query, data)
|
||||
}
|
||||
|
||||
func (s SqlNotifyAdminStore) Save(data *model.NotifyAdminData) (*model.NotifyAdminData, error) {
|
||||
if err := data.IsValid(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data.PreSave()
|
||||
|
||||
_, err := s.insert(data)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to save Notify Admin data")
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s SqlNotifyAdminStore) GetDataByUserIdAndFeature(userId string, feature model.MattermostPaidFeature) ([]*model.NotifyAdminData, error) {
|
||||
data := []*model.NotifyAdminData{}
|
||||
query, args, err := s.getQueryBuilder().
|
||||
Select("*").
|
||||
From("NotifyAdmin").
|
||||
Where(sq.Eq{"UserId": userId, "RequiredFeature": feature}).
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not build sql query to get all notifcation data by user id and required feature")
|
||||
}
|
||||
|
||||
if err := s.GetReplicaX().Select(&data, query, args...); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, store.NewErrNotFound("NotifyAdmin", fmt.Sprintf("user id: %s and required feature: %s", userId, feature))
|
||||
}
|
||||
return nil, errors.Wrapf(err, "notifcation data by user id: %s and required feature: %s", userId, feature)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s SqlNotifyAdminStore) Get(trial bool) ([]*model.NotifyAdminData, error) {
|
||||
data := []*model.NotifyAdminData{}
|
||||
query, args, err := s.getQueryBuilder().
|
||||
Select("*").
|
||||
From("NotifyAdmin").
|
||||
Where(sq.Eq{"trial": trial}).
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not build sql query to get all notifcation data")
|
||||
}
|
||||
|
||||
if err := s.GetReplicaX().Select(&data, query, args...); err != nil {
|
||||
return nil, errors.Wrap(err, "notifcation data")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (s SqlNotifyAdminStore) DeleteBefore(trial bool, now int64) error {
|
||||
if _, err := s.GetMasterX().Exec("DELETE FROM NotifyAdmin WHERE trial = ? AND createat < ?", trial, now); err != nil {
|
||||
return errors.Wrapf(err, "failed to remove all notification data with trial=%t", trial)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
14
store/sqlstore/notify_admin_store_test.go
Обычный файл
14
store/sqlstore/notify_admin_store_test.go
Обычный файл
@@ -0,0 +1,14 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/store/storetest"
|
||||
)
|
||||
|
||||
func TestNotifyAdminStore(t *testing.T) {
|
||||
StoreTest(t, storetest.TestNotifyAdminStore)
|
||||
}
|
||||
@@ -108,6 +108,7 @@ type SqlStoreStores struct {
|
||||
UserTermsOfService store.UserTermsOfServiceStore
|
||||
linkMetadata store.LinkMetadataStore
|
||||
sharedchannel store.SharedChannelStore
|
||||
notifyAdmin store.NotifyAdminStore
|
||||
}
|
||||
|
||||
type SqlStore struct {
|
||||
@@ -212,6 +213,7 @@ func New(settings model.SqlSettings, metrics einterfaces.MetricsInterface) *SqlS
|
||||
store.stores.scheme = newSqlSchemeStore(store)
|
||||
store.stores.group = newSqlGroupStore(store)
|
||||
store.stores.productNotices = newSqlProductNoticesStore(store)
|
||||
store.stores.notifyAdmin = newSqlNotifyAdminStore(store)
|
||||
|
||||
store.stores.preference.(*SqlPreferenceStore).deleteUnusedFeatures()
|
||||
|
||||
@@ -914,6 +916,10 @@ func (ss *SqlStore) LinkMetadata() store.LinkMetadataStore {
|
||||
return ss.stores.linkMetadata
|
||||
}
|
||||
|
||||
func (ss *SqlStore) NotifyAdmin() store.NotifyAdminStore {
|
||||
return ss.stores.notifyAdmin
|
||||
}
|
||||
|
||||
func (ss *SqlStore) SharedChannel() store.SharedChannelStore {
|
||||
return ss.stores.sharedchannel
|
||||
}
|
||||
|
||||
@@ -82,6 +82,7 @@ type Store interface {
|
||||
CheckIntegrity() <-chan model.IntegrityCheckResult
|
||||
SetContext(context context.Context)
|
||||
Context() context.Context
|
||||
NotifyAdmin() NotifyAdminStore
|
||||
}
|
||||
|
||||
type RetentionPolicyStore interface {
|
||||
@@ -923,6 +924,13 @@ type LinkMetadataStore interface {
|
||||
Get(url string, timestamp int64) (*model.LinkMetadata, error)
|
||||
}
|
||||
|
||||
type NotifyAdminStore interface {
|
||||
Save(data *model.NotifyAdminData) (*model.NotifyAdminData, error)
|
||||
GetDataByUserIdAndFeature(userId string, feature model.MattermostPaidFeature) ([]*model.NotifyAdminData, error)
|
||||
Get(trial bool) ([]*model.NotifyAdminData, error)
|
||||
DeleteBefore(trial bool, now int64) error
|
||||
}
|
||||
|
||||
type SharedChannelStore interface {
|
||||
Save(sc *model.SharedChannel) (*model.SharedChannel, error)
|
||||
Get(channelId string) (*model.SharedChannel, error)
|
||||
|
||||
98
store/storetest/mocks/NotifyAdminStore.go
Обычный файл
98
store/storetest/mocks/NotifyAdminStore.go
Обычный файл
@@ -0,0 +1,98 @@
|
||||
// Code generated by mockery v2.10.4. DO NOT EDIT.
|
||||
|
||||
// Regenerate this file using `make store-mocks`.
|
||||
|
||||
package mocks
|
||||
|
||||
import (
|
||||
model "github.com/mattermost/mattermost-server/v6/model"
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
// NotifyAdminStore is an autogenerated mock type for the NotifyAdminStore type
|
||||
type NotifyAdminStore struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// DeleteBefore provides a mock function with given fields: trial, now
|
||||
func (_m *NotifyAdminStore) DeleteBefore(trial bool, now int64) error {
|
||||
ret := _m.Called(trial, now)
|
||||
|
||||
var r0 error
|
||||
if rf, ok := ret.Get(0).(func(bool, int64) error); ok {
|
||||
r0 = rf(trial, now)
|
||||
} else {
|
||||
r0 = ret.Error(0)
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// Get provides a mock function with given fields: trial
|
||||
func (_m *NotifyAdminStore) Get(trial bool) ([]*model.NotifyAdminData, error) {
|
||||
ret := _m.Called(trial)
|
||||
|
||||
var r0 []*model.NotifyAdminData
|
||||
if rf, ok := ret.Get(0).(func(bool) []*model.NotifyAdminData); ok {
|
||||
r0 = rf(trial)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]*model.NotifyAdminData)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(bool) error); ok {
|
||||
r1 = rf(trial)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetDataByUserIdAndFeature provides a mock function with given fields: userId, feature
|
||||
func (_m *NotifyAdminStore) GetDataByUserIdAndFeature(userId string, feature model.MattermostPaidFeature) ([]*model.NotifyAdminData, error) {
|
||||
ret := _m.Called(userId, feature)
|
||||
|
||||
var r0 []*model.NotifyAdminData
|
||||
if rf, ok := ret.Get(0).(func(string, model.MattermostPaidFeature) []*model.NotifyAdminData); ok {
|
||||
r0 = rf(userId, feature)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]*model.NotifyAdminData)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(string, model.MattermostPaidFeature) error); ok {
|
||||
r1 = rf(userId, feature)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// Save provides a mock function with given fields: data
|
||||
func (_m *NotifyAdminStore) Save(data *model.NotifyAdminData) (*model.NotifyAdminData, error) {
|
||||
ret := _m.Called(data)
|
||||
|
||||
var r0 *model.NotifyAdminData
|
||||
if rf, ok := ret.Get(0).(func(*model.NotifyAdminData) *model.NotifyAdminData); ok {
|
||||
r0 = rf(data)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(*model.NotifyAdminData)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(1).(func(*model.NotifyAdminData) error); ok {
|
||||
r1 = rf(data)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
@@ -395,6 +395,22 @@ func (_m *Store) MarkSystemRanUnitTests() {
|
||||
_m.Called()
|
||||
}
|
||||
|
||||
// NotifyAdmin provides a mock function with given fields:
|
||||
func (_m *Store) NotifyAdmin() store.NotifyAdminStore {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 store.NotifyAdminStore
|
||||
if rf, ok := ret.Get(0).(func() store.NotifyAdminStore); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(store.NotifyAdminStore)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// OAuth provides a mock function with given fields:
|
||||
func (_m *Store) OAuth() store.OAuthStore {
|
||||
ret := _m.Called()
|
||||
|
||||
194
store/storetest/notify_admin_store.go
Обычный файл
194
store/storetest/notify_admin_store.go
Обычный файл
@@ -0,0 +1,194 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package storetest
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
"github.com/mattermost/mattermost-server/v6/store"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNotifyAdminStore(t *testing.T, ss store.Store) {
|
||||
t.Run("Save", func(t *testing.T) { testNotifyAdminStoreSave(t, ss) })
|
||||
t.Run("testGetDataByUserIdAndFeature", func(t *testing.T) { testGetDataByUserIdAndFeature(t, ss) })
|
||||
t.Run("testGet", func(t *testing.T) { testGet(t, ss) })
|
||||
t.Run("testDeleteBefore", func(t *testing.T) { testDeleteBefore(t, ss) })
|
||||
}
|
||||
|
||||
func tearDown(t *testing.T, ss store.Store) {
|
||||
err := ss.NotifyAdmin().DeleteBefore(true, model.GetMillis()+model.GetMillis())
|
||||
require.NoError(t, err)
|
||||
|
||||
err = ss.NotifyAdmin().DeleteBefore(false, model.GetMillis()+model.GetMillis())
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func testNotifyAdminStoreSave(t *testing.T, ss store.Store) {
|
||||
d1 := &model.NotifyAdminData{
|
||||
UserId: model.NewId(),
|
||||
RequiredPlan: model.LicenseShortSkuProfessional,
|
||||
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
||||
}
|
||||
|
||||
_, err := ss.NotifyAdmin().Save(d1)
|
||||
require.NoError(t, err)
|
||||
|
||||
// unknow plan error
|
||||
d2 := &model.NotifyAdminData{
|
||||
UserId: model.NewId(),
|
||||
RequiredPlan: model.LicenseShortSkuProfessional,
|
||||
RequiredFeature: "Unknown feature",
|
||||
}
|
||||
|
||||
_, err = ss.NotifyAdmin().Save(d2)
|
||||
require.Error(t, err)
|
||||
|
||||
// unknown feature error
|
||||
d3 := &model.NotifyAdminData{
|
||||
UserId: model.NewId(),
|
||||
RequiredPlan: "Unknown plan",
|
||||
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
||||
}
|
||||
_, err = ss.NotifyAdmin().Save(d3)
|
||||
require.Error(t, err)
|
||||
|
||||
// same user requesting same feature error
|
||||
singleUserId := model.NewId()
|
||||
d5 := &model.NotifyAdminData{
|
||||
UserId: singleUserId,
|
||||
RequiredPlan: model.LicenseShortSkuProfessional,
|
||||
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
||||
}
|
||||
_, err = ss.NotifyAdmin().Save(d5)
|
||||
require.NoError(t, err)
|
||||
|
||||
d6 := &model.NotifyAdminData{
|
||||
UserId: singleUserId,
|
||||
RequiredPlan: model.LicenseShortSkuProfessional,
|
||||
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
||||
}
|
||||
_, err = ss.NotifyAdmin().Save(d6)
|
||||
require.Error(t, err)
|
||||
|
||||
tearDown(t, ss)
|
||||
}
|
||||
|
||||
func testGet(t *testing.T, ss store.Store) {
|
||||
userId1 := model.NewId()
|
||||
d1 := &model.NotifyAdminData{
|
||||
UserId: userId1,
|
||||
RequiredPlan: model.LicenseShortSkuProfessional,
|
||||
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
||||
}
|
||||
|
||||
_, err := ss.NotifyAdmin().Save(d1)
|
||||
require.NoError(t, err)
|
||||
|
||||
d1Trial := &model.NotifyAdminData{
|
||||
UserId: userId1,
|
||||
RequiredPlan: model.LicenseShortSkuEnterprise,
|
||||
RequiredFeature: model.PaidFeatureAllEnterprisefeatures,
|
||||
Trial: true,
|
||||
}
|
||||
_, err = ss.NotifyAdmin().Save(d1Trial)
|
||||
require.NoError(t, err)
|
||||
|
||||
d1Trial2 := &model.NotifyAdminData{
|
||||
UserId: model.NewId(),
|
||||
RequiredPlan: model.LicenseShortSkuEnterprise,
|
||||
RequiredFeature: model.PaidFeatureAllEnterprisefeatures,
|
||||
Trial: true,
|
||||
}
|
||||
_, err = ss.NotifyAdmin().Save(d1Trial2)
|
||||
require.NoError(t, err)
|
||||
|
||||
upgradeRequests, err := ss.NotifyAdmin().Get(false)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(upgradeRequests), 1)
|
||||
|
||||
trialRequests, err := ss.NotifyAdmin().Get(true)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(trialRequests), 2)
|
||||
|
||||
tearDown(t, ss)
|
||||
}
|
||||
|
||||
func testGetDataByUserIdAndFeature(t *testing.T, ss store.Store) {
|
||||
userId1 := model.NewId()
|
||||
d1 := &model.NotifyAdminData{
|
||||
UserId: userId1,
|
||||
RequiredPlan: model.LicenseShortSkuProfessional,
|
||||
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
||||
}
|
||||
|
||||
_, err := ss.NotifyAdmin().Save(d1)
|
||||
require.NoError(t, err)
|
||||
|
||||
userId2 := model.NewId()
|
||||
d2 := &model.NotifyAdminData{
|
||||
UserId: userId2,
|
||||
RequiredPlan: model.LicenseShortSkuProfessional,
|
||||
RequiredFeature: model.PaidFeatureCustomUsergroups,
|
||||
}
|
||||
|
||||
_, err = ss.NotifyAdmin().Save(d2)
|
||||
require.NoError(t, err)
|
||||
|
||||
user1Request, err := ss.NotifyAdmin().GetDataByUserIdAndFeature(userId1, model.PaidFeatureAllProfessionalfeatures)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(user1Request), 1)
|
||||
require.Equal(t, user1Request[0].RequiredFeature, model.PaidFeatureAllProfessionalfeatures)
|
||||
|
||||
tearDown(t, ss)
|
||||
}
|
||||
|
||||
func testDeleteBefore(t *testing.T, ss store.Store) {
|
||||
userId1 := model.NewId()
|
||||
d1 := &model.NotifyAdminData{
|
||||
UserId: userId1,
|
||||
RequiredPlan: model.LicenseShortSkuProfessional,
|
||||
RequiredFeature: model.PaidFeatureAllProfessionalfeatures,
|
||||
}
|
||||
|
||||
_, err := ss.NotifyAdmin().Save(d1)
|
||||
require.NoError(t, err)
|
||||
|
||||
d1Trial := &model.NotifyAdminData{
|
||||
UserId: userId1,
|
||||
RequiredPlan: model.LicenseShortSkuProfessional,
|
||||
RequiredFeature: model.PaidFeatureAllEnterprisefeatures,
|
||||
Trial: true,
|
||||
}
|
||||
_, err = ss.NotifyAdmin().Save(d1Trial)
|
||||
require.NoError(t, err)
|
||||
|
||||
d1Trial2 := &model.NotifyAdminData{
|
||||
UserId: model.NewId(),
|
||||
RequiredPlan: model.LicenseShortSkuProfessional,
|
||||
RequiredFeature: model.PaidFeatureAllEnterprisefeatures,
|
||||
Trial: true,
|
||||
}
|
||||
_, err = ss.NotifyAdmin().Save(d1Trial2)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = ss.NotifyAdmin().DeleteBefore(false, model.GetMillis()+model.GetMillis()) // delete all upgrade requests
|
||||
require.NoError(t, err)
|
||||
|
||||
upgradeRequests, err := ss.NotifyAdmin().Get(false)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(upgradeRequests), 0)
|
||||
|
||||
trialRequests, err := ss.NotifyAdmin().Get(true)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(trialRequests), 2) // trial requests should still exist
|
||||
|
||||
err = ss.NotifyAdmin().DeleteBefore(true, model.GetMillis()+model.GetMillis()) // delete all trial requests
|
||||
require.NoError(t, err)
|
||||
|
||||
trialRequests, err = ss.NotifyAdmin().Get(false)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(trialRequests), 0)
|
||||
}
|
||||
@@ -55,6 +55,7 @@ type Store struct {
|
||||
SharedChannelStore mocks.SharedChannelStore
|
||||
ProductNoticesStore mocks.ProductNoticesStore
|
||||
context context.Context
|
||||
NotifyAdminStore mocks.NotifyAdminStore
|
||||
}
|
||||
|
||||
func (s *Store) SetContext(context context.Context) { s.context = context }
|
||||
@@ -95,6 +96,7 @@ func (s *Store) UserTermsOfService() store.UserTermsOfServiceStore { return &s.U
|
||||
func (s *Store) ChannelMemberHistory() store.ChannelMemberHistoryStore {
|
||||
return &s.ChannelMemberHistoryStore
|
||||
}
|
||||
func (s *Store) NotifyAdmin() store.NotifyAdminStore { return &s.NotifyAdminStore }
|
||||
func (s *Store) Group() store.GroupStore { return &s.GroupStore }
|
||||
func (s *Store) LinkMetadata() store.LinkMetadataStore { return &s.LinkMetadataStore }
|
||||
func (s *Store) SharedChannel() store.SharedChannelStore { return &s.SharedChannelStore }
|
||||
@@ -154,5 +156,6 @@ func (s *Store) AssertExpectations(t mock.TestingT) bool {
|
||||
&s.ThreadStore,
|
||||
&s.ProductNoticesStore,
|
||||
&s.SharedChannelStore,
|
||||
&s.NotifyAdminStore,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ type TimerLayer struct {
|
||||
JobStore store.JobStore
|
||||
LicenseStore store.LicenseStore
|
||||
LinkMetadataStore store.LinkMetadataStore
|
||||
NotifyAdminStore store.NotifyAdminStore
|
||||
OAuthStore store.OAuthStore
|
||||
PluginStore store.PluginStore
|
||||
PostStore store.PostStore
|
||||
@@ -113,6 +114,10 @@ func (s *TimerLayer) LinkMetadata() store.LinkMetadataStore {
|
||||
return s.LinkMetadataStore
|
||||
}
|
||||
|
||||
func (s *TimerLayer) NotifyAdmin() store.NotifyAdminStore {
|
||||
return s.NotifyAdminStore
|
||||
}
|
||||
|
||||
func (s *TimerLayer) OAuth() store.OAuthStore {
|
||||
return s.OAuthStore
|
||||
}
|
||||
@@ -275,6 +280,11 @@ type TimerLayerLinkMetadataStore struct {
|
||||
Root *TimerLayer
|
||||
}
|
||||
|
||||
type TimerLayerNotifyAdminStore struct {
|
||||
store.NotifyAdminStore
|
||||
Root *TimerLayer
|
||||
}
|
||||
|
||||
type TimerLayerOAuthStore struct {
|
||||
store.OAuthStore
|
||||
Root *TimerLayer
|
||||
@@ -4507,6 +4517,70 @@ func (s *TimerLayerLinkMetadataStore) Save(linkMetadata *model.LinkMetadata) (*m
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerNotifyAdminStore) DeleteBefore(trial bool, now int64) error {
|
||||
start := time.Now()
|
||||
|
||||
err := s.NotifyAdminStore.DeleteBefore(trial, now)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("NotifyAdminStore.DeleteBefore", success, elapsed)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *TimerLayerNotifyAdminStore) Get(trial bool) ([]*model.NotifyAdminData, error) {
|
||||
start := time.Now()
|
||||
|
||||
result, err := s.NotifyAdminStore.Get(trial)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("NotifyAdminStore.Get", success, elapsed)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerNotifyAdminStore) GetDataByUserIdAndFeature(userId string, feature model.MattermostPaidFeature) ([]*model.NotifyAdminData, error) {
|
||||
start := time.Now()
|
||||
|
||||
result, err := s.NotifyAdminStore.GetDataByUserIdAndFeature(userId, feature)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("NotifyAdminStore.GetDataByUserIdAndFeature", success, elapsed)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerNotifyAdminStore) Save(data *model.NotifyAdminData) (*model.NotifyAdminData, error) {
|
||||
start := time.Now()
|
||||
|
||||
result, err := s.NotifyAdminStore.Save(data)
|
||||
|
||||
elapsed := float64(time.Since(start)) / float64(time.Second)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if err == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("NotifyAdminStore.Save", success, elapsed)
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (s *TimerLayerOAuthStore) DeleteApp(id string) error {
|
||||
start := time.Now()
|
||||
|
||||
@@ -11112,6 +11186,7 @@ func New(childStore store.Store, metrics einterfaces.MetricsInterface) *TimerLay
|
||||
newStore.JobStore = &TimerLayerJobStore{JobStore: childStore.Job(), Root: &newStore}
|
||||
newStore.LicenseStore = &TimerLayerLicenseStore{LicenseStore: childStore.License(), Root: &newStore}
|
||||
newStore.LinkMetadataStore = &TimerLayerLinkMetadataStore{LinkMetadataStore: childStore.LinkMetadata(), Root: &newStore}
|
||||
newStore.NotifyAdminStore = &TimerLayerNotifyAdminStore{NotifyAdminStore: childStore.NotifyAdmin(), Root: &newStore}
|
||||
newStore.OAuthStore = &TimerLayerOAuthStore{OAuthStore: childStore.OAuth(), Root: &newStore}
|
||||
newStore.PluginStore = &TimerLayerPluginStore{PluginStore: childStore.Plugin(), Root: &newStore}
|
||||
newStore.PostStore = &TimerLayerPostStore{PostStore: childStore.Post(), Root: &newStore}
|
||||
|
||||
Ссылка в новой задаче
Block a user