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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
package api
import (
"context"
"fmt"
"log"
"sort"
"strings"
"time"
"task-dashboard/internal/models"
"google.golang.org/api/calendar/v3"
"google.golang.org/api/option"
)
type GoogleCalendarClient struct {
srv *calendar.Service
calendarIDs []string
}
// NewGoogleCalendarClient creates a client that fetches from multiple calendars.
// calendarIDs can be comma-separated (e.g., "cal1@group.calendar.google.com,cal2@group.calendar.google.com")
func NewGoogleCalendarClient(ctx context.Context, credentialsFile, calendarIDs string) (*GoogleCalendarClient, error) {
srv, err := calendar.NewService(ctx, option.WithCredentialsFile(credentialsFile))
if err != nil {
return nil, fmt.Errorf("unable to retrieve Calendar client: %v", err)
}
// Parse comma-separated calendar IDs
ids := strings.Split(calendarIDs, ",")
var trimmedIDs []string
for _, id := range ids {
if trimmed := strings.TrimSpace(id); trimmed != "" {
trimmedIDs = append(trimmedIDs, trimmed)
}
}
return &GoogleCalendarClient{
srv: srv,
calendarIDs: trimmedIDs,
}, nil
}
func (c *GoogleCalendarClient) GetUpcomingEvents(ctx context.Context, maxResults int) ([]models.CalendarEvent, error) {
t := time.Now().Format(time.RFC3339)
var allEvents []models.CalendarEvent
for _, calendarID := range c.calendarIDs {
events, err := c.srv.Events.List(calendarID).ShowDeleted(false).
SingleEvents(true).TimeMin(t).MaxResults(int64(maxResults)).OrderBy("startTime").Do()
if err != nil {
log.Printf("Warning: failed to fetch events from calendar %s: %v", calendarID, err)
continue // Don't fail entirely, just skip this calendar
}
for _, item := range events.Items {
var start, end time.Time
if item.Start.DateTime == "" {
// All-day event - parse in local timezone
start, _ = time.ParseInLocation("2006-01-02", item.Start.Date, time.Local)
end, _ = time.ParseInLocation("2006-01-02", item.End.Date, time.Local)
} else {
// Timed event - parse RFC3339 then convert to local
start, _ = time.Parse(time.RFC3339, item.Start.DateTime)
end, _ = time.Parse(time.RFC3339, item.End.DateTime)
start = start.Local()
end = end.Local()
}
allEvents = append(allEvents, models.CalendarEvent{
ID: item.Id,
Summary: item.Summary,
Description: item.Description,
Start: start,
End: end,
HTMLLink: item.HtmlLink,
})
}
}
// Deduplicate events (same event may appear in multiple calendars)
// Use Unix timestamp to handle timezone differences
seen := make(map[string]bool)
var uniqueEvents []models.CalendarEvent
for _, event := range allEvents {
// Use summary + unix timestamp as dedup key (handles timezone differences)
key := fmt.Sprintf("%s|%d", event.Summary, event.Start.Unix())
if !seen[key] {
seen[key] = true
uniqueEvents = append(uniqueEvents, event)
}
}
// Sort all events by start time
sort.Slice(uniqueEvents, func(i, j int) bool {
return uniqueEvents[i].Start.Before(uniqueEvents[j].Start)
})
// Limit to maxResults
if len(uniqueEvents) > maxResults {
uniqueEvents = uniqueEvents[:maxResults]
}
return uniqueEvents, nil
}
func (c *GoogleCalendarClient) GetEventsByDateRange(ctx context.Context, start, end time.Time) ([]models.CalendarEvent, error) {
timeMin := start.Format(time.RFC3339)
timeMax := end.Format(time.RFC3339)
var allEvents []models.CalendarEvent
for _, calendarID := range c.calendarIDs {
events, err := c.srv.Events.List(calendarID).ShowDeleted(false).
SingleEvents(true).TimeMin(timeMin).TimeMax(timeMax).OrderBy("startTime").Do()
if err != nil {
log.Printf("Warning: failed to fetch events from calendar %s: %v", calendarID, err)
continue
}
for _, item := range events.Items {
var evtStart, evtEnd time.Time
if item.Start.DateTime == "" {
// All-day event - parse in local timezone
evtStart, _ = time.ParseInLocation("2006-01-02", item.Start.Date, time.Local)
evtEnd, _ = time.ParseInLocation("2006-01-02", item.End.Date, time.Local)
} else {
// Timed event - parse RFC3339 then convert to local
evtStart, _ = time.Parse(time.RFC3339, item.Start.DateTime)
evtEnd, _ = time.Parse(time.RFC3339, item.End.DateTime)
evtStart = evtStart.Local()
evtEnd = evtEnd.Local()
}
allEvents = append(allEvents, models.CalendarEvent{
ID: item.Id,
Summary: item.Summary,
Description: item.Description,
Start: evtStart,
End: evtEnd,
HTMLLink: item.HtmlLink,
})
}
}
// Deduplicate
seen := make(map[string]bool)
var uniqueEvents []models.CalendarEvent
for _, event := range allEvents {
key := fmt.Sprintf("%s|%d", event.Summary, event.Start.Unix())
if !seen[key] {
seen[key] = true
uniqueEvents = append(uniqueEvents, event)
}
}
sort.Slice(uniqueEvents, func(i, j int) bool {
return uniqueEvents[i].Start.Before(uniqueEvents[j].Start)
})
return uniqueEvents, nil
}
|