Skip to content

Commit

Permalink
Make use of multiple calendars in GCal module optional
Browse files Browse the repository at this point in the history
The change in PR #211 to make the GCal module use all calendars
writable to the user is not desirable in all situations. At my
organization, I have write access to calendars for conference
rooms, for people's OOO events, etc. that fill my screen,
obscuring the events on my own calendar.

This change puts the behavior in that change behind a new config
flag, `wtf.mods.gcal.multiCalendar`. It defaults to `false`, but
feel free to change that if you think this is behavior that most
users would want (I tend to default towards preserving existing
behavior, in this case from before that change).
  • Loading branch information
baustinanki committed Jun 19, 2018
1 parent 8a0ace3 commit 4c70cae
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions gcal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,7 @@ func Fetch() (*calendar.Events, error) {
return nil, err
}

// Get all user calendars with at the least writing access
pageToken := ""
var calendarIds []string
for {
calendarList, err := srv.CalendarList.List().ShowHidden(false).MinAccessRole("writer").PageToken(pageToken).Do()
for _, calendarListItem := range calendarList.Items {
calendarIds = append(calendarIds, calendarListItem.Id)
}

pageToken = calendarList.NextPageToken
if err != nil || pageToken == "" {
break
}
}
if err != nil {
return nil, err
}
calendarIds, err := getCalendarIdList(srv)

// Get calendar events
var events calendar.Events
Expand Down Expand Up @@ -179,3 +163,33 @@ func saveToken(file string, token *oauth2.Token) {

json.NewEncoder(f).Encode(token)
}

func getCalendarIdList(srv *calendar.Service) ([]string, error) {
// Return single calendar if settings specify we should
if !wtf.Config.UBool("wtf.mods.gcal.multiCalendar", false) {
id, err := srv.CalendarList.Get("primary").Do()
if err != nil {
return nil, err
}
return []string{id.Id}, nil
}

// Get all user calendars with at the least writing access
var calendarIds []string
var pageToken string
for {
calendarList, err := srv.CalendarList.List().ShowHidden(false).MinAccessRole("writer").PageToken(pageToken).Do()
if err != nil {
return nil, err
}
for _, calendarListItem := range calendarList.Items {
calendarIds = append(calendarIds, calendarListItem.Id)
}

pageToken = calendarList.NextPageToken
if pageToken == "" {
break
}
}
return calendarIds, nil
}

0 comments on commit 4c70cae

Please sign in to comment.