| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "reflect" |
| 7 | "sort" |
| 8 | |
| 9 | "amadan.net/oficina/slides/internal/slides/deck" |
| 10 | ) |
| 11 | |
| 12 | // What the server holds a scene to on the way into the log, over and |
| 13 | // above the core's ops. |
| 14 | // |
| 15 | // Shape. A scene's id is the document's id pattern; "__proto__" is never |
| 16 | // a state id, a word key or a key in its data; its data is at most |
| 17 | // deck.MaxSceneData (960 KiB) of canonical JSON. The core's ops refuse |
| 18 | // anything else, so applyOpsOn needs nothing; bytes off the wire can |
| 19 | // write anything, so appendUpdate asks sceneRefusal about what a posted |
| 20 | // update leaves, before anything is stored. Only scenes the update added |
| 21 | // or changed are looked at: a scene it did not touch is not its doing, |
| 22 | // and a block naming a scene or state that is gone is never a reason to |
| 23 | // refuse. |
| 24 | // |
| 25 | // Author. A scene's author is whoever wrote its current code (scenes and |
| 26 | // clips spec §2, §8.9), and the server is the only one who says so. Both |
| 27 | // write paths into a document come through here: |
| 28 | // |
| 29 | // - applyOpsOn (the API, an agent's edit, the app's own batches) hands |
| 30 | // the core a batch, and stampSceneOps rewrites it first: an |
| 31 | // add_scene, or a set_scene carrying code, gets the actor as its |
| 32 | // author, and any other author an op carries is dropped. |
| 33 | // - appendUpdate (live sync) hands the core bytes off the wire, which |
| 34 | // can say anything. sceneAuthorOps compares the scenes before and |
| 35 | // after them and answers the set_scene ops that put things right: a |
| 36 | // scene whose code is new or changed is the poster's, and an author |
| 37 | // changed with the code left alone goes back to what it was. Those |
| 38 | // are applied as a second update and stored in the same transaction, |
| 39 | // as the poster's. |
| 40 | // |
| 41 | // The actor is the string every write records (actorFor, agentActor): |
| 42 | // person:<id> or agent:<id>. |
| 43 | |
| 44 | // stampSceneOps is ops with every scene op's author set from actor (see |
| 45 | // above). A batch with no scene op, or one that is not a list of |
| 46 | // objects (the core refuses that by name), comes back as it was. |
| 47 | func stampSceneOps(ops []byte, actor string) ([]byte, error) { |
| 48 | var batch []map[string]json.RawMessage |
| 49 | if json.Unmarshal(ops, &batch) != nil { |
| 50 | return ops, nil |
| 51 | } |
| 52 | author, err := json.Marshal(actor) |
| 53 | if err != nil { |
| 54 | return nil, fmt.Errorf("slides: encode a scene author: %w", err) |
| 55 | } |
| 56 | changed := false |
| 57 | for _, op := range batch { |
| 58 | var name string |
| 59 | if raw, ok := op["op"]; !ok || json.Unmarshal(raw, &name) != nil { |
| 60 | continue |
| 61 | } |
| 62 | code, hasCode := op["code"] |
| 63 | switch { |
| 64 | case name == "add_scene", name == "set_scene" && hasCode && string(code) != "null": |
| 65 | op["author"] = author |
| 66 | changed = true |
| 67 | case name == "set_scene": |
| 68 | delete(op, "author") |
| 69 | changed = true |
| 70 | } |
| 71 | } |
| 72 | if !changed { |
| 73 | return ops, nil |
| 74 | } |
| 75 | out, err := json.Marshal(batch) |
| 76 | if err != nil { |
| 77 | return nil, fmt.Errorf("slides: encode a stamped batch: %w", err) |
| 78 | } |
| 79 | return out, nil |
| 80 | } |
| 81 | |
| 82 | // sceneAuthorOps is the batch that gives each scene in after the author |
| 83 | // the rule says it has, given before: actor where the scene is new or |
| 84 | // its code changed, its author in before otherwise. Nil when every |
| 85 | // scene already has it. |
| 86 | func sceneAuthorOps(before, after *deck.Deck, actor string) ([]byte, error) { |
| 87 | if after == nil || len(after.Scenes) == 0 { |
| 88 | return nil, nil |
| 89 | } |
| 90 | ids := make([]string, 0, len(after.Scenes)) |
| 91 | for id := range after.Scenes { |
| 92 | ids = append(ids, id) |
| 93 | } |
| 94 | sort.Strings(ids) |
| 95 | var ops []map[string]string |
| 96 | for _, id := range ids { |
| 97 | s := after.Scenes[id] |
| 98 | want := actor |
| 99 | if before != nil { |
| 100 | if prev, ok := before.Scenes[id]; ok && prev.Code == s.Code { |
| 101 | want = prev.Author |
| 102 | } |
| 103 | } |
| 104 | if s.Author != want { |
| 105 | ops = append(ops, map[string]string{"op": "set_scene", "id": id, "author": want}) |
| 106 | } |
| 107 | } |
| 108 | if len(ops) == 0 { |
| 109 | return nil, nil |
| 110 | } |
| 111 | out, err := json.Marshal(ops) |
| 112 | if err != nil { |
| 113 | return nil, fmt.Errorf("slides: encode scene authors: %w", err) |
| 114 | } |
| 115 | return out, nil |
| 116 | } |
| 117 | |
| 118 | // sceneRefusal is why the update that turned before into after may not |
| 119 | // be stored, or nil. It looks at each scene the update added or changed |
| 120 | // (one equal to its namesake in before is not this update's doing) and |
| 121 | // holds it to what the core's ops hold an op to: a well-formed id, no |
| 122 | // "__proto__" as a state id, a word key or a key anywhere in data, and |
| 123 | // data within deck.MaxSceneData. |
| 124 | func sceneRefusal(before, after *deck.Deck) error { |
| 125 | if after == nil { |
| 126 | return nil |
| 127 | } |
| 128 | ids := make([]string, 0, len(after.Scenes)) |
| 129 | for id := range after.Scenes { |
| 130 | ids = append(ids, id) |
| 131 | } |
| 132 | sort.Strings(ids) |
| 133 | for _, id := range ids { |
| 134 | s := after.Scenes[id] |
| 135 | if before != nil { |
| 136 | if prev, ok := before.Scenes[id]; ok && reflect.DeepEqual(prev, s) { |
| 137 | continue |
| 138 | } |
| 139 | } |
| 140 | if !deck.SceneID(id) { |
| 141 | return fmt.Errorf("scene id %q is not 12 lowercase base32 characters", id) |
| 142 | } |
| 143 | for _, st := range s.States { |
| 144 | if st.ID == deck.Proto { |
| 145 | return fmt.Errorf("scene %q: a state id may not be %q", id, deck.Proto) |
| 146 | } |
| 147 | } |
| 148 | if _, ok := s.Words[deck.Proto]; ok { |
| 149 | return fmt.Errorf("scene %q: words may not have the key %q", id, deck.Proto) |
| 150 | } |
| 151 | if deck.HoldsProto(s.Data) { |
| 152 | return fmt.Errorf("scene %q: data may not hold the key %q", id, deck.Proto) |
| 153 | } |
| 154 | if deck.SceneDataSize(s) > deck.MaxSceneData { |
| 155 | return fmt.Errorf("scene %q holds more than %d bytes of data", id, deck.MaxSceneData) |
| 156 | } |
| 157 | } |
| 158 | return nil |
| 159 | } |
| 160 | |