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
63 changes: 44 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, 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, 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.availability)
case "settings":
handler = c.settings
handler = c.requireConnectedUser(c.settings)
}
out, err := handler(parameters...)
if err != nil {
Expand Down Expand Up @@ -119,3 +118,29 @@ 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, error) {
connected, err := c.isConnected()
if err != nil {
return "", err
}

if !connected {
return getNotConnectedText(), 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, 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, nil
}

func getCommandText(s string) string {
return fmt.Sprintf("/%s %s\n", config.CommandTrigger, s)
}
56 changes: 8 additions & 48 deletions server/command/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,17 @@ package command

import (
"fmt"

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

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

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

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

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

case len(parameters) == 1 && parameters[0] == "delete":
err := c.MSCalendar.DeleteMyEventSubscription()
if err != nil {
return "", err
}
return fmt.Sprintf("User's subscription deleted"), nil

case len(parameters) == 2 && parameters[0] == "delete":
err := c.MSCalendar.DeleteOrphanedSubscription(parameters[1])
if err != nil {
return "", err
}
return fmt.Sprintf("Subscription %s deleted", parameters[1]), nil
_, err := c.MSCalendar.LoadMyEventSubscription()
if err == nil {
return "Already subscribed to events.", nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return "Already subscribed to events.", nil
return "You are already subscribed to events.", nil

}

storedSub, err := c.MSCalendar.CreateMyEventSubscription()
if err != nil {
return "", err
}
return "bad syntax", nil
return fmt.Sprintf("Subscription %s created.", storedSub.Remote.ID), nil
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to say something more user-friendly like You are now subscribed to events.?

}
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, error) {
_, err := c.MSCalendar.LoadMyEventSubscription()
if err != nil {
return "You are not subscribed to events.", nil
}

err = c.MSCalendar.DeleteMyEventSubscription()
if err != nil {
return "", err
}
return fmt.Sprintf("User's subscription deleted"), nil
}