forked from anujdecoder/ics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
119 lines (101 loc) · 2.52 KB
/
generator.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package ics
import (
"bytes"
"encoding/hex"
"strings"
"text/template"
"time"
)
func Generate(prodId string, events ...*Event) (string, error) {
obj := &generator{
ProdId: prodId,
Events: []string{},
}
eventTmpl, err := template.New("events").Parse(vevent)
if err != nil {
return "", err
}
for _, event := range events {
for idx := range event.Attendees {
if event.Attendees[idx].Rsvp == "" {
event.Attendees[idx].Rsvp = Rsvp_False
}
}
e := &vEvent{
Event: event,
DtStamp: FormatDateTime(time.Now()),
DtEnd: FormatDateTime(event.DtEnd),
DtStart: FormatDateTime(event.DtStart),
ExDate: make([]string, len(event.ExDate)),
Description: strings.Join(strings.Split(event.Description, "\n"), `\n`),
}
for i, exd := range event.ExDate {
e.ExDate[i] = FormatDateTime(exd)
}
event.dtStamp = e.DtStamp
event.UID = hex.EncodeToString([]byte(e.UID))
buf := &bytes.Buffer{}
if err := eventTmpl.Execute(buf, e); err != nil {
return "", err
}
obj.Events = append(obj.Events, buf.String())
}
buf := &bytes.Buffer{}
icsTmpl, err := template.New("ics").Parse(ics)
if err != nil {
return "", err
}
if err := icsTmpl.Execute(buf, obj); err != nil {
return "", err
}
return buf.String(), nil
}
func (event *Event) Generate(prodId string) (string, error) {
obj := &generator{
ProdId: prodId,
Events: []string{},
}
for idx := range event.Attendees {
if event.Attendees[idx].Rsvp == "" {
event.Attendees[idx].Rsvp = Rsvp_False
}
}
e := &vEvent{
Event: event,
DtStamp: FormatDateTime(time.Now()),
DtEnd: FormatDateTime(event.DtEnd),
DtStart: FormatDateTime(event.DtStart),
ExDate: make([]string, len(event.ExDate)),
Description: strings.Join(strings.Split(event.Description, "\n"), `\n`),
}
for i, exd := range event.ExDate {
e.ExDate[i] = FormatDateTime(exd)
}
event.dtStamp = e.DtStamp
event.UID = hex.EncodeToString([]byte(e.UID))
eventTmpl, err := template.New("events").Parse(vevent)
if err != nil {
return "", err
}
buf := &bytes.Buffer{}
if err := eventTmpl.Execute(buf, e); err != nil {
return "", err
}
obj.Events = append(obj.Events, buf.String())
buf = &bytes.Buffer{}
icsTmpl, err := template.New("ics").Parse(ics)
if err != nil {
return "", err
}
if err := icsTmpl.Execute(buf, obj); err != nil {
return "", err
}
return buf.String(), nil
}
type generator struct {
ProdId string
Events []string
}
func FormatDateTime(t time.Time) string {
return t.Format("20060102T150405")
}