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 "context"
5 "slices"
6 "strings"
7
8 "gorm.io/gorm"
9
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13// thinkingKey is the person's thinking words setting.
14func thinkingKey(person string) string { return "thinking:" + person }
15
16// ThinkingSets are the sets with their names and phrases, from the copy.
17func (s *Store) ThinkingSets() []wire.ThinkingSet {
18 words := func(key string) string {
19 if s.authority.Copy == nil {
20 return ""
21 }
22 return s.authority.Copy(key)
23 }
24 out := make([]wire.ThinkingSet, 0, len(wire.ThinkingSets))
25 for _, id := range wire.ThinkingSets {
26 set := wire.ThinkingSet{ID: id, Name: words("thinking.set." + id), Description: words("thinking.about." + id), Phrases: map[string][]string{}}
27 if id != wire.ThinkingRandom {
28 for _, sit := range wire.ThinkingSituations {
29 for _, p := range strings.Split(words("thinking."+id+"."+sit), "|") {
30 if p = strings.TrimSpace(p); p != "" {
31 set.Phrases[sit] = append(set.Phrases[sit], p)
32 }
33 }
34 }
35 }
36 out = append(out, set)
37 }
38 return out
39}
40
41// Thinking is the person's thinking words: their set (the default until
42// they pick one) and every set.
43func (s *Store) Thinking(ctx context.Context, person string) (out wire.ThinkingWords, err error) {
44 err = s.read(ctx, func(tx *gorm.DB) error {
45 if err := s.authority.CurrentPerson(ctx, tx); err != nil {
46 return err
47 }
48 value, _, revision, err := (&txn{s: s, db: tx}).settingRevision(thinkingKey(person))
49 if value == "" {
50 value = wire.DefaultThinking
51 }
52 out = wire.ThinkingWords{Set: value, Revision: revision, Sets: s.ThinkingSets()}
53 return err
54 })
55 return out, err
56}
57
58// SetThinking records the person's set against the revision they saw
59// (ErrConflict when it moved) and tells their open pages.
60func (s *Store) SetThinking(ctx context.Context, person string, in wire.ThinkingChoice) (out wire.ThinkingWords, err error) {
61 if !slices.Contains(wire.ThinkingSets, in.Set) {
62 return out, ErrInvalid
63 }
64 err = s.tx(ctx, func(t *txn) error {
65 if err := s.authority.CurrentPerson(ctx, t.db); err != nil {
66 return err
67 }
68 value, _, revision, err := t.settingRevision(thinkingKey(person))
69 if err != nil {
70 return err
71 }
72 if in.Revision != revision {
73 return ErrConflict
74 }
75 if value != in.Set || revision == 0 {
76 if revision, err = t.setSetting(thinkingKey(person), in.Set, person); err != nil {
77 return err
78 }
79 if err := t.event("", person, wire.EventThinking, "thinking", person, revision, "", "", 0, wire.ThinkingChoice{Set: in.Set, Revision: revision}); err != nil {
80 return err
81 }
82 }
83 out = wire.ThinkingWords{Set: in.Set, Revision: revision, Sets: s.ThinkingSets()}
84 return nil
85 })
86 return out, err
87}
88