MM-52804 - Implement SendPushNotification plugin api method (#24273)

* add SendPushNotification plugin api method

* lint; add testing.Short bc of the sleep

* add interface and generated layers

* add fields to PluginPushNotification; generate mocks

* SendPushNotification -> SendPluginPushNotification; improved comments

* more comments; fix test

* send api.ctx
Этот коммит содержится в:
Christopher Poile
2023-08-18 13:05:26 -04:00
коммит произвёл GitHub
родитель f13a531bca
Коммит 8418eefb75
9 изменённых файлов: 198 добавлений и 7 удалений

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

@@ -143,9 +143,12 @@ func (a *App) sendPushNotification(notification *PostNotification, user *model.U
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,
Post: notification.Post.ForPlugin(),
Channel: notification.Channel,
UserID: user.Id,
ExplicitMention: explicitMention,
ChannelWideMention: channelWideMention,
ReplyToThreadType: replyToThreadType,
})
if cancelled {
mlog.Info("Notification cancelled by plugin")

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

@@ -1266,3 +1266,31 @@ func (api *PluginAPI) GetUploadSession(uploadID string) (*model.UploadSession, e
}
return fi, nil
}
func (api *PluginAPI) SendPluginPushNotification(notification *model.PluginPushNotification) error {
var profiles map[string]*model.User
var err error
if notification.Channel.Type == model.ChannelTypeGroup {
if profiles, err = api.app.Srv().Store().User().GetAllProfilesInChannel(api.ctx.Context(), notification.Channel.Id, true); err != nil {
return err
}
}
sender, appErr := api.app.GetUser(notification.Post.UserId)
if appErr != nil {
return appErr
}
user, appErr := api.app.GetUser(notification.UserID)
if appErr != nil {
return appErr
}
postNotification := &PostNotification{
Post: notification.Post,
Channel: notification.Channel,
ProfileMap: profiles,
Sender: sender,
}
api.app.sendPushNotification(postNotification, user, notification.ExplicitMention, notification.ChannelWideMention, notification.ReplyToThreadType)
return nil
}

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

@@ -19,6 +19,7 @@ import (
"path"
"path/filepath"
"strings"
"sync"
"testing"
"time"
@@ -2204,3 +2205,91 @@ func TestConfigurationWillBeSavedHook(t *testing.T) {
}, newCfg.PluginSettings.Plugins["custom_plugin"])
})
}
func TestSendPushNotification(t *testing.T) {
if testing.Short() {
t.Skip("skipping TestSendPushNotification test in short mode")
}
th := Setup(t).InitBasic()
defer th.TearDown()
api := th.SetupPluginAPI()
// 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.FullNotification
*cfg.EmailSettings.PushNotificationServer = pushServer.URL
})
var wg sync.WaitGroup
for _, data := range userSessions {
wg.Add(1)
go func(user model.User) {
defer wg.Done()
post := th.CreatePost(th.BasicChannel)
post.Message = "started a conversation"
notification := &model.PluginPushNotification{
Post: post,
Channel: th.BasicChannel,
UserID: user.Id,
}
appErr := api.SendPluginPushNotification(notification)
require.NoError(t, appErr)
}(*data.user)
}
wg.Wait()
// Hack to let the worker goroutines complete.
time.Sleep(1 * time.Second)
// Server side verification.
var numMessages int
for _, n := range handler.notifications() {
switch n.Type {
case model.PushTypeMessage:
numMessages++
assert.Equal(t, th.BasicChannel.Id, n.ChannelId)
assert.Equal(t, fmt.Sprintf("@%s: started a conversation", th.BasicUser.GetDisplayName(model.ShowUsername)), n.Message)
default:
assert.Fail(t, "should not receive any other push notification types")
}
}
assert.Equal(t, 6, numMessages)
}