╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1520 if (std.mem.eql(u8, tail, "resolve")) { 1521 if (!params.only(&.{.theirs})) return null; 1522 return repoIssuesResolveRoute(pair, issue_id, params.values.get(.theirs) orelse ""); 1523 } 1524 return null; 1525 } 1526 if (!params.only(&.{ .start, .label, .search })) return null; 1527 return withEncodedFilters(repoThreadCommentsRoute(.issue, pair, issue_id, params.start() orelse return null), params.values.get(.label) orelse "", params.values.get(.search) orelse ""); 1528 } 1529 if (std.mem.startsWith(u8, tab, patch_seg)) { 1530 const patch_id = tab[patch_seg.len..]; 1531 if (patch_id.len == 0) return null; 1532 if (segments.peek()) |tail| { 1533 if (std.mem.startsWith(u8, tail, comment_seg)) { 1534 _ = segments.next(); 1535 const comment_id = tail[comment_seg.len..]; 1536 if (comment_id.len == 0) return null; 1537 const word = params.scanTail(&segments) catch return null; 1538 if (word) |last| { 1539 if (!params.only(&.{})) return null; 1540 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.patch, pair, patch_id, comment_id); 1541 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.patch, pair, patch_id, comment_id); 1542 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.patch, pair, patch_id, comment_id); 1543 return null; 1544 } 1545 if (!params.only(&.{.start})) return null; 1546 return repoThreadCommentRoute(.patch, pair, patch_id, comment_id, params.start() orelse return null); 1547 } 1548 } 1549 const word = params.scanTail(&segments) catch return null; 1550 if (word) |tail| { 1551 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.patch, pair, patch_id, "") else null; 1552 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.patch, pair, patch_id) else null; 1553 if (std.mem.eql(u8, tail, "publish")) return if (params.only(&.{})) repoPatchPublishRoute(pair, patch_id) else null; 1554 if (std.mem.eql(u8, tail, "merge")) return if (params.only(&.{})) repoPatchMergeRoute(pair, patch_id) else null; 1555 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.patch, pair, patch_id, "") else null; 1556 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.patch, pair, patch_id) else null; 1557 if (std.mem.eql(u8, tail, "resolve")) { 1558 if (!params.only(&.{.theirs})) return null; 1559 return repoPatchesResolveRoute(pair, patch_id, params.values.get(.theirs) orelse ""); 1560 } 1561 return null; 1562 } 1563 if (!params.only(&.{ .start, .label, .search })) return null; 1564 return withEncodedFilters(repoThreadCommentsRoute(.patch, pair, patch_id, params.start() orelse return null), params.values.get(.label) orelse "", params.values.get(.search) orelse ""); 1565 } 1566 if (std.mem.startsWith(u8, tab, discuss_seg)) { 1567 const discussion_id = tab[discuss_seg.len..]; 1568 if (discussion_id.len == 0) return null; 1569 if (segments.peek()) |tail| { 1570 if (std.mem.startsWith(u8, tail, comment_seg)) { 1571 _ = segments.next(); 1572 const comment_id = tail[comment_seg.len..]; 1573 if (comment_id.len == 0) return null; 1574 const word = params.scanTail(&segments) catch return null; 1575 if (word) |last| { 1576 if (!params.only(&.{})) return null; 1577 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.discuss, pair, discussion_id, comment_id); 1578 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.discuss, pair, discussion_id, comment_id); 1579 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.discuss, pair, discussion_id, comment_id); 1580 return null; 1581 } 1582 if (!params.only(&.{.start})) return null; 1583 return repoThreadCommentRoute(.discuss, pair, discussion_id, comment_id, params.start() orelse return null); 1584 } 1585 } 1586 const word = params.scanTail(&segments) catch return null; 1587 if (word) |tail| { 1588 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.discuss, pair, discussion_id, "") else null; 1589 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.discuss, pair, discussion_id) else null; 1590 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.discuss, pair, discussion_id, "") else null; 1591 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.discuss, pair, discussion_id) else null; 1592 return null; 1593 } 1594 if (!params.only(&.{ .start, .label, .search })) return null; 1595 return withEncodedFilters(repoThreadCommentsRoute(.discuss, pair, discussion_id, params.start() orelse return null), params.values.get(.label) orelse "", params.values.get(.search) orelse ""); 1596 } 1597 if (std.mem.eql(u8, tab, "issues")) { 1598 // the word is a list view; a filter with neither is never emitted. 1599 const word = params.scanTail(&segments) catch return null; 1600 const label_value = params.values.get(.label) orelse ""; 1601 const w = word orelse return if (params.only(&.{})) repoIssuesRoute(pair, .open, "", "") else null; 1602 // the conflicts list windows by an issue id, not a number, so it 1603 // reads the start param raw 1604 if (std.mem.eql(u8, w, "conflicts")) { 1605 if (!params.only(&.{.start})) return null; 1606 return repoIssuesConflictsRoute(pair, params.values.get(.start) orelse ""); 1607 } 1608 if (!params.only(&.{ .label, .search })) return null; 1609 const search_value = params.values.get(.search) orelse ""; 1610 if (std.meta.stringToEnum(evt.Issue.Status, w)) |status| return withEncodedSearch(repoIssuesRoute(pair, status, label_value, ""), search_value); 1611 if (std.mem.eql(u8, w, "labels")) return withEncodedSearch(repoThreadLabelsRoute(.issue, pair, label_value), search_value); 1612 if (std.mem.eql(u8, w, "new")) return if (label_value.len == 0 and search_value.len == 0) repoThreadNewRoute(.issue, pair) else null; 1613 return null; 1614 } 1615 if (std.mem.eql(u8, tab, "patches")) { 1616 const word = params.scanTail(&segments) catch return null; 1617 const label_value = params.values.get(.label) orelse ""; 1618 const view = word orelse return if (params.only(&.{})) repoPatchesRoute(pair, .open, "", "") else null; 1619 if (std.mem.eql(u8, view, "conflicts")) { 1620 if (!params.only(&.{.start})) return null; 1621 return repoPatchesConflictsRoute(pair, params.values.get(.start) orelse ""); 1622 } 1623 if (!params.only(&.{ .label, .search })) return null; 1624 const search_value = params.values.get(.search) orelse ""; 1625 if (std.meta.stringToEnum(evt.Patch.StatusKind, view)) |status| return withEncodedSearch(repoPatchesRoute(pair, status, label_value, ""), search_value); 1626 if (std.mem.eql(u8, view, "labels")) return withEncodedSearch(repoThreadLabelsRoute(.patch, pair, label_value), search_value); 1627 if (std.mem.eql(u8, view, "new")) return if (label_value.len == 0 and search_value.len == 0) repoThreadNewRoute(.patch, pair) else null; 1628 if (std.mem.eql(u8, view, "drafts")) return if (label_value.len == 0 and search_value.len == 0) repoPatchesDraftsRoute(pair) else null; 1629 return null; 1630 } 1631 if (std.mem.eql(u8, tab, "discussions")) { 1632 const word = params.scanTail(&segments) catch return null; 1633 const label_value = params.values.get(.label) orelse ""; 1634 const view = word orelse return if (params.only(&.{})) repoDiscussionsRoute(pair, "", "") else null; 1635 if (!params.only(&.{ .label, .search })) return null; 1636 const search_value = params.values.get(.search) orelse ""; 1637 if (std.mem.eql(u8, view, "recent")) return withEncodedSearch(repoDiscussionsRoute(pair, label_value, ""), search_value); 1638 if (std.mem.eql(u8, view, "labels")) return withEncodedSearch(repoThreadLabelsRoute(.discuss, pair, label_value), search_value); 1639 if (std.mem.eql(u8, view, "new")) return if (label_value.len == 0 and search_value.len == 0) repoThreadNewRoute(.discuss, pair) else null; 1640 return null; 1641 } 1642 if (std.mem.eql(u8, tab, "undo")) { 1643 const word = segments.next(); 1644 if (segments.next() != null) return null; 1645 const value = word orelse return repoUndoRoute(pair, null); 1646 if (std.mem.eql(u8, value, "clear")) return repoUndoClearRoute(pair); 1647 return repoUndoRoute(pair, std.fmt.parseInt(u64, value, 10) catch return null); 1648 } 1649 if (std.mem.eql(u8, tab, "events")) { 1650 const word = params.scanTail(&segments) catch return null; 1651 if (!params.only(&.{.moment})) return null; 1652 const view = if (word) |value| std.meta.stringToEnum(EventsView, value) orelse return null else .active; 1653 return repoEventsRoute(pair, view, null, "", params.moment() catch return null); 1654 } 1655 if (std.mem.startsWith(u8, tab, "event:")) { 1656 const event_id = tab["event:".len..]; 1657 const kind_segment = segments.next() orelse return null; 1658 if (!std.mem.startsWith(u8, kind_segment, "kind:")) return null; 1659 const kind = std.meta.stringToEnum(evt.EventKind, kind_segment["kind:".len..]) orelse return null; 1660 if (event_id.len == 0) return null; 1661 params.scanPairs(&segments) catch return null; 1662 if (!params.only(&.{.moment}) or segments.next() != null) return null; 1663 return repoEventsRoute(pair, .active, kind, event_id, params.moment() catch return null); 1664 } 1665 if (std.mem.eql(u8, tab, files_seg)) { 1666 params.scanPairs(&segments) catch return null; 1667 if (params.values.get(.patchrev)) |id| { 1668 if (!params.only(&.{ .line, .patchrev })) return null; 1669 return repoPatchRevFilesRoute(pair, id, pathValue(segments.rest()) orelse return null, params.line() orelse return null); 1670 } 1671 if (!params.only(&.{ .line, .branch, .tag, .object, .find })) return null; 1672 const line = params.line() orelse return null; 1673 const dir = pathValue(segments.rest()) orelse return null; 1674 const find = params.values.get(.find) orelse ""; 1675 const ref = params.ref() catch return null; 1676 // a find route's `path` names the selected result, so unlike a 1677 // directory path it doesn't need a ref beside it 1678 var route = if (ref) |r| 1679 repoFilesRoute(pair, r.kind, r.value, dir, line) orelse return null 1680 else if (dir.len == 0 or find.len != 0) 1681 repoFilesRoute(pair, null, "", "", line) orelse return null 1682 else 1683 return null; 1684 if (find.len != 0) { 1685 route.repo_files.find = Array(find_route_max_len).from(find) orelse return null; 1686 route.repo_files.path = Array(repo_route_max_len).from(dir) orelse return null; 1687 } 1688 return route; 1689 } 1690 if (std.mem.eql(u8, tab, commits_seg)) { 1691 params.scanPairs(&segments) catch return null; 1692 if (params.values.get(.merge)) |oid| { 1693 if (!params.only(&.{.merge}) or segments.next() != null) return null; 1694 return repoCommitsMergeRoute(pair, oid); 1695 } 1696 if (!params.only(&.{ .start, .branch, .tag, .object, .base, .search, .from })) return null; 1697 const base_oid = params.values.get(.base) orelse ""; 1698 const search = params.values.get(.search) orelse ""; 1699 const from = params.values.get(.from) orelse ""; 1700 const start = params.start() orelse return null; 1701 const ref = (params.ref() catch return null) orelse { 1702 if (search.len != 0 or from.len != 0) return null; 1703 const file = pathValue(segments.rest()) orelse return null; 1704 return if (file.len == 0) repoCommitsRoute(pair, null, "", start, "", base_oid) else null; 1705 }; 1706 // the index covers branch and tag tips, so only they can be 1707 // searched or paged by commit; an object roots the list itself 1708 if ((search.len != 0 or from.len != 0) and ref.kind == .object) return null; 1709 // the message page has no diff window or file filter 1710 var route = if (std.mem.eql(u8, segments.rest(), message_seg)) 1711 (if (start == 0) repoCommitMessageRoute(pair, ref.kind, ref.value, base_oid) else null) orelse return null 1712 else 1713 repoCommitsRoute(pair, ref.kind, ref.value, start, pathValue(segments.rest()) orelse return null, base_oid) orelse return null; 1714 if (search.len != 0) route.repo_commits.search = Array(search_route_max_len).from(search) orelse return null; 1715 if (from.len != 0) route.repo_commits.from = Array(ref_route_max_len).from(from) orelse return null; 1716 return route; 1717 } 1718 if (std.mem.eql(u8, tab, "diff")) { 1719 params.scanPairs(&segments) catch return null; 1720 if (params.values.get(.patchrev)) |id| { 1721 if (!params.only(&.{ .start, .patchrev })) return null; 1722 return repoPatchRevDiffRoute(pair, id, params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1723 } 1724 if (!params.only(&.{ .start, .branch, .tag, .object, .base })) return null; 1725 const base_oid = params.values.get(.base) orelse return null; 1726 const start = params.start() orelse return null; 1727 const ref = params.ref() catch return null; 1728 const file = pathValue(segments.rest()) orelse return null; 1729 return repoDiffRoute(pair, if (ref) |r| r.kind else null, if (ref) |r| r.value else "", start, file, base_oid); 1730 } 1731 return null; // unknown sub-path 1732 } 1733 1734 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 1735 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1736 return switch (a) { 1737 .home_users => |a_u| a_u.start == b.home_users.start and std.mem.eql(u8, a_u.search.slice(), b.home_users.search.slice()), 1738 .user_repos => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_repos.name.slice()) and a_u.start == b.user_repos.start and std.mem.eql(u8, a_u.search.slice(), b.user_repos.search.slice()), 1739 .user_forks => |a_u| std.mem.eql(u8, a_u.name.slice(), b.user_forks.name.slice()) and a_u.start == b.user_forks.start, 1740 .user_new_repo => |a_name| std.mem.eql(u8, a_name.slice(), b.user_new_repo.slice()), 1741 .user_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.user_auth.slice()), 1742 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 1743 a_f.ref_kind == b.repo_files.ref_kind and 1744 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 1745 std.mem.eql(u8, a_f.patchrev_id.slice(), b.repo_files.patchrev_id.slice()) and 1746 std.mem.eql(u8, a_f.find.slice(), b.repo_files.find.slice()) and 1747 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 1748 a_f.line == b.repo_files.line, 1749 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 1750 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 1751 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 1752 std.mem.eql(u8, a_c.base_oid.slice(), b.repo_commits.base_oid.slice()) and 1753 std.mem.eql(u8, a_c.search.slice(), b.repo_commits.search.slice()) and 1754 std.mem.eql(u8, a_c.from.slice(), b.repo_commits.from.slice()) and 1755 std.mem.eql(u8, a_c.merge.slice(), b.repo_commits.merge.slice()) and 1756 switch (a_c.content) { 1757 .diff => |a_d| switch (b.repo_commits.content) { 1758 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1759 .message => false, 1760 }, 1761 .message => std.meta.activeTag(b.repo_commits.content) == .message, 1762 }, 1763 .repo_diff => |a_d| std.mem.eql(u8, a_d.name.slice(), b.repo_diff.name.slice()) and 1764 a_d.ref_or_oid == b.repo_diff.ref_or_oid and 1765 std.mem.eql(u8, a_d.value.slice(), b.repo_diff.value.slice()) and 1766 std.mem.eql(u8, a_d.base_oid.slice(), b.repo_diff.base_oid.slice()) and 1767 std.mem.eql(u8, a_d.patchrev_id.slice(), b.repo_diff.patchrev_id.slice()) and 1768 a_d.start == b.repo_diff.start and std.mem.eql(u8, a_d.path.slice(), b.repo_diff.path.slice()), 1769 .repo_refs => |a_r| std.mem.eql(u8, a_r.name.slice(), b.repo_refs.name.slice()) and a_r.kind == b.repo_refs.kind and std.mem.eql(u8, a_r.from.slice(), b.repo_refs.from.slice()) and std.mem.eql(u8, a_r.search.slice(), b.repo_refs.search.slice()), 1770 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 1771 std.mem.eql(u8, a_i.label.slice(), b.repo_issues.label.slice()) and 1772 std.mem.eql(u8, a_i.search.slice(), b.repo_issues.search.slice()) and 1773 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 1774 std.mem.eql(u8, a_i.theirs.slice(), b.repo_issues.theirs.slice()) and 1775 a_i.view == b.repo_issues.view and 1776 a_i.comments_start == b.repo_issues.comments_start and 1777 std.mem.eql(u8, a_i.comment.slice(), b.repo_issues.comment.slice()), 1778 .repo_discussions => |a_t| std.mem.eql(u8, a_t.name.slice(), b.repo_discussions.name.slice()) and 1779 std.mem.eql(u8, a_t.label.slice(), b.repo_discussions.label.slice()) and 1780 std.mem.eql(u8, a_t.search.slice(), b.repo_discussions.search.slice()) and 1781 std.mem.eql(u8, a_t.selected.slice(), b.repo_discussions.selected.slice()) and 1782 a_t.view == b.repo_discussions.view and 1783 a_t.comments_start == b.repo_discussions.comments_start and 1784 std.mem.eql(u8, a_t.comment.slice(), b.repo_discussions.comment.slice()), 1785 .repo_patches => |a_p| std.mem.eql(u8, a_p.name.slice(), b.repo_patches.name.slice()) and 1786 std.mem.eql(u8, a_p.label.slice(), b.repo_patches.label.slice()) and 1787 std.mem.eql(u8, a_p.search.slice(), b.repo_patches.search.slice()) and 1788 std.mem.eql(u8, a_p.selected.slice(), b.repo_patches.selected.slice()) and 1789 std.mem.eql(u8, a_p.theirs.slice(), b.repo_patches.theirs.slice()) and 1790 a_p.view == b.repo_patches.view and 1791 a_p.comments_start == b.repo_patches.comments_start and 1792 std.mem.eql(u8, a_p.comment.slice(), b.repo_patches.comment.slice()), 1793 .repo_undo => |a_u| std.mem.eql(u8, a_u.name.slice(), b.repo_undo.name.slice()) and 1794 a_u.index == b.repo_undo.index and 1795 a_u.clear == b.repo_undo.clear, 1796 .repo_events => |a_e| std.mem.eql(u8, a_e.name.slice(), b.repo_events.name.slice()) and 1797 a_e.view == b.repo_events.view and 1798 a_e.moment == b.repo_events.moment and 1799 a_e.kind == b.repo_events.kind and 1800 std.mem.eql(u8, a_e.selected.slice(), b.repo_events.selected.slice()), 1801 .repo_new_repo => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_new_repo.slice()), 1802 .repo_auth => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_auth.slice()), 1803 .fork_patch => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_patch.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_patch.id.slice()), 1804 .fork_diff => |a_d| std.mem.eql(u8, a_d.fork.name.slice(), b.fork_diff.fork.name.slice()) and 1805 std.mem.eql(u8, a_d.fork.id.slice(), b.fork_diff.fork.id.slice()) and 1806 a_d.start == b.fork_diff.start and std.mem.eql(u8, a_d.path.slice(), b.fork_diff.path.slice()), 1807 .fork_files => |a_f| std.mem.eql(u8, a_f.fork.name.slice(), b.fork_files.fork.name.slice()) and 1808 std.mem.eql(u8, a_f.fork.id.slice(), b.fork_files.fork.id.slice()) and 1809 std.mem.eql(u8, a_f.oid.slice(), b.fork_files.oid.slice()) and 1810 std.mem.eql(u8, a_f.find.slice(), b.fork_files.find.slice()) and 1811 std.mem.eql(u8, a_f.path.slice(), b.fork_files.path.slice()) and 1812 a_f.line == b.fork_files.line, 1813 .fork_commits => |a_c| std.mem.eql(u8, a_c.fork.name.slice(), b.fork_commits.fork.name.slice()) and 1814 std.mem.eql(u8, a_c.fork.id.slice(), b.fork_commits.fork.id.slice()) and 1815 std.mem.eql(u8, a_c.oid.slice(), b.fork_commits.oid.slice()) and 1816 switch (a_c.content) { 1817 .diff => |a_d| switch (b.fork_commits.content) { 1818 .diff => |b_d| a_d.start == b_d.start and std.mem.eql(u8, a_d.path.slice(), b_d.path.slice()), 1819 .message => false, 1820 }, 1821 .message => std.meta.activeTag(b.fork_commits.content) == .message, 1822 }, 1823 .fork_new_repo => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_new_repo.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_new_repo.id.slice()), 1824 .fork_auth => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_auth.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_auth.id.slice()), 1825 else => true, 1826 }; 1827 } 1828 1829 // true when following a link from `b` to `a` (both repo routes) should reload 1830 // rather than stay in page 1831 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 1832 if (a.parent() != .repo or b.parent() != .repo) return false; 1833 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 1834 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 1835 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 1836 return !a.eql(b); 1837 } 1838 1839 pub fn forkPageChanged(a: RoutablePage, b: RoutablePage) bool { 1840 return a.parent() == .fork and b.parent() == .fork and !a.eql(b); 1841 } 1842 1843 // true when `a` and `b` are the same user paginated to a different repos 1844 // window or search. switching between a user's tabs is in-page, so only a 1845 // changed `start` or search on the same list navigates. 1846 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 1847 return switch (a) { 1848 .user_repos => |aa| switch (b) { 1849 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and !a.eql(b), 1850 else => false, 1851 }, 1852 .user_forks => |aa| switch (b) { 1853 .user_forks => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1854 else => false, 1855 }, 1856 else => false, 1857 }; 1858 } 1859 1860 // true when `a` and `b` are the same home list tab paginated to a different 1861 // window or search. switching tabs is in-page, so only a changed `start` or 1862 // search on the same tab navigates. 1863 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 1864 return switch (a) { 1865 .home_users => switch (b) { 1866 .home_users => !a.eql(b), 1867 else => false, 1868 }, 1869 else => false, 1870 }; 1871 } 1872 }; 1873 1874 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 1875 fn isUnreserved(c: u8) bool { 1876 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 1877 } 1878 1879 // percent-encode a ref name for use as a single url path segment. 1880 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 1881 var out: std.Io.Writer.Allocating = .init(aa); 1882 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 1883 return out.written(); 1884 } 1885 1886 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 1887 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1888 return struct { 1889 pub const hex_len = hash.hexLen(repo_opts.hash); 1890 1891 // the concrete ref/oid (a null request resolves to the default branch) 1892 ref_or_oid: RoutablePage.RefOrOid, 1893 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 1894 // the route layer — which splits on '/' — stores them encoded). duped 1895 // into `aa`. 1896 value: []const u8, 1897 // the commit oid it points at 1898 oid: [hex_len]u8, 1899 1900 const Self = @This(); 1901 1902 // resolve a requested ref/oid (null = the repo's default branch) to a 1903 // concrete ref/oid plus the commit oid it points at. `requested_value` is 1904 // the url-encoded form (as it appears in the route). null when it doesn't 1905 // resolve (an unknown branch/tag, or a malformed/unknown oid). 1906 pub fn init( 1907 repo: *rp.Repo(repo_kind, repo_opts), 1908 io: std.Io, 1909 aa: std.mem.Allocator, 1910 requested_ref_or_oid: ?RoutablePage.RefOrOid, 1911 requested_value: []const u8, 1912 ) !?Self { 1913 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 1914 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 1915 var value: []const u8 = if (requested_ref_or_oid == null) 1916 requested_value 1917 else 1918 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 1919 // no ref named: fall back to HEAD's branch (or its oid when detached). 1920 if (requested_ref_or_oid == null) { 1921 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1922 if (repo.head(io, &head_buf)) |head| switch (head) { 1923 .ref => |r| { 1924 ref_or_oid = .branch; 1925 value = r.name; 1926 }, 1927 .oid => |o| { 1928 ref_or_oid = .object; 1929 value = o; 1930 }, 1931 } else |_| {} 1932 } 1933 1934 var oid: [hex_len]u8 = undefined; 1935 switch (ref_or_oid) { 1936 .object => { 1937 if (value.len != hex_len) return null; 1938 @memcpy(&oid, value); 1939 }, 1940 .branch, .tag => { 1941 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 1942 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 1943 }, 1944 } 1945 // store url-encoded so the route layer can hold the value verbatim. 1946 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 1947 } 1948 }; 1949 } 1950 1951 // who a repo page is built for, which picks the actions it shows 1952 pub const Viewer = struct { 1953 role: evt.Repo.Role, 1954 email: []const u8, 1955 1956 // the session's viewer of `repo`, or null when logged out. local mode owns everything. 1957 pub fn init(session: *Session, arena: *std.heap.ArenaAllocator, repo: evt.Repo.Record) !?Viewer { 1958 if (session.data.host_kind == .local) return .{ .role = .owner, .email = "" }; 1959 const user_id = session.userId() orelse return null; 1960 const moment = session.haxy_moment orelse unreachable; 1961 const user = (try activeUser(moment, arena, user_id)) orelse return null; 1962 return .{ .role = evt.Repo.roleOf(repo, user_id), .email = user.event.email }; 1963 } 1964 }; 1965 1966 // a repo a page shows, and what its viewer may do there 1967 pub const RepoHandle = struct { 1968 location: RoutablePage.RepoLocation, 1969 viewer: ?Viewer = null, 1970 1971 pub fn dupe(self: RepoHandle, allocator: std.mem.Allocator) !RepoHandle { 1972 var copy = self; 1973 copy.location = try self.location.dupe(allocator); 1974 return copy; 1975 } 1976 1977 // pushing, and seeing the events and undo tabs. local mode is always owner. 1978 pub fn canWrite(self: RepoHandle) bool { 1979 const viewer = self.viewer orelse return false; 1980 return viewer.role.atLeast(.write); 1981 } 1982 1983 pub fn canUndo(self: RepoHandle) bool { 1984 const viewer = self.viewer orelse return false; 1985 return viewer.role == .owner; 1986 } 1987 }; 1988 1989 // where a repo page reads its on-disk repo from 1990 pub const RepoSource = struct { 1991 path: []const u8, 1992 repo_kind: rp.RepoKind, 1993 // the user's global git config, so local mode picks up their identity and 1994 // their signing and ssh settings. null on the server paths, which must not 1995 // read whoever runs the server. 1996 global_config_path: ?[]const u8 = null, 1997 1998 pub fn localInitOpts(self: RepoSource) rp.InitOpts { 1999 return .{ .path = self.path, .global_config_path = self.global_config_path }; 2000 } 2001 2002 pub fn hasBranch(self: RepoSource, io: std.Io, allocator: std.mem.Allocator, branch: []const u8) !bool { 2003 if (!xit.ref.validateName(branch)) return false; 2004 return switch (self.repo_kind) { 2005 inline else => |repo_kind| blk: { 2006 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 2007 defer any_repo.deinit(io, allocator); 2008 break :blk switch (any_repo) { 2009 inline else => |*repo| (try repo.readRef(io, .{ .kind = .head, .name = branch })) != null, 2010 }; 2011 }, 2012 }; 2013 } 2014 2015 // whether the actor may change the event `id` names 2016 pub fn canModify( 2017 self: RepoSource, 2018 io: std.Io, 2019 allocator: std.mem.Allocator, 2020 actor: Actor, 2021 kind: evt.EventKind, 2022 id: *const [evt.event_id_size]u8, 2023 ) !bool { 2024 // a role that may change an event with no author needs no read 2025 if (actor.role.canModify(actor.author.email, null)) return true; 2026 2027 var arena = std.heap.ArenaAllocator.init(allocator); 2028 defer arena.deinit(); 2029 return switch (self.repo_kind) { 2030 inline else => |repo_kind| blk: { 2031 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 2032 defer any_repo.deinit(io, allocator); 2033 break :blk switch (any_repo) { 2034 inline else => |*repo| actor.role.canModify(actor.author.email, try evt.readAuthorEmail(repo_kind, repo.self_repo_opts, io, allocator, &arena, repo, kind, id)), 2035 }; 2036 }, 2037 }; 2038 } 2039 }; 2040 2041 // who a write acts as and the role they hold in its repo 2042 pub const Actor = struct { 2043 author: evt.CommitAuthor, 2044 // local mode has no accounts, so it has no user id 2045 user_id: ?[evt.event_id_size]u8 = null, 2046 role: evt.Repo.Role, 2047 // the repo owner's id, which local mode lacks 2048 repo_user_id: ?[evt.event_id_size]u8 = null, 2049 }; 2050 2051 pub const Authorization = union(enum) { 2052 actor: Actor, 2053 login_required, 2054 repo_not_found, 2055 forbidden, 2056 }; 2057 2058 // the account `user_id` names, or null once it has been removed. a user id is 2059 // checked here wherever one enters, so the role lookups can trust theirs. 2060 pub fn activeUser( 2061 moment: evt.AdminDB.HashMap(.read_only), 2062 arena: *std.heap.ArenaAllocator, 2063 user_id: [evt.event_id_size]u8, 2064 ) !?evt.User.Record { 2065 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id)) orelse return null; 2066 return if (user.removed) null else user; 2067 } 2068 2069 // whether the user may write to the repo `identity` names with at least 2070 // `min_role` 2071 pub fn authorizeUser( 2072 io: std.Io, 2073 moment: evt.AdminDB.HashMap(.read_only), 2074 arena: *std.heap.ArenaAllocator, 2075 users_dir: []const u8, 2076 user_id: [evt.event_id_size]u8, 2077 identity: []const u8, 2078 min_role: evt.Repo.Role, 2079 ) !Authorization { 2080 const user = (try activeUser(moment, arena, user_id)) orelse return .login_required; 2081 2082 const owner_repo = evt.parseOwnerRepoPath(identity) orelse return .repo_not_found; 2083 const repo = (try evt.readRepoByOwnerAndName(io, arena.child_allocator, arena, moment, users_dir, owner_repo.owner, owner_repo.name)) orelse return .repo_not_found; 2084 const role = evt.Repo.roleOf(repo.repo, user_id); 2085 // a repo the user can't read doesn't exist to them 2086 if (role == .none) return .repo_not_found; 2087 if (!role.atLeast(min_role)) return .forbidden; 2088 return .{ .actor = .{ 2089 .author = .{ .name = user.event.name, .email = user.event.email }, 2090 .user_id = user_id, 2091 .role = role, 2092 .repo_user_id = repo.repo.event.user_id[0..evt.event_id_size].*, 2093 } }; 2094 } 2095 2096 // events created in local mode have no account behind them, so the commit 2097 // author comes from the repo's config, which the global config feeds into. 2098 pub const local_author_fallback = evt.CommitAuthor{ .name = "haxy", .email = "user@haxy" }; 2099 2100 pub fn localAuthor( 2101 src: RepoSource, 2102 io: std.Io, 2103 allocator: std.mem.Allocator, 2104 arena: *std.heap.ArenaAllocator, 2105 ) !evt.CommitAuthor { 2106 switch (src.repo_kind) { 2107 inline else => |repo_kind| { 2108 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()) catch return local_author_fallback; 2109 defer any_repo.deinit(io, allocator); 2110 2111 switch (any_repo) { 2112 inline else => |*repo| { 2113 var config = try repo.listConfig(io, allocator); 2114 defer config.deinit(); 2115 2116 const user = config.sections.get("user") orelse return local_author_fallback; 2117 const name = user.get("name") orelse return local_author_fallback; 2118 const email = user.get("email") orelse return local_author_fallback; 2119 return .{ 2120 .name = try arena.allocator().dupe(u8, name), 2121 .email = try arena.allocator().dupe(u8, email), 2122 }; 2123 }, 2124 } 2125 }, 2126 } 2127 } 2128 2129 // per-connection mutable state. each SSH session / web session / local TUI 2130 // run gets its own. 2131 pub const Session = struct { 2132 data: Data = .{}, 2133 // session-lifetime allocations: login/user_id, persisted prefs, queued 2134 // actions. lives as long as the connection. 2135 arena: *std.heap.ArenaAllocator, 2136 // current page's allocations: page data and the page-scoped strings widgets 2137 // build (form actions, links). owned by Nav, swapped on each navigation, so 2138 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 2139 // where a render is one-shot, this points at the same arena as `arena`. 2140 page_arena: *std.heap.ArenaAllocator, 2141 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 2142 // filesystem io and the path to <server>/repos, for opening on-disk repos 2143 // during page construction. both null on wasm, which has no filesystem and 2144 // rebuilds pages from the serialized snapshot rather than from disk. 2145 io: ?std.Io = null, 2146 admin_repo: ?*rp.Repo(.xit, evt.admin_repo_opts) = null, 2147 users_dir: ?[]const u8 = null, 2148 // the single on-disk repo this session views, when running in local mode 2149 // (haxy invoked with no arguments inside a repo). null on the server paths, 2150 // which resolve repos from the admin db instead. 2151 local: ?RepoSource = null, 2152 pending: std.ArrayList(Action) = .empty, // actions queued by widgets this frame 2153 // focus id -> the live TextInput, refreshed each frame by the views that own 2154 // inputs. web/wasm form handling looks widgets up here by focus id. 2155 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput) = .empty, 2156 back: enum { unavailable, available, requested } = .unavailable, 2157 refresh_requested: bool = false, // set by input (ctrl+r) 2158 // a requested forward navigation. set this (via navigate) to move to a new 2159 // page; Nav.sync builds it and then copies it into current_page, clearing 2160 // this back to null. setting current_page directly only updates the url and 2161 // does not navigate. 2162 next_page: ?RoutablePage = null, 2163 is_terminal: bool = false, // true on remote SSH and local TUI 2164 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 2165 // url. null on the web itself (no footer there). 2166 web_port: ?u16 = null, 2167 // a host operation requested by a widget 2168 host_request: ?HostRequest = null, 2169 quit_requested: bool = false, 2170 2171 pub const HostRequest = union(enum) { 2172 show_copyable_text: []const u8, 2173 sync_events, 2174 undo: RoutablePage.RepoUndoRoute, 2175 }; 2176 2177 pub const FormFeedback = union(enum) { 2178 pub const LoginFailure = enum { unknown_user, wrong_password }; 2179 pub const ThreadFailure = enum { required_title }; 2180 pub const PatchFailure = enum { 2181 required_title, 2182 invalid_target_branch, 2183 invalid_source_branch, 2184 same_branch, 2185 unrelated_branches, 2186 2187 pub fn fromError(err: anyerror) ?PatchFailure { 2188 return switch (err) { 2189 error.InvalidTargetBranch => .invalid_target_branch, 2190 error.InvalidSourceBranch => .invalid_source_branch, 2191 error.SameBranch => .same_branch, 2192 error.UnrelatedBranches => .unrelated_branches, 2193 else => null, 2194 }; 2195 } 2196 }; 2197 2198 pub const RepoFailure = enum { 2199 required_name, 2200 invalid_name, 2201 name_taken, 2202 2203 pub fn fromError(err: anyerror) ?RepoFailure { 2204 return switch (err) { 2205 error.NameEmpty => .required_name, 2206 error.NameTooLong, error.InvalidName => .invalid_name, 2207 error.NameTaken => .name_taken, 2208 else => null, 2209 }; 2210 } 2211 }; 2212 2213 login: struct { 2214 failure: LoginFailure, 2215 username: []const u8, 2216 }, 2217 repo: struct { 2218 failure: RepoFailure, 2219 fields: struct { 2220 name: []const u8, 2221 description: []const u8, 2222 hash: hash.HashKind, 2223 access: evt.Repo.Access, 2224 }, 2225 }, 2226 issue: struct { 2227 failure: ThreadFailure, 2228 fields: ?struct { 2229 title: []const u8, 2230 labels: []const u8, 2231 description: []const u8, 2232 } = null, 2233 }, 2234 patch: struct { 2235 failure: PatchFailure, 2236 fields: ?struct { 2237 title: []const u8, 2238 labels: []const u8, 2239 description: []const u8, 2240 target_branch: []const u8, 2241 source_branch: []const u8 = "", 2242 source_kind: Repo.Patches.Source.Kind = .fork, 2243 } = null, 2244 }, 2245 discussion: struct { 2246 failure: ThreadFailure, 2247 fields: ?struct { 2248 title: []const u8, 2249 labels: []const u8, 2250 description: []const u8, 2251 } = null, 2252 }, 2253 }; 2254 2255 const Self = @This(); 2256 2257 // serializable data sent down to web client 2258 pub const Data = struct { 2259 user_id: ?[]const u8 = null, 2260 // the logged-in user's name, for the views that show it 2261 user_name: ?[]const u8 = null, 2262 // a failed form retained long enough to render it for correction 2263 form_feedback: ?FormFeedback = null, 2264 // a transient local event-sync error 2265 sync_failure: ?[]const u8 = null, 2266 // a failed terminal undo retained on the current tab 2267 undo_failure: ?[]const u8 = null, 2268 current_page: RoutablePage = .default, 2269 // whether to render the ANSI art backdrop 2270 enable_ansi: bool = true, 2271 // the selected ANSI art. the server serializes one image for WASM so 2272 // the browser does not need the whole build-generated collection. 2273 ansi_art: []const u8 = "", 2274 // the bound git-service ports. absent in local mode, where remote urls 2275 // are not shown. 2276 git_http_port: ?u16 = null, 2277 git_ssh_port: ?u16 = null, 2278 git_ssh_prefix: []const u8 = "", 2279 // whether this session views a local repo or the server. unlike `local` 2280 // (the host-side filesystem source) this travels in the snapshot, so 2281 // the wasm side also parses elided link urls and hides the multi-user 2282 // chrome. 2283 host_kind: evt.HostKind = .server, 2284 }; 2285 2286 // a user-initiated state change. widgets enqueue these on the session during 2287 // input instead of mutating state or touching the DB themselves; the host 2288 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 2289 // side effects out of the widget tree and gives every render path one place to 2290 // turn a UI action into a state change + event. 2291 pub const Action = union(enum) { 2292 toggle_ansi, 2293 }; 2294 2295 pub fn init( 2296 arena: *std.heap.ArenaAllocator, 2297 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2298 data: Data, 2299 ) !Self { 2300 var session = Self{ 2301 .data = data, 2302 .arena = arena, 2303 // until a host swaps in a page-scoped arena (Nav does this), page 2304 // allocations land in the session arena. on the web path that's the 2305 // intended behavior, since the whole arena is per-request. 2306 .page_arena = arena, 2307 .admin_repo = repo, 2308 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 2309 }; 2310 try session.loadUser(); 2311 return session; 2312 } 2313 2314 // load the logged-in user's name and persisted preferences from the db 2315 pub fn loadUser(self: *Self) !void { 2316 const user_id = self.userId() orelse return; 2317 const moment = self.haxy_moment orelse return; 2318 const user = (try activeUser(moment, self.arena, user_id)) orelse return self.logOut(); 2319 self.data.user_name = user.event.name; 2320 self.data.enable_ansi = user.event.enable_ansi; 2321 } 2322 2323 // forget the logged-in user, on a logout or once their account is removed 2324 pub fn logOut(self: *Self) void { 2325 self.data.user_id = null; 2326 self.data.user_name = null; 2327 } 2328 2329 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 2330 const bytes = self.data.user_id orelse return null; 2331 if (bytes.len != evt.event_id_size) return null; 2332 var id: [evt.event_id_size]u8 = undefined; 2333 @memcpy(&id, bytes); 2334 return id; 2335 } 2336 2337 pub fn formFeedback(self: *const Self, comptime tag: std.meta.Tag(FormFeedback)) ?@FieldType(FormFeedback, @tagName(tag)) { 2338 const feedback = self.data.form_feedback orelse return null; 2339 if (std.meta.activeTag(feedback) != tag) return null; 2340 return @field(feedback, @tagName(tag)); 2341 } 2342 2343 pub fn clearFormFeedback(self: *Self) void { 2344 self.data.form_feedback = null; 2345 } 2346 2347 // the actor for a write to the repo `identity` names, or null when the 2348 // session may not write there with at least `min_role`. local mode always 2349 // writes. 2350 pub fn authorize(self: *Self, identity: []const u8, min_role: evt.Repo.Role) !?Actor { 2351 if (self.data.host_kind == .local) { 2352 const author = blk: { 2353 const src = self.local orelse break :blk local_author_fallback; 2354 const io = self.io orelse break :blk local_author_fallback; 2355 break :blk try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 2356 }; 2357 return .{ .author = author, .role = .owner }; 2358 } 2359 const user_id = self.userId() orelse return null; 2360 // the session's moment predates this input, so read the roles afresh 2361 const admin_repo = self.admin_repo orelse return null; 2362 const io = self.io orelse return null; 2363 const users_dir = self.users_dir orelse return null; 2364 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2365 return switch (try authorizeUser(io, moment, self.page_arena, users_dir, user_id, identity, min_role)) { 2366 .actor => |actor| actor, 2367 .login_required, .repo_not_found, .forbidden => null, 2368 }; 2369 } 2370 2371 // where this session's event writes go. a server session always has a users dir 2372 pub fn eventHost(self: *const Self) evt.Host { 2373 return switch (self.data.host_kind) { 2374 .local => .local, 2375 .server => .{ .server = .{ .users_dir = self.users_dir orelse unreachable } }, 2376 }; 2377 } 2378 2379 // queue an action for the host to drain this frame. 2380 pub fn push(self: *Self, action: Action) !void { 2381 try self.pending.append(self.arena.allocator(), action); 2382 } 2383 2384 // apply a single action's in-memory effect only (no persistence). 2385 fn apply(self: *Self, action: Action) void { 2386 switch (action) { 2387 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 2388 } 2389 } 2390 2391 // drain the session's queued actions by applying them to the session. 2392 // used on the wasm path, which has no repo to persist to. 2393 pub fn applyPending(self: *Self) void { 2394 for (self.pending.items) |action| self.apply(action); 2395 self.pending.clearRetainingCapacity(); 2396 } 2397 2398 // drain the session's queued actions by applying them to the session and 2399 // writing them to the db 2400 pub fn applyAndWritePending( 2401 self: *Self, 2402 io: std.Io, 2403 allocator: std.mem.Allocator, 2404 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2405 ) !void { 2406 defer self.pending.clearRetainingCapacity(); 2407 for (self.pending.items) |action| { 2408 self.apply(action); 2409 switch (action) { 2410 .toggle_ansi => if (self.data.user_id) |user_id| { 2411 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, self.users_dir orelse unreachable, user_id); 2412 }, 2413 } 2414 } 2415 } 2416 2417 // reload the moment from the admin repo and re-read user preferences 2418 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 2419 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 2420 self.haxy_moment = moment; 2421 2422 const user_id = self.userId() orelse return; 2423 var arena = std.heap.ArenaAllocator.init(allocator); 2424 defer arena.deinit(); 2425 const user = (try activeUser(moment, &arena, user_id)) orelse return self.logOut(); 2426 self.data.enable_ansi = user.event.enable_ansi; 2427 } 2428 2429 // request a forward navigation to `route`; the host consumes next_page 2430 // (Nav.sync on the terminal, the wasm tick on the web). 2431 pub fn navigate(self: *Session, route: RoutablePage) !void { 2432 self.clearFormFeedback(); 2433 self.next_page = route; 2434 } 2435 }; 2436 2437 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2438 var nav = try Nav.init(allocator, session); 2439 defer nav.deinit(allocator); 2440 2441 var terminal = try term.Terminal.init(io, allocator); 2442 var terminal_live = true; 2443 defer if (terminal_live) terminal.deinit(io); 2444 2445 // set term as active so it will be properly cooked 2446 // when a panic/segfault happens 2447 term.setActive(&terminal); 2448 defer term.setActive(null); 2449 2450 while (!terminal.shouldQuit()) { 2451 const grid_changed = try terminal.render(&nav.root); 2452 2453 // process any inputs. 2454 // 2455 // if the grid didn't change, then first do a blocking 2456 // read, so the thread will sleep until further input. 2457 // after that, all remaining reads are non-blocking so 2458 // we can process the rest of the queued inputs. 2459 // 2460 // if the grid *did* change, then only do non-blocking 2461 // reads. we do not want to sleep the thread because 2462 // there may be an animation that requires more looping. 2463 var blocking = !grid_changed; 2464 while (try terminal.readKey(io, blocking)) |key| { 2465 blocking = false; 2466 try inputKey(allocator, &nav.root, key, session); 2467 } 2468 2469 if (session.host_request) |request| { 2470 session.host_request = null; 2471 switch (request) { 2472 .show_copyable_text => |copyable_text| { 2473 term.setActive(null); 2474 terminal.deinit(io); 2475 terminal_live = false; 2476 showCopyableText(io, copyable_text) catch |show_err| { 2477 terminal = try term.Terminal.init(io, allocator); 2478 terminal_live = true; 2479 term.setActive(&terminal); 2480 return show_err; 2481 }; 2482 terminal = try term.Terminal.init(io, allocator); 2483 terminal_live = true; 2484 term.setActive(&terminal); 2485 }, 2486 .undo => |target| Repo.Undo.handleRequest(allocator, session, target), 2487 .sync_events => { 2488 try nav.root.build(allocator, .{ 2489 .min_size = .{ .width = null, .height = null }, 2490 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2491 }, nav.root.getFocus()); 2492 _ = try terminal.render(&nav.root); 2493 try Repo.Events.performSync(allocator, session); 2494 }, 2495 } 2496 } 2497 2498 // persist queued actions when there's a repo; local mode has none, 2499 // so just apply in-memory 2500 if (repo_maybe) |repo| { 2501 try session.applyAndWritePending(io, allocator, repo); 2502 // reload so the next navigation builds its page from the current moment 2503 try session.reloadMoment(allocator, repo); 2504 } else { 2505 session.applyPending(); 2506 } 2507 2508 // reconcile navigation: forward to a new page, or back on escape. 2509 try nav.sync(allocator, session); 2510 2511 // the quit button (on the quit tab) asks the host to tear down. 2512 if (session.quit_requested) terminal.requestQuit(); 2513 2514 try nav.root.build(allocator, .{ 2515 .min_size = .{ .width = null, .height = null }, 2516 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2517 }, nav.root.getFocus()); 2518 } 2519 } 2520 2521 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2522 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2523 null 2524 else 2525 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2526 defer if (tty) |file| file.close(io); 2527 2528 const input = if (tty) |file| file else std.Io.File.stdin(); 2529 const output = if (tty) |file| file else std.Io.File.stdout(); 2530 2531 var write_buf: [1024]u8 = undefined; 2532 var writer = output.writer(io, &write_buf); 2533 try writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 2534 try writer.interface.flush(); 2535 2536 var read_buf: [1]u8 = undefined; 2537 var reader = input.reader(io, &read_buf); 2538 while (true) { 2539 const byte = try reader.interface.takeByte(); 2540 if (byte == '\r' or byte == '\n') return; 2541 } 2542 } 2543 2544 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2545 const root_focus = root.getFocus(); 2546 const focused_before = root_focus.grandchild_id; 2547 var activate_in_page_link = false; 2548 switch (key) { 2549 // request a navigation pop; the host's Nav.sync goes back a page, or 2550 // quits when there's no history left. 2551 .escape => { 2552 session.back = .requested; 2553 return; 2554 }, 2555 .ctrl => |letter| switch (letter) { 2556 'r' => { 2557 session.refresh_requested = true; 2558 return; 2559 }, 2560 else => try root.input(allocator, key, root_focus), 2561 }, 2562 .enter => { 2563 if (root_focus.grandchild_id) |gid| { 2564 // follow a cross-page link 2565 if (crossPageLink(root_focus, gid, session.data)) |route| { 2566 return session.navigate(route); 2567 } 2568 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2569 } 2570 activate_in_page_link = true; 2571 try root.input(allocator, key, root_focus); 2572 }, 2573 .mouse => |mouse| { 2574 if (mouse.action == .press and mouse.action.press == .left) { 2575 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2576 const focus_id = hit.id; 2577 if (hit.kind == .background) { 2578 root_focus.setFocus(focus_id); 2579 return; 2580 } 2581 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2582 if (session.back == .available) session.back = .requested; 2583 return; 2584 } 2585 // follow a cross-page link 2586 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2587 return session.navigate(route); 2588 } 2589 if (rawLink(root_focus, focus_id)) |url| { 2590 if (!mouse.ctrl) try requestRawLink(session, url); 2591 return; 2592 } 2593 root_focus.setFocus(focus_id); 2594 activate_in_page_link = true; 2595 } 2596 // forward the press into the widget tree so buttons (and any 2597 // future click-aware widgets) can react. widgets that don't 2598 // care about presses ignore it. 2599 try root.input(allocator, key, root_focus); 2600 } else { 2601 try root.input(allocator, key, root_focus); 2602 } 2603 }, 2604 else => try root.input(allocator, key, root_focus), 2605 } 2606 2607 const focused_after = root_focus.grandchild_id; 2608 if (!activate_in_page_link and focused_before == focused_after) return; 2609 const focus_id = focused_after orelse return; 2610 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2611 } 2612 2613 // a link to bytes the server serves directly rather than to a page route, so 2614 // it never resolves to a RoutablePage 2615 pub const raw_link_prefix = "ax:"; 2616 2617 // a route already loaded into the current page; focusing it changes the url 2618 // without rebuilding the page 2619 pub const in_page_link_prefix = "ai:"; 2620 2621 // an in-page tab's base route, or the exact current route when it is selected 2622 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2623 const target = if (selected) session.data.current_page else route; 2624 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2625 } 2626 2627 // a button the web renderer covers with a file picker that posts the chosen 2628 // file to the url after the prefix 2629 pub const file_input_prefix = "file:"; 2630 2631 // a submit button that overrides its enclosing form's action 2632 pub const submit_action_prefix = "submit:"; 2633 2634 // one option of a radio field, as `radio:<name>=<value>` 2635 pub const radio_prefix = "radio:"; 2636 2637 // the url an `ax:` link points at, or null for anything else. a host that 2638 // suppresses the anchor's own navigation needs this to follow the link. 2639 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2640 const child = root_focus.children.get(focus_id) orelse return null; 2641 const custom = switch (child.focus.kind) { 2642 .custom => |c| c, 2643 else => return null, 2644 }; 2645 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2646 return custom[raw_link_prefix.len..]; 2647 } 2648 2649 fn requestRawLink(session: *Session, url: []const u8) !void { 2650 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2651 } 2652 2653 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2654 // an absolute url is already complete 2655 if (!std.mem.startsWith(u8, path, "/")) return path; 2656 return if (session.web_port) |port| 2657 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2658 else 2659 path; 2660 } 2661 2662 // if the focus target at focus_id is an `a:` link that should navigate (a 2663 // different parent page, or a different files directory within the repo page), 2664 // return its route; otherwise null. lets a host turn a click / enter on such a 2665 // link into a navigation rather than a focus change. 2666 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2667 const current = data.current_page; 2668 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2669 // a link to a different parent page always navigates; within a page, a 2670 // files-directory / commits-page / list-window change navigates while tab 2671 // links stay in-page. 2672 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.forkPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 2673 return null; 2674 } 2675 2676 // the route carried by an `ai:` focus target, or null for any other target 2677 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2678 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2679 } 2680 2681 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2682 const child = root_focus.children.get(focus_id) orelse return null; 2683 const custom = switch (child.focus.kind) { 2684 .custom => |c| c, 2685 else => return null, 2686 }; 2687 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2688 const path = custom[prefix.len..]; 2689 // local sessions build (and parse) links with the repo identity elided 2690 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2691 } 2692 2693 // a display author, resolved against the admin db at read time 2694 pub const Author = union(enum) { 2695 unknown, 2696 email: []const u8, // an email no user matches 2697 user_name: []const u8, 2698 2699 // the commit author line's email, resolved like initFromEmail 2700 pub fn init( 2701 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2702 arena: *std.heap.ArenaAllocator, 2703 author_line: []const u8, 2704 ) !Author { 2705 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2706 } 2707 2708 // from a record's stored author email: it resolves to the user currently 2709 // holding it, else stays the email. the result is allocated in `arena`, 2710 // so the email may borrow transient memory. 2711 pub fn initFromEmail( 2712 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2713 arena: *std.heap.ArenaAllocator, 2714 author_email: ?[]const u8, 2715 ) !Author { 2716 const email = author_email orelse return .unknown; 2717 if (haxy_moment) |moment| { 2718 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2719 return .{ .user_name = user.name }; 2720 } 2721 } 2722 return .{ .email = try arena.allocator().dupe(u8, email) }; 2723 } 2724 }; 2725 2726 // the "a:" link to user `name`'s page. 2727 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2728 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2729 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2730 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2731 } 2732 2733 // a focusable " author " box showing `author`, linking to their user page 2734 // when it is a known user 2735 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2736 const text = switch (author) { 2737 .unknown => "", 2738 .email, .user_name => |t| t, 2739 }; 2740 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = " author " }); 2741 errdefer tb.deinit(allocator); 2742 tb.getFocus().mode = .all; 2743 switch (author) { 2744 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2745 else => {}, 2746 } 2747 return tb; 2748 } 2749 2750 // native-TUI navigation 2751 pub const Nav = struct { 2752 root: Widget, 2753 // backs the current page (its Page data and the page-scoped strings its 2754 // widgets build). owned here, swapped on each navigation. session.page_arena 2755 // tracks whichever of these is current so widgets allocate into it. 2756 arena: *std.heap.ArenaAllocator, 2757 // the login the current root was built for 2758 user_id: ?[evt.event_id_size]u8, 2759 history: std.ArrayList(Entry), 2760 2761 // each retained page keeps its own arena, freed when the page leaves the 2762 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2763 // long-lived session from accumulating every page it ever visited. 2764 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2765 2766 // cap on retained back-history so the chain can't grow memory without bound 2767 const max_history: usize = 16; 2768 2769 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2770 session.back = .unavailable; 2771 const arena = try allocator.create(std.heap.ArenaAllocator); 2772 arena.* = std.heap.ArenaAllocator.init(allocator); 2773 errdefer { 2774 arena.deinit(); 2775 allocator.destroy(arena); 2776 } 2777 2778 return .{ 2779 .root = try initCurrentRoot(allocator, arena, session), 2780 .arena = arena, 2781 .user_id = session.userId(), 2782 .history = .empty, 2783 }; 2784 } 2785 2786 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2787 self.root.deinit(allocator); 2788 freeArena(allocator, self.arena); 2789 for (self.history.items) |*entry| { 2790 entry.root.deinit(allocator); 2791 freeArena(allocator, entry.arena); 2792 } 2793 self.history.deinit(allocator); 2794 } 2795 2796 // rebuild the current page in place from a current moment 2797 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2798 if (session.haxy_moment == null and session.local == null) return; 2799 const arena = try allocator.create(std.heap.ArenaAllocator); 2800 arena.* = std.heap.ArenaAllocator.init(allocator); 2801 errdefer freeArena(allocator, arena); 2802 2803 const new_root = try initCurrentRoot(allocator, arena, session); 2804 2805 self.root.deinit(allocator); 2806 freeArena(allocator, self.arena); 2807 self.root = new_root; 2808 self.arena = arena; 2809 self.user_id = session.userId(); 2810 } 2811 2812 // build the current page's root in `arena`, or the not-found page's when 2813 // the route names nothing 2814 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 2815 session.page_arena = arena; 2816 const page = try arena.allocator().create(Page); 2817 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 2818 error.NotFound => blk: { 2819 session.data.current_page = .not_found; 2820 break :blk .not_found; 2821 }, 2822 else => |e| return e, 2823 }; 2824 return initRoot(allocator, page, session); 2825 } 2826 2827 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2828 arena.deinit(); 2829 allocator.destroy(arena); 2830 } 2831 2832 // reconcile the displayed root with the session's navigation state. a 2833 // forward nav (current_page moved to a different parent page) pushes the 2834 // current root and builds the new page in a fresh arena; a back request 2835 // frees the current page and restores the previous one. when escape is 2836 // pressed with no history left we switch to the quit tab instead of quitting. 2837 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2838 // refresh: rebuild the current page in place from a current moment 2839 if (session.refresh_requested) { 2840 session.refresh_requested = false; 2841 try self.rebuild(allocator, session); 2842 return; 2843 } 2844 2845 if (session.back == .requested) { 2846 if (self.history.pop()) |entry| { 2847 session.back = if (self.history.items.len == 0) .unavailable else .available; 2848 self.root.deinit(allocator); 2849 freeArena(allocator, self.arena); 2850 self.root = entry.root; 2851 self.arena = entry.arena; 2852 self.user_id = entry.user_id; 2853 session.page_arena = entry.arena; 2854 session.data.current_page = entry.route; 2855 // a page built under another login is rebuilt rather than restored 2856 if (!std.meta.eql(entry.user_id, session.userId())) { 2857 try self.rebuild(allocator, session); 2858 return; 2859 } 2860 chooseAnsiArtForNavigation(session); 2861 return; 2862 } 2863 session.back = .unavailable; 2864 // nothing to go back to; switch to the quit confirmation 2865 const root_focus = self.root.getFocus(); 2866 var iter = root_focus.children.iterator(); 2867 while (iter.next()) |entry| { 2868 switch (entry.value_ptr.focus.kind) { 2869 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2870 root_focus.setFocus(entry.key_ptr.*); 2871 // a Stack only builds its selected child, so the quit 2872 // button isn't in the focus tree until a build runs with 2873 // the quit tab selected. build once, then send a 2874 // synthetic arrow_down to drop focus from the tab onto 2875 // the button so it's ready to confirm. 2876 try self.root.build(allocator, .{ 2877 .min_size = .{ .width = null, .height = 40 }, 2878 .max_size = .{ .width = 80, .height = null }, 2879 }, root_focus); 2880 try self.root.input(allocator, .arrow_down, root_focus); 2881 break; 2882 }, 2883 else => {}, 2884 } 2885 } 2886 return; 2887 } 2888 2889 // forward navigation: navigate() set next_page to the page to move to (a 2890 // cross-page link or tab change crossing pages) 2891 if (session.next_page) |route| { 2892 session.next_page = null; 2893 if (session.haxy_moment == null and session.local == null) return; 2894 const previous_route = session.data.current_page; 2895 // the page we navigated to becomes the current page 2896 session.data.current_page = route; 2897 session.back = .available; 2898 2899 const arena = try allocator.create(std.heap.ArenaAllocator); 2900 arena.* = std.heap.ArenaAllocator.init(allocator); 2901 errdefer freeArena(allocator, arena); 2902 2903 const new_root = try initCurrentRoot(allocator, arena, session); 2904 2905 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 2906 // drop the oldest entry (freeing its widget tree and arena) once over cap 2907 if (self.history.items.len > max_history) { 2908 var oldest = self.history.orderedRemove(0); 2909 oldest.root.deinit(allocator); 2910 freeArena(allocator, oldest.arena); 2911 } 2912 self.root = new_root; 2913 self.arena = arena; 2914 self.user_id = session.userId(); 2915 } 2916 } 2917 }; 2918 2919 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2920 const page_widget: Widget = switch (page.*) { 2921 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2922 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2923 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2924 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2925 .not_found => .{ .not_found = try .init(allocator, session) }, 2926 }; 2927 2928 chooseAnsiArtForNavigation(session); 2929 2930 // on the TUI/SSH, the page sits above a one-row footer showing its web 2931 // url, when there's a web UI to point at 2932 var root = if (session.is_terminal) blk: { 2933 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2934 errdefer box.deinit(allocator); 2935 const bg_id = bg_blk: { 2936 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2937 errdefer background.deinit(allocator); 2938 const id = background.getFocus().id; 2939 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2940 break :bg_blk id; 2941 }; 2942 if (session.web_port != null) { 2943 var footer = try widget.Footer.init(allocator, session); 2944 errdefer footer.deinit(allocator); 2945 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2946 } 2947 box.getFocus().child_id = bg_id; 2948 break :blk Widget{ .box = box }; 2949 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2950 errdefer root.deinit(allocator); 2951 2952 // input-owning views build their TextInputs in init, so reset their 2953 // registrations here 2954 session.text_inputs.clearRetainingCapacity(); 2955 2956 try root.build(allocator, .{ 2957 .min_size = .{ .width = null, .height = 40 }, 2958 .max_size = .{ .width = 100, .height = null }, 2959 }, root.getFocus()); 2960 2961 return root; 2962 } 2963 2964 // native and server-side sessions choose from the embedded collection. WASM 2965 // keeps the art serialized by the server instead. 2966 fn chooseAnsiArtForNavigation(session: *Session) void { 2967 if (comptime ansi_art.art.len == 0) return; 2968 if (session.data.current_page == .not_found) { 2969 session.data.ansi_art = ansi_art.not_found; 2970 return; 2971 } 2972 const io = session.io orelse return; 2973 var random: usize = undefined; 2974 io.random(std.mem.asBytes(&random)); 2975 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 2976 } 2977