Skip to content

Commit

Permalink
[GH57] Receive notifications of upcoming events (#58)
Browse files Browse the repository at this point in the history
* Receive notifications of upcoming events

* Fix compile error

* Update error on subject empty, use user timezone on event reminder, simplify time operations, extract event rendering and extract statusSyncJobInterval to mscalendar

* Revert debug change

* Improve timezone fetching and error messages

* Simplify no subject message

* Change "No subject" for "Subject: unkwnonw"

Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
Co-authored-by: mattermod <mattermod@users.noreply.github.com>
  • Loading branch information
3 people authored Apr 16, 2020
1 parent 396a032 commit a4cd92a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
7 changes: 1 addition & 6 deletions server/jobs/status_sync_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@
package jobs

import (
"time"

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

// Unique id for the status sync job
const statusSyncJobID = "status_sync"

// Run status sync job every 5 minutes
const statusSyncJobInterval = 5 * time.Minute

// NewStatusSyncJob creates a RegisteredJob with the parameters specific to the StatusSyncJob
func NewStatusSyncJob() RegisteredJob {
return RegisteredJob{
id: statusSyncJobID,
interval: statusSyncJobInterval,
interval: mscalendar.StatusSyncJobInterval,
work: runStatusSyncJob,
isEnabledByConfig: isStatusSyncJobEnabled,
}
Expand Down
39 changes: 38 additions & 1 deletion server/mscalendar/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ import (
"fmt"
"time"

"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar/views"
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote"
"github.com/mattermost/mattermost-plugin-mscalendar/server/store"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils"
"github.com/pkg/errors"
)

const (
availabilityTimeWindowSize = 15
availabilityTimeWindowSize = 15
StatusSyncJobInterval = 5 * time.Minute
upcomingEventNotificationTime = 10 * time.Minute
upcomingEventNotificationWindow = (StatusSyncJobInterval * 9) / 10 //90% of the interval
)

type Availability interface {
Expand Down Expand Up @@ -105,6 +109,8 @@ func (m *mscalendar) setUserStatuses(users store.UserIndex, schedules []*remote.
}

mattermostUserID := usersByEmail[s.ScheduleID].MattermostUserID

m.notifyUpcomingEvent(mattermostUserID, s.ScheduleItems)
status, ok := statusMap[mattermostUserID]
if !ok {
continue
Expand All @@ -119,6 +125,37 @@ func (m *mscalendar) setUserStatuses(users store.UserIndex, schedules []*remote.
return utils.JSONBlock(schedules), nil
}

func (m *mscalendar) notifyUpcomingEvent(mattermostUserID string, items []remote.ScheduleItem) {
var timezone string
for _, scheduleItem := range items {
upcomingTime := time.Now().Add(upcomingEventNotificationTime)
start := scheduleItem.Start.Time()
diff := start.Sub(upcomingTime)

if (diff < upcomingEventNotificationWindow) && (diff > -upcomingEventNotificationWindow) {
var err error
if timezone == "" {
timezone, err = m.GetTimezoneByID(mattermostUserID)
if err != nil {
m.Logger.Errorf("notifyUpcomingEvent error getting timezone, err=%s", err.Error())
return
}
}

message, err := views.RenderScheduleItem(scheduleItem, timezone)
if err != nil {
m.Logger.Errorf("notifyUpcomingEvent error rendering schedule item, err=", err.Error())
continue
}
err = m.Poster.DM(mattermostUserID, message)
if err != nil {
m.Logger.Errorf("notifyUpcomingEvent error creating DM, err=", err.Error())
continue
}
}
}
}

func (m *mscalendar) GetAvailabilities(remoteUserID string, scheduleIDs []string) ([]*remote.ScheduleInformation, error) {
err := m.Filter(withClient)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions server/mscalendar/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func (m *mscalendar) GetTimezone(user *User) (string, error) {
return settings.TimeZone, nil
}

func (m *mscalendar) GetTimezoneByID(mattermostUserID string) (string, error) {
return m.GetTimezone(NewUser(mattermostUserID))
}

func (user *User) String() string {
if user.MattermostUser != nil {
return fmt.Sprintf("@%s", user.MattermostUser.Username)
Expand Down
19 changes: 19 additions & 0 deletions server/mscalendar/views/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,22 @@ func groupEventsByDate(events []*remote.Event) [][]*remote.Event {
}
return result
}

func RenderScheduleItem(s remote.ScheduleItem, timezone string) (string, error) {
message := "You have an upcoming event:"
start := s.Start.In(timezone).Time()
end := s.End.In(timezone).Time()

message += fmt.Sprintf("\n%s-%s (%s)", start.Format(time.Kitchen), end.Format(time.Kitchen), timezone)
if s.Subject == "" {
message += "\nSubject: unknown"
} else {
message += fmt.Sprintf("\nSubject: %s", s.Subject)
}

if s.Location != "" {
message += fmt.Sprintf("\nLocation: %s", s.Location)
}

return message, nil
}
18 changes: 17 additions & 1 deletion server/remote/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ const (
AvailabilityViewBusy = '2'
AvailabilityViewOutOfOffice = '3'
AvailabilityViewWorkingElsewhere = '4'

ScheduleStatusFree = "free"
ScheduleStatusTentative = "tentative"
ScheduleStatusBusy = "busy"
ScheduleStatusOof = "oof"
ScheduleStatusWorkingElsewhere = "workingElsewhere"
ScheduleStatusUnknown = "unknown"
)

type ScheduleInformationError struct {
Expand All @@ -29,7 +36,16 @@ type ScheduleInformation struct {

Error *ScheduleInformationError `json:"error"`

// ScheduleItems []interface{} `json:"scheduleItems,omitempty"`
ScheduleItems []ScheduleItem `json:"scheduleItems,omitempty"`
// WorkingHours interface{} `json:"workingHours,omitempty"`
// Error *FreeBusyError `json:"error,omitempty"`
}

type ScheduleItem struct {
IsPrivate bool
Status string
Subject string
Location string
Start DateTime
End DateTime
}

0 comments on commit a4cd92a

Please sign in to comment.