From 6336c0e628dd4d96959c0087ef013df0856cb37a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Tue, 5 May 2020 18:05:28 +0200 Subject: [PATCH] Improve notification looks (#102) --- server/mscalendar/notification.go | 80 +++++++++++++++++++------------ 1 file changed, 49 insertions(+), 31 deletions(-) diff --git a/server/mscalendar/notification.go b/server/mscalendar/notification.go index 9386e63f..c22602f7 100644 --- a/server/mscalendar/notification.go +++ b/server/mscalendar/notification.go @@ -6,6 +6,7 @@ package mscalendar import ( "context" "fmt" + "strings" "time" "github.com/pkg/errors" @@ -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) @@ -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, }) } @@ -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, }) } @@ -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, }) } @@ -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, }) } @@ -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()) @@ -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 +}