oficina / fichas Public

Clone
git clone https://amadan.net/oficina/fichas

Plain git — no account needed to clone.

Download

Download this file

1package fichas
2
3// The list, and the card beside it (level-up §5.4). Every state lives in
4// the URL (list.go), so every state is a link and the page works with
5// scripts off; ?card= opens a card in a pane beside a narrowed list.
6
7import (
8 "net/http"
9 "strconv"
10 "strings"
11 "time"
12)
13
14// index is a card type's list, as a table or as cards, searched,
15// filtered, sorted and paged from the in-memory index, with a card open
16// beside it when ?card= names one.
17func (a *app) index(w http.ResponseWriter, r *http.Request) {
18 ti, err := a.currentType(r)
19 if err != nil {
20 a.notFoundOrFail(w, r, err)
21 return
22 }
23 fields, err := a.ix.Fields(ti.Key)
24 if err != nil {
25 a.fail(w, r, err)
26 return
27 }
28 base := typeHref(ti.Key)
29 l := parseList(r, base, fields)
30 shown := shownColumns(r, ti.Key, fields)
31 if l.Sort != "" && !shown[l.Sort] {
32 l.Sort, l.Desc = "", false
33 }
34 data := map[string]any{"TypeKey": ti.Key, "Fields": fields, "Base": base, "Shown": shown}
35 if l.Card > 0 {
36 if !a.openCard(w, r, &l, ti, data) {
37 return
38 }
39 }
40 res, err := a.ix.Query(ti.Key, l.query())
41 if err != nil {
42 a.fail(w, r, err)
43 return
44 }
45 a.listData(r, l, res, fields, shown, data)
46 a.page(w, r, "index", data)
47}
48
49// listData is everything the list draws.
50func (a *app) listData(r *http.Request, l listState, res Result, fields []FieldView, shown map[string]bool, data map[string]any) {
51 filtered := l.Q != "" || len(l.Filters) > 0
52 reset := listState{base: l.base, View: l.View, Filters: map[string]string{}}
53 open := l
54 open.Page = 0
55 for k, v := range map[string]any{
56 "Result": res, "Cards": res.Cards, "Face": a.ix.Face(res.Type.Key),
57 "View": l.View, "Q": l.Q, "Hidden": l.hidden(),
58 "ClearHref": l.with(func(s *listState) { s.Q = "" }).href(),
59 "Filters": l.filterMenus(fields), "Columns": l.columns(fields, shown),
60 "Choices": columnChoices(fields, shown), "Density": densityOf(r),
61 "Here": l.href(), // the list as shown: a dropped sort stays dropped
62 "Pages": l.pages(res), "Count": countLine(a.readerL(r), res, filtered),
63 "Filtered": filtered,
64 "TableHref": l.with(func(s *listState) { s.View = "table" }).href(),
65 "CardsHref": l.with(func(s *listState) { s.View = "cards" }).href(),
66 "ResetHref": reset.href(), "List": open, "ListQuery": listQuery(open),
67 "Notice": a.noticeOf(r),
68 } {
69 data[k] = v
70 }
71}
72
73// listQuery is the list's state as a query string, which a cell edit
74// posts back so the server can say whether the card is still in it.
75func listQuery(l listState) string {
76 l.Card, l.Page = 0, 0
77 h := l.href()
78 _, q, _ := strings.Cut(h, "?")
79 return q
80}
81
82// OpenHref is this list with a card open beside it.
83func (l listState) OpenHref(id int64) string {
84 c := l
85 c.Card, c.Page = id, 0
86 return c.href()
87}
88
89// CloseHref is this list with no card open, on the page it was on.
90func (l listState) CloseHref() string {
91 c := l
92 c.Card = 0
93 return c.href()
94}
95
96// openCard loads the card named by ?card= and where it sits in the
97// list. A card not in the list still opens, marked "Not in this view",
98// with no Next or Previous. false when it answered the request itself.
99func (a *app) openCard(w http.ResponseWriter, r *http.Request, l *listState, ti TypeInfo, data map[string]any) bool {
100 card, err := LoadCard(r.Context(), a.db.G, a.sealer, l.Card)
101 if err == nil && card.TypeKey != ti.Key {
102 err = errNotFound
103 }
104 if err != nil {
105 a.notFoundOrFail(w, r, err)
106 return false
107 }
108 pane := cardPane{Card: card}
109 if pos, ok := a.ix.Position(ti.Key, l.query(), card.Card.ID); ok {
110 pane.Pos = &pos
111 if r.URL.Query().Get("page") == "" {
112 l.Page = pos.Page
113 }
114 if pos.Prev != 0 {
115 pane.PrevHref = l.OpenHref(pos.Prev)
116 }
117 if pos.Next != 0 {
118 pane.NextHref = l.OpenHref(pos.Next)
119 }
120 }
121 pane.CloseHref = l.CloseHref()
122 pane.EditedBy, pane.EditedAt = a.editedBy(r, card)
123 entryFields, err := EntryFields(r.Context(), a.db.G, a.sealer, TypeCallLog)
124 if err != nil {
125 a.fail(w, r, err)
126 return false
127 }
128 data["Open"] = &card
129 data["Pane"] = pane
130 data["EntryFields"] = entryFields
131 data["Card"] = card
132 return true
133}
134
135// cardPane is the card beside the list.
136type cardPane struct {
137 Card CardView
138 Pos *Position
139 PrevHref, NextHref string
140 CloseHref string
141 EditedBy string
142 EditedAt time.Time
143}
144
145// editedBy is "Edited by": the person behind the card's last created or
146// updated event made by a person (§6.1), and when.
147func (a *app) editedBy(r *http.Request, card CardView) (string, time.Time) {
148 var e Event
149 err := a.db.G.WithContext(r.Context()).
150 Where("object_kind = ? AND object_id = ? AND kind IN ? AND actor_id IS NOT NULL",
151 ObjectCard, card.Card.ID, []string{EventCreated, EventUpdated}).
152 Order("id desc").First(&e).Error
153 if err != nil {
154 return "", time.Time{}
155 }
156 address, err := memberAddress(a.db.G.WithContext(r.Context()), a.sealer, *e.ActorID)
157 if err != nil {
158 return "", e.CreatedAt
159 }
160 return personName(address), e.CreatedAt
161}
162
163// notice is the one-line answer a scriptless edit comes back with.
164type notice struct {
165 Kind string // saved, undone, refused, conflict
166 Text string
167 EventID string // for Saved's Undo and Undone's Redo
168 CardID int64 // the card the event is on, which the Undo posts to
169}
170
171// noticeOf is the request's notice, with the card its event is on: a
172// list's notice is about whichever card was edited from it.
173func (a *app) noticeOf(r *http.Request) *notice {
174 n := noticeFrom(r)
175 if n != nil && n.EventID != "" {
176 var e Event
177 if err := a.db.G.WithContext(r.Context()).Select("object_id").
178 Where("id = ? AND object_kind = ?", n.EventID, ObjectCard).First(&e).Error; err == nil {
179 n.CardID = e.ObjectID
180 } else {
181 n.EventID = ""
182 }
183 }
184 return n
185}
186
187func noticeFrom(r *http.Request) *notice {
188 q := r.URL.Query()
189 switch {
190 case q.Get("saved") != "":
191 return &notice{Kind: "saved", Text: "Saved.", EventID: digits(q.Get("saved"))}
192 case q.Get("undone") != "":
193 raw, left := strings.CutSuffix(q.Get("undone"), "-left")
194 id := digits(raw)
195 switch {
196 case id == "" || id == "0":
197 return &notice{Kind: "undone", Text: "Nothing was undone because it was changed again. History shows who changed it."}
198 case left:
199 return &notice{Kind: "undone", Text: "Undone except where a value was changed again. History shows what was left.", EventID: id}
200 }
201 return &notice{Kind: "undone", Text: "Undone.", EventID: id}
202 // Refusals come back as a flag, never as text from the URL, so a
203 // crafted link cannot put words in Fichas' mouth.
204 case q.Get("refused") != "":
205 return &notice{Kind: "refused", Text: "That value could not be saved, so nothing changed."}
206 case q.Get("conflict") != "":
207 return &notice{Kind: "conflict", Text: "Someone changed that field since you opened this page, so nothing changed."}
208 }
209 return nil
210}
211
212// digits is s when it is a positive number, "" otherwise: a notice's
213// event id goes into a form, never anything else from the URL.
214func digits(s string) string {
215 if n, err := strconv.ParseInt(s, 10, 64); err == nil && n >= 0 {
216 return strconv.FormatInt(n, 10)
217 }
218 return ""
219}
220