@@ -41,40 +41,46 @@ func (c *mailboxClient) count(ctx context.Context, method string) (int, error) {
4141 return queryTotal (result )
4242}
4343
44- func (c * mailboxClient ) collectionState (ctx context.Context , collection string ) (string , error ) {
44+ type collectionChanges struct {
45+ state string
46+ liveChanges bool
47+ }
48+
49+ func (c * mailboxClient ) collectionChanges (ctx context.Context , collection string ) (collectionChanges , error ) {
4550 changes , err := c .call (ctx , collection + "/changes" , object {"sinceState" : "n" })
4651 if err != nil {
47- return "" , err
52+ return collectionChanges {} , err
4853 }
49- state := text (changes , "newState" )
50- if text (changes , "oldState" ) != "n" || state == "" || changes ["hasMoreChanges" ] != false {
51- return "" , fmt .Errorf ("census %s: incomplete change state" , collection )
54+ result := collectionChanges { state : text (changes , "newState" )}
55+ if text (changes , "oldState" ) != "n" || result . state == "" || changes ["hasMoreChanges" ] != false {
56+ return collectionChanges {} , fmt .Errorf ("census %s: incomplete change state" , collection )
5257 }
53- if state == "n" {
54- for _ , field := range []string {"created" , "updated" , "destroyed" } {
55- rows , ok := changes [field ].([]any )
56- if ! ok || len (rows ) != 0 {
57- return "" , fmt .Errorf ("census %s: ambiguous initial state" , collection )
58- }
58+ for _ , field := range []string {"created" , "updated" , "destroyed" } {
59+ rows , ok := changes [field ].([]any )
60+ if ! ok || (result .state == "n" && len (rows ) != 0 ) {
61+ return collectionChanges {}, fmt .Errorf ("census %s: inconsistent changes" , collection )
62+ }
63+ if field != "destroyed" && len (rows ) != 0 {
64+ result .liveChanges = true
5965 }
6066 }
61- return state , nil
67+ return result , nil
6268}
6369
6470// listAll avoids initializing an untouched collection. An initialized
6571// collection must retain its state while listed, so regenerated entries
6672// cannot pass as restored data.
6773func (c * mailboxClient ) listAll (ctx context.Context , collection string ) (int , error ) {
68- state , err := c .collectionState (ctx , collection )
69- if err != nil || state == "n" {
74+ changes , err := c .collectionChanges (ctx , collection )
75+ if err != nil || changes . state == "n" {
7076 return 0 , err
7177 }
7278 result , err := c .call (ctx , collection + "/get" , object {"ids" : nil , "properties" : []any {"id" }})
7379 if err != nil {
7480 return 0 , err
7581 }
7682 list , ok := result ["list" ].([]any )
77- if text (result , "state" ) != state {
83+ if text (result , "state" ) != changes . state {
7884 return 0 , fmt .Errorf ("census %s: collection changed during listing" , collection )
7985 }
8086 if ! ok || len (items (result , "list" )) != len (list ) || ! noneMissing (result ) {
@@ -175,8 +181,15 @@ func (c *mailboxClient) census(ctx context.Context) (storeCensus, error) {
175181 if s .events , s .tasks , err = c .calendarObjects (ctx ); err != nil {
176182 return s , err
177183 }
178- } else if state , err := c .collectionState (ctx , "CalendarEvent" ); err != nil || state != "n" {
179- return s , errors .New ("census-calendar-objects-without-calendar" )
184+ } else {
185+ // Calendar/get established that no calendar exists. A calendar event
186+ // query can create a default calendar when the server rebuilds its
187+ // cache. Use the owner's change feed here to reject surviving events
188+ // or tasks while allowing a collection whose last objects were deleted.
189+ changes , err := c .collectionChanges (ctx , "CalendarEvent" )
190+ if err != nil || changes .liveChanges {
191+ return s , errors .New ("census-calendar-objects-without-calendar" )
192+ }
180193 }
181194 if s .identities , err = c .listAll (ctx , "Identity" ); err != nil {
182195 return s , err
0 commit comments