| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | |
| 8 | "the-valley/webui/export" |
| 9 | ) |
| 10 | |
| 11 | // knotPage is one knot: where it sits, its fields, the knots it links, |
| 12 | // its body and its comments. |
| 13 | type knotPage struct { |
| 14 | Knot knotLink |
| 15 | Type string |
| 16 | Status string |
| 17 | Trail []knotLink |
| 18 | Facts []fact |
| 19 | BlockedBy []reference |
| 20 | Blocks []knotLink |
| 21 | Children []knotLink |
| 22 | Changes []string |
| 23 | Body string |
| 24 | Comments []comment |
| 25 | } |
| 26 | |
| 27 | // fact is one of a knot's fields. A fact with a URL links to what it |
| 28 | // names. |
| 29 | type fact struct { |
| 30 | Label string |
| 31 | Text string |
| 32 | URL string |
| 33 | } |
| 34 | |
| 35 | // reference is one entry of blocked_by: a knot the page links to, or a |
| 36 | // reference it can only name, with what is known of it. |
| 37 | type reference struct { |
| 38 | Ref string |
| 39 | Knot *knotLink |
| 40 | Note string |
| 41 | } |
| 42 | |
| 43 | // comment is one comment as the page shows it. An edited comment says so. |
| 44 | type comment struct { |
| 45 | Author string |
| 46 | At string |
| 47 | Edited bool |
| 48 | EditedAt string |
| 49 | EditedBy string |
| 50 | Body string |
| 51 | } |
| 52 | |
| 53 | func (q *request) knot(project, id string) { |
| 54 | p, ok := q.project(project) |
| 55 | if !ok { |
| 56 | q.notFound() |
| 57 | return |
| 58 | } |
| 59 | knots := q.idx.knots[p.Name] |
| 60 | k, ok := knots[id] |
| 61 | if !ok { |
| 62 | q.notFound() |
| 63 | return |
| 64 | } |
| 65 | page := knotPage{Knot: linkOf(p.Name, k), Type: k.Type, Status: k.Status, Body: trimEnd(k.Body), |
| 66 | Changes: k.Changes} |
| 67 | |
| 68 | // The trail runs from the milestone down to the knot's parent. |
| 69 | seen := map[string]bool{k.ID: true} |
| 70 | for at := k.Parent; at != "" && !seen[at]; { |
| 71 | seen[at] = true |
| 72 | parent, ok := knots[at] |
| 73 | if !ok { |
| 74 | break |
| 75 | } |
| 76 | page.Trail = append([]knotLink{linkOf(p.Name, parent)}, page.Trail...) |
| 77 | at = parent.Parent |
| 78 | } |
| 79 | |
| 80 | add := func(label, text, url string) { |
| 81 | if text != "" { |
| 82 | page.Facts = append(page.Facts, fact{label, text, url}) |
| 83 | } |
| 84 | } |
| 85 | add("State", stateLabel(k.State), "") |
| 86 | add("Created", k.Created+" by "+k.CreatedBy, "") |
| 87 | add("Owner", k.Owner, "") |
| 88 | add("Source", k.Source, "") |
| 89 | if c := k.Claim; c != nil { |
| 90 | add("Claimed", "by "+c.By+", "+stampOf(c.At), "") |
| 91 | } |
| 92 | if pr := k.Progress; pr != nil { |
| 93 | add("Progress", strconv.Itoa(pr.Done)+" of "+strconv.Itoa(pr.Of)+" done", "") |
| 94 | } |
| 95 | if k.Target != "" { |
| 96 | add("Lands in", k.Target, q.refURL(p.Name, k.Target)) |
| 97 | } |
| 98 | if k.GraduatedInto != "" { |
| 99 | add("Graduated into", k.GraduatedInto, q.refURL(p.Name, k.GraduatedInto)) |
| 100 | } |
| 101 | |
| 102 | for _, ref := range k.BlockedBy { |
| 103 | page.BlockedBy = append(page.BlockedBy, q.reference(p.Name, k, ref)) |
| 104 | } |
| 105 | for _, id := range k.Blocks { |
| 106 | if b, ok := knots[id]; ok { |
| 107 | page.Blocks = append(page.Blocks, linkOf(p.Name, b)) |
| 108 | } |
| 109 | } |
| 110 | for _, id := range k.Children { |
| 111 | if c, ok := knots[id]; ok { |
| 112 | page.Children = append(page.Children, linkOf(p.Name, c)) |
| 113 | } |
| 114 | } |
| 115 | for _, c := range k.Comments { |
| 116 | page.Comments = append(page.Comments, comment{Author: c.Author, At: stampOf(c.At), |
| 117 | Edited: c.Edited != "", EditedAt: stampOf(c.Edited), EditedBy: c.EditedBy, Body: trimEnd(c.Body)}) |
| 118 | } |
| 119 | q.page(http.StatusOK, "knot", k.ID+" · "+k.Title, page) |
| 120 | } |
| 121 | |
| 122 | // reference resolves one blocked_by entry of k's. A knot of this project |
| 123 | // or of another public one is linked. A reference to a project the |
| 124 | // export does not hold is named and no more is said of it. |
| 125 | func (q *request) reference(project string, k *export.Knot, ref string) reference { |
| 126 | other, id, foreign := strings.Cut(ref, ":") |
| 127 | if !foreign { |
| 128 | other, id = project, ref |
| 129 | } |
| 130 | if _, ok := q.project(other); ok { |
| 131 | if b, ok := q.idx.knots[other][id]; ok { |
| 132 | link := linkOf(other, b) |
| 133 | return reference{Ref: ref, Knot: &link} |
| 134 | } |
| 135 | } |
| 136 | for _, e := range k.Edges { |
| 137 | if e.Field != "blocked_by" || e.Ref != ref { |
| 138 | continue |
| 139 | } |
| 140 | switch e.State { |
| 141 | case export.EdgeHidden: |
| 142 | return reference{Ref: ref, Note: "not published"} |
| 143 | case export.EdgeDangling: |
| 144 | return reference{Ref: ref, Note: "not found in its graph"} |
| 145 | } |
| 146 | } |
| 147 | return reference{Ref: ref, Note: "not found"} |
| 148 | } |
| 149 | |
| 150 | // refURL is the page of what a target or graduated_into names: a knot of |
| 151 | // a public project, or the tree of a public project's published tip. It |
| 152 | // is "" for anything else. |
| 153 | func (q *request) refURL(project, ref string) string { |
| 154 | other, id, foreign := strings.Cut(ref, ":") |
| 155 | if !foreign { |
| 156 | if _, ok := q.idx.knots[project][ref]; ok { |
| 157 | return knotURL(project, ref) |
| 158 | } |
| 159 | if p, ok := q.project(ref); ok { |
| 160 | return treeURL(p.Name, p.SourceTip, nil) |
| 161 | } |
| 162 | return "" |
| 163 | } |
| 164 | if _, ok := q.project(other); ok { |
| 165 | if _, ok := q.idx.knots[other][id]; ok { |
| 166 | return knotURL(other, id) |
| 167 | } |
| 168 | } |
| 169 | return "" |
| 170 | } |