From e3ec66ed0f01d83536d8453893a35ffa192edf86 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 4 Sep 2017 11:46:14 +0200 Subject: [PATCH] carddav: paging support --- carddav/carddav.go | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/carddav/carddav.go b/carddav/carddav.go index 0a3fe44..49d033a 100644 --- a/carddav/carddav.go +++ b/carddav/carddav.go @@ -64,19 +64,29 @@ func (ab *addressBook) ListAddressObjects() ([]carddav.AddressObject, error) { return aos, nil } - // TODO: paging support - total, contacts, err := ab.c.ListContactsExport(0, 0) - if err != nil { - return nil, err - } + var aos []carddav.AddressObject + page := 0 + for { + total, contacts, err := ab.c.ListContactsExport(page, 0) + if err != nil { + return nil, err + } + ab.total = total - ab.total = total + if aos == nil { + aos = make([]carddav.AddressObject, 0, total) + } - aos := make([]carddav.AddressObject, len(contacts)) - for i, contact := range contacts { - ao := &addressObject{c: ab.c, contact: contact} - ab.cache[contact.ID] = ao - aos[i] = ao + for _, contact := range contacts { + ao := &addressObject{c: ab.c, contact: contact} + ab.cache[contact.ID] = ao + aos = append(aos, ao) + } + + if len(aos) == total || len(contacts) == 0 { + break + } + page++ } return aos, nil