-
Notifications
You must be signed in to change notification settings - Fork 1
/
tag.go
221 lines (178 loc) · 4.54 KB
/
tag.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package vangogh_local_data
import (
"encoding/json"
"fmt"
"github.com/arelate/southern_light/gog_integration"
"github.com/boggydigital/nod"
"golang.org/x/exp/slices"
"net/http"
"net/url"
)
func postTagResp(httpClient *http.Client, url *url.URL, respVal interface{}) error {
resp, err := httpClient.Post(url.String(), "", nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("unexpected status: %s", resp.Status)
}
return json.NewDecoder(resp.Body).Decode(&respVal)
}
func TagIdByName(tagName string) (string, error) {
rxa, err := NewReduxReader(TagNameProperty)
if err != nil {
return "", err
}
tagIds := rxa.Match(map[string][]string{TagNameProperty: {tagName}})
if len(tagIds) == 0 {
return "", fmt.Errorf("unknown tag-name %s", tagName)
}
if len(tagIds) > 1 {
return "", fmt.Errorf("ambiguous tag-name %s, matching tag-ids: %v",
tagName,
tagIds)
}
tagId := ""
for _, ti := range tagIds {
tagId = ti
}
return tagId, nil
}
func CreateTag(httpClient *http.Client, tagName string) error {
rdx, err := NewReduxWriter(TagNameProperty)
if err != nil {
return err
}
createTagUrl := gog_integration.CreateTagUrl(tagName)
var ctResp gog_integration.CreateTagResp
if err := postTagResp(httpClient, createTagUrl, &ctResp); err != nil {
return err
}
if ctResp.Id == "" {
return fmt.Errorf("invalid create tag response")
}
if !rdx.HasValue(TagNameProperty, ctResp.Id, tagName) {
if err := rdx.AddValues(TagNameProperty, ctResp.Id, tagName); err != nil {
return err
}
}
return nil
}
func DeleteTag(httpClient *http.Client, tagName, tagId string) error {
rdx, err := NewReduxWriter(TagNameProperty)
if err != nil {
return err
}
deleteTagUrl := gog_integration.DeleteTagUrl(tagId)
var dtResp gog_integration.DeleteTagResp
if err := postTagResp(httpClient, deleteTagUrl, &dtResp); err != nil {
return err
}
if dtResp.Status != "deleted" {
return fmt.Errorf("invalid delete tag response")
}
if rdx.HasValue(TagNameProperty, tagId, tagName) {
if err := rdx.CutValues(TagNameProperty, tagId, tagName); err != nil {
return err
}
}
return nil
}
func AddTags(
httpClient *http.Client,
ids, tags []string,
tpw nod.TotalProgressWriter) error {
rxa, err := NewReduxWriter(TagIdProperty)
if err != nil {
return err
}
nod.TotalInt(tpw, len(ids)*len(tags))
for _, id := range ids {
for _, tag := range tags {
if rxa.HasValue(TagIdProperty, id, tag) {
nod.Increment(tpw)
continue
}
addTagUrl := gog_integration.AddTagUrl(id, tag)
var artResp gog_integration.AddRemoveTagResp
if err := postTagResp(httpClient, addTagUrl, &artResp); err != nil {
if tpw != nil {
tpw.Increment()
}
return err
}
if !artResp.Success {
nod.Increment(tpw)
return fmt.Errorf("failed to add tag %s", tag)
}
if err := rxa.AddValues(TagIdProperty, id, tag); err != nil {
nod.Increment(tpw)
return err
}
nod.Increment(tpw)
}
}
return nil
}
func RemoveTags(
httpClient *http.Client,
ids, tags []string,
tpw nod.TotalProgressWriter) error {
rxa, err := NewReduxWriter(TagIdProperty)
if err != nil {
return err
}
nod.TotalInt(tpw, len(ids)*len(tags))
for _, id := range ids {
for _, tag := range tags {
if !rxa.HasValue(TagIdProperty, id, tag) {
nod.Increment(tpw)
continue
}
removeTagUrl := gog_integration.RemoveTagUrl(id, tag)
var artResp gog_integration.AddRemoveTagResp
if err := postTagResp(httpClient, removeTagUrl, &artResp); err != nil {
nod.Increment(tpw)
return err
}
if !artResp.Success {
nod.Increment(tpw)
return fmt.Errorf("failed to remove tag %s", tag)
}
if err := rxa.CutValues(TagIdProperty, id, tag); err != nil {
nod.Increment(tpw)
return err
}
nod.Increment(tpw)
}
}
return nil
}
func diffTagProperty(
tagProperty string,
id string,
newTags []string) (add []string, rem []string, err error) {
add = make([]string, 0)
rem = make([]string, 0)
rxa, err := NewReduxReader(tagProperty)
if err != nil {
return add, rem, err
}
//we need empty slice to detect new values
currentVals, _ := rxa.GetAllValues(tagProperty, id)
for _, tag := range newTags {
if !slices.Contains(currentVals, tag) {
add = append(add, tag)
}
}
for _, tag := range currentVals {
if !slices.Contains(newTags, tag) {
rem = append(rem, tag)
}
}
return add, rem, nil
}
func DiffTags(id string, newTags []string) (add []string, rem []string, err error) {
return diffTagProperty(TagIdProperty, id, newTags)
}