gunk-dev/the-valley

knot.go

sourceba15a5d4fa13main's published tip history
6.3 KiB
1package server
2
3import (
4 "net/http"
5 "slices"
6 "strconv"
7 "strings"
8
9 "the-valley/knot"
10 "the-valley/webui/export"
11)
12
13// knotPage is one knot: where it sits, its fields, the knots it links,
14// its body and its comments.
15type knotPage struct {
16 Knot knotLink
17 Type string
18 Status string
19 Trail []knotLink
20 TrailCut bool
21 Facts []fact
22 BlockedBy []reference
23 Blocks []knotLink
24 Children []knotLink
25 Changes []string
26 Body string
27 Comments []comment
28}
29
30// fact is one of a knot's fields. A fact with a URL links to what it
31// names.
32type fact struct {
33 Label string
34 Text string
35 URL string
36}
37
38// reference is one entry of blocked_by: a knot the page links to, a
39// reference it can only name, with what is known of it, or a knot of an
40// unpublished project, which it does not name at all.
41type reference struct {
42 Ref string
43 Knot *knotLink
44 Note string
45}
46
47// comment is one comment as the page shows it. An edited comment says so.
48type comment struct {
49 Author string
50 At string
51 Edited bool
52 EditedAt string
53 EditedBy string
54 Body string
55}
56
57func (q *request) knot(project, id string) {
58 p, ok := q.project(project)
59 if !ok {
60 q.notFound()
61 return
62 }
63 knots := q.idx.knots[p.Name]
64 k, ok := knots[id]
65 if !ok {
66 q.notFound()
67 return
68 }
69 page := knotPage{Knot: linkOf(p.Name, k), Type: k.Type, Status: k.Status, Body: trimEnd(k.Body)}
70
71 // The trail runs from the milestone down to the knot's parent. It
72 // names the nearest MaxPlanDepth parents, and marks any above them.
73 seen := map[string]bool{k.ID: true}
74 for at := k.Parent; at != "" && !seen[at]; {
75 parent, ok := knots[at]
76 if !ok {
77 break
78 }
79 if len(page.Trail) == MaxPlanDepth {
80 page.TrailCut = true
81 break
82 }
83 seen[at] = true
84 page.Trail = append(page.Trail, linkOf(p.Name, parent))
85 at = parent.Parent
86 }
87 slices.Reverse(page.Trail)
88
89 add := func(label, text, url string) {
90 if text != "" {
91 page.Facts = append(page.Facts, fact{label, text, url})
92 }
93 }
94 add("State", stateLabel(k.State), "")
95 add("Created", k.Created+" by "+k.CreatedBy, "")
96 add("Owner", k.Owner, "")
97 add("Source", k.Source, "")
98 if c := k.Claim; c != nil {
99 add("Claimed", "by "+c.By+", "+stampOf(c.At), "")
100 }
101 if pr := k.Progress; pr != nil {
102 add("Progress", strconv.Itoa(pr.Done)+" of "+strconv.Itoa(pr.Of)+" done", "")
103 }
104 if k.Target != "" {
105 text, url := q.idx.named(p.Name, k.Target)
106 add("Lands in", text, url)
107 }
108 if k.GraduatedInto != "" {
109 text, url := q.idx.named(p.Name, k.GraduatedInto)
110 add("Graduated into", text, url)
111 }
112 for _, c := range k.Changes {
113 page.Changes = append(page.Changes, q.idx.change(c))
114 }
115
116 for _, ref := range k.BlockedBy {
117 page.BlockedBy = append(page.BlockedBy, q.reference(p.Name, k, ref))
118 }
119 for _, id := range k.Blocks {
120 if b, ok := knots[id]; ok {
121 page.Blocks = append(page.Blocks, linkOf(p.Name, b))
122 }
123 }
124 for _, id := range k.Children {
125 if c, ok := knots[id]; ok {
126 page.Children = append(page.Children, linkOf(p.Name, c))
127 }
128 }
129 for _, c := range k.Comments {
130 page.Comments = append(page.Comments, comment{Author: c.Author, At: stampOf(c.At),
131 Edited: c.Edited != "", EditedAt: stampOf(c.Edited), EditedBy: c.EditedBy, Body: trimEnd(c.Body)})
132 }
133 q.page(http.StatusOK, "knot", k.ID+" · "+k.Title, page)
134}
135
136// The words a page shows for a reference into a project the export does
137// not hold as public. They name nothing of what is referred to: not the
138// project, not the knot, and not the reference's text.
139const (
140 unpublishedKnot = "a knot in an unpublished project"
141 unpublishedProject = "an unpublished project"
142 unpublishedChange = "a change in an unpublished project"
143)
144
145// reference resolves one blocked_by entry of k's. A knot of this project
146// or of another public one is linked. A reference into a project the
147// export does not hold as public is shown as unpublished, with none of
148// its text. Any other reference is named, and said to be missing.
149func (q *request) reference(project string, k *export.Knot, ref string) reference {
150 other, id, foreign := strings.Cut(ref, ":")
151 if !foreign {
152 other, id = project, ref
153 }
154 if _, ok := q.project(other); !ok {
155 return reference{Note: unpublishedKnot}
156 }
157 if b, ok := q.idx.knots[other][id]; ok {
158 link := linkOf(other, b)
159 return reference{Ref: ref, Knot: &link}
160 }
161 for _, e := range k.Edges {
162 if e.Field == "blocked_by" && e.Ref == ref && e.State == export.EdgeDangling {
163 return reference{Ref: ref, Note: "not found in its graph"}
164 }
165 }
166 return reference{Ref: ref, Note: "not found"}
167}
168
169// named is how a page names what a target or graduated_into holds, and
170// the page it links to: a knot of a public project, or the tree of a
171// public project's published tip. A reference into a project the export
172// does not hold as public is named only as unpublished, and not linked. A
173// knot of a public project that the export does not hold is named and not
174// linked.
175func (idx *index) named(project, ref string) (text, url string) {
176 other, id, qualified := strings.Cut(ref, ":")
177 switch {
178 case qualified:
179 if _, ok := idx.project(other); !ok {
180 return unpublishedKnot, ""
181 }
182 if _, ok := idx.knots[other][id]; ok {
183 return ref, knotURL(other, id)
184 }
185 return ref, ""
186 case knot.IsLocalID(ref):
187 if _, ok := idx.knots[project][ref]; ok {
188 return ref, knotURL(project, ref)
189 }
190 return ref, ""
191 }
192 if p, ok := idx.project(ref); ok {
193 return ref, treeURL(p.Name, p.SourceTip, nil)
194 }
195 return unpublishedProject, ""
196}
197
198// change is how a page names one of a knot's change links. A valley
199// change of a project the export does not hold as public is named only as
200// unpublished.
201func (idx *index) change(link string) string {
202 if rest, ok := strings.CutPrefix(link, "valley:"); ok {
203 if project, _, ok := strings.Cut(rest, "/"); ok {
204 if _, public := idx.project(project); !public {
205 return unpublishedChange
206 }
207 }
208 }
209 return link
210}
211
212// waitsOn is the knots a blocked knot waits on, as a tree row names them.
213// The knots of projects the export does not hold as public are named once,
214// as unpublished.
215func (idx *index) waitsOn(refs []string) string {
216 var out []string
217 hidden := false
218 for _, ref := range refs {
219 idx.checkBuild()
220 if other, _, ok := strings.Cut(ref, ":"); ok {
221 if _, public := idx.project(other); !public {
222 if !hidden {
223 out = append(out, unpublishedKnot)
224 }
225 hidden = true
226 continue
227 }
228 }
229 out = append(out, ref)
230 }
231 return strings.Join(out, ", ")
232}