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