forked from keybase/managed-bots
/
event.go
101 lines (87 loc) · 2.67 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package gcalbot
import (
"fmt"
"strings"
"time"
"google.golang.org/api/calendar/v3"
)
type EventStatus string
const (
EventStatusConfirmed EventStatus = "confirmed"
EventStatusTentative EventStatus = "tentative"
EventStatusCancelled EventStatus = "cancelled"
)
func FormatEvent(
event *calendar.Event,
calendarSummary string,
timezone *time.Location,
format24HourTime bool,
) (string, error) {
message := `%s
> What: *%s*
> When: %s%s%s%s
> Calendar: %s%s`
// strip protocol to skip unfurl prompt
url := strings.TrimPrefix(event.HtmlLink, "https://")
what := event.Summary
// TODO(marcel): better date formatting for recurring events
when, err := FormatTimeRange(event.Start, event.End, timezone, format24HourTime)
if err != nil {
return "", err
}
var where string
if event.Location != "" {
where = fmt.Sprintf("\n> Where: %s", event.Location)
}
var isOrganizer bool
if event.Attendees == nil {
isOrganizer = true
} else {
for _, attendee := range event.Attendees {
if attendee.Self && attendee.Organizer {
isOrganizer = true
}
}
}
var organizer string
// don't show organizer for self-organized event
if !isOrganizer {
if event.Organizer.DisplayName != "" && event.Organizer.Email != "" {
organizer = fmt.Sprintf("\n> Organizer: %s <%s>", event.Organizer.DisplayName, event.Organizer.Email)
} else if event.Organizer.DisplayName != "" {
organizer = fmt.Sprintf("\n> Organizer: %s", event.Organizer.DisplayName)
} else if event.Organizer.Email != "" {
organizer = fmt.Sprintf("\n> Organizer: %s", event.Organizer.Email)
}
}
var conferenceData string
if event.ConferenceData != nil {
for _, entryPoint := range event.ConferenceData.EntryPoints {
uri := strings.TrimPrefix(entryPoint.Uri, "https://")
switch entryPoint.EntryPointType {
case "video", "more":
conferenceData += fmt.Sprintf("\n> Join online: %s", uri)
case "phone":
conferenceData += fmt.Sprintf("\n> Join by phone: %s", entryPoint.Label)
if entryPoint.Pin != "" {
conferenceData += fmt.Sprintf(" PIN: %s", entryPoint.Pin)
}
case "sip":
conferenceData += fmt.Sprintf("\n> Join by SIP: %s", entryPoint.Label)
}
}
}
// note: description can contain HTML
var description string
if event.Description != "" {
// quote all newlines
if strings.Contains(event.Description, "\n") {
descriptionBody := strings.ReplaceAll(event.Description, "\n", "\n> > ")
description = fmt.Sprintf("\n> Description:\n> > %s", descriptionBody)
} else {
description = fmt.Sprintf("\n> Description: %s", event.Description)
}
}
return fmt.Sprintf(message,
url, what, when, where, conferenceData, organizer, calendarSummary, description), nil
}