Skip to content
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

[MI-3832] Add websocket events for linking and unlinking channels #42

Merged
merged 10 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ func NewAPI(p *Plugin, store store.Store) *API {
router.HandleFunc("/connect", api.handleAuthRequired(api.connect)).Methods(http.MethodGet)
router.HandleFunc("/disconnect", api.handleAuthRequired(api.checkUserConnected(api.disconnect))).Methods(http.MethodGet)
router.HandleFunc("/linked-channels", api.handleAuthRequired(api.getLinkedChannels)).Methods(http.MethodGet)
router.HandleFunc("/link-channels", api.handleAuthRequired(api.checkUserConnected(api.linkChannels))).Methods(http.MethodPost)
router.HandleFunc("/oauth-redirect", api.oauthRedirectHandler).Methods(http.MethodGet)
router.HandleFunc("/connected-users", api.getConnectedUsers).Methods(http.MethodGet)
router.HandleFunc("/connected-users/download", api.getConnectedUsersFile).Methods(http.MethodGet)
Expand Down
26 changes: 22 additions & 4 deletions server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,33 @@ func (p *Plugin) ExecuteCommand(_ *plugin.Context, args *model.CommandArgs) (*mo
}

func (p *Plugin) executeLinkCommand(args *model.CommandArgs, parameters []string) (*model.CommandResponse, *model.AppError) {
if len(parameters) < 2 {
return p.cmdError(args.UserId, args.ChannelId, "Invalid link command, please pass the MS Teams team id and channel id as parameters.")
}

client, err := p.GetClientForUser(args.UserId)
if err != nil {
p.API.LogError("Unable to get the client for user", "MMUserID", args.UserId, "Error", err.Error())
return p.cmdError(args.UserId, args.ChannelId, "Unable to link the channel, looks like your account is not connected to MS Teams")
}

if len(parameters) < 2 {
p.API.PublishWebSocketEvent(
"link_channels",
nil,
&model.WebsocketBroadcast{UserId: args.UserId},
)

return &model.CommandResponse{}, nil
}

p.sendBotEphemeralPost(args.UserId, args.ChannelId, commandWaitingMessage)
if errMsg, _ := p.LinkChannels(args.UserId, args.TeamId, args.ChannelId, parameters[0], parameters[1], client); errMsg != "" {
return p.cmdError(args.UserId, args.ChannelId, errMsg)
}

p.API.PublishWebSocketEvent(
"link",
nil,
&model.WebsocketBroadcast{UserId: args.UserId},
)

p.sendBotEphemeralPost(args.UserId, args.ChannelId, "The MS Teams channel is now linked to this Mattermost channel.")
return &model.CommandResponse{}, nil
}
Expand All @@ -171,6 +183,12 @@ func (p *Plugin) executeUnlinkCommand(args *model.CommandArgs) (*model.CommandRe
return p.cmdError(args.UserId, args.ChannelId, errMsg)
}

p.API.PublishWebSocketEvent(
"unlink",
nil,
&model.WebsocketBroadcast{UserId: args.UserId},
)

p.sendBotEphemeralPost(args.UserId, args.ChannelId, "The MS Teams channel is no longer linked to this Mattermost channel.")

return &model.CommandResponse{}, nil
Expand Down
16 changes: 14 additions & 2 deletions server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestExecuteUnlinkCommand(t *testing.T) {
Type: model.ChannelTypeOpen,
}, nil).Times(1)
api.On("HasPermissionToChannel", testutils.GetUserID(), testutils.GetChannelID(), model.PermissionManageChannelRoles).Return(true).Times(1)
api.On("PublishWebSocketEvent", "unlink", (map[string]interface{})(nil), &model.WebsocketBroadcast{UserId: testutils.GetUserID()}).Return().Once()
api.On("SendEphemeralPost", testutils.GetUserID(), testutils.GetEphemeralPost("bot-user-id", testutils.GetChannelID(), "The MS Teams channel is no longer linked to this Mattermost channel.")).Return(testutils.GetPost(testutils.GetChannelID(), testutils.GetUserID(), time.Now().UnixMicro())).Times(1)
api.On("LogDebug", "Unable to delete the subscription on MS Teams", "subscriptionID", "testSubscriptionID", "error", "unable to delete the subscription").Return().Once()
},
Expand Down Expand Up @@ -162,6 +163,7 @@ func TestExecuteUnlinkCommand(t *testing.T) {
Type: model.ChannelTypeOpen,
}, nil).Times(1)
api.On("HasPermissionToChannel", testutils.GetUserID(), testutils.GetChannelID(), model.PermissionManageChannelRoles).Return(true).Times(1)
api.On("PublishWebSocketEvent", "unlink", (map[string]interface{})(nil), &model.WebsocketBroadcast{UserId: testutils.GetUserID()}).Return().Once()
api.On("SendEphemeralPost", testutils.GetUserID(), testutils.GetEphemeralPost("bot-user-id", testutils.GetChannelID(), "The MS Teams channel is no longer linked to this Mattermost channel.")).Return(testutils.GetPost(testutils.GetChannelID(), testutils.GetUserID(), time.Now().UnixMicro())).Times(1)
api.On("LogDebug", "Unable to get the subscription by MS Teams channel ID", "error", "unable to get the subscription").Return().Once()
},
Expand All @@ -186,6 +188,7 @@ func TestExecuteUnlinkCommand(t *testing.T) {
Type: model.ChannelTypeOpen,
}, nil).Times(1)
api.On("HasPermissionToChannel", testutils.GetUserID(), testutils.GetChannelID(), model.PermissionManageChannelRoles).Return(true).Times(1)
api.On("PublishWebSocketEvent", "unlink", (map[string]interface{})(nil), &model.WebsocketBroadcast{UserId: testutils.GetUserID()}).Return().Once()
api.On("SendEphemeralPost", testutils.GetUserID(), testutils.GetEphemeralPost("bot-user-id", testutils.GetChannelID(), "The MS Teams channel is no longer linked to this Mattermost channel.")).Return(testutils.GetPost(testutils.GetChannelID(), testutils.GetUserID(), time.Now().UnixMicro())).Times(1)
api.On("LogDebug", "Unable to delete the subscription from the DB", "subscriptionID", "testSubscriptionID", "error", "unable to delete the subscription").Return().Once()
},
Expand Down Expand Up @@ -417,7 +420,7 @@ func TestExecuteDisconnectCommand(t *testing.T) {
setupAPI: func(api *plugintest.API) {
api.On("SendEphemeralPost", testutils.GetUserID(), testutils.GetEphemeralPost("bot-user-id", "", "Your account has been disconnected.")).Return(testutils.GetPost("", testutils.GetUserID(), time.Now().UnixMicro())).Times(1)
api.On("LogDebug", "Unable to delete the last prompt timestamp for the user", "MMUserID", testutils.GetUserID(), "Error", "error in deleting prompt time").Once()
api.On("PublishWebSocketEvent", "disconnect", mock.AnythingOfType("map[string]interface {}"), &model.WebsocketBroadcast{UserId: testutils.GetUserID()}).Return().Once()
api.On("PublishWebSocketEvent", "disconnect", (map[string]interface{})(nil), &model.WebsocketBroadcast{UserId: testutils.GetUserID()}).Return().Once()
},
setupStore: func(s *mockStore.Store) {
s.On("MattermostToTeamsUserID", testutils.GetUserID()).Return(testutils.GetTeamsUserID(), nil).Times(1)
Expand Down Expand Up @@ -577,6 +580,7 @@ func TestExecuteLinkCommand(t *testing.T) {
}, nil).Times(2)
api.On("HasPermissionToChannel", testutils.GetUserID(), testutils.GetChannelID(), model.PermissionManageChannelRoles).Return(true).Times(1)
api.On("SendEphemeralPost", testutils.GetUserID(), testutils.GetEphemeralPost("bot-user-id", testutils.GetChannelID(), commandWaitingMessage)).Return(testutils.GetPost(testutils.GetChannelID(), testutils.GetUserID(), time.Now().UnixMicro())).Times(1)
api.On("PublishWebSocketEvent", "link", (map[string]interface{})(nil), &model.WebsocketBroadcast{UserId: testutils.GetUserID()}).Return().Once()
api.On("SendEphemeralPost", testutils.GetUserID(), testutils.GetEphemeralPost("bot-user-id", testutils.GetChannelID(), "The MS Teams channel is now linked to this Mattermost channel.")).Return(testutils.GetPost(testutils.GetChannelID(), testutils.GetUserID(), time.Now().UnixMicro())).Times(1)
},
setupStore: func(s *mockStore.Store) {
Expand Down Expand Up @@ -721,10 +725,18 @@ func TestExecuteLinkCommand(t *testing.T) {
api.On("GetChannel", testutils.GetChannelID()).Return(&model.Channel{
Type: model.ChannelTypeOpen,
}, nil).Times(1)
api.On("GetConfig").Return(&model.Config{
ServiceSettings: model.ServiceSettings{
SiteURL: model.NewString("/"),
},
}, nil).Times(1)
api.On("HasPermissionToChannel", testutils.GetUserID(), testutils.GetChannelID(), model.PermissionManageChannelRoles).Return(true).Times(1)
api.On("PublishWebSocketEvent", "link_channels", (map[string]interface{})(nil), &model.WebsocketBroadcast{UserId: testutils.GetUserID()}).Return().Once()
api.On("SendEphemeralPost", testutils.GetUserID(), testutils.GetEphemeralPost("bot-user-id", testutils.GetChannelID(), "Invalid link command, please pass the MS Teams team id and channel id as parameters.")).Return(testutils.GetPost(testutils.GetChannelID(), testutils.GetUserID(), time.Now().UnixMicro())).Times(1)
},
setupStore: func(s *mockStore.Store) {},
setupStore: func(s *mockStore.Store) {
s.On("GetTokenForMattermostUser", testutils.GetUserID()).Return(&oauth2.Token{}, nil).Times(1)
},
setupClient: func(c *mockClient.Client, uc *mockClient.Client) {},
setupMetrics: func(mockmetrics *mockMetrics.Metrics) {},
},
Expand Down
Loading
Loading