MM-53924 - Implement NotificationWillBePushed plugin hook (#24263)

* add NotificationWillBePushed hook

* mocks

* use a struct for hook parameters; simplify number of parameters sent across RPC

* missing wg.Wait

* change to a bool return value
Этот коммит содержится в:
Christopher Poile
2023-08-18 12:01:50 -04:00
коммит произвёл GitHub
родитель 56a0becbca
Коммит f13a531bca
10 изменённых файлов: 257 добавлений и 4 удалений

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

@@ -843,6 +843,40 @@ func (s *hooksRPCServer) ConfigurationWillBeSaved(args *Z_ConfigurationWillBeSav
return nil
}
func init() {
hookNameToId["NotificationWillBePushed"] = NotificationWillBePushedID
}
type Z_NotificationWillBePushedArgs struct {
A *model.PluginPushNotification
}
type Z_NotificationWillBePushedReturns struct {
A bool
}
func (g *hooksRPCClient) NotificationWillBePushed(pushNotification *model.PluginPushNotification) (cancel bool) {
_args := &Z_NotificationWillBePushedArgs{pushNotification}
_returns := &Z_NotificationWillBePushedReturns{}
if g.implemented[NotificationWillBePushedID] {
if err := g.client.Call("Plugin.NotificationWillBePushed", _args, _returns); err != nil {
g.log.Error("RPC call NotificationWillBePushed to plugin failed.", mlog.Err(err))
}
}
return _returns.A
}
func (s *hooksRPCServer) NotificationWillBePushed(args *Z_NotificationWillBePushedArgs, returns *Z_NotificationWillBePushedReturns) error {
if hook, ok := s.impl.(interface {
NotificationWillBePushed(pushNotification *model.PluginPushNotification) (cancel bool)
}); ok {
returns.A = hook.NotificationWillBePushed(args.A)
} else {
return encodableError(fmt.Errorf("Hook NotificationWillBePushed called but not implemented."))
}
return nil
}
type Z_RegisterCommandArgs struct {
A *model.Command
}

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

@@ -50,6 +50,7 @@ const (
deprecatedGetCollectionMetadataByIdsID = 32
deprecatedGetTopicMetadataByIdsID = 33
ConfigurationWillBeSavedID = 34
NotificationWillBePushedID = 35
TotalHooksID = iota
)
@@ -284,4 +285,12 @@ type Hooks interface {
// config object can be returned to be stored in place of the provided one.
// Minimum server version: 8.0
ConfigurationWillBeSaved(newCfg *model.Config) (*model.Config, error)
// NotificationWillBePushed is invoked before a push notification is sent to the push
// notification server. The intention is to allow plugins to cancel a push notification.
//
// To cancel a push notification, return true.
//
// Minimum server version: 9.0
NotificationWillBePushed(pushNotification *model.PluginPushNotification) (cancel bool)
}

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

@@ -218,3 +218,10 @@ func (hooks *hooksTimerLayer) ConfigurationWillBeSaved(newCfg *model.Config) (*m
hooks.recordTime(startTime, "ConfigurationWillBeSaved", _returnsB == nil)
return _returnsA, _returnsB
}
func (hooks *hooksTimerLayer) NotificationWillBePushed(pushNotification *model.PluginPushNotification) (cancel bool) {
startTime := timePkg.Now()
_returnsA := hooks.hooksImpl.NotificationWillBePushed(pushNotification)
hooks.recordTime(startTime, "NotificationWillBePushed", true)
return _returnsA
}

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

@@ -193,6 +193,20 @@ func (_m *Hooks) MessageWillBeUpdated(c *plugin.Context, newPost *model.Post, ol
return r0, r1
}
// NotificationWillBePushed provides a mock function with given fields: pushNotification
func (_m *Hooks) NotificationWillBePushed(pushNotification *model.PluginPushNotification) bool {
ret := _m.Called(pushNotification)
var r0 bool
if rf, ok := ret.Get(0).(func(*model.PluginPushNotification) bool); ok {
r0 = rf(pushNotification)
} else {
r0 = ret.Get(0).(bool)
}
return r0
}
// OnActivate provides a mock function with given fields:
func (_m *Hooks) OnActivate() error {
ret := _m.Called()

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

@@ -118,6 +118,10 @@ type ConfigurationWillBeSavedIFace interface {
ConfigurationWillBeSaved(newCfg *model.Config) (*model.Config, error)
}
type NotificationWillBePushedIFace interface {
NotificationWillBePushed(pushNotification *model.PluginPushNotification) (cancel bool)
}
type HooksAdapter struct {
implemented map[int]struct{}
productHooks any
@@ -365,6 +369,15 @@ func NewAdapter(productHooks any) (*HooksAdapter, error) {
return nil, errors.New("hook has ConfigurationWillBeSaved method but does not implement plugin.ConfigurationWillBeSaved interface")
}
// Assessing the type of the productHooks if it individually implements NotificationWillBePushed interface.
tt = reflect.TypeOf((*NotificationWillBePushedIFace)(nil)).Elem()
if ft.Implements(tt) {
a.implemented[NotificationWillBePushedID] = struct{}{}
} else if _, ok := ft.MethodByName("NotificationWillBePushed"); ok {
return nil, errors.New("hook has NotificationWillBePushed method but does not implement plugin.NotificationWillBePushed interface")
}
return a, nil
}
@@ -601,3 +614,12 @@ func (a *HooksAdapter) ConfigurationWillBeSaved(newCfg *model.Config) (*model.Co
return a.productHooks.(ConfigurationWillBeSavedIFace).ConfigurationWillBeSaved(newCfg)
}
func (a *HooksAdapter) NotificationWillBePushed(pushNotification *model.PluginPushNotification) (cancel bool) {
if _, ok := a.implemented[NotificationWillBePushedID]; !ok {
panic("product hooks must implement NotificationWillBePushed")
}
return a.productHooks.(NotificationWillBePushedIFace).NotificationWillBePushed(pushNotification)
}