| 1 | package wire |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "slices" |
| 7 | ) |
| 8 | |
| 9 | // The Assistant's abilities (spec 2026-11-26-assistant-abilities §3): what |
| 10 | // the Base owner can turn off for a member. Base's internal/equipo has the |
| 11 | // same keys, in the same order. |
| 12 | const ( |
| 13 | AbilityWeb = "web" |
| 14 | AbilityAppsRead = "apps_read" |
| 15 | AbilityAppsWrite = "apps_write" |
| 16 | AbilityBuild = "build" |
| 17 | AbilityGoLive = "go_live" |
| 18 | AbilityServices = "services" |
| 19 | AbilityServicesAdd = "services_add" |
| 20 | ) |
| 21 | |
| 22 | // Abilities are the keys in the order Base lists them. |
| 23 | var Abilities = []string{AbilityWeb, AbilityAppsRead, AbilityAppsWrite, AbilityBuild, AbilityGoLive, AbilityServices, AbilityServicesAdd} |
| 24 | |
| 25 | // KnownAbility reports whether key is one of Abilities. |
| 26 | func KnownAbility(key string) bool { return slices.Contains(Abilities, key) } |
| 27 | |
| 28 | // The values of Base-Capabilities: what Base and every client its member |
| 29 | // uses can draw. They only decide presentation, never what may happen. |
| 30 | const ( |
| 31 | CapJobActions = "job_actions" |
| 32 | CapLookupAnswers = "lookup_answers" |
| 33 | ) |
| 34 | |
| 35 | // The codes a switched-off ability is refused with. |
| 36 | const ( |
| 37 | // CodeAbilityOff: the person's ability is off (Base's switch, the |
| 38 | // operator's fixed key, or one it needs). |
| 39 | CodeAbilityOff = "ability_off" |
| 40 | // CodeAbilityUnmapped: an operation no ability covers is never run. |
| 41 | CodeAbilityUnmapped = "ability_unmapped" |
| 42 | // CodeOperationChanged: the operation resolved at send is not the one |
| 43 | // the call was checked for. |
| 44 | CodeOperationChanged = "operation_changed" |
| 45 | // CodeHostUnverified: PUT host's token was not honoured by Base. |
| 46 | CodeHostUnverified = "host_unverified" |
| 47 | // CodeHostElsewhere: the person's Base is another, which still |
| 48 | // honours them. |
| 49 | CodeHostElsewhere = "host_elsewhere" |
| 50 | // CodeResearchOff: Research for a job whose person can't have web |
| 51 | // pages read. |
| 52 | CodeResearchOff = "research_off" |
| 53 | ) |
| 54 | |
| 55 | // AbilityBoard is Base's answer to GET /api/v1/abilities with a member's |
| 56 | // host token: the member's switches at a revision, whether they |
| 57 | // administer Base, and who to ask. Keys Equipo doesn't know are dropped |
| 58 | // as it is read. |
| 59 | type AbilityBoard struct { |
| 60 | Revision int64 `json:"revision"` |
| 61 | Admin bool `json:"admin"` |
| 62 | Ask string `json:"ask"` |
| 63 | Abilities map[string]bool `json:"abilities"` |
| 64 | } |
| 65 | |
| 66 | // UnmarshalJSON reads a board, keeping only known ability keys. It is |
| 67 | // strict about the envelope: an object with revision (a number), admin |
| 68 | // (true or false), ask (a string) and abilities (an object of true and |
| 69 | // false), each present and not null. Anything else is not a board, so a |
| 70 | // Base answering 200 with something else changes nothing. |
| 71 | func (b *AbilityBoard) UnmarshalJSON(raw []byte) error { |
| 72 | var fields map[string]json.RawMessage |
| 73 | if err := json.Unmarshal(raw, &fields); err != nil || fields == nil { |
| 74 | return errNotBoard |
| 75 | } |
| 76 | var out AbilityBoard |
| 77 | var abilities map[string]bool |
| 78 | for name, into := range map[string]any{"revision": &out.Revision, "admin": &out.Admin, "ask": &out.Ask, "abilities": &abilities} { |
| 79 | v, ok := fields[name] |
| 80 | if !ok || string(v) == "null" || json.Unmarshal(v, into) != nil { |
| 81 | return errNotBoard |
| 82 | } |
| 83 | } |
| 84 | if abilities == nil { |
| 85 | return errNotBoard |
| 86 | } |
| 87 | out.Abilities = map[string]bool{} |
| 88 | for k, v := range abilities { |
| 89 | if KnownAbility(k) { |
| 90 | out.Abilities[k] = v |
| 91 | } |
| 92 | } |
| 93 | *b = out |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | var errNotBoard = errors.New("wire: not an ability board") |
| 98 | |
| 99 | // AssistantAbilities is GET /api/v1/assistant/abilities: the caller's own |
| 100 | // effective switches (every key), the keys the operator fixed for |
| 101 | // everyone, and who can change them. |
| 102 | type AssistantAbilities struct { |
| 103 | Abilities map[string]bool `json:"abilities"` |
| 104 | Fixed []string `json:"fixed"` |
| 105 | Ask string `json:"ask"` |
| 106 | } |
| 107 | |
| 108 | // AbilitiesRefreshed is POST /api/v1/assistant/abilities/refresh's 200: |
| 109 | // the board's revision Equipo now holds for the person. |
| 110 | type AbilitiesRefreshed struct { |
| 111 | Revision int64 `json:"revision"` |
| 112 | } |
| 113 | |