Adding 'MessageHasBeenDeleted' hook to plugins (#22476)
* Adding 'MessageHasBeenDeleted' hook to plugins * Remove debug logs * Add unit test * Bumping the required version * Fix CI problems * Increasing the minimum version * go fmt --------- Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
c731630e88
Коммит
17ff2049ec
@@ -290,6 +290,40 @@ func (s *hooksRPCServer) MessageHasBeenUpdated(args *Z_MessageHasBeenUpdatedArgs
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
hookNameToId["MessageHasBeenDeleted"] = MessageHasBeenDeletedID
|
||||
}
|
||||
|
||||
type Z_MessageHasBeenDeletedArgs struct {
|
||||
A *Context
|
||||
B *model.Post
|
||||
}
|
||||
|
||||
type Z_MessageHasBeenDeletedReturns struct {
|
||||
}
|
||||
|
||||
func (g *hooksRPCClient) MessageHasBeenDeleted(c *Context, post *model.Post) {
|
||||
_args := &Z_MessageHasBeenDeletedArgs{c, post}
|
||||
_returns := &Z_MessageHasBeenDeletedReturns{}
|
||||
if g.implemented[MessageHasBeenDeletedID] {
|
||||
if err := g.client.Call("Plugin.MessageHasBeenDeleted", _args, _returns); err != nil {
|
||||
g.log.Error("RPC call MessageHasBeenDeleted to plugin failed.", mlog.Err(err))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *hooksRPCServer) MessageHasBeenDeleted(args *Z_MessageHasBeenDeletedArgs, returns *Z_MessageHasBeenDeletedReturns) error {
|
||||
if hook, ok := s.impl.(interface {
|
||||
MessageHasBeenDeleted(c *Context, post *model.Post)
|
||||
}); ok {
|
||||
hook.MessageHasBeenDeleted(args.A, args.B)
|
||||
} else {
|
||||
return encodableError(fmt.Errorf("Hook MessageHasBeenDeleted called but not implemented."))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
hookNameToId["ChannelHasBeenCreated"] = ChannelHasBeenCreatedID
|
||||
}
|
||||
|
||||
@@ -52,6 +52,7 @@ const (
|
||||
ConfigurationWillBeSavedID = 34
|
||||
NotificationWillBePushedID = 35
|
||||
UserHasBeenDeactivatedID = 36
|
||||
MessageHasBeenDeletedID = 37
|
||||
TotalHooksID = iota
|
||||
)
|
||||
|
||||
@@ -169,6 +170,13 @@ type Hooks interface {
|
||||
// Minimum server version: 5.2
|
||||
MessageHasBeenUpdated(c *Context, newPost, oldPost *model.Post)
|
||||
|
||||
// MessageHasBeenDeleted is invoked after the message has been deleted from the database.
|
||||
// Note that this method will be called for posts deleted by plugins, including the plugin that
|
||||
// deleted the post.
|
||||
//
|
||||
// Minimum server version: 9.1
|
||||
MessageHasBeenDeleted(c *Context, post *model.Post)
|
||||
|
||||
// ChannelHasBeenCreated is invoked after the channel has been committed to the database.
|
||||
//
|
||||
// Minimum server version: 5.2
|
||||
|
||||
@@ -113,6 +113,12 @@ func (hooks *hooksTimerLayer) MessageHasBeenUpdated(c *Context, newPost, oldPost
|
||||
hooks.recordTime(startTime, "MessageHasBeenUpdated", true)
|
||||
}
|
||||
|
||||
func (hooks *hooksTimerLayer) MessageHasBeenDeleted(c *Context, post *model.Post) {
|
||||
startTime := timePkg.Now()
|
||||
hooks.hooksImpl.MessageHasBeenDeleted(c, post)
|
||||
hooks.recordTime(startTime, "MessageHasBeenDeleted", true)
|
||||
}
|
||||
|
||||
func (hooks *hooksTimerLayer) ChannelHasBeenCreated(c *Context, channel *model.Channel) {
|
||||
startTime := timePkg.Now()
|
||||
hooks.hooksImpl.ChannelHasBeenCreated(c, channel)
|
||||
|
||||
@@ -131,6 +131,11 @@ func (_m *Hooks) Implemented() ([]string, error) {
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// MessageHasBeenDeleted provides a mock function with given fields: c, post
|
||||
func (_m *Hooks) MessageHasBeenDeleted(c *plugin.Context, post *model.Post) {
|
||||
_m.Called(c, post)
|
||||
}
|
||||
|
||||
// MessageHasBeenPosted provides a mock function with given fields: c, post
|
||||
func (_m *Hooks) MessageHasBeenPosted(c *plugin.Context, post *model.Post) {
|
||||
_m.Called(c, post)
|
||||
|
||||
@@ -50,6 +50,10 @@ type MessageHasBeenUpdatedIFace interface {
|
||||
MessageHasBeenUpdated(c *Context, newPost, oldPost *model.Post)
|
||||
}
|
||||
|
||||
type MessageHasBeenDeletedIFace interface {
|
||||
MessageHasBeenDeleted(c *Context, post *model.Post)
|
||||
}
|
||||
|
||||
type ChannelHasBeenCreatedIFace interface {
|
||||
ChannelHasBeenCreated(c *Context, channel *model.Channel)
|
||||
}
|
||||
@@ -220,6 +224,15 @@ func NewAdapter(productHooks any) (*HooksAdapter, error) {
|
||||
return nil, errors.New("hook has MessageHasBeenUpdated method but does not implement plugin.MessageHasBeenUpdated interface")
|
||||
}
|
||||
|
||||
// Assessing the type of the productHooks if it individually implements MessageHasBeenDeleted interface.
|
||||
tt = reflect.TypeOf((*MessageHasBeenDeletedIFace)(nil)).Elem()
|
||||
|
||||
if ft.Implements(tt) {
|
||||
a.implemented[MessageHasBeenDeletedID] = struct{}{}
|
||||
} else if _, ok := ft.MethodByName("MessageHasBeenDeleted"); ok {
|
||||
return nil, errors.New("hook has MessageHasBeenDeleted method but does not implement plugin.MessageHasBeenDeleted interface")
|
||||
}
|
||||
|
||||
// Assessing the type of the productHooks if it individually implements ChannelHasBeenCreated interface.
|
||||
tt = reflect.TypeOf((*ChannelHasBeenCreatedIFace)(nil)).Elem()
|
||||
|
||||
@@ -475,6 +488,15 @@ func (a *HooksAdapter) MessageHasBeenUpdated(c *Context, newPost, oldPost *model
|
||||
|
||||
}
|
||||
|
||||
func (a *HooksAdapter) MessageHasBeenDeleted(c *Context, post *model.Post) {
|
||||
if _, ok := a.implemented[MessageHasBeenDeletedID]; !ok {
|
||||
panic("product hooks must implement MessageHasBeenDeleted")
|
||||
}
|
||||
|
||||
a.productHooks.(MessageHasBeenDeletedIFace).MessageHasBeenDeleted(c, post)
|
||||
|
||||
}
|
||||
|
||||
func (a *HooksAdapter) ChannelHasBeenCreated(c *Context, channel *model.Channel) {
|
||||
if _, ok := a.implemented[ChannelHasBeenCreatedID]; !ok {
|
||||
panic("product hooks must implement ChannelHasBeenCreated")
|
||||
|
||||
Ссылка в новой задаче
Block a user