-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MM-58292] Suppress push notifications to call thread if receiver is in call #807
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/mattermost/mattermost-plugin-calls/server/public" | ||
|
||
pluginMocks "github.com/mattermost/mattermost-plugin-calls/server/mocks/github.com/mattermost/mattermost/server/public/plugin" | ||
|
||
"github.com/mattermost/mattermost/server/public/model" | ||
"github.com/mattermost/mattermost/server/public/plugin" | ||
|
||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNotificationWillBePushed(t *testing.T) { | ||
mockAPI := &pluginMocks.MockAPI{} | ||
|
||
store, tearDown := NewTestStore(t) | ||
t.Cleanup(tearDown) | ||
|
||
p := Plugin{ | ||
MattermostPlugin: plugin.MattermostPlugin{ | ||
API: mockAPI, | ||
}, | ||
store: store, | ||
} | ||
|
||
t.Run("not a call post", func(t *testing.T) { | ||
res, msg := p.NotificationWillBePushed(&model.PushNotification{}, "userID") | ||
require.Nil(t, res) | ||
require.Empty(t, msg) | ||
}) | ||
|
||
t.Run("user not in call", func(t *testing.T) { | ||
channelID := model.NewId() | ||
threadID := model.NewId() | ||
|
||
err := p.store.CreateCall(&public.Call{ | ||
ID: model.NewId(), | ||
CreateAt: time.Now().UnixMilli(), | ||
ChannelID: channelID, | ||
StartAt: time.Now().UnixMilli(), | ||
PostID: model.NewId(), | ||
ThreadID: threadID, | ||
OwnerID: model.NewId(), | ||
}) | ||
require.NoError(t, err) | ||
|
||
res, msg := p.NotificationWillBePushed(&model.PushNotification{ | ||
ChannelId: channelID, | ||
RootId: threadID, | ||
}, "userID") | ||
require.Nil(t, res) | ||
require.Empty(t, msg) | ||
}) | ||
|
||
t.Run("user in call", func(t *testing.T) { | ||
defer mockAPI.AssertExpectations(t) | ||
|
||
channelID := model.NewId() | ||
threadID := model.NewId() | ||
userID := model.NewId() | ||
callID := model.NewId() | ||
|
||
err := p.store.CreateCall(&public.Call{ | ||
ID: callID, | ||
CreateAt: time.Now().UnixMilli(), | ||
ChannelID: channelID, | ||
StartAt: time.Now().UnixMilli(), | ||
PostID: model.NewId(), | ||
ThreadID: threadID, | ||
OwnerID: model.NewId(), | ||
}) | ||
require.NoError(t, err) | ||
|
||
err = p.store.CreateCallSession(&public.CallSession{ | ||
ID: model.NewId(), | ||
CallID: callID, | ||
UserID: userID, | ||
JoinAt: time.Now().UnixMilli(), | ||
}) | ||
require.NoError(t, err) | ||
|
||
mockAPI.On("LogDebug", "calls: suppressing notification on call thread for connected user", | ||
"origin", mock.AnythingOfType("string"), | ||
"userID", userID, "channelID", channelID, "threadID", threadID, "callID", callID).Once() | ||
|
||
res, msg := p.NotificationWillBePushed(&model.PushNotification{ | ||
ChannelId: channelID, | ||
RootId: threadID, | ||
}, userID) | ||
require.Nil(t, res) | ||
require.Equal(t, "calls: suppressing notification on call thread for connected user", msg) | ||
}) | ||
|
||
t.Run("DM/GM ringing", func(t *testing.T) { | ||
defer mockAPI.AssertExpectations(t) | ||
var cfg configuration | ||
cfg.SetDefaults() | ||
|
||
mockAPI.On("GetLicense").Return(&model.License{}, nil).Times(2) | ||
*cfg.EnableRinging = true | ||
err := p.setConfiguration(cfg.Clone()) | ||
require.NoError(t, err) | ||
require.True(t, *p.getConfiguration().EnableRinging) | ||
|
||
t.Run("not a call post", func(t *testing.T) { | ||
res, msg := p.NotificationWillBePushed(&model.PushNotification{}, "userID") | ||
require.Nil(t, res) | ||
require.Empty(t, msg) | ||
}) | ||
|
||
t.Run("call post but ringing disabled", func(t *testing.T) { | ||
*cfg.EnableRinging = false | ||
err := p.setConfiguration(cfg.Clone()) | ||
require.NoError(t, err) | ||
require.False(t, *p.getConfiguration().EnableRinging) | ||
|
||
res, msg := p.NotificationWillBePushed(&model.PushNotification{ | ||
PostType: callStartPostType, | ||
}, "userID") | ||
require.Nil(t, res) | ||
require.Empty(t, msg) | ||
}) | ||
|
||
t.Run("custom notification for DMs/GMs", func(t *testing.T) { | ||
*cfg.EnableRinging = true | ||
err := p.setConfiguration(cfg.Clone()) | ||
require.NoError(t, err) | ||
require.True(t, *p.getConfiguration().EnableRinging) | ||
|
||
res, msg := p.NotificationWillBePushed(&model.PushNotification{ | ||
PostType: callStartPostType, | ||
ChannelType: model.ChannelTypeDirect, | ||
}, "userID") | ||
require.Nil(t, res) | ||
require.Equal(t, "calls plugin will handle this notification", msg) | ||
|
||
res, msg = p.NotificationWillBePushed(&model.PushNotification{ | ||
PostType: callStartPostType, | ||
ChannelType: model.ChannelTypeGroup, | ||
}, "userID") | ||
require.Nil(t, res) | ||
require.Equal(t, "calls plugin will handle this notification", msg) | ||
}) | ||
|
||
t.Run("regular channel", func(t *testing.T) { | ||
mockAPI.On("GetUser", "receiverID").Return(&model.User{ | ||
FirstName: "Firstname", | ||
LastName: "Lastname", | ||
}, nil).Twice() | ||
|
||
var serverConfig model.Config | ||
serverConfig.SetDefaults() | ||
mockAPI.On("GetConfig").Return(&serverConfig).Once() | ||
|
||
mockAPI.On("GetPreferencesForUser", "receiverID").Return([]model.Preference{}, nil).Once() | ||
|
||
mockAPI.On("GetUser", "senderID").Return(&model.User{ | ||
FirstName: "Sender Firstname", | ||
LastName: "Sender Lastname", | ||
}, nil).Once() | ||
|
||
res, msg := p.NotificationWillBePushed(&model.PushNotification{ | ||
PostType: callStartPostType, | ||
ChannelType: model.ChannelTypeOpen, | ||
SenderId: "senderID", | ||
}, "receiverID") | ||
require.Equal(t, &model.PushNotification{ | ||
PostType: callStartPostType, | ||
ChannelType: model.ChannelTypeOpen, | ||
SenderId: "senderID", | ||
Message: "\u200bapp.push_notification.inviting_message", | ||
}, res) | ||
require.Empty(t, msg) | ||
|
||
t.Run("id loaded", func(t *testing.T) { | ||
res, msg := p.NotificationWillBePushed(&model.PushNotification{ | ||
PostType: callStartPostType, | ||
ChannelType: model.ChannelTypeOpen, | ||
SenderId: "senderID", | ||
IsIdLoaded: true, | ||
}, "receiverID") | ||
require.Equal(t, &model.PushNotification{ | ||
PostType: callStartPostType, | ||
ChannelType: model.ChannelTypeOpen, | ||
SenderId: "senderID", | ||
IsIdLoaded: true, | ||
Message: "app.push_notification.generic_message", | ||
}, res) | ||
require.Empty(t, msg) | ||
}) | ||
}) | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think of the performance impact of this approach? I guess we don't really have any other option, and it's not an event that will happen too often, but just curious.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cpoile I'd expect this to be extremely efficient, given that
callID
is an index and the sessions table is very small. Plus the query doesn't need to return any structured data. The only further optimization would be an index oncalls_sessions.userid
, but given the premise (small table + filter on call), it's not worth it since a scan will be the best option anyway. To put it in comparison, I think it could be more costly the RPC hook call itself than the DB query.