| 1 | package tinyclient |
| 2 | |
| 3 | import "encoding/json" |
| 4 | |
| 5 | // The in-process backend can return typed slices inside a map. Normalize the |
| 6 | // slice before handing it to templates, just as a JSON client would receive it. |
| 7 | func collaborationSlice(value any, field string) []any { |
| 8 | items := valueMap(value)[field] |
| 9 | if rows, ok := items.([]any); ok { |
| 10 | return rows |
| 11 | } |
| 12 | var rows []any |
| 13 | encoded, err := json.Marshal(items) |
| 14 | if err == nil { |
| 15 | _ = json.Unmarshal(encoded, &rows) |
| 16 | } |
| 17 | return rows |
| 18 | } |
| 19 | |
| 20 | func collaborationItems(value any) []any { return collaborationSlice(value, "items" ) } |
| 21 | func collaborationReplies(value any) []any { return collaborationSlice(value, "replies" ) } |
| 22 | |
| 23 | // collaborationProposalView is how an issue, pull request or comment's |
| 24 | // proposal state reads for one viewer, with what the decision buttons need. |
| 25 | // Shown, Decide, State, At and By follow wikiProposalView. |
| 26 | type collaborationProposalView struct { |
| 27 | wikiProposalView |
| 28 | ID, Author, Kind string |
| 29 | } |
| 30 | |
| 31 | // collaborationProposal reads an item's proposal state for the viewer. The |
| 32 | // item is a list or detail item, whose author is under author, or a reply, |
| 33 | // whose author is under pubkey. canApprove is the browse result's |
| 34 | // can_approve flag. |
| 35 | func collaborationProposal(item any, actor string, canApprove any) collaborationProposalView { |
| 36 | m := valueMap(item) |
| 37 | if m == nil || m["proposal" ] != true { |
| 38 | return collaborationProposalView{} |
| 39 | } |
| 40 | author := plainString(m["author" ]) |
| 41 | if author == "" { |
| 42 | author = plainString(m["pubkey" ]) |
| 43 | } |
| 44 | view := collaborationProposalView{ID: plainString(m["id" ]), Author: author, Kind: plainString(m["kind" ])} |
| 45 | view.wikiProposalView = wikiProposal(map[string]any{"proposal" : true, "author" : author, "approval" : m["approval" ], "approval_at" : m["approval_at" ], "approval_by" : m["approval_by" ]}, actor, canApprove) |
| 46 | return view |
| 47 | } |
| 48 | |
| 49 | func collaborationLabels(value any) []string { |
| 50 | var labels []string |
| 51 | encoded, err := json.Marshal(valueMap(value)["labels" ]) |
| 52 | if err == nil { |
| 53 | _ = json.Unmarshal(encoded, &labels) |
| 54 | } |
| 55 | return labels |
| 56 | } |