Skip to content

Commit

Permalink
Improve notification looks (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
larkox authored May 5, 2020
1 parent 3aa0ee7 commit 6336c0e
Showing 1 changed file with 49 additions and 31 deletions.
80 changes: 49 additions & 31 deletions server/mscalendar/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package mscalendar
import (
"context"
"fmt"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -47,7 +48,14 @@ const (
ResponseNone = "notResponded"
)

var importantNotificationChanges []string = []string{FieldSubject, FieldDuration, FieldWhen}
var importantNotificationChanges []string = []string{FieldSubject, FieldWhen}

var notificationFieldOrder []string = []string{
FieldWhen,
FieldLocation,
FieldAttendees,
FieldImportance,
}

type NotificationProcessor interface {
Configure(Env)
Expand Down Expand Up @@ -222,16 +230,13 @@ func (processor *notificationProcessor) newEventSlackAttachment(n *remote.Notifi
sa := processor.newSlackAttachment(n)
sa.Title = "(new) " + sa.Title

for n, v := range eventToFields(n.Event) {
// skip some fields
switch n {
case FieldBodyPreview, FieldSubject, FieldOrganizer, FieldResponseStatus:
continue
}
fields := eventToFields(n.Event)
for _, k := range notificationFieldOrder {
v := fields[k]

sa.Fields = append(sa.Fields, &model.SlackAttachmentField{
Title: n,
Value: fmt.Sprintf("%s", v.Strings()),
Title: k,
Value: fmt.Sprintf("%s", strings.Join(v.Strings(), ", ")),
Short: true,
})
}
Expand Down Expand Up @@ -274,7 +279,7 @@ func (processor *notificationProcessor) updatedEventSlackAttachment(n *remote.No
}
sa.Fields = append(sa.Fields, &model.SlackAttachmentField{
Title: k,
Value: newFields[k].Strings(),
Value: strings.Join(newFields[k].Strings(), ", "),
Short: true,
})
}
Expand All @@ -284,7 +289,7 @@ func (processor *notificationProcessor) updatedEventSlackAttachment(n *remote.No
}
sa.Fields = append(sa.Fields, &model.SlackAttachmentField{
Title: k,
Value: fmt.Sprintf("~~%s~~ \u2192 %s", priorFields[k].Strings(), newFields[k].Strings()),
Value: fmt.Sprintf("~~%s~~ \u2192 %s", strings.Join(priorFields[k].Strings(), ", "), strings.Join(newFields[k].Strings(), ", ")),
Short: true,
})
}
Expand All @@ -294,7 +299,7 @@ func (processor *notificationProcessor) updatedEventSlackAttachment(n *remote.No
}
sa.Fields = append(sa.Fields, &model.SlackAttachmentField{
Title: k,
Value: fmt.Sprintf("~~%s~~", priorFields[k].Strings()),
Value: fmt.Sprintf("~~%s~~", strings.Join(priorFields[k].Strings(), ", ")),
Short: true,
})
}
Expand Down Expand Up @@ -384,21 +389,22 @@ func NewPostActionForEventResponse(eventID, response, url string) []*model.PostA
}

func eventToFields(e *remote.Event) fields.Fields {
date := func(dt *remote.DateTime) (time.Time, string) {
if dt == nil {
return time.Time{}, "n/a"
date := func(dtStart, dtEnd *remote.DateTime) (time.Time, time.Time, string) {
if dtStart == nil || dtEnd == nil {
return time.Time{}, time.Time{}, "n/a"
}
t := dt.Time()
format := "Monday, January 02"
if t.Year() != time.Now().Year() {
format = "Monday, January 02, 2006"
tStart := dtStart.Time()
tEnd := dtEnd.Time()
startFormat := "Monday, January 02"
if tStart.Year() != time.Now().Year() {
startFormat = "Monday, January 02, 2006"
}
format += " at " + time.Kitchen
return t, t.Format(format)
startFormat += " · (" + time.Kitchen
endFormat := " - " + time.Kitchen + ")"
return tStart, tEnd, tStart.Format(startFormat) + tEnd.Format(endFormat)
}

start, startDate := date(e.Start)
end, _ := date(e.End)
start, end, formattedDate := date(e.Start, e.End)

minutes := int(end.Sub(start).Round(time.Minute).Minutes())
hours := int(end.Sub(start).Hours())
Expand Down Expand Up @@ -434,23 +440,35 @@ func eventToFields(e *remote.Event) fields.Fields {
attendees := []fields.Value{}
for _, a := range e.Attendees {
attendees = append(attendees, fields.NewStringValue(
fmt.Sprintf("[%s](mailto:%s) (%s)",
a.EmailAddress.Name, a.EmailAddress.Address, a.Status.Response)))
fmt.Sprintf("[%s](mailto:%s)",
a.EmailAddress.Name, a.EmailAddress.Address)))
}

if len(attendees) == 0 {
attendees = append(attendees, fields.NewStringValue("None"))
}

ff := fields.Fields{
FieldSubject: fields.NewStringValue(e.Subject),
FieldBodyPreview: fields.NewStringValue(e.BodyPreview),
FieldImportance: fields.NewStringValue(e.Importance),
FieldWhen: fields.NewStringValue(startDate),
FieldDuration: fields.NewStringValue(dur),
FieldSubject: fields.NewStringValue(valueOrNotDefined(e.Subject)),
FieldBodyPreview: fields.NewStringValue(valueOrNotDefined(e.BodyPreview)),
FieldImportance: fields.NewStringValue(valueOrNotDefined(e.Importance)),
FieldWhen: fields.NewStringValue(valueOrNotDefined(formattedDate)),
FieldDuration: fields.NewStringValue(valueOrNotDefined(dur)),
FieldOrganizer: fields.NewStringValue(
fmt.Sprintf("[%s](mailto:%s)",
e.Organizer.EmailAddress.Name, e.Organizer.EmailAddress.Address)),
FieldLocation: fields.NewStringValue(e.Location.DisplayName),
FieldLocation: fields.NewStringValue(valueOrNotDefined(e.Location.DisplayName)),
FieldResponseStatus: fields.NewStringValue(e.ResponseStatus.Response),
FieldAttendees: fields.NewMultiValue(attendees...),
}

return ff
}

func valueOrNotDefined(s string) string {
if s == "" {
return "Not defined"
}

return s
}

0 comments on commit 6336c0e

Please sign in to comment.