This repository has been archived by the owner on Oct 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
use delta queries for calendar events #2154
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,29 +144,25 @@ func (c Events) EnumerateContainers( | |
// item pager | ||
// --------------------------------------------------------------------------- | ||
|
||
type eventWrapper struct { | ||
models.EventCollectionResponseable | ||
} | ||
|
||
func (ew eventWrapper) GetOdataDeltaLink() *string { | ||
return nil | ||
} | ||
|
||
var _ itemPager = &eventPager{} | ||
|
||
const ( | ||
eventBetaDeltaURLTemplate = "https://graph.microsoft.com/beta/users/%s/calendars/%s/events/delta" | ||
) | ||
|
||
type eventPager struct { | ||
gs graph.Servicer | ||
builder *users.ItemCalendarsItemEventsRequestBuilder | ||
options *users.ItemCalendarsItemEventsRequestBuilderGetRequestConfiguration | ||
builder *users.ItemCalendarsItemEventsDeltaRequestBuilder | ||
options *users.ItemCalendarsItemEventsDeltaRequestBuilderGetRequestConfiguration | ||
} | ||
|
||
func (p *eventPager) getPage(ctx context.Context) (pageLinker, error) { | ||
resp, err := p.builder.Get(ctx, p.options) | ||
return eventWrapper{resp}, err | ||
return resp, err | ||
} | ||
|
||
func (p *eventPager) setNext(nextLink string) { | ||
p.builder = users.NewItemCalendarsItemEventsRequestBuilder(nextLink, p.gs.Adapter()) | ||
p.builder = users.NewItemCalendarsItemEventsDeltaRequestBuilder(nextLink, p.gs.Adapter()) | ||
} | ||
|
||
func (p *eventPager) valuesIn(pl pageLinker) ([]getIDAndAddtler, error) { | ||
|
@@ -182,23 +178,54 @@ func (c Events) GetAddedAndRemovedItemIDs( | |
return nil, nil, DeltaUpdate{}, err | ||
} | ||
|
||
var errs *multierror.Error | ||
var ( | ||
resetDelta bool | ||
errs *multierror.Error | ||
) | ||
|
||
options, err := optionsForEventsByCalendar([]string{"id"}) | ||
options, err := optionsForEventsByCalendarDelta([]string{"id"}) | ||
if err != nil { | ||
return nil, nil, DeltaUpdate{}, err | ||
} | ||
|
||
builder := service.Client().UsersById(user).CalendarsById(calendarID).Events() | ||
if len(oldDelta) > 0 { | ||
builder := users.NewItemCalendarsItemEventsDeltaRequestBuilder(oldDelta, service.Adapter()) | ||
pgr := &eventPager{service, builder, options} | ||
|
||
added, removed, deltaURL, err := getItemsAddedAndRemovedFromContainer(ctx, pgr) | ||
// note: happy path, not the error condition | ||
if err == nil { | ||
return added, removed, DeltaUpdate{deltaURL, false}, errs.ErrorOrNil() | ||
} | ||
// only return on error if it is NOT a delta issue. | ||
// on bad deltas we retry the call with the regular builder | ||
if graph.IsErrInvalidDelta(err) == nil { | ||
return nil, nil, DeltaUpdate{}, err | ||
} | ||
|
||
resetDelta = true | ||
errs = nil | ||
} | ||
|
||
// Graph SDK only supports delta queries against events on the beta version, so we're | ||
// manufacturing use of the beta version url to make the call instead. | ||
// See: https://learn.microsoft.com/ko-kr/graph/api/event-delta?view=graph-rest-beta&tabs=http | ||
// Note that the delta item body is skeletal compared to the actual event struct. Lucky | ||
// for us, we only need the item ID. As a result, even though we hacked the version, the | ||
// response body parses properly into the v1.0 structs and complies with our wanted interfaces. | ||
// Likewise, the NextLink and DeltaLink odata tags carry our hack forward, so the rest of the code | ||
// works as intended (until, at least, we want to _not_ call the beta anymore). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
rawURL := fmt.Sprintf(eventBetaDeltaURLTemplate, user, calendarID) | ||
builder := users.NewItemCalendarsItemEventsDeltaRequestBuilder(rawURL, service.Adapter()) | ||
pgr := &eventPager{service, builder, options} | ||
|
||
added, _, _, err := getItemsAddedAndRemovedFromContainer(ctx, pgr) | ||
added, removed, deltaURL, err := getItemsAddedAndRemovedFromContainer(ctx, pgr) | ||
if err != nil { | ||
return nil, nil, DeltaUpdate{}, err | ||
} | ||
|
||
// Events don't have a delta endpoint so just return an empty string. | ||
return added, nil, DeltaUpdate{}, errs.ErrorOrNil() | ||
return added, removed, DeltaUpdate{deltaURL, resetDelta}, errs.ErrorOrNil() | ||
} | ||
|
||
// --------------------------------------------------------------------------- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you check that items don't get skipped accidentally due to us selecting the bare minimum fields? Tbh, I'm not sure if this is a feature or a "bug" in how Graph works, but using
$select
can sometimes cause a delta endpoint to skip returning results if they don't match what's in the$select
. For example, not havingisRead
in the select for emails will cause emails where theisRead
flag has been changed to be skipped because it doesn't update the mod time. This will take some playing around and is probably easiest to do with GraphExplorer/postmanThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, good call out. I'll do some manual testing. This might be especially interesting in this case, because the delta body for an event are only able to contain the id, startTime, and endTime. Hopefully the delta will catch changes on unrepresented properties, like renames and etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, two findings.