oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "amadan.net/oficina/equipo/internal/copydesk"
5 "encoding/json"
6)
7
8func (in *CopyImport) UnmarshalJSON(raw []byte) error {
9 type plain CopyImport
10 var current plain
11 if err := json.Unmarshal(raw, &current); err != nil {
12 return err
13 }
14 if current.Version != 0 {
15 *in = CopyImport(current)
16 return nil
17 }
18 var legacy struct {
19 Action string `json:"action"`
20 Review string `json:"review"`
21 Generation int64 `json:"generation"`
22 Strings []struct {
23 ID string `json:"id"`
24 Original string `json:"original"`
25 Text string `json:"text"`
26 Edited bool `json:"edited"`
27 Done bool `json:"done"`
28 Rerolled bool `json:"rerolled"`
29 } `json:"strings"`
30 }
31 if err := json.Unmarshal(raw, &legacy); err != nil {
32 return err
33 }
34 if legacy.Action != "approve" || len(legacy.Strings) > 200 {
35 return ErrInvalid
36 }
37 canonical, _ := json.Marshal(legacy)
38 *in = CopyImport{Version: 1, SourceID: "copy-review:" + copydesk.Hash(string(canonical)), originals: map[string]string{}}
39 seen := map[string]bool{}
40 for _, s := range legacy.Strings {
41 if seen[s.ID] {
42 return ErrInvalid
43 }
44 seen[s.ID] = true
45 if !s.Edited || !s.Done || s.Rerolled || s.Original == s.Text {
46 continue
47 }
48 in.Strings = append(in.Strings, copydesk.Feedback{Key: s.ID, Before: s.Original, After: s.Text})
49 in.originals[s.ID] = s.Original
50 }
51 return nil
52}
53