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
7 changes: 7 additions & 0 deletions server/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ func (c *Command) Handle() (string, bool, error) {
handler = c.requireConnectedUser(c.requireAdminUser(c.debugAvailability))
case "settings":
handler = c.requireConnectedUser(c.settings)
// Aliases
case "today":
parameters = []string{"today"}
handler = c.requireConnectedUser(c.dailySummary)
case "tomorrow":
parameters = []string{"tomorrow"}
handler = c.requireConnectedUser(c.dailySummary)
}
out, mustRedirectToDM, err := handler(parameters...)
if err != nil {
Expand Down
14 changes: 10 additions & 4 deletions server/command/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package command

import (
"fmt"
"time"

"github.com/mattermost/mattermost-plugin-mscalendar/server/store"
)
Expand All @@ -21,12 +22,18 @@ func (c *Command) dailySummary(parameters ...string) (string, bool, error) {
}

switch parameters[0] {
case "view":
postStr, err := c.MSCalendar.GetDailySummaryForUser(c.user())
case "view", "today":
postStr, err := c.MSCalendar.GetDaySummaryForUser(time.Now(), c.user())
if err != nil {
return err.Error(), false, err
}
return postStr, false, nil
case "tomorrow":
_, err := c.MSCalendar.GetDaySummaryForUser(time.Now().Add(time.Hour*24), c.user())
if err != nil {
return err.Error(), false, err
}
return "", false, nil
case "time":
if len(parameters) != 2 {
return dailySummarySetTimeErrorMessage, false, nil
Expand Down Expand Up @@ -59,9 +66,8 @@ func (c *Command) dailySummary(parameters ...string) (string, bool, error) {
return err.Error(), false, err
}
return dailySummaryResponse(dsum), false, nil
default:
return "Invalid command. Please try again\n\n" + dailySummaryHelp, false, nil
}
return "Invalid command. Please try again\n\n" + dailySummaryHelp, false, nil
}

func dailySummaryResponse(dsum *store.DailySummaryUserSettings) string {
Expand Down
17 changes: 13 additions & 4 deletions server/mscalendar/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const dailySummaryTimeWindow = time.Minute * 2
const DailySummaryJobInterval = 15 * time.Minute

type DailySummary interface {
GetDailySummaryForUser(user *User) (string, error)
GetDaySummaryForUser(now time.Time, user *User) (string, error)
GetDailySummarySettingsForUser(user *User) (*store.DailySummaryUserSettings, error)
SetDailySummaryPostTime(user *User, timeStr string) (*store.DailySummaryUserSettings, error)
SetDailySummaryEnabled(user *User, enable bool) (*store.DailySummaryUserSettings, error)
Expand Down Expand Up @@ -174,18 +174,27 @@ func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {
return nil
}

func (m *mscalendar) GetDailySummaryForUser(user *User) (string, error) {
func (m *mscalendar) GetDaySummaryForUser(day time.Time, user *User) (string, error) {
tz, err := m.GetTimezone(user)
if err != nil {
return "", err
}

calendarData, err := m.getTodayCalendarEvents(user, time.Now(), tz)
calendarData, err := m.getTodayCalendarEvents(user, day, tz)
if err != nil {
return "Failed to get calendar events", err
}

return views.RenderCalendarView(calendarData, tz)
message, attachments, err := views.RenderDaySummary(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
}

func shouldPostDailySummary(dsum *store.DailySummaryUserSettings, now time.Time) (bool, error) {
Expand Down
28 changes: 14 additions & 14 deletions server/mscalendar/mock_mscalendar/mock_mscalendar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions server/mscalendar/views/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"sort"
"time"

"github.com/mattermost/mattermost-server/v6/model"

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

Expand Down Expand Up @@ -41,6 +43,45 @@ func RenderCalendarView(events []*remote.Event, timeZone string) (string, error)
return resp, nil
}

func RenderDaySummary(events []*remote.Event, timezone string) (string, []*model.SlackAttachment, error) {
if len(events) == 0 {
return "You have no events for that day", nil, nil
}

if timezone != "" {
for _, e := range events {
e.Start = e.Start.In(timezone)
e.End = e.End.In(timezone)
}
}

message := fmt.Sprintf("Agenda for %s.\nTimes are shown in %s", events[0].Start.Time().Format("Monday, 02 January"), events[0].Start.TimeZone)

var attachments []*model.SlackAttachment
for _, event := range events {
var actions []*model.PostAction

fields := []*model.SlackAttachmentField{}
if event.Location != nil && event.Location.DisplayName != "" {
fields = append(fields, &model.SlackAttachmentField{
Title: "Location",
Value: event.Location.DisplayName,
Short: true,
})
}

attachments = append(attachments, &model.SlackAttachment{
Title: event.Subject,
// Text: event.BodyPreview,
Text: fmt.Sprintf("(%s - %s)", event.Start.In(timezone).Time().Format(time.Kitchen), event.End.In(timezone).Time().Format(time.Kitchen)),
Fields: fields,
Actions: actions,
})
}

return message, attachments, nil
}

func renderTableHeader() string {
return `| Time | Subject |
| :--: | :-- |`
Expand Down
20 changes: 20 additions & 0 deletions server/utils/bot/mock_bot/mock_poster.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions server/utils/bot/poster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type Poster interface {
// Often used to include post actions.
DMWithAttachments(mattermostUserID string, attachments ...*model.SlackAttachment) (string, error)

// DMWithMessageAndAttachments posts a Direct Message that contains Slack attachments and a message.
DMWithMessageAndAttachments(mattermostUserID, message string, attachments ...*model.SlackAttachment) (string, error)

// Ephemeral sends an ephemeral message to a user
Ephemeral(mattermostUserID, channelID, format string, args ...interface{})

Expand Down Expand Up @@ -50,6 +53,13 @@ func (bot *bot) DMWithAttachments(mattermostUserID string, attachments ...*model
return bot.dm(mattermostUserID, &post)
}

// DMWithMessageAndAttachments posts a Direct Message that contains Slack attachments and a message.
func (bot *bot) DMWithMessageAndAttachments(mattermostUserID, message string, attachments ...*model.SlackAttachment) (string, error) {
post := model.Post{Message: message}
model.ParseSlackAttachment(&post, attachments)
return bot.dm(mattermostUserID, &post)
}

func (bot *bot) dm(mattermostUserID string, post *model.Post) (string, error) {
channel, err := bot.pluginAPI.GetDirectChannel(mattermostUserID, bot.mattermostUserID)
if err != nil {
Expand Down