Add UserHasBeenDeactivated plugin hook (#20894)

Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
Этот коммит содержится в:
Matthew Dorner
2023-08-30 01:55:20 -05:00
коммит произвёл GitHub
родитель 8c8b8b7e79
Коммит 5de1e306de
7 изменённых файлов: 144 добавлений и 14 удалений

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

@@ -879,6 +879,40 @@ func (s *hooksRPCServer) NotificationWillBePushed(args *Z_NotificationWillBePush
return nil
}
func init() {
hookNameToId["UserHasBeenDeactivated"] = UserHasBeenDeactivatedID
}
type Z_UserHasBeenDeactivatedArgs struct {
A *Context
B *model.User
}
type Z_UserHasBeenDeactivatedReturns struct {
}
func (g *hooksRPCClient) UserHasBeenDeactivated(c *Context, user *model.User) {
_args := &Z_UserHasBeenDeactivatedArgs{c, user}
_returns := &Z_UserHasBeenDeactivatedReturns{}
if g.implemented[UserHasBeenDeactivatedID] {
if err := g.client.Call("Plugin.UserHasBeenDeactivated", _args, _returns); err != nil {
g.log.Error("RPC call UserHasBeenDeactivated to plugin failed.", mlog.Err(err))
}
}
}
func (s *hooksRPCServer) UserHasBeenDeactivated(args *Z_UserHasBeenDeactivatedArgs, returns *Z_UserHasBeenDeactivatedReturns) error {
if hook, ok := s.impl.(interface {
UserHasBeenDeactivated(c *Context, user *model.User)
}); ok {
hook.UserHasBeenDeactivated(args.A, args.B)
} else {
return encodableError(fmt.Errorf("Hook UserHasBeenDeactivated called but not implemented."))
}
return nil
}
type Z_RegisterCommandArgs struct {
A *model.Command
}

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

@@ -51,6 +51,7 @@ const (
deprecatedGetTopicMetadataByIdsID = 33
ConfigurationWillBeSavedID = 34
NotificationWillBePushedID = 35
UserHasBeenDeactivatedID = 36
TotalHooksID = iota
)
@@ -298,4 +299,9 @@ type Hooks interface {
//
// Minimum server version: 9.0
NotificationWillBePushed(pushNotification *model.PushNotification, userID string) (*model.PushNotification, string)
// UserHasBeenDeactivated is invoked when a user is deactivated.
//
// Minimum server version: 9.1
UserHasBeenDeactivated(c *Context, user *model.User)
}

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

@@ -225,3 +225,9 @@ func (hooks *hooksTimerLayer) NotificationWillBePushed(pushNotification *model.P
hooks.recordTime(startTime, "NotificationWillBePushed", true)
return _returnsA, _returnsB
}
func (hooks *hooksTimerLayer) UserHasBeenDeactivated(c *Context, user *model.User) {
startTime := timePkg.Now()
hooks.hooksImpl.UserHasBeenDeactivated(c, user)
hooks.recordTime(startTime, "UserHasBeenDeactivated", true)
}

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

@@ -344,6 +344,11 @@ func (_m *Hooks) UserHasBeenCreated(c *plugin.Context, user *model.User) {
_m.Called(c, user)
}
// UserHasBeenDeactivated provides a mock function with given fields: c, user
func (_m *Hooks) UserHasBeenDeactivated(c *plugin.Context, user *model.User) {
_m.Called(c, user)
}
// UserHasJoinedChannel provides a mock function with given fields: c, channelMember, actor
func (_m *Hooks) UserHasJoinedChannel(c *plugin.Context, channelMember *model.ChannelMember, actor *model.User) {
_m.Called(c, channelMember, actor)

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

@@ -122,6 +122,10 @@ type NotificationWillBePushedIFace interface {
NotificationWillBePushed(pushNotification *model.PushNotification, userID string) (*model.PushNotification, string)
}
type UserHasBeenDeactivatedIFace interface {
UserHasBeenDeactivated(c *Context, user *model.User)
}
type HooksAdapter struct {
implemented map[int]struct{}
productHooks any
@@ -378,6 +382,15 @@ func NewAdapter(productHooks any) (*HooksAdapter, error) {
return nil, errors.New("hook has NotificationWillBePushed method but does not implement plugin.NotificationWillBePushed interface")
}
// Assessing the type of the productHooks if it individually implements UserHasBeenDeactivated interface.
tt = reflect.TypeOf((*UserHasBeenDeactivatedIFace)(nil)).Elem()
if ft.Implements(tt) {
a.implemented[UserHasBeenDeactivatedID] = struct{}{}
} else if _, ok := ft.MethodByName("UserHasBeenDeactivated"); ok {
return nil, errors.New("hook has UserHasBeenDeactivated method but does not implement plugin.UserHasBeenDeactivated interface")
}
return a, nil
}
@@ -623,3 +636,12 @@ func (a *HooksAdapter) NotificationWillBePushed(pushNotification *model.PushNoti
return a.productHooks.(NotificationWillBePushedIFace).NotificationWillBePushed(pushNotification, userID)
}
func (a *HooksAdapter) UserHasBeenDeactivated(c *Context, user *model.User) {
if _, ok := a.implemented[UserHasBeenDeactivatedID]; !ok {
panic("product hooks must implement UserHasBeenDeactivated")
}
a.productHooks.(UserHasBeenDeactivatedIFace).UserHasBeenDeactivated(c, user)
}