From 3f1cbfcc5fdcd932e77f60d9728f550fc26ffd65 Mon Sep 17 00:00:00 2001 From: ABDUL SATTAR MAPARA <32198670+abdulsmapara@users.noreply.github.com> Date: Tue, 26 May 2020 17:01:01 +0530 Subject: [PATCH] Created functions for ExecuteCommand Switch statement, resolved redeclaration error and ensured idiomatic go (#5) --- server/command_hooks.go | 249 ++++++++++++++++++++++------------------ server/utils.go | 1 - 2 files changed, 137 insertions(+), 113 deletions(-) diff --git a/server/command_hooks.go b/server/command_hooks.go index 1b2a911..9716e82 100644 --- a/server/command_hooks.go +++ b/server/command_hooks.go @@ -48,7 +48,6 @@ func (p *Plugin) postCommandResponse(args *model.CommandArgs, text string) { } func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*model.CommandResponse, *model.AppError) { - userID := args.UserId split := strings.Fields(args.Command) command := split[0] action := "" @@ -71,137 +70,163 @@ func (p *Plugin) ExecuteCommand(c *plugin.Context, args *model.CommandArgs) (*mo return &model.CommandResponse{}, nil } } + messageToPost := "" + switch action { + case "list": + messageToPost = p.executeCommandList(args) + case "summary": + messageToPost = p.executeCommandSummary(args) + case "create": + messageToPost = p.executeCommandCreate(args) + case "help": + messageToPost = p.executeCommandHelp(args) + } - srv, err := p.getCalendarService(args.UserId) - if err != nil { - p.postCommandResponse(args, err.Error()) - return &model.CommandResponse{}, nil + if messageToPost != "" { + p.postCommandResponse(args, messageToPost) } + return &model.CommandResponse{}, nil +} + +func (p *Plugin) executeCommandList(args *model.CommandArgs) string { + maxResults := 5 + split := strings.Fields(args.Command) + userID := args.UserId location := p.getPrimaryCalendarLocation(userID) + srv, err := p.getCalendarService(userID) + if err != nil { + return err.Error() + } - switch action { - case "list": - maxResults := 5 - var err error - if len(split) == 3 { - maxResults, err = strconv.Atoi(split[2]) - } + if len(split) == 3 { + maxResults, err = strconv.Atoi(split[2]) + } - if err != nil { - p.postCommandResponse(args, "Incorrect Max Results parameter entered, will use default of 5") - maxResults = 5 - } + if err != nil { + p.postCommandResponse(args, "Incorrect Max Results parameter entered, will use default of 5") + maxResults = 5 + } - t := time.Now().Format(time.RFC3339) - events, err := srv.Events.List("primary").ShowDeleted(false). - SingleEvents(true).TimeMin(t).MaxResults(int64(maxResults)).OrderBy("startTime").Do() + t := time.Now().Format(time.RFC3339) + events, err := srv.Events.List("primary").ShowDeleted(false). + SingleEvents(true).TimeMin(t).MaxResults(int64(maxResults)).OrderBy("startTime").Do() - if err != nil { - p.postCommandResponse(args, fmt.Sprintf("Unable to retrieve next %v of the user's events: %v", maxResults, err)) - return &model.CommandResponse{}, nil - } + if err != nil { + return fmt.Sprintf("Unable to retrieve next %v of the user's events: %v", maxResults, err) + } - if len(events.Items) == 0 { - p.postCommandResponse(args, "No upcoming events") - return &model.CommandResponse{}, nil - } else { - text := "# Upcoming Events: \n" - var date string - var startTime time.Time - for _, item := range events.Items { - startTime, _ = time.Parse(time.RFC3339, item.Start.DateTime) - endTime, _ := time.Parse(time.RFC3339, item.End.DateTime) - if date != startTime.Format(dateFormat) { - date = startTime.Format(dateFormat) - - currentTime := time.Now().In(location).Format(dateFormat) - tomorrowTime := time.Now().AddDate(0, 0, 1).In(location).Format(dateFormat) - titleForEventsToDisplay := date - if date == currentTime { - titleForEventsToDisplay = fmt.Sprintf("Today (%s)", date) - } else if date == tomorrowTime { - titleForEventsToDisplay = fmt.Sprintf("Tomorrow (%s)", date) - } - text += fmt.Sprintf("### %v\n", titleForEventsToDisplay) - } - timeToDisplay := fmt.Sprintf("%v to %v", startTime.Format(timeFormat), endTime.Format(timeFormat)) - if startTime.Format(timeFormat) == "12:00 AM UTC" && endTime.Format(timeFormat) == "12:00 AM UTC" { - timeToDisplay = "All-day" - } - text += fmt.Sprintf("- [%v](%s) @ %s | [Delete Event](%s/plugins/calendar/delete?evtid=%s)\n", - item.Summary, item.HtmlLink, timeToDisplay, *config.ServiceSettings.SiteURL, item.Id) + if len(events.Items) == 0 { + return "No upcoming events" + } + text := "# Upcoming Events: \n" + var date string + var startTime time.Time + siteURL := *p.API.GetConfig().ServiceSettings.SiteURL + for _, item := range events.Items { + startTime, _ = time.Parse(time.RFC3339, item.Start.DateTime) + endTime, _ := time.Parse(time.RFC3339, item.End.DateTime) + if date != startTime.Format(dateFormat) { + date = startTime.Format(dateFormat) + + currentTime := time.Now().In(location).Format(dateFormat) + tomorrowTime := time.Now().AddDate(0, 0, 1).In(location).Format(dateFormat) + titleForEventsToDisplay := date + if date == currentTime { + titleForEventsToDisplay = fmt.Sprintf("Today (%s)", date) + } else if date == tomorrowTime { + titleForEventsToDisplay = fmt.Sprintf("Tomorrow (%s)", date) } - p.postCommandResponse(args, text) - return &model.CommandResponse{}, nil + text += fmt.Sprintf("### %v\n", titleForEventsToDisplay) } - case "summary": - date := time.Now().In(location) - dateToDisplay := "Today" - titleToDisplay := "Today's" - if len(split) == 3 { - if split[2] == "tomorrow" { - date = time.Now().AddDate(0, 0, 1).In(location) - dateToDisplay = "Tomorrow" - titleToDisplay = "Tomorrow's" - } else { - date, _ = time.ParseInLocation(customFormatNoTime, split[2], location) - dateToDisplay = date.Format(dateFormat) - titleToDisplay = dateToDisplay - } + timeToDisplay := fmt.Sprintf("%v to %v", startTime.Format(timeFormat), endTime.Format(timeFormat)) + if startTime.Format(timeFormat) == "12:00 AM UTC" && endTime.Format(timeFormat) == "12:00 AM UTC" { + timeToDisplay = "All-day" } - beginOfDay := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, location).Format(time.RFC3339) - endOfDay := time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, location).Format(time.RFC3339) + text += fmt.Sprintf("- [%v](%s) @ %s | [Delete Event](%s/plugins/calendar/delete?evtid=%s)\n", + item.Summary, item.HtmlLink, timeToDisplay, siteURL, item.Id) + } + return text +} - events, err := srv.Events.List("primary").ShowDeleted(false). - SingleEvents(true).TimeMin(beginOfDay).TimeMax(endOfDay).OrderBy("startTime").Do() +func (p *Plugin) executeCommandSummary(args *model.CommandArgs) string { + split := strings.Fields(args.Command) + userID := args.UserId + location := p.getPrimaryCalendarLocation(userID) + srv, err := p.getCalendarService(userID) + if err != nil { + return err.Error() + } - if err != nil { - p.postCommandResponse(args, "Error retrieiving events") - return &model.CommandResponse{}, nil + date := time.Now().In(location) + dateToDisplay := "Today" + titleToDisplay := "Today's" + if len(split) == 3 { + date, _ = time.ParseInLocation(customFormatNoTime, split[2], location) + dateToDisplay = date.Format(dateFormat) + titleToDisplay = dateToDisplay + if split[2] == "tomorrow" { + date = time.Now().AddDate(0, 0, 1).In(location) + dateToDisplay = "Tomorrow" + titleToDisplay = "Tomorrow's" } + } + beginOfDay := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, location).Format(time.RFC3339) + endOfDay := time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, location).Format(time.RFC3339) - if len(events.Items) == 0 { - p.CreateBotDMPost(userID, "It seems that you don't have any events happening.") - return &model.CommandResponse{}, nil - } else { - text := fmt.Sprintf("#### %s Schedule:\n", titleToDisplay) - for _, item := range events.Items { - text += p.printEventSummary(userID, item) - } - p.CreateBotDMPost(userID, text) - return &model.CommandResponse{}, nil - } - case "create": - r, _ := regexp.Compile("\"(.*?)\"") + events, err := srv.Events.List("primary").ShowDeleted(false). + SingleEvents(true).TimeMin(beginOfDay).TimeMax(endOfDay).OrderBy("startTime").Do() - matchedString := r.FindString(args.Command) - newCommand := strings.Replace(args.Command, matchedString, "", -1) - split = strings.Fields(newCommand) - matchedString = matchedString[1 : len(matchedString)-1] + if err != nil { + return "Error retrieiving events" + } - startTime, _ := time.ParseInLocation(customFormat, split[2], location) - endTime, _ := time.ParseInLocation(customFormat, split[3], location) + if len(events.Items) == 0 { + p.CreateBotDMPost(userID, "It seems that you don't have any events happening.") + return "" + } + text := fmt.Sprintf("#### %s Schedule:\n", titleToDisplay) + for _, item := range events.Items { + text += p.printEventSummary(userID, item) + } + p.CreateBotDMPost(userID, text) + return "" - newEvent := calendar.Event{ - Summary: matchedString, - Start: &calendar.EventDateTime{DateTime: startTime.Format(time.RFC3339)}, - End: &calendar.EventDateTime{DateTime: endTime.Format(time.RFC3339)}, - } - createdEvent, err := srv.Events.Insert("primary", &newEvent).Do() - if err != nil { - p.postCommandResponse(args, fmt.Sprintf("Failed to create calendar event. Error: %v", err)) - return &model.CommandResponse{}, nil - } - p.CreateBotDMPost(args.UserId, fmt.Sprintf("Success! Event _[%s](%s)_ on %v has been created.", - createdEvent.Summary, createdEvent.HtmlLink, startTime.Format(dateFormat))) - return &model.CommandResponse{}, nil +} - case "help": - text := "###### Mattermost Google Calendar Plugin - Slash Command Help\n" + strings.Replace(COMMAND_HELP, "|", "`", -1) - p.postCommandResponse(args, text) - return &model.CommandResponse{}, nil +func (p *Plugin) executeCommandCreate(args *model.CommandArgs) string { + split := strings.Fields(args.Command) + userID := args.UserId + location := p.getPrimaryCalendarLocation(userID) + srv, err := p.getCalendarService(userID) + if err != nil { + return err.Error() } - return &model.CommandResponse{}, nil + r, _ := regexp.Compile("\"(.*?)\"") + + matchedString := r.FindString(args.Command) + newCommand := strings.Replace(args.Command, matchedString, "", -1) + split = strings.Fields(newCommand) + matchedString = matchedString[1 : len(matchedString)-1] + + startTime, _ := time.ParseInLocation(customFormat, split[2], location) + endTime, _ := time.ParseInLocation(customFormat, split[3], location) + + newEvent := calendar.Event{ + Summary: matchedString, + Start: &calendar.EventDateTime{DateTime: startTime.Format(time.RFC3339)}, + End: &calendar.EventDateTime{DateTime: endTime.Format(time.RFC3339)}, + } + createdEvent, err := srv.Events.Insert("primary", &newEvent).Do() + if err != nil { + return fmt.Sprintf("Failed to create calendar event. Error: %v", err) + } + p.CreateBotDMPost(args.UserId, fmt.Sprintf("Success! Event _[%s](%s)_ on %v has been created.", + createdEvent.Summary, createdEvent.HtmlLink, startTime.Format(dateFormat))) + return "" +} + +func (p *Plugin) executeCommandHelp(args *model.CommandArgs) string { + return "###### Mattermost Google Calendar Plugin - Slash Command Help\n" + strings.Replace(COMMAND_HELP, "|", "`", -1) } diff --git a/server/utils.go b/server/utils.go index a3f65bb..069763f 100644 --- a/server/utils.go +++ b/server/utils.go @@ -370,7 +370,6 @@ func (p *Plugin) printEventSummary(userID string, item *calendar.Event) string { attendee := p.retrieveMyselfForEvent(item) if attendee != nil { if attendee.ResponseStatus == "needsAction" { - config := p.API.GetConfig() url := fmt.Sprintf("%s/plugins/calendar/handleresponse?evtid=%s&", *config.ServiceSettings.SiteURL, item.Id) text += fmt.Sprintf("**Going?**: [Yes](%s) | [No](%s) | [Maybe](%s)\n",