Repository navigation
Expand file tree
/
Copy pathdefaults.go
More file actions
75 lines (68 loc) · 2.74 KB
/
Copy pathdefaults.go
File metadata and controls
75 lines (68 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// SPDX-FileCopyrightText: 2026 groupkit contributors
//
// SPDX-License-Identifier: Apache-2.0
package groupkit
import (
"context"
"errors"
"fmt"
"github.com/LukasHirt/groupkit/store"
)
// EnsureDefaultCollections gives owner an address book named "contacts"
// (display name "Contacts") and a calendar named "personal" (display name
// "Personal", for events), each only if the owner has none of that kind. It
// does nothing for a kind the owner already has, whatever the collection is
// called, so it is safe to call again and from several goroutines at once. An
// owner who deleted every address book or calendar gets the default back the
// next time the application calls it.
//
// It creates both kinds. A program that disables a protocol calls
// Server.EnsureDefaultCollections instead.
//
// Applications call it when they provision a user, so that clients find a
// collection to save contacts and events in. The server never calls it.
func EnsureDefaultCollections(ctx context.Context, st store.Store, owner string) error {
return ensureDefaults(ctx, st, owner, true, true)
}
// EnsureDefaultCollections is the package function of the same name for the
// protocols this server has enabled: it skips the address book when
// Config.DisableAddressBooks is set and the calendar when
// Config.DisableCalendars is set. The store is the one in the Config.
func (s *Server) EnsureDefaultCollections(ctx context.Context, owner string) error {
return ensureDefaults(ctx, s.store, owner, !s.disableBooks, !s.disableCalendars)
}
func ensureDefaults(ctx context.Context, st store.Store, owner string, books, calendars bool) error {
if st == nil {
return errors.New("groupkit: store is nil")
}
if owner == "" {
return errors.New("groupkit: owner is empty")
}
if books {
err := ensureCollection(ctx, st, owner, store.KindAddressBook, "contacts", store.Props{DisplayName: "Contacts"})
if err != nil {
return err
}
}
if calendars {
return ensureCollection(ctx, st, owner, store.KindCalendar, "personal",
store.Props{DisplayName: "Personal", Components: []string{"VEVENT"}})
}
return nil
}
// ensureCollection creates the named collection unless owner has one of the
// kind. Losing a race to another caller is not an error.
func ensureCollection(ctx context.Context, st store.Store, owner string, kind store.Kind, name string, props store.Props) error {
existing, err := st.ListCollections(ctx, owner, kind)
if err != nil {
return fmt.Errorf("groupkit: list %ss: %w", kind, err)
}
if len(existing) > 0 {
return nil
}
ref := store.CollectionRef{Owner: owner, Kind: kind, Name: name}
if _, err := st.CreateCollection(ctx, ref, props); err != nil && !errors.Is(err, store.ErrExists) {
return fmt.Errorf("groupkit: create %s: %w", kind, err)
}
return nil
}