| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "sync" |
| 9 | "time" |
| 10 | // Zones are Equipo's own: the instance's image need carry none. |
| 11 | _ "time/tzdata" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/equipo/personapi" |
| 14 | "amadan.net/oficina/equipo/internal/wire" |
| 15 | "gorm.io/gorm" |
| 16 | ) |
| 17 | |
| 18 | // A person's time zone (paused work, decision 4). Every day boundary, |
| 19 | // digest, quiet hour, reset and resume time is the person's: the zone |
| 20 | // their browser, their Mac or iPhone, or Base last said, Europe/Dublin |
| 21 | // until one has (Paul, 2026-11-13). It follows the device; there is no |
| 22 | // setting to keep in step. |
| 23 | |
| 24 | // DefaultZone is the zone of a person who has not said theirs. |
| 25 | const DefaultZone = "Europe/Dublin" |
| 26 | |
| 27 | // zoneRecord is one person's zone, an IANA name. |
| 28 | type zoneRecord struct { |
| 29 | Person string `gorm:"primaryKey"` |
| 30 | Zone string `gorm:"not null"` |
| 31 | // Source is who said it: the person's own browser or device |
| 32 | // (ZoneDevice), or Base for the team (ZoneBase). The person's own wins. |
| 33 | Source string |
| 34 | UpdatedAt time.Time |
| 35 | } |
| 36 | |
| 37 | // Where a person's zone came from. |
| 38 | const ( |
| 39 | ZoneDevice = "device" |
| 40 | ZoneBase = "base" |
| 41 | ) |
| 42 | |
| 43 | func (zoneRecord) TableName() string { return "chat_person_zones" } |
| 44 | |
| 45 | // zones caches loaded locations by name: time.LoadLocation reads and |
| 46 | // parses the embedded database each call. |
| 47 | var zones sync.Map |
| 48 | |
| 49 | // LoadZone is the location of an IANA zone name, ok false for one Equipo |
| 50 | // does not know. "UTC" is a zone; "Local" and "" are not: the instance's |
| 51 | // own zone is nobody's. |
| 52 | func LoadZone(name string) (*time.Location, bool) { |
| 53 | if v, ok := zones.Load(name); ok { |
| 54 | return v.(*time.Location), true |
| 55 | } |
| 56 | if name == "" || name == "Local" || len(name) > 64 || strings.ContainsAny(name, " \t\r\n.\\") { |
| 57 | return nil, false |
| 58 | } |
| 59 | loc, err := time.LoadLocation(name) |
| 60 | if err != nil { |
| 61 | return nil, false |
| 62 | } |
| 63 | zones.Store(name, loc) |
| 64 | return loc, true |
| 65 | } |
| 66 | |
| 67 | // defaultLocation is DefaultZone's location; it is embedded, so it loads. |
| 68 | func defaultLocation() *time.Location { |
| 69 | loc, _ := LoadZone(DefaultZone) |
| 70 | return loc |
| 71 | } |
| 72 | |
| 73 | // zone is the person's zone. A read that fails is the default: a zone is |
| 74 | // a presentation, never an authority, and the page must still draw. |
| 75 | func (t *txn) zone(person string) *time.Location { |
| 76 | g := t.db |
| 77 | if g == nil { |
| 78 | g = t.s.transactionSession(context.Background()) |
| 79 | } |
| 80 | var r zoneRecord |
| 81 | if err := g.Where("person = ?", person).Limit(1).Find(&r).Error; err != nil || r.Zone == "" { |
| 82 | return defaultLocation() |
| 83 | } |
| 84 | if loc, ok := LoadZone(r.Zone); ok { |
| 85 | return loc |
| 86 | } |
| 87 | return defaultLocation() |
| 88 | } |
| 89 | |
| 90 | // SetZone records the zone the person's own browser or device says (PUT |
| 91 | // /api/v1/time-zone, the web page's quiet post). It always wins. |
| 92 | func (s *Store) SetZone(ctx context.Context, person, name string) (string, error) { |
| 93 | return s.SetZoneFrom(ctx, person, name, ZoneDevice, false) |
| 94 | } |
| 95 | |
| 96 | // SetZoneFrom records the person's zone from source: the roster checked |
| 97 | // in the transaction; ErrInvalid for a zone Equipo does not know. With |
| 98 | // onlyIfUnset (Base's team zone, PUT /api/v1/assistant/time-zone) it is |
| 99 | // stored only when the person has none from their own browser or device. |
| 100 | // It answers the zone in effect; the same zone again writes nothing. |
| 101 | func (s *Store) SetZoneFrom(ctx context.Context, person, name, source string, onlyIfUnset bool) (string, error) { |
| 102 | if _, ok := LoadZone(name); !ok || (source != ZoneDevice && source != ZoneBase) { |
| 103 | return "", ErrInvalid |
| 104 | } |
| 105 | effect := name |
| 106 | err := s.tx(ctx, func(t *txn) error { |
| 107 | if err := s.authority.CurrentPerson(ctx, t.db); err != nil { |
| 108 | return err |
| 109 | } |
| 110 | var r zoneRecord |
| 111 | if err := t.db.Where("person = ?", person).Limit(1).Find(&r).Error; err != nil { |
| 112 | return err |
| 113 | } |
| 114 | if onlyIfUnset && r.Person != "" && r.Source != ZoneBase { |
| 115 | // The person's own browser or device said it: theirs wins. |
| 116 | effect = r.Zone |
| 117 | return nil |
| 118 | } |
| 119 | if r.Zone == name && r.Source == source { |
| 120 | return nil |
| 121 | } |
| 122 | now := t.now() |
| 123 | if r.Person == "" { |
| 124 | return t.db.Create(&zoneRecord{Person: person, Zone: name, Source: source, UpdatedAt: now}).Error |
| 125 | } |
| 126 | return t.db.Model(&zoneRecord{}).Where("person = ?", person).Select("zone", "source", "updated_at").Updates(&zoneRecord{Zone: name, Source: source, UpdatedAt: now}).Error |
| 127 | }) |
| 128 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 129 | err = ErrNotFound |
| 130 | } |
| 131 | if err != nil { |
| 132 | return "", err |
| 133 | } |
| 134 | return effect, nil |
| 135 | } |
| 136 | |
| 137 | // setZone is PUT /time-zone and PUT /assistant/time-zone: 200 with the |
| 138 | // zone, 422 invalid_input for one Equipo does not know. |
| 139 | func (h *HTTP) setZone(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| 140 | var in wire.TimeZone |
| 141 | if err := readJSON(r, &in); err != nil { |
| 142 | h.fail(w, r, err) |
| 143 | return |
| 144 | } |
| 145 | out, err := h.Store.SetZone(r.Context(), p.Address, in.TimeZone) |
| 146 | if err != nil { |
| 147 | h.fail(w, r, err) |
| 148 | return |
| 149 | } |
| 150 | writeJSON(w, 200, wire.TimeZone{TimeZone: out}) |
| 151 | } |
| 152 | |
| 153 | // setAssistantZone is PUT /assistant/time-zone: Base's zone for the |
| 154 | // person, kept from overriding their own device's with only_if_unset; 200 |
| 155 | // with the zone in effect. |
| 156 | func (h *HTTP) setAssistantZone(w http.ResponseWriter, r *http.Request, p personapi.PersonPrincipal) { |
| 157 | var in wire.AssistantTimeZone |
| 158 | if err := readJSON(r, &in); err != nil { |
| 159 | h.fail(w, r, err) |
| 160 | return |
| 161 | } |
| 162 | out, err := h.Store.SetZoneFrom(r.Context(), p.Address, in.TimeZone, ZoneBase, in.OnlyIfUnset) |
| 163 | if err != nil { |
| 164 | h.fail(w, r, err) |
| 165 | return |
| 166 | } |
| 167 | writeJSON(w, 200, wire.TimeZone{TimeZone: out}) |
| 168 | } |
| 169 | |