|
| 1 | +package jmap |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/opencloud-eu/opencloud/pkg/log" |
| 7 | +) |
| 8 | + |
| 9 | +var NS_ADDRESSBOOKS = ns(JmapContacts) |
| 10 | + |
| 11 | +type AddressBooksResponse struct { |
| 12 | + AddressBooks []AddressBook `json:"addressbooks"` |
| 13 | + NotFound []string `json:"notFound,omitempty"` |
| 14 | +} |
| 15 | + |
| 16 | +func (j *Client) GetAddressbooks(accountId string, session *Session, ctx context.Context, logger *log.Logger, acceptLanguage string, ids []string) (AddressBooksResponse, SessionState, State, Language, Error) { |
| 17 | + logger = j.logger("GetAddressbooks", session, logger) |
| 18 | + |
| 19 | + cmd, err := j.request(session, logger, NS_ADDRESSBOOKS, |
| 20 | + invocation(CommandAddressBookGet, AddressBookGetCommand{AccountId: accountId, Ids: ids}, "0"), |
| 21 | + ) |
| 22 | + if err != nil { |
| 23 | + return AddressBooksResponse{}, "", "", "", err |
| 24 | + } |
| 25 | + |
| 26 | + return command(j.api, logger, ctx, session, j.onSessionOutdated, cmd, acceptLanguage, func(body *Response) (AddressBooksResponse, State, Error) { |
| 27 | + var response AddressBookGetResponse |
| 28 | + err = retrieveResponseMatchParameters(logger, body, CommandAddressBookGet, "0", &response) |
| 29 | + if err != nil { |
| 30 | + return AddressBooksResponse{}, response.State, err |
| 31 | + } |
| 32 | + return AddressBooksResponse{ |
| 33 | + AddressBooks: response.List, |
| 34 | + NotFound: response.NotFound, |
| 35 | + }, response.State, nil |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +type AddressBookChanges struct { |
| 40 | + HasMoreChanges bool `json:"hasMoreChanges"` |
| 41 | + OldState State `json:"oldState,omitempty"` |
| 42 | + NewState State `json:"newState"` |
| 43 | + Created []AddressBook `json:"created,omitempty"` |
| 44 | + Updated []AddressBook `json:"updated,omitempty"` |
| 45 | + Destroyed []string `json:"destroyed,omitempty"` |
| 46 | +} |
| 47 | + |
| 48 | +// Retrieve Address Book changes since a given state. |
| 49 | +// @apidoc addressbook,changes |
| 50 | +func (j *Client) GetAddressbookChanges(accountId string, session *Session, ctx context.Context, logger *log.Logger, acceptLanguage string, sinceState State, maxChanges uint) (AddressBookChanges, SessionState, State, Language, Error) { |
| 51 | + return changesTemplate(j, "GetAddressbookChanges", NS_ADDRESSBOOKS, |
| 52 | + CommandAddressBookChanges, CommandAddressBookGet, |
| 53 | + func() AddressBookChangesCommand { |
| 54 | + return AddressBookChangesCommand{AccountId: accountId, SinceState: sinceState, MaxChanges: posUIntPtr(maxChanges)} |
| 55 | + }, |
| 56 | + func(path string, rof string) AddressBookGetRefCommand { |
| 57 | + return AddressBookGetRefCommand{ |
| 58 | + AccountId: accountId, |
| 59 | + IdsRef: &ResultReference{ |
| 60 | + Name: CommandAddressBookChanges, |
| 61 | + Path: path, |
| 62 | + ResultOf: rof, |
| 63 | + }, |
| 64 | + } |
| 65 | + }, |
| 66 | + func(resp AddressBookChangesResponse) (State, State, bool, []string) { |
| 67 | + return resp.OldState, resp.NewState, resp.HasMoreChanges, resp.Destroyed |
| 68 | + }, |
| 69 | + func(resp AddressBookGetResponse) []AddressBook { return resp.List }, |
| 70 | + func(oldState, newState State, hasMoreChanges bool, created, updated []AddressBook, destroyed []string) AddressBookChanges { |
| 71 | + return AddressBookChanges{ |
| 72 | + OldState: oldState, |
| 73 | + NewState: newState, |
| 74 | + HasMoreChanges: hasMoreChanges, |
| 75 | + Created: created, |
| 76 | + Updated: updated, |
| 77 | + Destroyed: destroyed, |
| 78 | + } |
| 79 | + }, |
| 80 | + func(resp AddressBookGetResponse) State { return resp.State }, |
| 81 | + session, ctx, logger, acceptLanguage, |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +func (j *Client) CreateAddressBook(accountId string, session *Session, ctx context.Context, logger *log.Logger, acceptLanguage string, create AddressBookChange) (*AddressBook, SessionState, State, Language, Error) { |
| 86 | + return createTemplate(j, "CreateAddressBook", NS_ADDRESSBOOKS, AddressBookType, CommandAddressBookSet, CommandAddressBookGet, |
| 87 | + func(accountId string, create map[string]AddressBookChange) AddressBookSetCommand { |
| 88 | + return AddressBookSetCommand{AccountId: accountId, Create: create} |
| 89 | + }, |
| 90 | + func(accountId string, ref string) AddressBookGetCommand { |
| 91 | + return AddressBookGetCommand{AccountId: accountId, Ids: []string{ref}} |
| 92 | + }, |
| 93 | + func(resp AddressBookSetResponse) map[string]*AddressBook { |
| 94 | + return resp.Created |
| 95 | + }, |
| 96 | + func(resp AddressBookSetResponse) map[string]SetError { |
| 97 | + return resp.NotCreated |
| 98 | + }, |
| 99 | + func(resp AddressBookGetResponse) []AddressBook { |
| 100 | + return resp.List |
| 101 | + }, |
| 102 | + func(resp AddressBookSetResponse) State { |
| 103 | + return resp.NewState |
| 104 | + }, |
| 105 | + accountId, session, ctx, logger, acceptLanguage, create, |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +func (j *Client) DeleteAddressBook(accountId string, destroy []string, session *Session, ctx context.Context, logger *log.Logger, acceptLanguage string) (map[string]SetError, SessionState, State, Language, Error) { |
| 110 | + return deleteTemplate(j, "DeleteAddressBook", NS_ADDRESSBOOKS, CommandAddressBookSet, |
| 111 | + func(accountId string, destroy []string) AddressBookSetCommand { |
| 112 | + return AddressBookSetCommand{AccountId: accountId, Destroy: destroy} |
| 113 | + }, |
| 114 | + func(resp AddressBookSetResponse) map[string]SetError { return resp.NotDestroyed }, |
| 115 | + func(resp AddressBookSetResponse) State { return resp.NewState }, |
| 116 | + accountId, destroy, session, ctx, logger, acceptLanguage, |
| 117 | + ) |
| 118 | +} |
0 commit comments