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
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
56a0becbca
Коммит
f13a531bca
@@ -14,6 +14,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/plugin"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/i18n"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
@@ -138,6 +140,24 @@ func (a *App) sendPushNotificationToAllSessions(msg *model.PushNotification, use
|
||||
}
|
||||
|
||||
func (a *App) sendPushNotification(notification *PostNotification, user *model.User, explicitMention, channelWideMention bool, replyToThreadType string) {
|
||||
cancelled := false
|
||||
a.ch.RunMultiHook(func(hooks plugin.Hooks) bool {
|
||||
cancelled = hooks.NotificationWillBePushed(&model.PluginPushNotification{
|
||||
Post: notification.Post.ForPlugin(),
|
||||
Channel: notification.Channel,
|
||||
UserID: user.Id,
|
||||
})
|
||||
if cancelled {
|
||||
mlog.Info("Notification cancelled by plugin")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}, plugin.NotificationWillBePushedID)
|
||||
|
||||
if cancelled {
|
||||
return
|
||||
}
|
||||
|
||||
cfg := a.Config()
|
||||
channel := notification.Channel
|
||||
post := notification.Post
|
||||
|
||||
@@ -1468,7 +1468,6 @@ func TestPushNotificationRace(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
s.products["channels"] = ch
|
||||
|
||||
app := New(ServerConnector(s.Channels()))
|
||||
require.NotPanics(t, func() {
|
||||
s.createPushNotificationsHub(th.Context)
|
||||
|
||||
@@ -1476,9 +1475,9 @@ func TestPushNotificationRace(t *testing.T) {
|
||||
|
||||
// Now we start sending messages after the PN hub is shut down.
|
||||
// We test all 3 notification types.
|
||||
app.clearPushNotification("currentSessionId", "userId", "channelId", "")
|
||||
th.App.clearPushNotification("currentSessionId", "userId", "channelId", "")
|
||||
|
||||
app.UpdateMobileAppBadge("userId")
|
||||
th.App.UpdateMobileAppBadge("userId")
|
||||
|
||||
notification := &PostNotification{
|
||||
Post: &model.Post{},
|
||||
@@ -1488,7 +1487,7 @@ func TestPushNotificationRace(t *testing.T) {
|
||||
},
|
||||
Sender: &model.User{},
|
||||
}
|
||||
app.sendPushNotification(notification, &model.User{}, true, false, model.CommentsNotifyAny)
|
||||
th.App.sendPushNotification(notification, &model.User{}, true, false, model.CommentsNotifyAny)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -6,11 +6,14 @@ package app
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -1331,3 +1334,118 @@ func TestHookOnCloudLimitsUpdated(t *testing.T) {
|
||||
|
||||
require.True(t, hookCalled)
|
||||
}
|
||||
|
||||
//go:embed test_templates/hook_notification_will_be_pushed.tmpl
|
||||
var hookNotificationWillBePushedTmpl string
|
||||
|
||||
func TestHookNotificationWillBePushed(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping TestHookNotificationWillBePushed test in short mode")
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
testCode string
|
||||
expectedNotifications int
|
||||
}{
|
||||
{
|
||||
name: "successfully pushed",
|
||||
testCode: `return false`,
|
||||
expectedNotifications: 6,
|
||||
},
|
||||
{
|
||||
name: "push notification rejected",
|
||||
testCode: `return true`,
|
||||
expectedNotifications: 0,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
templatedPlugin := fmt.Sprintf(hookNotificationWillBePushedTmpl, tt.testCode)
|
||||
tearDown, _, _ := SetAppEnvironmentWithPlugins(t, []string{templatedPlugin}, th.App, th.NewPluginAPI)
|
||||
defer tearDown()
|
||||
|
||||
// Create 3 users, each having 2 sessions.
|
||||
type userSession struct {
|
||||
user *model.User
|
||||
session *model.Session
|
||||
}
|
||||
var userSessions []userSession
|
||||
for i := 0; i < 3; i++ {
|
||||
u := th.CreateUser()
|
||||
sess, err := th.App.CreateSession(&model.Session{
|
||||
UserId: u.Id,
|
||||
DeviceId: "deviceID" + u.Id,
|
||||
ExpiresAt: model.GetMillis() + 100000,
|
||||
})
|
||||
require.Nil(t, err)
|
||||
// We don't need to track the 2nd session.
|
||||
_, err = th.App.CreateSession(&model.Session{
|
||||
UserId: u.Id,
|
||||
DeviceId: "deviceID" + u.Id,
|
||||
ExpiresAt: model.GetMillis() + 100000,
|
||||
})
|
||||
require.Nil(t, err)
|
||||
_, err = th.App.AddTeamMember(th.Context, th.BasicTeam.Id, u.Id)
|
||||
require.Nil(t, err)
|
||||
th.AddUserToChannel(u, th.BasicChannel)
|
||||
userSessions = append(userSessions, userSession{
|
||||
user: u,
|
||||
session: sess,
|
||||
})
|
||||
}
|
||||
|
||||
handler := &testPushNotificationHandler{
|
||||
t: t,
|
||||
behavior: "simple",
|
||||
}
|
||||
pushServer := httptest.NewServer(
|
||||
http.HandlerFunc(handler.handleReq),
|
||||
)
|
||||
defer pushServer.Close()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.EmailSettings.PushNotificationContents = model.GenericNotification
|
||||
*cfg.EmailSettings.PushNotificationServer = pushServer.URL
|
||||
})
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for _, data := range userSessions {
|
||||
wg.Add(1)
|
||||
go func(user model.User) {
|
||||
defer wg.Done()
|
||||
notification := &PostNotification{
|
||||
Post: th.CreatePost(th.BasicChannel),
|
||||
Channel: th.BasicChannel,
|
||||
ProfileMap: map[string]*model.User{
|
||||
user.Id: &user,
|
||||
},
|
||||
Sender: &user,
|
||||
}
|
||||
th.App.sendPushNotification(notification, &user, true, false, model.CommentsNotifyAny)
|
||||
}(*data.user)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// Hack to let the worker goroutines complete.
|
||||
time.Sleep(1 * time.Second)
|
||||
// Server side verification.
|
||||
assert.Equal(t, tt.expectedNotifications, handler.numReqs())
|
||||
var numMessages int
|
||||
for _, n := range handler.notifications() {
|
||||
switch n.Type {
|
||||
case model.PushTypeMessage:
|
||||
numMessages++
|
||||
assert.Equal(t, th.BasicChannel.Id, n.ChannelId)
|
||||
assert.Contains(t, n.Message, "mentioned you")
|
||||
default:
|
||||
assert.Fail(t, "should not receive any other push notification types")
|
||||
}
|
||||
}
|
||||
assert.Equal(t, tt.expectedNotifications, numMessages)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost/server/public/plugin"
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) NotificationWillBePushed(notification *model.PluginPushNotification) (cancel bool) {
|
||||
%s
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
Ссылка в новой задаче
Block a user