-
Notifications
You must be signed in to change notification settings - Fork 0
/
property.go
141 lines (96 loc) · 2.87 KB
/
property.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package activitypub
import (
"context"
"fmt"
"time"
"github.com/sfomuseum/go-activitypub/id"
)
type Property struct {
Id int64 `json:"id"`
AccountId int64 `json:"account_id"`
Key string `json:"key"`
Value string `json:"value"`
Created int64 `json:"created"`
}
func (pr *Property) String() string {
return fmt.Sprintf("%s=%s", pr.Key, pr.Value)
}
func NewProperty(ctx context.Context, acct *Account, k string, v string) (*Property, error) {
prop_id, err := id.NewId()
if err != nil {
return nil, err
}
now := time.Now()
ts := now.Unix()
pr := &Property{
Id: prop_id,
AccountId: acct.Id,
Key: k,
Value: v,
Created: ts,
}
return pr, nil
}
func PropertiesMapForAccount(ctx context.Context, properties_db PropertiesDatabase, acct *Account) (map[string]*Property, error) {
props_map := make(map[string]*Property)
cb := func(ctx context.Context, pr *Property) error {
_, exists := props_map[pr.Key]
if exists {
return fmt.Errorf("Duplicate key for %s", pr.Key)
}
props_map[pr.Key] = pr
return nil
}
err := properties_db.GetPropertiesForAccount(ctx, acct.Id, cb)
if err != nil {
return nil, fmt.Errorf("Failed to derive properties for account, %w", err)
}
return props_map, nil
}
func DerivePropertiesUpdates(ctx context.Context, acct *Account, props_lookup map[string]*Property, updates map[string]string) ([]*Property, []*Property, error) {
to_add := make([]*Property, 0)
to_update := make([]*Property, 0)
for k, v := range updates {
pr, exists := props_lookup[k]
if exists {
if pr.Value != v {
pr.Value = v
to_update = append(to_update, pr)
}
} else {
pr, err := NewProperty(ctx, acct, k, v)
if err != nil {
return nil, nil, fmt.Errorf("Failed to create %s property for %d, %w", k, acct.Id, err)
}
to_add = append(to_add, pr)
}
}
return to_add, to_update, nil
}
func ApplyPropertiesUpdates(ctx context.Context, properties_db PropertiesDatabase, acct *Account, updates map[string]string) (int, int, error) {
props_lookup, err := PropertiesMapForAccount(ctx, properties_db, acct)
if err != nil {
return 0, 0, fmt.Errorf("Failed to derive properties map for %d, %w", acct.Id, err)
}
to_add, to_update, err := DerivePropertiesUpdates(ctx, acct, props_lookup, updates)
if err != nil {
return 0, 0, fmt.Errorf("Failed to derive properties updates for %d, %w", acct.Id, err)
}
added := 0
updated := 0
for _, pr := range to_add {
err := properties_db.AddProperty(ctx, pr)
if err != nil {
return added, updated, fmt.Errorf("Failed to add property (%s) for %d, %w", pr, acct.Id, err)
}
added += 1
}
for _, pr := range to_update {
err := properties_db.UpdateProperty(ctx, pr)
if err != nil {
return added, updated, fmt.Errorf("Failed to update property (%s) for %d, %w", pr, acct.Id, err)
}
updated += 1
}
return added, updated, nil
}