-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevent.go
85 lines (72 loc) · 2.33 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
package main
import (
"regexp"
"sort"
"strings"
"time"
)
type EventId string
type Event struct {
Id EventId `json:"-"`
Owner UserKey `json:"owner"`
IsMine bool `json:"-"`
Title string `json:"title" binding:"min=1,max=50"`
Start time.Time `json:"start"`
End time.Time `json:"end" binding:"gtfield=Start"`
Location EventLocation `json:"location" binding:"required"`
Capacity uint16 `json:"capacity" binding:"min=1,max=1000"`
Description string `json:"description" binding:"min=1,max=500"`
}
type EventLocation struct {
Name string `json:"name" binding:"min=1,max=50"`
Url string `json:"url" binding:"url,max=100"`
}
func (event *Event) isInPast() bool {
return event.End.Before(time.Now())
}
var paragraphSeparator = regexp.MustCompile("(\\s*\n){2,}")
func (event *Event) descriptionParagraphs() []string {
return paragraphSeparator.Split(event.Description, -1)
}
func sortEvents(events []*Event) []*Event {
sort.Slice(events, func(i, j int) bool {
eventI, eventJ := events[i], events[j]
if eventI.IsMine == eventJ.IsMine {
return eventI.Start.Before(eventJ.Start)
}
return eventI.IsMine
})
return events
}
func sortPastEvents(events []*Event) []*Event {
sort.Slice(events, func(i, j int) bool {
eventI, eventJ := events[i], events[j]
if eventI.IsMine == eventJ.IsMine {
return eventI.End.After(eventJ.End)
}
return eventI.IsMine
})
return events
}
func iCalendarEvent(event *Event, host string) string {
return "BEGIN:VCALENDAR\n" +
"VERSION:2.0\n" +
"PRODID:-//yanascz//NONSGML LNURL Daemon//EN\n" +
"BEGIN:VEVENT\n" +
"UID:event-" + string(event.Id) + "@" + host + "\n" +
"DTSTAMP:" + iCalendarDateTime(time.Now()) + "\n" +
"SUMMARY:" + iCalendarText(event.Title) + "\n" +
"DTSTART:" + iCalendarDateTime(event.Start) + "\n" +
"DTEND:" + iCalendarDateTime(event.End) + "\n" +
"LOCATION:" + iCalendarText(event.Location.Name) + "\n" +
"DESCRIPTION:" + iCalendarText(event.Description) + "\n" +
"END:VEVENT\n" +
"END:VCALENDAR\n"
}
var iCalendarEscaper = strings.NewReplacer(`\`, `\\`, "\n", `\n`, `;`, `\;`, `,`, `\,`)
func iCalendarText(text string) string {
return iCalendarEscaper.Replace(text)
}
func iCalendarDateTime(dateTime time.Time) string {
return dateTime.UTC().Format("20060102T150405Z07")
}