| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "the-valley/web/export" |
| 7 | ) |
| 8 | |
| 9 | // projectForReader is what one reader's index is built from: a copy of |
| 10 | // each project of e that shown names, in e's order, with a copy of each of |
| 11 | // its knots, and the groups those projects belong to. It never changes e, |
| 12 | // which every reader's views share, and nothing changes the copies once |
| 13 | // they are made. |
| 14 | // |
| 15 | // A copied knot keeps its own fields, its State and its Ready as the host |
| 16 | // derived them, and what it says of the projects shown names. What it says |
| 17 | // of any other project's knots is taken out: |
| 18 | // |
| 19 | // - WaitingOn keeps the references into projects shown names, and |
| 20 | // WaitingOnHidden is set when the export set it or when a reference |
| 21 | // was taken out of WaitingOn. It says neither which nor how many. |
| 22 | // - ClearedByEnding, Dangling and Unresolved keep the references into |
| 23 | // projects shown names alone. |
| 24 | // - In each of those lists, a reference is a knot's ID in the knot's own |
| 25 | // graph, or <project>:<id> (knot/validate.go), each whole (knotRef). |
| 26 | // An entry of neither shape names no knot the reader may see, whatever |
| 27 | // it opens with, and is taken out as a reference into a project shown |
| 28 | // does not name is. |
| 29 | // - An edge into a project shown does not name is hidden: it keeps its |
| 30 | // field and reference, and loses its state, title, status and why, |
| 31 | // whatever the export said of them. An edge into a project shown names |
| 32 | // keeps what the export said. |
| 33 | // |
| 34 | // BlockedBy, Target, GraduatedInto and Changes are kept as they are, and |
| 35 | // every page names them through the index, which names nothing of a |
| 36 | // project it does not hold (reference, target, graduated, change and |
| 37 | // waitsOn). Nothing is inferred from BlockedBy: a blocker may be done. |
| 38 | // |
| 39 | // It checks ctx before each project, each knot, and each entry of a |
| 40 | // knot's lists and edges, so a cancelled build stops promptly. |
| 41 | func projectForReader(ctx context.Context, e *export.Export, shown map[string]bool) (*export.Export, error) { |
| 42 | out := &export.Export{Schema: e.Schema, Collected: e.Collected, Projects: []export.Project{}} |
| 43 | used := map[string]bool{} |
| 44 | for i := range e.Projects { |
| 45 | if err := ctx.Err(); err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | p := e.Projects[i] |
| 49 | if !shown[p.Name] { |
| 50 | continue |
| 51 | } |
| 52 | used[p.Group] = true |
| 53 | p.Knots = make([]export.Knot, len(e.Projects[i].Knots)) |
| 54 | for j := range e.Projects[i].Knots { |
| 55 | if err := ctx.Err(); err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | k, err := projectKnot(ctx, p.Name, e.Projects[i].Knots[j], shown) |
| 59 | if err != nil { |
| 60 | return nil, err |
| 61 | } |
| 62 | p.Knots[j] = k |
| 63 | } |
| 64 | out.Projects = append(out.Projects, p) |
| 65 | } |
| 66 | for _, g := range e.Groups { |
| 67 | if used[g.ID] { |
| 68 | out.Groups = append(out.Groups, g) |
| 69 | } |
| 70 | } |
| 71 | return out, nil |
| 72 | } |
| 73 | |
| 74 | // projectKnot is a copy of k, a knot of project's, with what it says of |
| 75 | // the knots of projects shown does not name taken out, as |
| 76 | // projectForReader says. It checks ctx before each entry of k's lists and |
| 77 | // edges. |
| 78 | func projectKnot(ctx context.Context, project string, k export.Knot, shown map[string]bool) (export.Knot, error) { |
| 79 | hidden := func(ref string) bool { |
| 80 | other, _, ok := knotRef(project, ref) |
| 81 | return !ok || !shown[other] |
| 82 | } |
| 83 | waiting, err := keep(ctx, k.WaitingOn, hidden) |
| 84 | if err != nil { |
| 85 | return export.Knot{}, err |
| 86 | } |
| 87 | k.WaitingOnHidden = k.WaitingOnHidden || len(waiting) < len(k.WaitingOn) |
| 88 | k.WaitingOn = waiting |
| 89 | for _, list := range []*[]string{&k.ClearedByEnding, &k.Dangling, &k.Unresolved} { |
| 90 | if *list, err = keep(ctx, *list, hidden); err != nil { |
| 91 | return export.Knot{}, err |
| 92 | } |
| 93 | } |
| 94 | edges := make([]export.Edge, 0, len(k.Edges)) |
| 95 | for _, edge := range k.Edges { |
| 96 | if err := ctx.Err(); err != nil { |
| 97 | return export.Knot{}, err |
| 98 | } |
| 99 | if !shown[edge.Project] { |
| 100 | edge = export.Edge{Field: edge.Field, Ref: edge.Ref, Project: edge.Project, ID: edge.ID, State: export.EdgeHidden} |
| 101 | } |
| 102 | edges = append(edges, edge) |
| 103 | } |
| 104 | k.Edges = edges |
| 105 | return k, nil |
| 106 | } |
| 107 | |
| 108 | // keep is a new list of the entries of list that hidden does not report, |
| 109 | // never nil. It checks ctx before each entry. |
| 110 | func keep(ctx context.Context, list []string, hidden func(string) bool) ([]string, error) { |
| 111 | kept := []string{} |
| 112 | for _, s := range list { |
| 113 | if err := ctx.Err(); err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | if !hidden(s) { |
| 117 | kept = append(kept, s) |
| 118 | } |
| 119 | } |
| 120 | return kept, nil |
| 121 | } |