-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcard.go
126 lines (103 loc) · 2.64 KB
/
vcard.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"math/rand"
"strings"
"github.com/emersion/go-vcard"
"github.com/google/uuid"
"github.com/spejder/ms-vcard/internal/ms"
)
type group int
const (
ScoutName group = iota + 1
MemberNumber
)
func (g group) Group() string {
return fmt.Sprintf("item%d", g)
}
//nolint:funlen
func toCard(profile ms.MemberProfile, relations *ms.ResPartnerRelationAlls) vcard.Card {
card := vcard.Card{}
card.SetValue(vcard.FieldUID, getUUID(profile.PartnerId.ID))
//nolint:exhaustivestruct
card.AddName(&vcard.Name{
FamilyName: profile.Lastname.Get(),
GivenName: profile.Firstname.Get(),
})
if profile.MemberNumber != nil {
//nolint:exhaustivestruct
card.Add("NOTE", &vcard.Field{
Value: fmt.Sprintf("Medlemsnummer: %s", profile.MemberNumber.Get()),
Group: MemberNumber.Group(),
})
//nolint:exhaustivestruct
card.Add("X-ABLabel", &vcard.Field{
Value: "_$!<Medlemsnummer>!$_",
Group: MemberNumber.Group(),
})
}
if profile.Street != nil {
//nolint:exhaustivestruct
card.AddAddress(&vcard.Address{
StreetAddress: profile.Street.Get(),
Locality: profile.City.Get(),
PostalCode: profile.Zip.Get(),
})
}
if profile.ScoutName != nil {
//nolint:exhaustivestruct
card.Set("NICKNAME", &vcard.Field{
Value: profile.ScoutName.Get(),
Group: ScoutName.Group(),
})
//nolint:exhaustivestruct
card.Add("X-ABLabel", &vcard.Field{
Value: "_$!<Spejdernavn>!$_",
Group: ScoutName.Group(),
})
}
if profile.Birthdate != nil {
card.SetValue("BDAY", profile.Birthdate.Get().Format("20060102"))
}
if profile.OrganizationId != nil {
card.SetValue("ORG", profile.OrganizationId.Name)
}
if profile.Email != nil {
card.SetValue("EMAIL", profile.Email.Get())
}
if profile.Phone != nil {
home := vcard.Params{}
home.Set("TYPE", "home")
//nolint:exhaustivestruct
card.Set("TEL", &vcard.Field{
Value: profile.Phone.Get(),
Params: home,
})
}
if profile.MobileClean != nil {
cell := vcard.Params{}
cell.Set("TYPE", "cell")
//nolint:exhaustivestruct
card.Set("TEL", &vcard.Field{
Value: profile.MobileClean.Get(),
Params: cell,
})
}
for _, relation := range *relations {
related := strings.TrimSpace(strings.Replace(relation.DisplayName.Get(), profile.DisplayName.Get(), "", 1))
//nolint:exhaustivestruct
card.Add("NOTE", &vcard.Field{
Value: related,
})
}
vcard.ToV4(card)
return card
}
// getUUID creates a deterministic UUID from Odoos int64 profile id.
func getUUID(id int64) string {
//nolint:gosec // we don't use random for security
rnd := rand.New(rand.NewSource(id))
uuid.SetRand(rnd)
u, _ := uuid.NewRandomFromReader(rnd)
return u.URN()
}