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

Various improvements over commands #106

Merged
merged 8 commits into from
May 6, 2020
10 changes: 1 addition & 9 deletions server/command/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@

package command

func (c *Command) availability(parameters ...string) (string, bool, error) {
authorized, err := c.MSCalendar.IsAuthorizedAdmin(c.Args.UserId)
if err != nil {
return "", false, err
}
if !authorized {
return "Not authorized", false, nil
}

func (c *Command) debugAvailability(parameters ...string) (string, bool, error) {
switch {
case len(parameters) == 0:
resString, err := c.MSCalendar.Sync(c.Args.UserId)
Expand Down
77 changes: 58 additions & 19 deletions server/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils"
"github.com/mattermost/mattermost-plugin-mscalendar/server/store"
)

// Handler handles commands
Expand All @@ -26,18 +26,15 @@ type Command struct {
MSCalendar mscalendar.MSCalendar
}

func getHelp() string {
help := `
TODO: help text.
`
return utils.CodeBlock(fmt.Sprintf(
help,
))
func getNotConnectedText() string {
return fmt.Sprintf("It looks like your Mattermost account is not connected to a Microsoft account. Please connect your account using `/%s connect`.", config.CommandTrigger)
}

// RegisterFunc is a function that allows the runner to register commands with the mattermost server.
type RegisterFunc func(*model.Command) error

type handleFunc func(parameters ...string) (string, bool, error)

// Register should be called by the plugin to register all necessary commands
func Register(registerFunc RegisterFunc) {
_ = registerFunc(&model.Command{
Expand All @@ -64,27 +61,29 @@ func (c *Command) Handle() (string, bool, error) {
case "connect":
handler = c.connect
case "disconnect":
handler = c.disconnect
handler = c.requireConnectedUser(c.disconnect)
case "summary":
handler = c.dailySummary
handler = c.requireConnectedUser(c.dailySummary)
case "viewcal":
handler = c.viewCalendar
handler = c.requireConnectedUser(c.viewCalendar)
case "createcal":
handler = c.createCalendar
handler = c.requireConnectedUser(c.createCalendar)
case "createevent":
handler = c.createEvent
handler = c.requireConnectedUser(c.createEvent)
case "deletecal":
handler = c.deleteCalendar
handler = c.requireConnectedUser(c.deleteCalendar)
case "subscribe":
handler = c.subscribe
handler = c.requireConnectedUser(c.subscribe)
case "unsubscribe":
handler = c.requireConnectedUser(c.unsubscribe)
case "findmeetings":
handler = c.findMeetings
handler = c.requireConnectedUser(c.findMeetings)
case "showcals":
handler = c.showCalendars
handler = c.requireConnectedUser(c.showCalendars)
case "availability":
handler = c.availability
handler = c.requireConnectedUser(c.requireAdminUser(c.debugAvailability))
case "settings":
handler = c.settings
handler = c.requireConnectedUser(c.settings)
}
out, mustRedirectToDM, err := handler(parameters...)
if err != nil {
Expand Down Expand Up @@ -119,3 +118,43 @@ func (c *Command) isValid() (subcommand string, parameters []string, err error)
func (c *Command) user() *mscalendar.User {
return mscalendar.NewUser(c.Args.UserId)
}

func (c *Command) requireConnectedUser(handle handleFunc) handleFunc {
return func(parameters ...string) (string, bool, error) {
connected, err := c.isConnected()
if err != nil {
return "", false, err
}

if !connected {
return getNotConnectedText(), false, nil
}
return handle(parameters...)
}
}

func (c *Command) requireAdminUser(handle handleFunc) handleFunc {
return func(parameters ...string) (string, bool, error) {
authorized, err := c.MSCalendar.IsAuthorizedAdmin(c.Args.UserId)
if err != nil {
return "", false, err
}
if !authorized {
return "Not authorized", false, nil
}

return handle(parameters...)
}
}

func (c *Command) isConnected() (bool, error) {
_, err := c.MSCalendar.GetRemoteUser(c.Args.UserId)
if err == store.ErrNotFound {
return false, nil
}
if err != nil {
return false, err
}

return true, nil
}
24 changes: 24 additions & 0 deletions server/command/disconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar/mock_mscalendar"
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote"
"github.com/mattermost/mattermost-plugin-mscalendar/server/store"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin"
"github.com/pkg/errors"
Expand All @@ -21,11 +23,32 @@ func TestDisconnect(t *testing.T) {
expectedOutput string
expectedError string
}{
{
name: "user not connected",
command: "disconnect",
setup: func(m mscalendar.MSCalendar) {
mscal := m.(*mock_mscalendar.MockMSCalendar)
mscal.EXPECT().GetRemoteUser("user_id").Return(&remote.User{}, store.ErrNotFound).Times(1)
},
expectedOutput: getNotConnectedText(),
expectedError: "",
},
{
name: "error fetching user",
command: "disconnect",
setup: func(m mscalendar.MSCalendar) {
mscal := m.(*mock_mscalendar.MockMSCalendar)
mscal.EXPECT().GetRemoteUser("user_id").Return(&remote.User{}, errors.New("Some error")).Times(1)
},
expectedOutput: "",
expectedError: "Command /mscalendar disconnect failed: Some error",
},
{
name: "disconnect failed",
command: "disconnect",
setup: func(m mscalendar.MSCalendar) {
mscal := m.(*mock_mscalendar.MockMSCalendar)
mscal.EXPECT().GetRemoteUser("user_id").Return(&remote.User{}, nil).Times(1)
mscal.EXPECT().DisconnectUser("user_id").Return(errors.New("Some error")).Times(1)
},
expectedOutput: "",
Expand All @@ -36,6 +59,7 @@ func TestDisconnect(t *testing.T) {
command: "disconnect",
setup: func(m mscalendar.MSCalendar) {
mscal := m.(*mock_mscalendar.MockMSCalendar)
mscal.EXPECT().GetRemoteUser("user_id").Return(&remote.User{}, nil).Times(1)
mscal.EXPECT().DisconnectUser("user_id").Return(nil).Times(1)
mscal.EXPECT().ClearSettingsPosts("user_id").Return().Times(1)
},
Expand Down
27 changes: 16 additions & 11 deletions server/command/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@ func (c *Command) help(parameters ...string) (string, bool, error) {
c.Config.BuildHash,
c.Config.BuildDate)
resp += "\n"
resp += "* /mscalendar\n"
resp += "* /mscalendar help\n"
resp += "* /mscalendar info\n"
resp += "* /mscalendar connect\n"
resp += "* /mscalendar viewcal\n"
resp += "* /mscalendar showcals\n"
resp += "* /mscalendar subscribe\n"
resp += "* /mscalendar createcal <name>\n"
resp += "* /mscalendar deletecal <id>\n"
resp += "* /mscalendar createevent\n"
resp += "* /mscalendar findmeetings (Optional: <attendees>)\n"
resp += getCommandText("")
resp += getCommandText("help")
resp += getCommandText("info")
resp += getCommandText("connect")
resp += getCommandText("viewcal")
resp += getCommandText("showcals")
resp += getCommandText("subscribe")
resp += getCommandText("unsubscribe")
resp += getCommandText("createcal <name>")
resp += getCommandText("deletecal <id>")
resp += getCommandText("createevent")
resp += getCommandText("findmeetings (Optional: <attendees>)")
resp += " * <attendees> - space delimited <type>:<email> combinations \n"
resp += " * <type> options - required, optional \n"
return resp, false, nil
}

func getCommandText(s string) string {
return fmt.Sprintf("/%s %s\n", config.CommandTrigger, s)
}
62 changes: 18 additions & 44 deletions server/command/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,26 @@ import (
)

func (c *Command) subscribe(parameters ...string) (string, bool, error) {
switch {
case len(parameters) == 0:
storedSub, err := c.MSCalendar.CreateMyEventSubscription()
if err != nil {
return "", false, err
}
return fmt.Sprintf("Subscription %s created.", storedSub.Remote.ID), false, nil

case len(parameters) == 1 && parameters[0] == "list":
subs, err := c.MSCalendar.ListRemoteSubscriptions()
if err != nil {
return "", false, err
}
return fmt.Sprintf("Subscriptions:%s", utils.JSONBlock(subs)), false, nil

case len(parameters) == 1 && parameters[0] == "show":
storedSub, err := c.MSCalendar.LoadMyEventSubscription()
if err != nil {
return "", false, err
}
return fmt.Sprintf("Subscription:%s", utils.JSONBlock(storedSub)), false, nil

case len(parameters) == 1 && parameters[0] == "renew":
storedSub, err := c.MSCalendar.RenewMyEventSubscription()
if err != nil {
return "", false, err
}
if storedSub == nil {
return fmt.Sprintf("Not subscribed. Use `/mscalendar subscribe` to subscribe."), false, nil
}
return fmt.Sprintf("Subscription %s renewed until %s", storedSub.Remote.ID, storedSub.Remote.ExpirationDateTime), false, nil
if len(parameters) > 0 && parameters[0] == "list" {
return c.debugList()
}

case len(parameters) == 1 && parameters[0] == "delete":
err := c.MSCalendar.DeleteMyEventSubscription()
if err != nil {
return "", false, err
}
return fmt.Sprintf("User's subscription deleted"), false, nil
_, err := c.MSCalendar.LoadMyEventSubscription()
if err == nil {
return "You are already subscribed to events.", false, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If for some reason the renew job does not end up renewing the user's subscription (MM server is taken down for a few days for example), does this make it so the user cannot remedy the situation? The renew job would also not be able to fix it either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't checked the details, but shouldn't a "unsubscribe/subscribe" fix it?

}

case len(parameters) == 2 && parameters[0] == "delete":
err := c.MSCalendar.DeleteOrphanedSubscription(parameters[1])
if err != nil {
return "", false, err
}
return fmt.Sprintf("Subscription %s deleted", parameters[1]), false, nil
_, err = c.MSCalendar.CreateMyEventSubscription()
if err != nil {
return "", false, err
}
return "You are now subscribed to events.", false, nil
}

func (c *Command) debugList() (string, bool, error) {
subs, err := c.MSCalendar.ListRemoteSubscriptions()
if err != nil {
return "", false, err
}
return "bad syntax", false, nil
return fmt.Sprintf("Subscriptions:%s", utils.JSONBlock(subs)), false, nil
}
21 changes: 21 additions & 0 deletions server/command/unsubscribe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
// See License for license information.

package command

import (
"fmt"
)

func (c *Command) unsubscribe(parameters ...string) (string, bool, error) {
_, err := c.MSCalendar.LoadMyEventSubscription()
if err != nil {
return "You are not subscribed to events.", false, nil
}

err = c.MSCalendar.DeleteMyEventSubscription()
if err != nil {
return "", false, err
}
return fmt.Sprintf("You have unsubscribed from events."), false, nil
}