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

[GCAL] today/tomorrow commands with styling #273

Merged
merged 11 commits into from
Aug 1, 2023
2 changes: 2 additions & 0 deletions server/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ var cmds = []*model.AutocompleteData{
model.NewAutocompleteData("disconnect", "", fmt.Sprintf("Disconnect from your %s account", config.Provider.DisplayName)),
model.NewAutocompleteData("summary", "", "View your events for today, or edit the settings for your daily summary."),
model.NewAutocompleteData("viewcal", "", "View your events for the upcoming week."),
model.NewAutocompleteData("today", "", "Display today's events."),
model.NewAutocompleteData("tomorrow", "", "Display tomorrow's events."),
model.NewAutocompleteData("settings", "", "Edit your user personal settings."),
model.NewAutocompleteData("subscribe", "", "Enable notifications for event invitations and updates."),
model.NewAutocompleteData("unsubscribe", "", "Disable notifications for event invitations and updates."),
Expand Down
4 changes: 2 additions & 2 deletions server/command/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func (c *Command) dailySummary(parameters ...string) (string, bool, error) {
}
return postStr, false, nil
case "tomorrow":
_, err := c.MSCalendar.GetDaySummaryForUser(time.Now().Add(time.Hour*24), c.user())
postStr, err := c.MSCalendar.GetDaySummaryForUser(time.Now().Add((time.Hour*24)*4), c.user())
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we add (time.Hour*24)*4 here? Also do we need to take into account weekends (maybe that's the intention here)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh no no no, I accidentaly commited that. Used to get the output of a specific day for a screenshot.

Copy link
Contributor

Choose a reason for hiding this comment

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

Though this begs the question, should we have a special case for weekends? Currently, the daily summary job avoids sending the summary on the weekend, so there is already some convention in the plugin to avoid weekends. Maybe the tomorrow command should do something similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Didn't check that code, is it configurable? Does it take into consideration countries were weekends are longer/shorter/on different days?

Copy link
Contributor

Choose a reason for hiding this comment

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

Here is the line that does this

if now.Weekday() == time.Saturday || now.Weekday() == time.Sunday {

It's currently not configurable, but we can use WorkingHours to figure out the correct days if available for gcal:

type WorkingHours struct {
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
TimeZone struct {
Name string `json:"name"`
}
DaysOfWeek []string `json:"daysOfWeek"`
}
type MailboxSettings struct {
TimeZone string `json:"timeZone"`
WorkingHours WorkingHours `json:"workingHours"`
}

if err != nil {
return err.Error(), false, err
}
return "", false, nil
return postStr, false, nil
case "time":
if len(parameters) != 2 {
return dailySummarySetTimeErrorMessage, false, nil
Expand Down
8 changes: 2 additions & 6 deletions server/mscalendar/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,12 @@ func (m *mscalendar) GetDaySummaryForUser(day time.Time, user *User) (string, er
return "Failed to get calendar events", err
}

message, attachments, err := views.RenderDaySummary(calendarData, tz)
messageString, err := views.RenderCalendarView(calendarData, tz)
if err != nil {
return "", errors.Wrap(err, "failed to render daily summary")
}

if _, err := m.Poster.DMWithMessageAndAttachments(user.MattermostUserID, message, attachments...); err != nil {
return "", errors.Wrap(err, "failed to send message to user")
}

return "", nil
return messageString, nil
}

func shouldPostDailySummary(dsum *store.DailySummaryUserSettings, now time.Time) (bool, error) {
Expand Down
18 changes: 14 additions & 4 deletions server/mscalendar/views/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"sort"
"strings"
"time"

"github.com/mattermost/mattermost-server/v6/model"
Expand Down Expand Up @@ -83,8 +84,8 @@ func RenderDaySummary(events []*remote.Event, timezone string) (string, []*model
}

func renderTableHeader() string {
return `| Time | Subject |
| :--: | :-- |`
return `| Time | Subject | |
| :-- | :-- | :--`
}

func renderEvent(event *remote.Event, asRow bool, timeZone string) (string, error) {
Expand All @@ -93,17 +94,26 @@ func renderEvent(event *remote.Event, asRow bool, timeZone string) (string, erro

format := "(%s - %s) [%s](%s)"
if asRow {
format = "| %s - %s | [%s](%s) |"
format = "| %s - %s | [%s](%s) | %s |"
}

link, err := url.QueryUnescape(event.Weblink)
if err != nil {
return "", err
}

var other string
if event.Location != nil && isKnownMeetingURL(event.Location.DisplayName) {
other = "[Join meeting](" + event.Location.DisplayName + ")"
}

subject := EnsureSubject(event.Subject)

return fmt.Sprintf(format, start, end, subject, link), nil
return fmt.Sprintf(format, start, end, subject, link, other), nil
}

func isKnownMeetingURL(location string) bool {
return strings.Contains(location, "zoom.us/j/") || strings.Contains(location, "discord.gg") || strings.Contains(location, "meet.google.com")
Copy link
Contributor

Choose a reason for hiding this comment

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

Neat 👍 Something comes to mind, that a customer may have other call-related domains, including Mattermost Calls. Maybe have this configurable in some way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I want to integrate calls too, but need to send the SITE_URL over to check the URL to do it properly, so it needs a bit more work. Also, Google sends conferenceData for some events instead of location (I'm assuming they have a specific integration), so it wont work every time.

}

func groupEventsByDate(events []*remote.Event) [][]*remote.Event {
Expand Down
14 changes: 7 additions & 7 deletions server/remote/gcal/get_default_calendar_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,23 @@ func (c *client) GetDefaultCalendarView(_ string, start, end time.Time) ([]*remo
req.SingleEvents(true)
req.OrderBy("startTime")

events, err := req.Do()
result, err := req.Do()
if err != nil {
return nil, errors.Wrap(err, "gcal GetDefaultCalendarView, error performing request")
}

result := []*remote.Event{}
if len(events.Items) == 0 {
return result, nil
if len(result.Items) == 0 {
return []*remote.Event{}, nil
}

for _, event := range events.Items {
events := []*remote.Event{}
for _, event := range result.Items {
if event.ICalUID != "" {
result = append(result, convertGCalEventToRemoteEvent(event))
events = append(events, convertGCalEventToRemoteEvent(event))
}
}

return result, nil
return events, nil
}

func convertGCalEventDateTimeToRemoteDateTime(dt *calendar.EventDateTime) *remote.DateTime {
Expand Down
Loading