Skip to content

Commit

Permalink
Login workflow (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
larkox authored Apr 28, 2020
1 parent cfe0d0b commit 4fdc115
Show file tree
Hide file tree
Showing 44 changed files with 1,288 additions and 134 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ ifneq ($(HAS_SERVER),)
go install github.com/golang/mock/mockgen
mockgen -destination server/jobs/mock_cluster/mock_cluster.go github.com/mattermost/mattermost-plugin-api/cluster JobPluginAPI
mockgen -destination server/mscalendar/mock_mscalendar/mock_mscalendar.go github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar MSCalendar
mockgen -destination server/mscalendar/mock_welcomer/mock_welcomer.go -package mock_welcomer github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar Welcomer
mockgen -destination server/mscalendar/mock_plugin_api/mock_plugin_api.go -package mock_plugin_api github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar PluginAPI
mockgen -destination server/remote/mock_remote/mock_remote.go github.com/mattermost/mattermost-plugin-mscalendar/server/remote Remote
mockgen -destination server/remote/mock_remote/mock_client.go github.com/mattermost/mattermost-plugin-mscalendar/server/remote Client
Expand All @@ -112,6 +113,7 @@ clean_mock:
ifneq ($(HAS_SERVER),)
rm -rf ./server/jobs/mock_cluster
rm -rf ./server/mscalendar/mock_mscalendar
rm -rf ./server/mscalendar/mock_welcomer
rm -rf ./server/mscalendar/mock_plugin_api
rm -rf ./server/remote/mock_remote
rm -rf ./server/utils/bot/mock_bot
Expand Down
12 changes: 6 additions & 6 deletions server/command/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

package command

func (c *Command) availability(parameters ...string) (string, error) {
func (c *Command) availability(parameters ...string) (string, bool, error) {
switch {
case len(parameters) == 0:
resString, err := c.MSCalendar.SyncStatus(c.Args.UserId)
if err != nil {
return "", err
return "", false, err
}

return resString, nil
return resString, false, nil
case len(parameters) == 1 && parameters[0] == "all":
resString, err := c.MSCalendar.SyncStatusAll()
if err != nil {
return "", err
return "", false, err
}

return resString, nil
return resString, false, nil
}

return "bad syntax", nil
return "bad syntax", false, nil
}
10 changes: 5 additions & 5 deletions server/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ func Register(registerFunc RegisterFunc) {
}

// Handle should be called by the plugin when a command invocation is received from the Mattermost server.
func (c *Command) Handle() (string, error) {
func (c *Command) Handle() (string, bool, error) {
cmd, parameters, err := c.isValid()
if err != nil {
return "", err
return "", false, err
}

handler := c.help
Expand Down Expand Up @@ -86,12 +86,12 @@ func (c *Command) Handle() (string, error) {
case "settings":
handler = c.settings
}
out, err := handler(parameters...)
out, mustRedirectToDM, err := handler(parameters...)
if err != nil {
return out, errors.WithMessagef(err, "Command /%s %s failed", config.CommandTrigger, cmd)
return out, false, errors.WithMessagef(err, "Command /%s %s failed", config.CommandTrigger, cmd)
}

return out, nil
return out, mustRedirectToDM, nil
}

func (c *Command) isValid() (subcommand string, parameters []string, err error) {
Expand Down
23 changes: 17 additions & 6 deletions server/command/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,25 @@ import (
"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
)

func (c *Command) connect(parameters ...string) (string, error) {
const (
ConnectBotAlreadyConnectedTemplate = "The bot account is already connected to %s account `%s`. To connect to a different account, first run `/%s disconnect_bot`."
ConnectBotSuccessTemplate = "[Click here to link the bot's %s account.](%s/oauth2/connect_bot)"
ConnectAlreadyConnectedTemplate = "Your Mattermost account is already connected to %s account `%s`. To connect to a different account, first run `/%s disconnect`."
ConnectErrorMessage = "There has been a problem while trying to connect. err="
)

func (c *Command) connect(parameters ...string) (string, bool, error) {
ru, err := c.MSCalendar.GetRemoteUser(c.Args.UserId)
if err == nil {
return fmt.Sprintf("Your Mattermost account is already connected to %s account `%s`. To connect to a different account, first run `/%s disconnect`.", config.ApplicationName, ru.Mail, config.CommandTrigger), nil
return fmt.Sprintf(ConnectAlreadyConnectedTemplate, config.ApplicationName, ru.Mail, config.CommandTrigger), false, nil
}

out := "" //fmt.Sprintf(mscalendar.WelcomeMessage, c.Config.PluginURL)

err = c.MSCalendar.Welcome(c.Args.UserId)
if err != nil {
out = ConnectErrorMessage + err.Error()
}

out := fmt.Sprintf("[Click here to link your %s account.](%s/oauth2/connect)",
config.ApplicationName,
c.Config.PluginURL)
return out, nil
return out, true, nil
}
5 changes: 3 additions & 2 deletions server/command/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ func TestConnect(t *testing.T) {
setup: func(m mscalendar.MSCalendar) {
mscal := m.(*mock_mscalendar.MockMSCalendar)
mscal.EXPECT().GetRemoteUser("user_id").Return(nil, errors.New("remote user not found")).Times(1)
mscal.EXPECT().Welcome("user_id").Return(nil)
},
expectedOutput: "[Click here to link your Microsoft Calendar account.](http://localhost/oauth2/connect)",
expectedOutput: "",
expectedError: "",
},
}
Expand Down Expand Up @@ -70,7 +71,7 @@ func TestConnect(t *testing.T) {
tc.setup(mscal)
}

out, err := command.Handle()
out, _, err := command.Handle()
if tc.expectedOutput != "" {
require.Equal(t, tc.expectedOutput, out)
}
Expand Down
8 changes: 4 additions & 4 deletions server/command/create_calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote"
)

func (c *Command) createCalendar(parameters ...string) (string, error) {
func (c *Command) createCalendar(parameters ...string) (string, bool, error) {
if len(parameters) != 1 {
return "Please provide the name of one calendar to create", nil
return "Please provide the name of one calendar to create", false, nil
}

calIn := &remote.Calendar{
Expand All @@ -15,7 +15,7 @@ func (c *Command) createCalendar(parameters ...string) (string, error) {

_, err := c.MSCalendar.CreateCalendar(c.user(), calIn)
if err != nil {
return "", err
return "", false, err
}
return "", nil
return "", false, nil
}
16 changes: 8 additions & 8 deletions server/command/create_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,40 +27,40 @@ func getCreateEventFlagSet() *flag.FlagSet {
return flagSet
}

func (c *Command) createEvent(parameters ...string) (string, error) {
func (c *Command) createEvent(parameters ...string) (string, bool, error) {

if len(parameters) == 0 {
return fmt.Sprintf(getCreateEventFlagSet().FlagUsages()), nil
return fmt.Sprintf(getCreateEventFlagSet().FlagUsages()), false, nil
}

tz, err := c.MSCalendar.GetTimezone(c.user())
if err != nil {
return "", nil
return "", false, nil
}

event, err := parseCreateArgs(parameters, tz)
if err != nil {
return err.Error(), nil
return err.Error(), false, nil
}

createFlagSet := getCreateEventFlagSet()
err = createFlagSet.Parse(parameters)
if err != nil {
return "", err
return "", false, err
}

mattermostUserIDs, err := createFlagSet.GetStringSlice("attendees")
if err != nil {
return "", err
return "", false, err
}

calEvent, err := c.MSCalendar.CreateEvent(c.user(), event, mattermostUserIDs)
if err != nil {
return "", err
return "", false, err
}
resp := "Event Created\n" + utils.JSONBlock(&calEvent)

return resp, nil
return resp, false, nil
}

func parseCreateArgs(args []string, timeZone string) (*remote.Event, error) {
Expand Down
28 changes: 14 additions & 14 deletions server/command/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,52 @@ const dailySummaryHelp = "### Daily summary commands:\n" +

const dailySummarySetTimeErrorMessage = "Please enter a time, for example:\n`/mscalendar summary time 8:00AM`"

func (c *Command) dailySummary(parameters ...string) (string, error) {
func (c *Command) dailySummary(parameters ...string) (string, bool, error) {
if len(parameters) == 0 {
return dailySummaryHelp, nil
return dailySummaryHelp, false, nil
}

switch parameters[0] {
case "view":
postStr, err := c.MSCalendar.GetDailySummaryForUser(c.user())
if err != nil {
return err.Error(), err
return err.Error(), false, err
}
return postStr, nil
return postStr, false, nil
case "time":
if len(parameters) != 2 {
return dailySummarySetTimeErrorMessage, nil
return dailySummarySetTimeErrorMessage, false, nil
}
val := parameters[1]

dsum, err := c.MSCalendar.SetDailySummaryPostTime(c.user(), val)
if err != nil {
return err.Error() + "\n" + dailySummarySetTimeErrorMessage, nil
return err.Error() + "\n" + dailySummarySetTimeErrorMessage, false, nil
}

return dailySummaryResponse(dsum), nil
return dailySummaryResponse(dsum), false, nil
case "settings":
dsum, err := c.MSCalendar.GetDailySummarySettingsForUser(c.user())
if err != nil {
return err.Error() + "\nYou may need to configure your daily summary using the commands below.\n" + dailySummaryHelp, nil
return err.Error() + "\nYou may need to configure your daily summary using the commands below.\n" + dailySummaryHelp, false, nil
}

return dailySummaryResponse(dsum), nil
return dailySummaryResponse(dsum), false, nil
case "enable":
dsum, err := c.MSCalendar.SetDailySummaryEnabled(c.user(), true)
if err != nil {
return err.Error(), err
return err.Error(), false, err
}

return dailySummaryResponse(dsum), nil
return dailySummaryResponse(dsum), false, nil
case "disable":
dsum, err := c.MSCalendar.SetDailySummaryEnabled(c.user(), false)
if err != nil {
return err.Error(), err
return err.Error(), false, err
}
return dailySummaryResponse(dsum), nil
return dailySummaryResponse(dsum), false, nil
default:
return "Invalid command. Please try again\n\n" + dailySummaryHelp, nil
return "Invalid command. Please try again\n\n" + dailySummaryHelp, false, nil
}
}

Expand Down
8 changes: 4 additions & 4 deletions server/command/delete_calendar.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package command

func (c *Command) deleteCalendar(parameters ...string) (string, error) {
func (c *Command) deleteCalendar(parameters ...string) (string, bool, error) {
if len(parameters) != 1 {
return "Please provide the ID of only one calendar ", nil
return "Please provide the ID of only one calendar ", false, nil
}

err := c.MSCalendar.DeleteCalendar(c.user(), parameters[0])
if err != nil {
return "", err
return "", false, err
}
return "", nil
return "", false, nil
}
6 changes: 3 additions & 3 deletions server/command/disconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

package command

func (c *Command) disconnect(parameters ...string) (string, error) {
func (c *Command) disconnect(parameters ...string) (string, bool, error) {
err := c.MSCalendar.DisconnectUser(c.Args.UserId)
if err != nil {
return "", err
return "", false, err
}
c.MSCalendar.ClearSettingsPosts(c.Args.UserId)

return "Successfully disconnected your account", nil
return "Successfully disconnected your account", false, nil
}
2 changes: 1 addition & 1 deletion server/command/disconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestDisconnect(t *testing.T) {
tc.setup(mscal)
}

out, err := command.Handle()
out, _, err := command.Handle()
if tc.expectedOutput != "" {
require.Equal(t, tc.expectedOutput, out)
}
Expand Down
6 changes: 3 additions & 3 deletions server/command/find_meeting_times.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils"
)

func (c *Command) findMeetings(parameters ...string) (string, error) {
func (c *Command) findMeetings(parameters ...string) (string, bool, error) {
meetingParams := &remote.FindMeetingTimesParameters{}

var attendees []remote.Attendee
Expand All @@ -30,7 +30,7 @@ func (c *Command) findMeetings(parameters ...string) (string, error) {

meetings, err := c.MSCalendar.FindMeetingTimes(c.user(), meetingParams)
if err != nil {
return "", err
return "", false, err
}

timeZone, _ := c.MSCalendar.GetTimezone(c.user())
Expand All @@ -43,7 +43,7 @@ func (c *Command) findMeetings(parameters ...string) (string, error) {
resp += utils.JSONBlock(renderMeetingTime(m))
}

return resp, nil
return resp, false, nil
}

func renderMeetingTime(m *remote.MeetingTimeSuggestion) string {
Expand Down
6 changes: 3 additions & 3 deletions server/command/get_calendars.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils"
)

func (c *Command) showCalendars(parameters ...string) (string, error) {
func (c *Command) showCalendars(parameters ...string) (string, bool, error) {
resp, err := c.MSCalendar.GetCalendars(c.user())
if err != nil {
return "", err
return "", false, err
}
return utils.JSONBlock(resp), nil
return utils.JSONBlock(resp), false, nil
}
4 changes: 2 additions & 2 deletions server/command/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
)

func (c *Command) help(parameters ...string) (string, error) {
func (c *Command) help(parameters ...string) (string, bool, error) {
resp := fmt.Sprintf("Mattermost Microsoft Calendar plugin version: %s, "+
"[%s](/~https://github.com/mattermost/%s/commit/%s), built %s\n",
c.Config.PluginVersion,
Expand All @@ -31,5 +31,5 @@ func (c *Command) help(parameters ...string) (string, error) {
resp += "* /mscalendar findmeetings (Optional: <attendees>)\n"
resp += " * <attendees> - space delimited <type>:<email> combinations \n"
resp += " * <type> options - required, optional \n"
return resp, nil
return resp, false, nil
}
4 changes: 2 additions & 2 deletions server/command/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
)

func (c *Command) info(parameters ...string) (string, error) {
func (c *Command) info(parameters ...string) (string, bool, error) {
resp := fmt.Sprintf("Mattermost Microsoft Calendar plugin version: %s, "+
"[%s](/~https://github.com/mattermost/%s/commit/%s), built %s\n",
c.Config.PluginVersion,
c.Config.BuildHashShort,
config.Repository,
c.Config.BuildHash,
c.Config.BuildDate)
return resp, nil
return resp, false, nil
}
5 changes: 2 additions & 3 deletions server/command/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

package command

func (c *Command) settings(parameters ...string) (string, error) {
func (c *Command) settings(parameters ...string) (string, bool, error) {
c.MSCalendar.PrintSettings(c.Args.UserId)
out := "The bot will show you the settings."
return out, nil
return "", true, nil
}
Loading

0 comments on commit 4fdc115

Please sign in to comment.