diff --git a/calendar/api/events.go b/calendar/api/events.go index 626154dc..ff5c443e 100644 --- a/calendar/api/events.go +++ b/calendar/api/events.go @@ -9,14 +9,13 @@ import ( "time" "github.com/mattermost/mattermost/server/public/model" + "github.com/pkg/errors" "github.com/mattermost/mattermost-plugin-mscalendar/calendar/engine/views" "github.com/mattermost/mattermost-plugin-mscalendar/calendar/remote" "github.com/mattermost/mattermost-plugin-mscalendar/calendar/store" "github.com/mattermost/mattermost-plugin-mscalendar/calendar/utils/bot" "github.com/mattermost/mattermost-plugin-mscalendar/calendar/utils/httputils" - - "github.com/pkg/errors" ) const ( diff --git a/calendar/command/command.go b/calendar/command/command.go index f117e713..f9a39394 100644 --- a/calendar/command/command.go +++ b/calendar/command/command.go @@ -117,21 +117,13 @@ func (c *Command) Handle() (string, bool, error) { handler = c.requireConnectedUser(c.dailySummary) case "viewcal": handler = c.requireConnectedUser(c.viewCalendar) - case "createcal": - handler = c.requireConnectedUser(c.createCalendar) - case "createevent": - handler = c.requireConnectedUser(c.createEvent) - case "deletecal": - handler = c.requireConnectedUser(c.deleteCalendar) - case "findmeetings": - handler = c.requireConnectedUser(c.findMeetings) - case "showcals": - handler = c.requireConnectedUser(c.showCalendars) case "settings": handler = c.requireConnectedUser(c.settings) case "events": handler = c.requireConnectedUser(c.event) // Admin only + case "showcals": + handler = c.requireConnectedUser(c.requireAdminUser(c.showCalendars)) case "avail": handler = c.requireConnectedUser(c.requireAdminUser(c.debugAvailability)) case "subscribe": diff --git a/calendar/command/create_calendar.go b/calendar/command/create_calendar.go deleted file mode 100644 index c3aa854a..00000000 --- a/calendar/command/create_calendar.go +++ /dev/null @@ -1,21 +0,0 @@ -package command - -import ( - "github.com/mattermost/mattermost-plugin-mscalendar/calendar/remote" -) - -func (c *Command) createCalendar(parameters ...string) (string, bool, error) { - if len(parameters) != 1 { - return "Please provide the name of one calendar to create", false, nil - } - - calIn := &remote.Calendar{ - Name: parameters[0], - } - - _, err := c.Engine.CreateCalendar(c.user(), calIn) - if err != nil { - return "", false, err - } - return "", false, nil -} diff --git a/calendar/command/create_event.go b/calendar/command/create_event.go deleted file mode 100644 index 211d0272..00000000 --- a/calendar/command/create_event.go +++ /dev/null @@ -1,180 +0,0 @@ -package command - -import ( - "fmt" - "strconv" - "strings" - "time" - - "github.com/pkg/errors" - flag "github.com/spf13/pflag" - - "github.com/mattermost/mattermost-plugin-mscalendar/calendar/remote" - "github.com/mattermost/mattermost-plugin-mscalendar/calendar/utils" -) - -func getCreateEventFlagSet() *flag.FlagSet { - flagSet := flag.NewFlagSet("create", flag.ContinueOnError) - flagSet.Bool("help", false, "show help") - flagSet.String("test-subject", "", "Subject of the event (no spaces for now)") - flagSet.String("test-body", "", "Body of the event (no spaces for now)") - flagSet.StringSlice("test-location", nil, "Location of the event (comma separated; no spaces)") - flagSet.String("starttime", time.Now().Format(time.RFC3339), "Start time for the event") - flagSet.Bool("allday", false, "Set as all day event (starttime/endtime must be set to midnight on different days - 2019-12-19T00:00:00-00:00)") - flagSet.Int("reminder", 15, "Reminder (in minutes)") - flagSet.String("endtime", time.Now().Add(time.Hour).Format(time.RFC3339), "End time for the event") - flagSet.StringSlice("attendees", nil, "A comma separated list of Mattermost UserIDs") - - return flagSet -} - -func (c *Command) createEvent(parameters ...string) (string, bool, error) { - if len(parameters) == 0 { - return getCreateEventFlagSet().FlagUsages(), false, nil - } - - tz, err := c.Engine.GetTimezone(c.user()) - if err != nil { - return "", false, nil - } - - event, err := parseCreateArgs(parameters, tz) - if err != nil { - return err.Error(), false, nil - } - - createFlagSet := getCreateEventFlagSet() - err = createFlagSet.Parse(parameters) - if err != nil { - return "", false, err - } - - mattermostUserIDs, err := createFlagSet.GetStringSlice("attendees") - if err != nil { - return "", false, err - } - - calEvent, err := c.Engine.CreateEvent(c.user(), event, mattermostUserIDs) - if err != nil { - return "", false, err - } - resp := "Event Created\n" + utils.JSONBlock(&calEvent) - - return resp, false, nil -} - -func parseCreateArgs(args []string, timeZone string) (*remote.Event, error) { - event := &remote.Event{} - - createFlagSet := getCreateEventFlagSet() - err := createFlagSet.Parse(args) - if err != nil { - return nil, err - } - - // check for required flags - requiredFlags := []string{"test-subject"} - flags := make(map[string]bool) - createFlagSet.Visit( - func(f *flag.Flag) { - flags[f.Name] = true - }) - for _, req := range requiredFlags { - if !flags[req] { - return nil, fmt.Errorf("missing required flag: `--%s` ", req) - } - } - - help, err := createFlagSet.GetBool("help") - if err != nil { - return nil, err - } - - if help { - return nil, errors.New(getCreateEventFlagSet().FlagUsages()) - } - - subject, err := createFlagSet.GetString("test-subject") - if err != nil { - return nil, err - } - // check that next arg is not a flag "--" - if strings.HasPrefix(subject, "--") { - return nil, errors.New("test-subject flag requires an argument") - } - event.Subject = subject - - body, err := createFlagSet.GetString("test-body") - if err != nil { - return nil, err - } - // check that next arg is not a flag "--" - if strings.HasPrefix(body, "--") { - return nil, errors.New("body flag requires an argument") - } - event.Body = &remote.ItemBody{ - Content: body, - } - - startTime, err := createFlagSet.GetString("starttime") - if err != nil { - return nil, err - } - if strings.HasPrefix(startTime, "--") { - return nil, errors.New("starttime flag requires an argument") - } - event.Start = &remote.DateTime{ - DateTime: startTime, - TimeZone: timeZone, - } - - endTime, err := createFlagSet.GetString("endtime") - if err != nil { - return nil, err - } - if strings.HasPrefix(endTime, "--") { - return nil, errors.New("endtime flag requires an argument") - } - event.End = &remote.DateTime{ - DateTime: endTime, - TimeZone: timeZone, - } - - allday, err := createFlagSet.GetBool("allday") - if err != nil { - return nil, err - } - event.IsAllDay = allday - - reminder, err := createFlagSet.GetInt("reminder") - if err != nil { - return nil, err - } - if strings.HasPrefix(strconv.Itoa(reminder), "--") { - return nil, errors.New("reminder flag requires an argument") - } - event.ReminderMinutesBeforeStart = reminder - - location, err := createFlagSet.GetStringSlice("test-location") - if err != nil { - return nil, err - } - if len(location) != 0 { - if len(location) != 6 { - return nil, errors.New("test-location flag requires 6 parameters, including a comma for empty values") - } - event.Location = &remote.Location{ - LocationType: "default", - DisplayName: location[0], - Address: &remote.Address{ - Street: location[1], - City: location[2], - State: location[3], - PostalCode: location[4], - CountryOrRegion: location[5], - }, - } - } - - return event, nil -} diff --git a/calendar/command/delete_calendar.go b/calendar/command/delete_calendar.go deleted file mode 100644 index 94f6ee3b..00000000 --- a/calendar/command/delete_calendar.go +++ /dev/null @@ -1,13 +0,0 @@ -package command - -func (c *Command) deleteCalendar(parameters ...string) (string, bool, error) { - if len(parameters) != 1 { - return "Please provide the ID of only one calendar ", false, nil - } - - err := c.Engine.DeleteCalendar(c.user(), parameters[0]) - if err != nil { - return "", false, err - } - return "", false, nil -} diff --git a/calendar/command/find_meeting_times.go b/calendar/command/find_meeting_times.go deleted file mode 100644 index abafd19b..00000000 --- a/calendar/command/find_meeting_times.go +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved. -// See License for license information. - -package command - -import ( - "fmt" - "strings" - - "github.com/mattermost/mattermost-plugin-mscalendar/calendar/remote" - "github.com/mattermost/mattermost-plugin-mscalendar/calendar/utils" -) - -func (c *Command) findMeetings(parameters ...string) (string, bool, error) { - meetingParams := &remote.FindMeetingTimesParameters{} - - var attendees []remote.Attendee - for _, parameter := range parameters { - s := strings.Split(parameter, ":") - if len(s) != 2 { - return "", false, fmt.Errorf("error in parameter %s", parameter) - } - t, email := s[0], s[1] - attendee := remote.Attendee{ - Type: t, - EmailAddress: &remote.EmailAddress{ - Address: email, - }, - } - attendees = append(attendees, attendee) - } - meetingParams.Attendees = attendees - - meetings, err := c.Engine.FindMeetingTimes(c.user(), meetingParams) - if err != nil { - return "", false, err - } - - timeZone, _ := c.Engine.GetTimezone(c.user()) - resp := "" - for _, m := range meetings.MeetingTimeSuggestions { - if timeZone != "" { - m.MeetingTimeSlot.Start = m.MeetingTimeSlot.Start.In(timeZone) - m.MeetingTimeSlot.End = m.MeetingTimeSlot.End.In(timeZone) - } - resp += utils.JSONBlock(renderMeetingTime(m)) - } - - return resp, false, nil -} - -func renderMeetingTime(m *remote.MeetingTimeSuggestion) string { - start := m.MeetingTimeSlot.Start.PrettyString() - end := m.MeetingTimeSlot.End.PrettyString() - return fmt.Sprintf("%s - %s (%s)", start, end, m.MeetingTimeSlot.Start.TimeZone) -} diff --git a/go.mod b/go.mod index cb91f555..f666af82 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/mattermost/mattermost/server/public v0.0.12 github.com/pkg/errors v0.9.1 github.com/rudderlabs/analytics-go v3.3.3+incompatible - github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.4 github.com/yaegashi/msgraph.go v0.0.0-20191104022859-3f9096c750b2 golang.org/x/oauth2 v0.15.0 diff --git a/go.sum b/go.sum index 3c8a3fcd..c187952c 100644 --- a/go.sum +++ b/go.sum @@ -180,8 +180,6 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sourcegraph/annotate v0.0.0-20160123013949-f4cad6c6324d/go.mod h1:UdhH50NIW0fCiwBSr0co2m7BnFLdv4fQTgdqdJTHFeE= github.com/sourcegraph/syntaxhighlight v0.0.0-20170531221838-bd320f5d308e/go.mod h1:HuIsMU8RRBOtsCgI77wP899iHVBQpCmg4ErYMZB+2IA= -github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= -github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0= github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=