forked from vavallee/bindery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathabs_conflicts.go
More file actions
254 lines (242 loc) · 7.86 KB
/
Copy pathabs_conflicts.go
File metadata and controls
254 lines (242 loc) · 7.86 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package api
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
"github.com/go-chi/chi/v5"
"github.com/vavallee/bindery/internal/abs"
"github.com/vavallee/bindery/internal/db"
"github.com/vavallee/bindery/internal/models"
)
type ABSConflictHandler struct {
conflicts *db.ABSMetadataConflictRepo
authors *db.AuthorRepo
books *db.BookRepo
}
type absConflictResponse struct {
ID int64 `json:"id"`
SourceID string `json:"sourceId"`
LibraryID string `json:"libraryId"`
ItemID string `json:"itemId"`
EntityType string `json:"entityType"`
LocalID int64 `json:"localId"`
EntityName string `json:"entityName"`
FieldName string `json:"fieldName"`
FieldLabel string `json:"fieldLabel"`
ABSValue string `json:"absValue"`
UpstreamValue string `json:"upstreamValue"`
AppliedSource string `json:"appliedSource"`
AppliedValue string `json:"appliedValue"`
PreferredSource string `json:"preferredSource"`
AuthorRelinkEligible bool `json:"authorRelinkEligible"`
ResolutionStatus string `json:"resolutionStatus"`
UpdatedAt string `json:"updatedAt"`
}
type absConflictListResponse struct {
Items []absConflictResponse `json:"items"`
Total int `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
func NewABSConflictHandler(conflicts *db.ABSMetadataConflictRepo, authors *db.AuthorRepo, books *db.BookRepo) *ABSConflictHandler {
return &ABSConflictHandler{conflicts: conflicts, authors: authors, books: books}
}
func (h *ABSConflictHandler) List(w http.ResponseWriter, r *http.Request) {
limit, offset := parseLimitOffset(r, 50, 100)
if h.conflicts == nil {
writeJSON(w, http.StatusOK, absConflictListResponse{
Items: []absConflictResponse{},
Total: 0,
Limit: limit,
Offset: offset,
})
return
}
conflicts, total, err := h.conflicts.ListPaginated(r.Context(), limit, offset)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
out := make([]absConflictResponse, 0, len(conflicts))
for _, conflict := range conflicts {
item, err := h.decorateConflict(r, &conflict)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
out = append(out, item)
}
writeJSON(w, http.StatusOK, absConflictListResponse{
Items: out,
Total: total,
Limit: limit,
Offset: offset,
})
}
func (h *ABSConflictHandler) Resolve(w http.ResponseWriter, r *http.Request) {
if h.conflicts == nil {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "abs conflict store not configured"})
return
}
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
if err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid id"})
return
}
conflict, err := h.conflicts.GetByID(r.Context(), id)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
if conflict == nil {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "conflict not found"})
return
}
var req struct {
Source string `json:"source"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid request body"})
return
}
source := strings.TrimSpace(req.Source)
switch source {
case abs.MetadataSourceABS, abs.MetadataSourceUpstream:
default:
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "source must be 'abs' or 'upstream'"})
return
}
// Atomically claim the conflict to prevent a TOCTOU race where two
// concurrent Resolve calls both read "pending", apply different entity
// values, and both write "resolved" — leaving the entity and status record
// disagreeing on which source won.
claimed, err := h.conflicts.Claim(r.Context(), id)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
if !claimed {
// Re-read and return the current state so the client can see the
// winning resolution without treating it as an error.
current, err := h.conflicts.GetByID(r.Context(), id)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
if current == nil {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "conflict not found"})
return
}
item, err := h.decorateConflict(r, current)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
writeJSON(w, http.StatusOK, item)
return
}
if err := h.applyConflictChoice(r, conflict, source); err != nil {
_ = h.conflicts.Unclaim(r.Context(), id)
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
conflict.AppliedSource = source
conflict.PreferredSource = source
conflict.ResolutionStatus = "resolved"
if err := h.conflicts.Upsert(r.Context(), conflict); err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
item, err := h.decorateConflict(r, conflict)
if err != nil {
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
return
}
writeJSON(w, http.StatusOK, item)
}
func (h *ABSConflictHandler) applyConflictChoice(r *http.Request, conflict *models.ABSMetadataConflict, source string) error {
value := conflict.UpstreamValue
if source == abs.MetadataSourceABS {
value = conflict.ABSValue
}
now := time.Now().UTC()
switch conflict.EntityType {
case "author":
author, err := h.authors.GetByID(r.Context(), conflict.LocalID)
if err != nil {
return err
}
if author == nil {
return nil
}
if err := abs.ApplyAuthorConflictValue(author, conflict.FieldName, value); err != nil {
return err
}
author.LastMetadataRefreshAt = &now
return h.authors.Update(r.Context(), author)
case "book":
book, err := h.books.GetByID(r.Context(), conflict.LocalID)
if err != nil {
return err
}
if book == nil {
return nil
}
if err := abs.ApplyBookConflictValue(book, conflict.FieldName, value); err != nil {
return err
}
book.LastMetadataRefreshAt = &now
return h.books.Update(r.Context(), book)
default:
return nil
}
}
func (h *ABSConflictHandler) decorateConflict(r *http.Request, conflict *models.ABSMetadataConflict) (absConflictResponse, error) {
resp := absConflictResponse{
ID: conflict.ID,
SourceID: conflict.SourceID,
LibraryID: conflict.LibraryID,
ItemID: conflict.ItemID,
EntityType: conflict.EntityType,
LocalID: conflict.LocalID,
FieldName: conflict.FieldName,
FieldLabel: abs.ConflictFieldLabel(conflict.FieldName),
ABSValue: conflict.ABSValue,
UpstreamValue: conflict.UpstreamValue,
AppliedSource: conflict.AppliedSource,
PreferredSource: conflict.PreferredSource,
ResolutionStatus: conflict.ResolutionStatus,
UpdatedAt: conflict.UpdatedAt.Format(time.RFC3339),
}
switch conflict.AppliedSource {
case abs.MetadataSourceABS:
resp.AppliedValue = conflict.ABSValue
case abs.MetadataSourceUpstream:
resp.AppliedValue = conflict.UpstreamValue
}
switch conflict.EntityType {
case "author":
author, err := h.authors.GetByID(r.Context(), conflict.LocalID)
if err != nil {
return absConflictResponse{}, err
}
if author != nil {
resp.EntityName = author.Name
resp.AuthorRelinkEligible = canRelinkAuthorToUpstream(author)
}
case "book":
book, err := h.books.GetByID(r.Context(), conflict.LocalID)
if err != nil {
return absConflictResponse{}, err
}
if book != nil {
resp.EntityName = book.Title
}
}
if resp.EntityName == "" {
resp.EntityName = conflict.ItemID
}
return resp, nil
}