| 1 | package equipo |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "strconv" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // Thinking words: the person picks the words the thinking line shows |
| 13 | // (Paul: "fun 'Thinking' variants... start with a more professional |
| 14 | // one... and 'Random'"). The phrases are copy (copy.go, "thinking.*"); |
| 15 | // the line keeps the real activity as its label and tooltip. |
| 16 | |
| 17 | // thinkingFor is the person's set and every set's phrases as JSON, for |
| 18 | // chat-stream.js: {"set": {"situation": ["phrase", ...]}}. |
| 19 | func (a *application) thinkingFor(ctx context.Context, address string) (string, string) { |
| 20 | words, err := a.store.Chat().Thinking(ctx, address) |
| 21 | if err != nil { |
| 22 | return wire.DefaultThinking, "{}" |
| 23 | } |
| 24 | return words.Set, thinkingJSON(words.Sets) |
| 25 | } |
| 26 | |
| 27 | func thinkingJSON(sets []wire.ThinkingSet) string { |
| 28 | out := map[string]map[string][]string{} |
| 29 | for _, s := range sets { |
| 30 | out[s.ID] = s.Phrases |
| 31 | } |
| 32 | raw, _ := json.Marshal(out) |
| 33 | return string(raw) |
| 34 | } |
| 35 | |
| 36 | // thinkingView is the Thinking words page. |
| 37 | type thinkingView struct { |
| 38 | wire.ThinkingWords |
| 39 | JSON string |
| 40 | Saved bool |
| 41 | } |
| 42 | |
| 43 | // thinkingSave is POST /settings/thinking: the same SetThinking a device |
| 44 | // makes, as the session's person. |
| 45 | func (a *application) thinkingSave(w http.ResponseWriter, r *http.Request) { |
| 46 | revision, _ := strconv.ParseInt(r.PostFormValue("revision"), 10, 64) |
| 47 | set := r.PostFormValue("set") |
| 48 | back := "/conversation/settings/thinking" |
| 49 | a.chatActionTo(w, r, back, func(ctx context.Context, address string) error { |
| 50 | _, err := a.store.Chat().SetThinking(ctx, address, wire.ThinkingChoice{Set: set, Revision: revision}) |
| 51 | return err |
| 52 | }) |
| 53 | } |
| 54 | |