╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
848 } 849 850 // build the default list route for a thread event kind. 851 pub fn repoThreadRoute(kind: evt.EventKind, identity: []const u8, label: []const u8, selected: []const u8) ?RoutablePage { 852 return switch (kind) { 853 .issue => repoIssuesRoute(identity, .open, label, selected), 854 .patch => repoPatchesRoute(identity, .open, label, selected), 855 .discuss => repoDiscussionsRoute(identity, label, selected), 856 else => null, 857 }; 858 } 859 860 // build a selected thread route at one of its comment windows. 861 pub fn repoThreadCommentsRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8, start: usize) ?RoutablePage { 862 var route = repoThreadRoute(kind, identity, "", selected) orelse return null; 863 switch (route) { 864 .repo_issues => |*thread| thread.comments_start = start, 865 .repo_patches => |*thread| thread.comments_start = start, 866 .repo_discussions => |*thread| thread.comments_start = start, 867 else => unreachable, 868 } 869 return route; 870 } 871 872 // build a comment permalink route at one of its immediate-reply windows. 873 pub fn repoThreadCommentRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8, start: usize) ?RoutablePage { 874 if (thread_id.len == 0 or comment_id.len == 0) return null; 875 var route = repoThreadCommentsRoute(kind, identity, thread_id, start) orelse return null; 876 const comment = Array(evt.event_id_size * 2).from(comment_id) orelse return null; 877 switch (route) { 878 .repo_issues => |*thread| thread.comment = comment, 879 .repo_patches => |*thread| thread.comment = comment, 880 .repo_discussions => |*thread| thread.comment = comment, 881 else => unreachable, 882 } 883 return route; 884 } 885 886 // build the form route for replying to a thread or one of its comments. 887 pub fn repoThreadCommentNewRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, parent_id: []const u8) ?RoutablePage { 888 if (thread_id.len == 0) return null; 889 var route = repoThreadCommentsRoute(kind, identity, thread_id, 0) orelse return null; 890 const parent_event_id = Array(evt.event_id_size * 2).from(parent_id) orelse return null; 891 switch (route) { 892 .repo_issues => |*thread| { 893 thread.comment = parent_event_id; 894 thread.view = .new_comment; 895 }, 896 .repo_patches => |*thread| { 897 thread.comment = parent_event_id; 898 thread.view = .new_comment; 899 }, 900 .repo_discussions => |*thread| { 901 thread.comment = parent_event_id; 902 thread.view = .new_comment; 903 }, 904 else => unreachable, 905 } 906 return route; 907 } 908 909 // build the form route for editing a comment. 910 pub fn repoThreadCommentEditRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?RoutablePage { 911 var route = repoThreadCommentRoute(kind, identity, thread_id, comment_id, 0) orelse return null; 912 switch (route) { 913 .repo_issues => |*thread| thread.view = .edit_comment, 914 .repo_patches => |*thread| thread.view = .edit_comment, 915 .repo_discussions => |*thread| thread.view = .edit_comment, 916 else => unreachable, 917 } 918 return route; 919 } 920 921 // build the confirmation route for removing a thread or comment. 922 pub fn repoThreadRemoveRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?RoutablePage { 923 var route = if (comment_id.len == 0) 924 repoThreadCommentsRoute(kind, identity, thread_id, 0) orelse return null 925 else 926 repoThreadCommentRoute(kind, identity, thread_id, comment_id, 0) orelse return null; 927 switch (route) { 928 .repo_issues => |*thread| thread.view = .remove, 929 .repo_patches => |*thread| thread.view = .remove, 930 .repo_discussions => |*thread| thread.view = .remove, 931 else => unreachable, 932 } 933 return route; 934 } 935 936 // build a thread labels route, keeping its url-encoded label filter. 937 pub fn repoThreadLabelsRoute(kind: evt.EventKind, identity: []const u8, label: []const u8) ?RoutablePage { 938 var route = repoThreadRoute(kind, identity, label, "") orelse return null; 939 switch (route) { 940 .repo_issues => |*thread| thread.view = .labels, 941 .repo_patches => |*thread| thread.view = .labels, 942 .repo_discussions => |*thread| thread.view = .labels, 943 else => unreachable, 944 } 945 return route; 946 } 947 948 // build a new-thread form route. 949 pub fn repoThreadNewRoute(kind: evt.EventKind, identity: []const u8) ?RoutablePage { 950 var route = repoThreadRoute(kind, identity, "", "") orelse return null; 951 switch (route) { 952 .repo_issues => |*thread| thread.view = .new, 953 .repo_patches => |*thread| thread.view = .new, 954 .repo_discussions => |*thread| thread.view = .new, 955 else => unreachable, 956 } 957 return route; 958 } 959 960 // build the edit form route for a thread. 961 pub fn repoThreadEditRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8) ?RoutablePage { 962 if (selected.len == 0) return null; 963 var route = repoThreadRoute(kind, identity, "", selected) orelse return null; 964 switch (route) { 965 .repo_issues => |*thread| thread.view = .edit, 966 .repo_patches => |*thread| thread.view = .edit, 967 .repo_discussions => |*thread| thread.view = .edit, 968 else => unreachable, 969 } 970 return route; 971 } 972 973 // build a route showing a thread's whole description. 974 pub fn repoThreadDescriptionRoute(kind: evt.EventKind, identity: []const u8, selected: []const u8) ?RoutablePage { 975 if (selected.len == 0) return null; 976 var route = repoThreadRoute(kind, identity, "", selected) orelse return null; 977 switch (route) { 978 .repo_issues => |*thread| thread.view = .description, 979 .repo_patches => |*thread| thread.view = .description, 980 .repo_discussions => |*thread| thread.view = .description, 981 else => unreachable, 982 } 983 return route; 984 } 985 986 // build a `.repo_issues` route showing the conflicts list, rooted at the 987 // issue with hex event id `start` ("" = the first window). 988 pub fn repoIssuesConflictsRoute(identity: []const u8, start: []const u8) ?RoutablePage { 989 return .{ .repo_issues = .{ 990 .name = Array(repo_route_max_len).from(identity) orelse return null, 991 .selected = Array(evt.event_id_size * 2).from(start) orelse return null, 992 .view = .conflicts, 993 } }; 994 } 995 996 // build a `.repo_issues` route showing the conflict resolve form for the 997 // issue with hex event id `selected`; `theirs` is the comma-separated list 998 // of fields prefilled from their side. 999 pub fn repoIssuesResolveRoute(identity: []const u8, selected: []const u8, theirs: []const u8) ?RoutablePage { 1000 if (selected.len == 0) return null; 1001 return .{ .repo_issues = .{ 1002 .name = Array(repo_route_max_len).from(identity) orelse return null, 1003 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 1004 .theirs = Array(theirs_route_max_len).from(theirs) orelse return null, 1005 .view = .resolve, 1006 } }; 1007 } 1008 1009 pub fn repoPatchesConflictsRoute(identity: []const u8, start: []const u8) ?RoutablePage { 1010 return .{ .repo_patches = .{ 1011 .name = Array(repo_route_max_len).from(identity) orelse return null, 1012 .selected = Array(evt.event_id_size * 2).from(start) orelse return null, 1013 .view = .conflicts, 1014 } }; 1015 } 1016 1017 pub fn repoPatchesResolveRoute(identity: []const u8, selected: []const u8, theirs: []const u8) ?RoutablePage { 1018 if (selected.len == 0) return null; 1019 return .{ .repo_patches = .{ 1020 .name = Array(repo_route_max_len).from(identity) orelse return null, 1021 .selected = Array(evt.event_id_size * 2).from(selected) orelse return null, 1022 .theirs = Array(theirs_route_max_len).from(theirs) orelse return null, 1023 .view = .resolve, 1024 } }; 1025 } 1026 1027 // an inline, owned array of data. keeping it in the route (rather than a 1028 // borrowed slice) makes RoutablePage a plain value: it can be copied, stored 1029 // in history, and serialized without any arena tracking. 1030 pub fn Array(comptime max_len: usize) type { 1031 return struct { 1032 bytes: [max_len]u8 = undefined, 1033 len: u16 = 0, 1034 1035 pub fn from(s: []const u8) ?Array(max_len) { 1036 if (s.len > max_len) return null; 1037 var name = Array(max_len){ .len = @intCast(s.len) }; 1038 @memcpy(name.bytes[0..s.len], s); 1039 return name; 1040 } 1041 1042 pub fn slice(self: *const Array(max_len)) []const u8 { 1043 return self.bytes[0..self.len]; 1044 } 1045 1046 // round-trip as a json string instead of an array of numbers. 1047 pub fn jsonStringify(self: *const Array(max_len), jw: anytype) !void { 1048 try jw.write(self.slice()); 1049 } 1050 1051 pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !Array(max_len) { 1052 const s = try std.json.innerParse([]const u8, allocator, source, options); 1053 // ValueTooLong is std.json's error for an over-long field. 1054 return from(s) orelse error.ValueTooLong; 1055 } 1056 }; 1057 } 1058 1059 pub fn toUrl(self: RoutablePage, arena: *std.heap.ArenaAllocator) ![]const u8 { 1060 return switch (self) { 1061 .home_about => "/", 1062 .home_users => |u| blk: { 1063 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1064 try out.writer.writeAll("/" ++ home_segment ++ "/users"); 1065 try writeListWindow(&out.writer, u.search.slice(), u.start); 1066 break :blk out.written(); 1067 }, 1068 .home_repo_new => "/" ++ home_segment ++ "/repo/new", 1069 .home_user_new => "/" ++ home_segment ++ "/user/new", 1070 .home_user => "/" ++ home_segment ++ "/user", 1071 .not_found => "/" ++ home_segment ++ "/not-found", 1072 .user_repos => |u| blk: { 1073 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1074 try out.writer.print("/{s}/repos", .{u.name.slice()}); 1075 try writeListWindow(&out.writer, u.search.slice(), u.start); 1076 break :blk out.written(); 1077 }, 1078 .user_forks => |u| if (u.start == 0) 1079 try arena.allocator().print("/{s}/forks", .{u.name.slice()}) 1080 else 1081 try arena.allocator().print("/{s}/forks/" ++ start_seg ++ "{d}", .{ u.name.slice(), u.start }), 1082 .user_repo_new => |name| try arena.allocator().print("/{s}/repo/new", .{name.slice()}), 1083 .user_user_new => |name| try arena.allocator().print("/{s}/user/new", .{name.slice()}), 1084 .user_user => |name| try arena.allocator().print("/{s}/user", .{name.slice()}), 1085 .repo_files => |f| blk: { 1086 const prefix = try repoUrlPrefix(arena, f.name.slice()); 1087 if (f.ref_kind == null and f.patchrev_id.len == 0 and f.find.len == 0 and f.line == 0) break :blk if (prefix.len == 0) "/" else prefix; 1088 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1089 try out.writer.print("{s}/" ++ files_seg, .{prefix}); 1090 if (f.ref_kind) |kind| if (f.ref_value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), f.ref_value.slice() }); 1091 if (f.patchrev_id.len != 0) try out.writer.print("/patchrev:{s}", .{f.patchrev_id.slice()}); 1092 if (f.find.len != 0) try out.writer.print("/" ++ find_seg ++ "{s}", .{f.find.slice()}); 1093 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 1094 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 1095 break :blk out.written(); 1096 }, 1097 .repo_commits => |c| blk: { 1098 const prefix = try repoUrlPrefix(arena, c.name.slice()); 1099 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1100 try out.writer.print("{s}/" ++ commits_seg, .{prefix}); 1101 if (c.ref_or_oid) |kind| if (c.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), c.value.slice() }); 1102 if (c.base_oid.len != 0) try out.writer.print("/" ++ base_seg ++ "{s}", .{c.base_oid.slice()}); 1103 if (c.search.len != 0) try out.writer.print("/" ++ search_seg ++ "{s}", .{c.search.slice()}); 1104 if (c.from.len != 0) try out.writer.print("/" ++ from_seg ++ "{s}", .{c.from.slice()}); 1105 if (c.merge.len != 0) try out.writer.print("/" ++ merge_seg ++ "{s}", .{c.merge.slice()}); 1106 if (c.message) try out.writer.print("/" ++ message_seg, .{}); 1107 break :blk out.written(); 1108 }, 1109 .repo_diff => |d| blk: { 1110 const prefix = try repoUrlPrefix(arena, d.name.slice()); 1111 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1112 try out.writer.print("{s}/diff", .{prefix}); 1113 if (d.ref_or_oid) |kind| if (d.value.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(kind), d.value.slice() }); 1114 if (d.patchrev_id.len != 0) try out.writer.print("/patchrev:{s}", .{d.patchrev_id.slice()}); 1115 if (d.base_oid.len != 0) try out.writer.print("/" ++ base_seg ++ "{s}", .{d.base_oid.slice()}); 1116 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 1117 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 1118 break :blk out.written(); 1119 }, 1120 .repo_refs => |r| blk: { 1121 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1122 try out.writer.print("{s}/refs", .{try repoUrlPrefix(arena, r.name.slice())}); 1123 if (r.from.len != 0) try out.writer.print("/{s}:{s}", .{ @tagName(r.kind), r.from.slice() }); 1124 if (r.search.len != 0) try out.writer.print("/" ++ search_seg ++ "{s}", .{r.search.slice()}); 1125 break :blk out.written(); 1126 }, 1127 .repo_issues => |i| blk: { 1128 const prefix = try repoUrlPrefix(arena, i.name.slice()); 1129 if (i.view == .edit) break :blk try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/edit", .{ prefix, i.selected.slice() }); 1130 if (i.view == .description) break :blk try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/description", .{ prefix, i.selected.slice() }); 1131 if (i.view == .resolve) break :blk if (i.theirs.len == 0) 1132 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/resolve", .{ prefix, i.selected.slice() }) 1133 else 1134 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, i.selected.slice(), i.theirs.slice() }); 1135 if (i.view == .new_comment) break :blk if (i.comment.len == 0) 1136 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/new", .{ prefix, i.selected.slice() }) 1137 else 1138 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, i.selected.slice(), i.comment.slice() }); 1139 if (i.view == .edit_comment) break :blk try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, i.selected.slice(), i.comment.slice() }); 1140 if (i.view == .remove) break :blk if (i.comment.len == 0) 1141 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/remove", .{ prefix, i.selected.slice() }) 1142 else 1143 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, i.selected.slice(), i.comment.slice() }); 1144 if (i.view == .conflicts) break :blk if (i.selected.len == 0) 1145 try arena.allocator().print("{s}/issues/conflicts", .{prefix}) 1146 else 1147 try arena.allocator().print("{s}/issues/" ++ start_seg ++ "{s}/conflicts", .{ prefix, i.selected.slice() }); 1148 if (i.comment.len != 0) break :blk if (i.comments_start == 0) 1149 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, i.selected.slice(), i.comment.slice() }) 1150 else 1151 try arena.allocator().print("{s}/" ++ issue_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, i.selected.slice(), i.comment.slice(), i.comments_start }); 1152 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1153 // the list the thread is selected in stays filtered, so paging 1154 // and reloading keep the label and the query 1155 if (i.selected.len != 0) { 1156 try out.writer.print("{s}/" ++ issue_seg ++ "{s}", .{ prefix, i.selected.slice() }); 1157 try writeListFilters(&out.writer, i.label.slice(), i.search.slice()); 1158 if (i.comments_start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{i.comments_start}); 1159 break :blk out.written(); 1160 } 1161 try out.writer.print("{s}/issues/{s}", .{ prefix, @tagName(i.view) }); 1162 try writeListFilters(&out.writer, i.label.slice(), i.search.slice()); 1163 break :blk out.written(); 1164 }, 1165 .repo_patches => |p| blk: { 1166 const prefix = try repoUrlPrefix(arena, p.name.slice()); 1167 if (p.view == .edit) break :blk try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/edit", .{ prefix, p.selected.slice() }); 1168 if (p.view == .publish) break :blk try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/publish", .{ prefix, p.selected.slice() }); 1169 if (p.view == .merge) break :blk try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/merge", .{ prefix, p.selected.slice() }); 1170 if (p.view == .description) break :blk try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/description", .{ prefix, p.selected.slice() }); 1171 if (p.view == .resolve) break :blk if (p.theirs.len == 0) 1172 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/resolve", .{ prefix, p.selected.slice() }) 1173 else 1174 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/" ++ theirs_seg ++ "{s}/resolve", .{ prefix, p.selected.slice(), p.theirs.slice() }); 1175 if (p.view == .new_comment) break :blk if (p.comment.len == 0) 1176 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/new", .{ prefix, p.selected.slice() }) 1177 else 1178 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, p.selected.slice(), p.comment.slice() }); 1179 if (p.view == .edit_comment) break :blk try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, p.selected.slice(), p.comment.slice() }); 1180 if (p.view == .remove) break :blk if (p.comment.len == 0) 1181 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/remove", .{ prefix, p.selected.slice() }) 1182 else 1183 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, p.selected.slice(), p.comment.slice() }); 1184 if (p.view == .conflicts) break :blk if (p.selected.len == 0) 1185 try arena.allocator().print("{s}/patches/conflicts", .{prefix}) 1186 else 1187 try arena.allocator().print("{s}/patches/" ++ start_seg ++ "{s}/conflicts", .{ prefix, p.selected.slice() }); 1188 if (p.comment.len != 0) break :blk if (p.comments_start == 0) 1189 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, p.selected.slice(), p.comment.slice() }) 1190 else 1191 try arena.allocator().print("{s}/" ++ patch_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, p.selected.slice(), p.comment.slice(), p.comments_start }); 1192 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1193 // the list the thread is selected in stays filtered, so paging 1194 // and reloading keep the label and the query 1195 if (p.selected.len != 0) { 1196 try out.writer.print("{s}/" ++ patch_seg ++ "{s}", .{ prefix, p.selected.slice() }); 1197 try writeListFilters(&out.writer, p.label.slice(), p.search.slice()); 1198 if (p.comments_start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{p.comments_start}); 1199 break :blk out.written(); 1200 } 1201 try out.writer.print("{s}/patches/{s}", .{ prefix, @tagName(p.view) }); 1202 try writeListFilters(&out.writer, p.label.slice(), p.search.slice()); 1203 break :blk out.written(); 1204 }, 1205 .repo_discussions => |t| blk: { 1206 const prefix = try repoUrlPrefix(arena, t.name.slice()); 1207 if (t.view == .edit) break :blk try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/edit", .{ prefix, t.selected.slice() }); 1208 if (t.view == .description) break :blk try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/description", .{ prefix, t.selected.slice() }); 1209 if (t.view == .new_comment) break :blk if (t.comment.len == 0) 1210 try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/new", .{ prefix, t.selected.slice() }) 1211 else 1212 try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/new", .{ prefix, t.selected.slice(), t.comment.slice() }); 1213 if (t.view == .edit_comment) break :blk try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/edit", .{ prefix, t.selected.slice(), t.comment.slice() }); 1214 if (t.view == .remove) break :blk if (t.comment.len == 0) 1215 try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/remove", .{ prefix, t.selected.slice() }) 1216 else 1217 try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/remove", .{ prefix, t.selected.slice(), t.comment.slice() }); 1218 if (t.comment.len != 0) break :blk if (t.comments_start == 0) 1219 try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}", .{ prefix, t.selected.slice(), t.comment.slice() }) 1220 else 1221 try arena.allocator().print("{s}/" ++ discuss_seg ++ "{s}/" ++ comment_seg ++ "{s}/" ++ start_seg ++ "{d}", .{ prefix, t.selected.slice(), t.comment.slice(), t.comments_start }); 1222 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1223 // the list the thread is selected in stays filtered, so paging 1224 // and reloading keep the label and the query 1225 if (t.selected.len != 0) { 1226 try out.writer.print("{s}/" ++ discuss_seg ++ "{s}", .{ prefix, t.selected.slice() }); 1227 try writeListFilters(&out.writer, t.label.slice(), t.search.slice()); 1228 if (t.comments_start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{t.comments_start}); 1229 break :blk out.written(); 1230 } 1231 try out.writer.print("{s}/discussions/{s}", .{ prefix, @tagName(t.view) }); 1232 try writeListFilters(&out.writer, t.label.slice(), t.search.slice()); 1233 break :blk out.written(); 1234 }, 1235 .repo_undo => |u| blk: { 1236 const prefix = try repoUrlPrefix(arena, u.name.slice()); 1237 break :blk if (u.clear) 1238 try arena.allocator().print("{s}/undo/clear", .{prefix}) 1239 else if (u.index) |index| 1240 try arena.allocator().print("{s}/undo/{d}", .{ prefix, index }) 1241 else 1242 try arena.allocator().print("{s}/undo", .{prefix}); 1243 }, 1244 .repo_events => |e| blk: { 1245 const prefix = try repoUrlPrefix(arena, e.name.slice()); 1246 const moment = if (e.moment) |value| try arena.allocator().print("/moment:{d}", .{value}) else ""; 1247 break :blk if (e.kind) |kind| 1248 try arena.allocator().print("{s}/event:{s}/kind:{s}{s}", .{ prefix, e.selected.slice(), @tagName(kind), moment }) 1249 else 1250 try arena.allocator().print("{s}/events/{s}{s}", .{ prefix, @tagName(e.view), moment }); 1251 }, 1252 .repo_repo_new => |name| try arena.allocator().print("{s}/repo/new", .{try repoUrlPrefix(arena, name.slice())}), 1253 .repo_user_new => |name| try arena.allocator().print("{s}/user/new", .{try repoUrlPrefix(arena, name.slice())}), 1254 .repo_settings => |name| try arena.allocator().print("{s}/repo/settings", .{try repoUrlPrefix(arena, name.slice())}), 1255 .repo_roles => |r| blk: { 1256 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1257 try out.writer.print("{s}/repo/roles", .{try repoUrlPrefix(arena, r.name.slice())}); 1258 if (r.user.len != 0) try out.writer.print("/" ++ user_seg ++ "{s}", .{r.user.slice()}); 1259 if (r.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{r.start}); 1260 break :blk out.written(); 1261 }, 1262 .repo_user => |name| try arena.allocator().print("{s}/user", .{try repoUrlPrefix(arena, name.slice())}), 1263 .fork_patch => |f| try arena.allocator().print("/{s}+{s}", .{ f.name.slice(), f.id.slice() }), 1264 .fork_diff => |d| blk: { 1265 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1266 try out.writer.print("/{s}+{s}/diff", .{ d.fork.name.slice(), d.fork.id.slice() }); 1267 if (d.oid.len != 0) try out.writer.print("/object:{s}/" ++ base_seg ++ "{s}", .{ d.oid.slice(), d.base_oid.slice() }); 1268 if (d.start != 0) try out.writer.print("/" ++ start_seg ++ "{d}", .{d.start}); 1269 if (d.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{d.path.slice()}); 1270 break :blk out.written(); 1271 }, 1272 .fork_files => |f| blk: { 1273 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1274 try out.writer.print("/{s}+{s}/" ++ files_seg, .{ f.fork.name.slice(), f.fork.id.slice() }); 1275 if (f.oid.len != 0) try out.writer.print("/object:{s}", .{f.oid.slice()}); 1276 if (f.find.len != 0) try out.writer.print("/" ++ find_seg ++ "{s}", .{f.find.slice()}); 1277 if (f.line != 0) try out.writer.print("/" ++ line_seg ++ "{d}", .{f.line}); 1278 if (f.path.len != 0) try out.writer.print("/" ++ path_seg ++ "{s}", .{f.path.slice()}); 1279 break :blk out.written(); 1280 }, 1281 .fork_commits => |c| blk: { 1282 var out: std.Io.Writer.Allocating = .init(arena.allocator()); 1283 try out.writer.print("/{s}+{s}/" ++ commits_seg, .{ c.fork.name.slice(), c.fork.id.slice() }); 1284 if (c.oid.len != 0) try out.writer.print("/object:{s}", .{c.oid.slice()}); 1285 if (c.message) try out.writer.print("/" ++ message_seg, .{}); 1286 break :blk out.written(); 1287 }, 1288 .fork_repo_new => |f| try arena.allocator().print("/{s}+{s}/repo/new", .{ f.name.slice(), f.id.slice() }), 1289 .fork_user_new => |f| try arena.allocator().print("/{s}+{s}/user/new", .{ f.name.slice(), f.id.slice() }), 1290 .fork_user => |f| try arena.allocator().print("/{s}+{s}/user", .{ f.name.slice(), f.id.slice() }), 1291 }; 1292 } 1293 1294 pub fn fromUrl(path: []const u8) ?RoutablePage { 1295 if (std.mem.eql(u8, path, "/")) return default; 1296 if (path.len < 2 or path[0] != '/') return null; 1297 var segments = std.mem.splitScalar(u8, path[1..], '/'); 1298 const first = segments.next() orelse return null; 1299 // "<forker>:<reponame>+<patch id>[/files|/commits]"; the patch 1300 // branch is implicit, while object ids and view-specific params 1301 // use the same tails as the repo files and commits routes. 1302 if (std.mem.indexOfScalar(u8, first, '+')) |plus| { 1303 const identity = first[0..plus]; 1304 _ = RepoIdentity.parse(identity) orelse return null; 1305 const id = first[plus + 1 ..]; 1306 if (id.len != evt.event_id_size * 2) return null; 1307 for (id) |c| if (!std.ascii.isHex(c)) return null; 1308 const tab = segments.next() orelse return forkPatchRoute(identity, id); 1309 var params = Params{}; 1310 if (std.mem.eql(u8, tab, "diff")) { 1311 params.scanPairs(&segments) catch return null; 1312 if (!params.only(&.{ .start, .object, .base })) return null; 1313 const ref = params.ref() catch return null; 1314 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1315 return forkDiffRoute(identity, id, oid, params.values.get(.base) orelse "", params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1316 } 1317 if (std.mem.eql(u8, tab, files_seg)) { 1318 params.scanPairs(&segments) catch return null; 1319 if (!params.only(&.{ .line, .object, .find })) return null; 1320 const line = params.line() orelse return null; 1321 const ref = params.ref() catch return null; 1322 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1323 const file_path = pathValue(segments.rest()) orelse return null; 1324 var route = forkFilesRoute(identity, id, oid, file_path, line) orelse return null; 1325 if (params.values.get(.find)) |find| route.fork_files.find = Array(find_route_max_len).from(find) orelse return null; 1326 return route; 1327 } 1328 if (std.mem.eql(u8, tab, commits_seg)) { 1329 params.scanPairs(&segments) catch return null; 1330 if (!params.only(&.{.object})) return null; 1331 const ref = params.ref() catch return null; 1332 const oid = if (ref) |value| if (value.kind == .object) value.value else return null else ""; 1333 if (std.mem.eql(u8, segments.rest(), message_seg)) 1334 return if (oid.len != 0) forkCommitMessageRoute(identity, id, oid) else null; 1335 return if (segments.rest().len == 0) forkCommitsRoute(identity, id, oid) else null; 1336 } 1337 if (std.mem.eql(u8, tab, "repo")) return if (isNewTail(&segments)) forkRepoNewRoute(identity, id) else null; 1338 if (std.mem.eql(u8, tab, "user")) return if (segments.peek() == null) forkUserRoute(identity, id) else if (isNewTail(&segments)) forkUserNewRoute(identity, id) else null; 1339 return null; 1340 } 1341 // "<username>:<reponame>[/<tab tail>]"; the bare pair is the files root 1342 if (std.mem.indexOfScalar(u8, first, ':') != null) { 1343 _ = RepoIdentity.parse(first) orelse return null; 1344 if (segments.peek() != null) { 1345 return repoSubRoute(first, segments.rest()); 1346 } 1347 return repoFilesRoute(first, null, "", "", 0); 1348 } 1349 // "home[/users[/search:<s>][/start:<n>]|/repo/new|/user[/new]|/not-found]" 1350 if (std.mem.eql(u8, first, home_segment)) { 1351 const sub = segments.next() orelse return default; 1352 if (std.mem.eql(u8, sub, "users")) { 1353 const params = listParams(&segments, &.{ .start, .search }) orelse return null; 1354 return withEncodedSearch(.{ .home_users = .{ .start = params.start() orelse return null } }, params.values.get(.search) orelse ""); 1355 } 1356 if (std.mem.eql(u8, sub, "repo")) return if (isNewTail(&segments)) .home_repo_new else null; 1357 if (std.mem.eql(u8, sub, "user")) return if (segments.peek() == null) .home_user else if (isNewTail(&segments)) .home_user_new else null; 1358 if (std.mem.eql(u8, sub, "not-found")) return if (segments.peek() == null) .not_found else null; 1359 return null; 1360 } 1361 // "<username>[/repos[/search:<s>][/start:<n>]|/forks|/repo/new|/user[/new]]" 1362 evt.User.validateName(first) catch return null; 1363 const parsed = Array(evt.User.name_max_len).from(first) orelse return null; 1364 const sub = segments.next() orelse return .{ .user_repos = .{ .name = parsed } }; 1365 if (std.mem.eql(u8, sub, "repos")) { 1366 const params = listParams(&segments, &.{ .start, .search }) orelse return null; 1367 return withEncodedSearch(.{ .user_repos = .{ .name = parsed, .start = params.start() orelse return null } }, params.values.get(.search) orelse ""); 1368 } 1369 if (std.mem.eql(u8, sub, "forks")) return .{ .user_forks = .{ .name = parsed, .start = listStart(&segments) orelse return null } }; 1370 if (std.mem.eql(u8, sub, "repo")) return if (isNewTail(&segments)) .{ .user_repo_new = parsed } else null; 1371 if (std.mem.eql(u8, sub, "user")) return if (segments.peek() == null) .{ .user_user = parsed } else if (isNewTail(&segments)) .{ .user_user_new = parsed } else null; 1372 return null; // unknown sub-path 1373 } 1374 1375 // parse a local-mode url, which elides the "/<owner>:<name>" prefix: 1376 // "/" is the files root and the sub-paths follow directly. 1377 pub fn fromUrlLocal(path: []const u8) ?RoutablePage { 1378 if (std.mem.eql(u8, path, "/")) return repoFilesRoute("", null, "", "", 0); 1379 if (path.len < 2 or path[0] != '/') return null; 1380 return repoSubRoute("", path[1..]); 1381 } 1382 1383 // the url prefix for a repo route's identity: "/<identity>", or "" 1384 // when the identity is elided. 1385 fn repoUrlPrefix(arena: *std.heap.ArenaAllocator, identity: []const u8) ![]const u8 { 1386 if (identity.len == 0) return ""; 1387 return arena.allocator().print("/{s}", .{identity}); 1388 } 1389 1390 // true when the page builds at its content height on the web, letting the 1391 // browser scroll the whole page, rather than being pinned to the viewport 1392 pub fn fullHeight(self: RoutablePage) bool { 1393 return switch (self) { 1394 .repo_issues => |i| i.view == .resolve, 1395 .repo_patches => |p| p.view == .resolve, 1396 else => false, 1397 }; 1398 } 1399 1400 // the "owner:name" a repo route carries, null for any other route. the 1401 // result borrows the route, so it must outlive the slice. 1402 pub fn repoIdentity(self: *const RoutablePage) ?[]const u8 { 1403 return switch (self.*) { 1404 .repo_files => |*f| f.name.slice(), 1405 .repo_commits => |*c| c.name.slice(), 1406 .repo_diff => |*d| d.name.slice(), 1407 .repo_refs => |*r| r.name.slice(), 1408 .repo_issues => |*i| i.name.slice(), 1409 .repo_patches => |*p| p.name.slice(), 1410 .repo_discussions => |*t| t.name.slice(), 1411 .repo_events => |*e| e.name.slice(), 1412 .repo_undo => |*u| u.name.slice(), 1413 .repo_roles => |*r| r.name.slice(), 1414 .repo_settings, .repo_repo_new, .repo_user_new, .repo_user => |*name| name.slice(), 1415 else => null, 1416 }; 1417 } 1418 1419 pub fn forkRoute(self: *const RoutablePage) ?*const ForkRoute { 1420 return switch (self.*) { 1421 .fork_patch => |*f| f, 1422 .fork_diff => |*d| &d.fork, 1423 .fork_files => |*f| &f.fork, 1424 .fork_commits => |*f| &f.fork, 1425 .fork_repo_new => |*f| f, 1426 .fork_user_new => |*f| f, 1427 .fork_user => |*f| f, 1428 else => null, 1429 }; 1430 } 1431 1432 // the route this route's page lands on, with no tab or params: the users 1433 // list, a user's repos, or a repo's files root. 1434 pub fn pageRoot(self: RoutablePage) RoutablePage { 1435 return switch (self.parent()) { 1436 .home => .default, 1437 .user => switch (self) { 1438 .user_repos => |u| .{ .user_repos = .{ .name = u.name } }, 1439 .user_forks => |u| .{ .user_repos = .{ .name = u.name } }, 1440 .user_repo_new, .user_user_new, .user_user => |name| .{ .user_repos = .{ .name = name } }, 1441 else => self, 1442 }, 1443 // every repo route stores the same identity, so it always fits 1444 .repo => repoFilesRoute(self.repoIdentity() orelse return self, null, "", "", 0) orelse self, 1445 .fork => blk: { 1446 const f = self.forkRoute() orelse break :blk self; 1447 break :blk forkPatchRoute(f.name.slice(), f.id.slice()) orelse self; 1448 }, 1449 .not_found => self, 1450 }; 1451 } 1452 1453 pub fn parent(self: RoutablePage) PageKind { 1454 return switch (self) { 1455 .home_about, .home_users, .home_repo_new, .home_user_new, .home_user => .home, 1456 .user_repos, .user_forks, .user_repo_new, .user_user_new, .user_user => .user, 1457 .repo_files, .repo_commits, .repo_diff, .repo_refs, .repo_issues, .repo_patches, .repo_discussions, .repo_events, .repo_undo, .repo_settings, .repo_roles, .repo_repo_new, .repo_user_new, .repo_user => .repo, 1458 .fork_patch, .fork_diff, .fork_files, .fork_commits, .fork_repo_new, .fork_user_new, .fork_user => .fork, 1459 .not_found => .not_found, 1460 }; 1461 } 1462 1463 // the dir a trailing "path:<dir>" names: the raw remainder ("" when 1464 // absent, null when malformed) 1465 fn pathValue(rest: []const u8) ?[]const u8 { 1466 if (rest.len == 0) return ""; 1467 if (!std.mem.startsWith(u8, rest, path_seg)) return null; 1468 const dir = rest[path_seg.len..]; 1469 return if (dir.len == 0) null else dir; 1470 } 1471 1472 // a list route's tail: at most a "start:<n>" param, null on anything else 1473 // whether the rest of `segments` is exactly "new" 1474 fn isNewTail(segments: *std.mem.SplitIterator(u8, .scalar)) bool { 1475 const next = segments.next() orelse return false; 1476 return std.mem.eql(u8, next, "new") and segments.next() == null; 1477 } 1478 1479 fn listStart(segments: *std.mem.SplitIterator(u8, .scalar)) ?usize { 1480 const params = listParams(segments, &.{.start}) orelse return null; 1481 return params.start(); 1482 } 1483 1484 // a list route's tail of `allowed` params, null on anything else 1485 fn listParams(segments: *std.mem.SplitIterator(u8, .scalar), allowed: []const Params.ParamKey) ?Params { 1486 var params = Params{}; 1487 const word = params.scanTail(segments) catch return null; 1488 if (word != null or !params.only(allowed)) return null; 1489 return params; 1490 } 1491 1492 // a searchable list url's search and window segments, each optional 1493 fn writeListWindow(writer: *std.Io.Writer, search: []const u8, start: usize) !void { 1494 if (search.len != 0) try writer.print("/" ++ search_seg ++ "{s}", .{search}); 1495 if (start != 0) try writer.print("/" ++ start_seg ++ "{d}", .{start}); 1496 } 1497 1498 // parse the sub-path after a repo route's identity ("" for a local 1499 // route): a tab keyword followed by "key:value" params in any order, plus 1500 // the files tab's trailing "path:<dir>". 1501 fn repoSubRoute(pair: []const u8, sub: []const u8) ?RoutablePage { 1502 var segments = std.mem.splitScalar(u8, sub, '/'); 1503 const tab = segments.next() orelse return null; 1504 var params = Params{}; 1505 if (std.mem.eql(u8, tab, "repo")) { 1506 const word = segments.peek() orelse return null; 1507 if (std.mem.eql(u8, word, "settings")) { 1508 _ = segments.next(); 1509 return if (segments.next() == null) repoSettingsRoute(pair) else null; 1510 } 1511 if (std.mem.eql(u8, word, "roles")) { 1512 _ = segments.next(); 1513 const roles_params = listParams(&segments, &.{ .user, .start }) orelse return null; 1514 var route = repoRolesRoute(pair, roles_params.start() orelse return null) orelse return null; 1515 if (roles_params.values.get(.user)) |user| route.repo_roles.user = Array(evt.User.name_max_len).from(user) orelse return null; 1516 return route; 1517 } 1518 return if (isNewTail(&segments)) .{ .repo_repo_new = Array(repo_route_max_len).from(pair) orelse return null } else null; 1519 } 1520 if (std.mem.eql(u8, tab, "user")) return if (segments.peek() == null) .{ .repo_user = Array(repo_route_max_len).from(pair) orelse return null } else if (isNewTail(&segments)) .{ .repo_user_new = Array(repo_route_max_len).from(pair) orelse return null } else null; 1521 if (std.mem.eql(u8, tab, "refs")) { 1522 const word = params.scanTail(&segments) catch return null; 1523 if (word != null or !params.only(&.{ .branch, .tag, .search })) return null; 1524 const ref = params.ref() catch return null; 1525 const kind: RefKind = if (ref) |r| std.meta.stringToEnum(RefKind, @tagName(r.kind)) orelse return null else .branch; 1526 const from = if (ref) |r| r.value else ""; 1527 return withEncodedSearch(repoRefsRoute(pair, kind, from), params.values.get(.search) orelse ""); 1528 } 1529 if (std.mem.startsWith(u8, tab, issue_seg)) { 1530 const issue_id = tab[issue_seg.len..]; 1531 if (issue_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(.issue, pair, issue_id, comment_id); 1541 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.issue, pair, issue_id, comment_id); 1542 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.issue, pair, issue_id, comment_id); 1543 return null; 1544 } 1545 if (!params.only(&.{.start})) return null; 1546 return repoThreadCommentRoute(.issue, pair, issue_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(.issue, pair, issue_id, "") else null; 1552 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.issue, pair, issue_id) else null; 1553 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.issue, pair, issue_id, "") else null; 1554 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.issue, pair, issue_id) else null; 1555 if (std.mem.eql(u8, tail, "resolve")) { 1556 if (!params.only(&.{.theirs})) return null; 1557 return repoIssuesResolveRoute(pair, issue_id, params.values.get(.theirs) orelse ""); 1558 } 1559 return null; 1560 } 1561 if (!params.only(&.{ .start, .label, .search })) return null; 1562 return withEncodedFilters(repoThreadCommentsRoute(.issue, pair, issue_id, params.start() orelse return null), params.values.get(.label) orelse "", params.values.get(.search) orelse ""); 1563 } 1564 if (std.mem.startsWith(u8, tab, patch_seg)) { 1565 const patch_id = tab[patch_seg.len..]; 1566 if (patch_id.len == 0) return null; 1567 if (segments.peek()) |tail| { 1568 if (std.mem.startsWith(u8, tail, comment_seg)) { 1569 _ = segments.next(); 1570 const comment_id = tail[comment_seg.len..]; 1571 if (comment_id.len == 0) return null; 1572 const word = params.scanTail(&segments) catch return null; 1573 if (word) |last| { 1574 if (!params.only(&.{})) return null; 1575 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.patch, pair, patch_id, comment_id); 1576 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.patch, pair, patch_id, comment_id); 1577 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.patch, pair, patch_id, comment_id); 1578 return null; 1579 } 1580 if (!params.only(&.{.start})) return null; 1581 return repoThreadCommentRoute(.patch, pair, patch_id, comment_id, params.start() orelse return null); 1582 } 1583 } 1584 const word = params.scanTail(&segments) catch return null; 1585 if (word) |tail| { 1586 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.patch, pair, patch_id, "") else null; 1587 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.patch, pair, patch_id) else null; 1588 if (std.mem.eql(u8, tail, "publish")) return if (params.only(&.{})) repoPatchPublishRoute(pair, patch_id) else null; 1589 if (std.mem.eql(u8, tail, "merge")) return if (params.only(&.{})) repoPatchMergeRoute(pair, patch_id) else null; 1590 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.patch, pair, patch_id, "") else null; 1591 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.patch, pair, patch_id) else null; 1592 if (std.mem.eql(u8, tail, "resolve")) { 1593 if (!params.only(&.{.theirs})) return null; 1594 return repoPatchesResolveRoute(pair, patch_id, params.values.get(.theirs) orelse ""); 1595 } 1596 return null; 1597 } 1598 if (!params.only(&.{ .start, .label, .search })) return null; 1599 return withEncodedFilters(repoThreadCommentsRoute(.patch, pair, patch_id, params.start() orelse return null), params.values.get(.label) orelse "", params.values.get(.search) orelse ""); 1600 } 1601 if (std.mem.startsWith(u8, tab, discuss_seg)) { 1602 const discussion_id = tab[discuss_seg.len..]; 1603 if (discussion_id.len == 0) return null; 1604 if (segments.peek()) |tail| { 1605 if (std.mem.startsWith(u8, tail, comment_seg)) { 1606 _ = segments.next(); 1607 const comment_id = tail[comment_seg.len..]; 1608 if (comment_id.len == 0) return null; 1609 const word = params.scanTail(&segments) catch return null; 1610 if (word) |last| { 1611 if (!params.only(&.{})) return null; 1612 if (std.mem.eql(u8, last, "new")) return repoThreadCommentNewRoute(.discuss, pair, discussion_id, comment_id); 1613 if (std.mem.eql(u8, last, "edit")) return repoThreadCommentEditRoute(.discuss, pair, discussion_id, comment_id); 1614 if (std.mem.eql(u8, last, "remove")) return repoThreadRemoveRoute(.discuss, pair, discussion_id, comment_id); 1615 return null; 1616 } 1617 if (!params.only(&.{.start})) return null; 1618 return repoThreadCommentRoute(.discuss, pair, discussion_id, comment_id, params.start() orelse return null); 1619 } 1620 } 1621 const word = params.scanTail(&segments) catch return null; 1622 if (word) |tail| { 1623 if (std.mem.eql(u8, tail, "new")) return if (params.only(&.{})) repoThreadCommentNewRoute(.discuss, pair, discussion_id, "") else null; 1624 if (std.mem.eql(u8, tail, "edit")) return if (params.only(&.{})) repoThreadEditRoute(.discuss, pair, discussion_id) else null; 1625 if (std.mem.eql(u8, tail, "remove")) return if (params.only(&.{})) repoThreadRemoveRoute(.discuss, pair, discussion_id, "") else null; 1626 if (std.mem.eql(u8, tail, "description")) return if (params.only(&.{})) repoThreadDescriptionRoute(.discuss, pair, discussion_id) else null; 1627 return null; 1628 } 1629 if (!params.only(&.{ .start, .label, .search })) return null; 1630 return withEncodedFilters(repoThreadCommentsRoute(.discuss, pair, discussion_id, params.start() orelse return null), params.values.get(.label) orelse "", params.values.get(.search) orelse ""); 1631 } 1632 if (std.mem.eql(u8, tab, "issues")) { 1633 // the word is a list view; a filter with neither is never emitted. 1634 const word = params.scanTail(&segments) catch return null; 1635 const label_value = params.values.get(.label) orelse ""; 1636 const w = word orelse return if (params.only(&.{})) repoIssuesRoute(pair, .open, "", "") else null; 1637 // the conflicts list windows by an issue id, not a number, so it 1638 // reads the start param raw 1639 if (std.mem.eql(u8, w, "conflicts")) { 1640 if (!params.only(&.{.start})) return null; 1641 return repoIssuesConflictsRoute(pair, params.values.get(.start) orelse ""); 1642 } 1643 if (!params.only(&.{ .label, .search })) return null; 1644 const search_value = params.values.get(.search) orelse ""; 1645 if (std.meta.stringToEnum(evt.Issue.Status, w)) |status| return withEncodedSearch(repoIssuesRoute(pair, status, label_value, ""), search_value); 1646 if (std.mem.eql(u8, w, "labels")) return withEncodedSearch(repoThreadLabelsRoute(.issue, pair, label_value), search_value); 1647 if (std.mem.eql(u8, w, "new")) return if (label_value.len == 0 and search_value.len == 0) repoThreadNewRoute(.issue, pair) else null; 1648 return null; 1649 } 1650 if (std.mem.eql(u8, tab, "patches")) { 1651 const word = params.scanTail(&segments) catch return null; 1652 const label_value = params.values.get(.label) orelse ""; 1653 const view = word orelse return if (params.only(&.{})) repoPatchesRoute(pair, .open, "", "") else null; 1654 if (std.mem.eql(u8, view, "conflicts")) { 1655 if (!params.only(&.{.start})) return null; 1656 return repoPatchesConflictsRoute(pair, params.values.get(.start) orelse ""); 1657 } 1658 if (!params.only(&.{ .label, .search })) return null; 1659 const search_value = params.values.get(.search) orelse ""; 1660 if (std.meta.stringToEnum(evt.Patch.StatusKind, view)) |status| return withEncodedSearch(repoPatchesRoute(pair, status, label_value, ""), search_value); 1661 if (std.mem.eql(u8, view, "labels")) return withEncodedSearch(repoThreadLabelsRoute(.patch, pair, label_value), search_value); 1662 if (std.mem.eql(u8, view, "new")) return if (label_value.len == 0 and search_value.len == 0) repoThreadNewRoute(.patch, pair) else null; 1663 if (std.mem.eql(u8, view, "drafts")) return if (label_value.len == 0 and search_value.len == 0) repoPatchesDraftsRoute(pair) else null; 1664 return null; 1665 } 1666 if (std.mem.eql(u8, tab, "discussions")) { 1667 const word = params.scanTail(&segments) catch return null; 1668 const label_value = params.values.get(.label) orelse ""; 1669 const view = word orelse return if (params.only(&.{})) repoDiscussionsRoute(pair, "", "") else null; 1670 if (!params.only(&.{ .label, .search })) return null; 1671 const search_value = params.values.get(.search) orelse ""; 1672 if (std.mem.eql(u8, view, "recent")) return withEncodedSearch(repoDiscussionsRoute(pair, label_value, ""), search_value); 1673 if (std.mem.eql(u8, view, "labels")) return withEncodedSearch(repoThreadLabelsRoute(.discuss, pair, label_value), search_value); 1674 if (std.mem.eql(u8, view, "new")) return if (label_value.len == 0 and search_value.len == 0) repoThreadNewRoute(.discuss, pair) else null; 1675 return null; 1676 } 1677 if (std.mem.eql(u8, tab, "undo")) { 1678 const word = segments.next(); 1679 if (segments.next() != null) return null; 1680 const value = word orelse return repoUndoRoute(pair, null); 1681 if (std.mem.eql(u8, value, "clear")) return repoUndoClearRoute(pair); 1682 return repoUndoRoute(pair, std.fmt.parseInt(u64, value, 10) catch return null); 1683 } 1684 if (std.mem.eql(u8, tab, "events")) { 1685 const word = params.scanTail(&segments) catch return null; 1686 if (!params.only(&.{.moment})) return null; 1687 const view = if (word) |value| std.meta.stringToEnum(EventsView, value) orelse return null else .active; 1688 return repoEventsRoute(pair, view, null, "", params.moment() catch return null); 1689 } 1690 if (std.mem.startsWith(u8, tab, "event:")) { 1691 const event_id = tab["event:".len..]; 1692 const kind_segment = segments.next() orelse return null; 1693 if (!std.mem.startsWith(u8, kind_segment, "kind:")) return null; 1694 const kind = std.meta.stringToEnum(evt.EventKind, kind_segment["kind:".len..]) orelse return null; 1695 if (event_id.len == 0) return null; 1696 params.scanPairs(&segments) catch return null; 1697 if (!params.only(&.{.moment}) or segments.next() != null) return null; 1698 return repoEventsRoute(pair, .active, kind, event_id, params.moment() catch return null); 1699 } 1700 if (std.mem.eql(u8, tab, files_seg)) { 1701 params.scanPairs(&segments) catch return null; 1702 if (params.values.get(.patchrev)) |id| { 1703 if (!params.only(&.{ .line, .patchrev })) return null; 1704 return repoPatchRevFilesRoute(pair, id, pathValue(segments.rest()) orelse return null, params.line() orelse return null); 1705 } 1706 if (!params.only(&.{ .line, .branch, .tag, .object, .find })) return null; 1707 const line = params.line() orelse return null; 1708 const dir = pathValue(segments.rest()) orelse return null; 1709 const find = params.values.get(.find) orelse ""; 1710 const ref = params.ref() catch return null; 1711 // a find route's `path` names the selected result, so unlike a 1712 // directory path it doesn't need a ref beside it 1713 var route = if (ref) |r| 1714 repoFilesRoute(pair, r.kind, r.value, dir, line) orelse return null 1715 else if (dir.len == 0 or find.len != 0) 1716 repoFilesRoute(pair, null, "", "", line) orelse return null 1717 else 1718 return null; 1719 if (find.len != 0) { 1720 route.repo_files.find = Array(find_route_max_len).from(find) orelse return null; 1721 route.repo_files.path = Array(repo_route_max_len).from(dir) orelse return null; 1722 } 1723 return route; 1724 } 1725 if (std.mem.eql(u8, tab, commits_seg)) { 1726 params.scanPairs(&segments) catch return null; 1727 if (params.values.get(.merge)) |oid| { 1728 if (!params.only(&.{.merge}) or segments.next() != null) return null; 1729 return repoCommitsMergeRoute(pair, oid); 1730 } 1731 if (!params.only(&.{ .branch, .tag, .object, .base, .search, .from })) return null; 1732 const base_oid = params.values.get(.base) orelse ""; 1733 const search = params.values.get(.search) orelse ""; 1734 const from = params.values.get(.from) orelse ""; 1735 const ref = (params.ref() catch return null) orelse { 1736 if (search.len != 0 or from.len != 0 or segments.rest().len != 0) return null; 1737 return repoCommitsRoute(pair, null, "", base_oid); 1738 }; 1739 // the index covers branch and tag tips, so only they can be 1740 // searched or paged by commit; an object roots the list itself 1741 if ((search.len != 0 or from.len != 0) and ref.kind == .object) return null; 1742 var route = if (std.mem.eql(u8, segments.rest(), message_seg)) 1743 repoCommitMessageRoute(pair, ref.kind, ref.value, base_oid) orelse return null 1744 else if (segments.rest().len == 0) 1745 repoCommitsRoute(pair, ref.kind, ref.value, base_oid) orelse return null 1746 else 1747 return null; 1748 if (search.len != 0) route.repo_commits.search = Array(search_route_max_len).from(search) orelse return null; 1749 if (from.len != 0) route.repo_commits.from = Array(ref_route_max_len).from(from) orelse return null; 1750 return route; 1751 } 1752 if (std.mem.eql(u8, tab, "diff")) { 1753 params.scanPairs(&segments) catch return null; 1754 if (params.values.get(.patchrev)) |id| { 1755 if (!params.only(&.{ .start, .patchrev })) return null; 1756 return repoPatchRevDiffRoute(pair, id, params.start() orelse return null, pathValue(segments.rest()) orelse return null); 1757 } 1758 if (!params.only(&.{ .start, .branch, .tag, .object, .base })) return null; 1759 const base_oid = params.values.get(.base) orelse return null; 1760 const start = params.start() orelse return null; 1761 const ref = params.ref() catch return null; 1762 const file = pathValue(segments.rest()) orelse return null; 1763 return repoDiffRoute(pair, if (ref) |r| r.kind else null, if (ref) |r| r.value else "", start, file, base_oid); 1764 } 1765 return null; // unknown sub-path 1766 } 1767 1768 pub fn eql(a: RoutablePage, b: RoutablePage) bool { 1769 if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false; 1770 return switch (a) { 1771 .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()), 1772 .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()), 1773 .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, 1774 .user_repo_new => |a_name| std.mem.eql(u8, a_name.slice(), b.user_repo_new.slice()), 1775 .user_user_new => |a_name| std.mem.eql(u8, a_name.slice(), b.user_user_new.slice()), 1776 .user_user => |a_name| std.mem.eql(u8, a_name.slice(), b.user_user.slice()), 1777 .repo_files => |a_f| std.mem.eql(u8, a_f.name.slice(), b.repo_files.name.slice()) and 1778 a_f.ref_kind == b.repo_files.ref_kind and 1779 std.mem.eql(u8, a_f.ref_value.slice(), b.repo_files.ref_value.slice()) and 1780 std.mem.eql(u8, a_f.patchrev_id.slice(), b.repo_files.patchrev_id.slice()) and 1781 std.mem.eql(u8, a_f.find.slice(), b.repo_files.find.slice()) and 1782 std.mem.eql(u8, a_f.path.slice(), b.repo_files.path.slice()) and 1783 a_f.line == b.repo_files.line, 1784 .repo_commits => |a_c| std.mem.eql(u8, a_c.name.slice(), b.repo_commits.name.slice()) and 1785 a_c.ref_or_oid == b.repo_commits.ref_or_oid and 1786 std.mem.eql(u8, a_c.value.slice(), b.repo_commits.value.slice()) and 1787 std.mem.eql(u8, a_c.base_oid.slice(), b.repo_commits.base_oid.slice()) and 1788 std.mem.eql(u8, a_c.search.slice(), b.repo_commits.search.slice()) and 1789 std.mem.eql(u8, a_c.from.slice(), b.repo_commits.from.slice()) and 1790 std.mem.eql(u8, a_c.merge.slice(), b.repo_commits.merge.slice()) and 1791 a_c.message == b.repo_commits.message, 1792 .repo_diff => |a_d| std.mem.eql(u8, a_d.name.slice(), b.repo_diff.name.slice()) and 1793 a_d.ref_or_oid == b.repo_diff.ref_or_oid and 1794 std.mem.eql(u8, a_d.value.slice(), b.repo_diff.value.slice()) and 1795 std.mem.eql(u8, a_d.base_oid.slice(), b.repo_diff.base_oid.slice()) and 1796 std.mem.eql(u8, a_d.patchrev_id.slice(), b.repo_diff.patchrev_id.slice()) and 1797 a_d.start == b.repo_diff.start and std.mem.eql(u8, a_d.path.slice(), b.repo_diff.path.slice()), 1798 .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()), 1799 .repo_issues => |a_i| std.mem.eql(u8, a_i.name.slice(), b.repo_issues.name.slice()) and 1800 std.mem.eql(u8, a_i.label.slice(), b.repo_issues.label.slice()) and 1801 std.mem.eql(u8, a_i.search.slice(), b.repo_issues.search.slice()) and 1802 std.mem.eql(u8, a_i.selected.slice(), b.repo_issues.selected.slice()) and 1803 std.mem.eql(u8, a_i.theirs.slice(), b.repo_issues.theirs.slice()) and 1804 a_i.view == b.repo_issues.view and 1805 a_i.comments_start == b.repo_issues.comments_start and 1806 std.mem.eql(u8, a_i.comment.slice(), b.repo_issues.comment.slice()), 1807 .repo_discussions => |a_t| std.mem.eql(u8, a_t.name.slice(), b.repo_discussions.name.slice()) and 1808 std.mem.eql(u8, a_t.label.slice(), b.repo_discussions.label.slice()) and 1809 std.mem.eql(u8, a_t.search.slice(), b.repo_discussions.search.slice()) and 1810 std.mem.eql(u8, a_t.selected.slice(), b.repo_discussions.selected.slice()) and 1811 a_t.view == b.repo_discussions.view and 1812 a_t.comments_start == b.repo_discussions.comments_start and 1813 std.mem.eql(u8, a_t.comment.slice(), b.repo_discussions.comment.slice()), 1814 .repo_patches => |a_p| std.mem.eql(u8, a_p.name.slice(), b.repo_patches.name.slice()) and 1815 std.mem.eql(u8, a_p.label.slice(), b.repo_patches.label.slice()) and 1816 std.mem.eql(u8, a_p.search.slice(), b.repo_patches.search.slice()) and 1817 std.mem.eql(u8, a_p.selected.slice(), b.repo_patches.selected.slice()) and 1818 std.mem.eql(u8, a_p.theirs.slice(), b.repo_patches.theirs.slice()) and 1819 a_p.view == b.repo_patches.view and 1820 a_p.comments_start == b.repo_patches.comments_start and 1821 std.mem.eql(u8, a_p.comment.slice(), b.repo_patches.comment.slice()), 1822 .repo_undo => |a_u| std.mem.eql(u8, a_u.name.slice(), b.repo_undo.name.slice()) and 1823 a_u.index == b.repo_undo.index and 1824 a_u.clear == b.repo_undo.clear, 1825 .repo_events => |a_e| std.mem.eql(u8, a_e.name.slice(), b.repo_events.name.slice()) and 1826 a_e.view == b.repo_events.view and 1827 a_e.moment == b.repo_events.moment and 1828 a_e.kind == b.repo_events.kind and 1829 std.mem.eql(u8, a_e.selected.slice(), b.repo_events.selected.slice()), 1830 .repo_repo_new => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_repo_new.slice()), 1831 .repo_user_new => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_user_new.slice()), 1832 .repo_settings => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_settings.slice()), 1833 .repo_roles => |a_r| std.mem.eql(u8, a_r.name.slice(), b.repo_roles.name.slice()) and 1834 a_r.start == b.repo_roles.start and 1835 std.mem.eql(u8, a_r.user.slice(), b.repo_roles.user.slice()), 1836 .repo_user => |a_name| std.mem.eql(u8, a_name.slice(), b.repo_user.slice()), 1837 .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()), 1838 .fork_diff => |a_d| std.mem.eql(u8, a_d.fork.name.slice(), b.fork_diff.fork.name.slice()) and 1839 std.mem.eql(u8, a_d.fork.id.slice(), b.fork_diff.fork.id.slice()) and 1840 std.mem.eql(u8, a_d.oid.slice(), b.fork_diff.oid.slice()) and 1841 std.mem.eql(u8, a_d.base_oid.slice(), b.fork_diff.base_oid.slice()) and 1842 a_d.start == b.fork_diff.start and std.mem.eql(u8, a_d.path.slice(), b.fork_diff.path.slice()), 1843 .fork_files => |a_f| std.mem.eql(u8, a_f.fork.name.slice(), b.fork_files.fork.name.slice()) and 1844 std.mem.eql(u8, a_f.fork.id.slice(), b.fork_files.fork.id.slice()) and 1845 std.mem.eql(u8, a_f.oid.slice(), b.fork_files.oid.slice()) and 1846 std.mem.eql(u8, a_f.find.slice(), b.fork_files.find.slice()) and 1847 std.mem.eql(u8, a_f.path.slice(), b.fork_files.path.slice()) and 1848 a_f.line == b.fork_files.line, 1849 .fork_commits => |a_c| std.mem.eql(u8, a_c.fork.name.slice(), b.fork_commits.fork.name.slice()) and 1850 std.mem.eql(u8, a_c.fork.id.slice(), b.fork_commits.fork.id.slice()) and 1851 std.mem.eql(u8, a_c.oid.slice(), b.fork_commits.oid.slice()) and 1852 a_c.message == b.fork_commits.message, 1853 .fork_repo_new => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_repo_new.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_repo_new.id.slice()), 1854 .fork_user_new => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_user_new.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_user_new.id.slice()), 1855 .fork_user => |a_f| std.mem.eql(u8, a_f.name.slice(), b.fork_user.name.slice()) and std.mem.eql(u8, a_f.id.slice(), b.fork_user.id.slice()), 1856 else => true, 1857 }; 1858 } 1859 1860 // true when following a link from `b` to `a` (both repo routes) should reload 1861 // rather than stay in page 1862 pub fn repoPageChanged(a: RoutablePage, b: RoutablePage) bool { 1863 if (a.parent() != .repo or b.parent() != .repo) return false; 1864 if (std.meta.activeTag(a) == .repo_refs and std.meta.activeTag(b) == .repo_refs and 1865 a.repo_refs.from.len == 0 and b.repo_refs.from.len == 0) 1866 return !std.mem.eql(u8, a.repo_refs.name.slice(), b.repo_refs.name.slice()); 1867 return !a.eql(b); 1868 } 1869 1870 pub fn forkPageChanged(a: RoutablePage, b: RoutablePage) bool { 1871 return a.parent() == .fork and b.parent() == .fork and !a.eql(b); 1872 } 1873 1874 // true when `a` and `b` are the same user paginated to a different repos 1875 // window or search. switching between a user's tabs is in-page, so only a 1876 // changed `start` or search on the same list navigates. 1877 pub fn userPageChanged(a: RoutablePage, b: RoutablePage) bool { 1878 return switch (a) { 1879 .user_repos => |aa| switch (b) { 1880 .user_repos => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and !a.eql(b), 1881 else => false, 1882 }, 1883 .user_forks => |aa| switch (b) { 1884 .user_forks => |bb| std.mem.eql(u8, aa.name.slice(), bb.name.slice()) and aa.start != bb.start, 1885 else => false, 1886 }, 1887 else => false, 1888 }; 1889 } 1890 1891 // true when `a` and `b` are the same home list tab paginated to a different 1892 // window or search. switching tabs is in-page, so only a changed `start` or 1893 // search on the same tab navigates. 1894 pub fn homePageChanged(a: RoutablePage, b: RoutablePage) bool { 1895 return switch (a) { 1896 .home_users => switch (b) { 1897 .home_users => !a.eql(b), 1898 else => false, 1899 }, 1900 else => false, 1901 }; 1902 } 1903 }; 1904 1905 // a `RefOrOid` resolved against an on-disk repo to the commit oid it points at 1906 fn isUnreserved(c: u8) bool { 1907 return std.ascii.isAlphanumeric(c) or c == '-' or c == '.' or c == '_' or c == '~'; 1908 } 1909 1910 // percent-encode a ref name for use as a single url path segment. 1911 pub fn urlEncodeRef(aa: std.mem.Allocator, raw: []const u8) ![]const u8 { 1912 var out: std.Io.Writer.Allocating = .init(aa); 1913 try std.Uri.Component.percentEncode(&out.writer, raw, isUnreserved); 1914 return out.written(); 1915 } 1916 1917 // a resolved ref/oid for a repo opened with `repo_kind`/`repo_opts` 1918 pub fn ResolvedRefOrOid(comptime repo_kind: rp.RepoKind, comptime repo_opts: rp.RepoOpts(repo_kind)) type { 1919 return struct { 1920 pub const hex_len = hash.hexLen(repo_opts.hash); 1921 1922 // the concrete ref/oid (a null request resolves to the default branch) 1923 ref_or_oid: RoutablePage.RefOrOid, 1924 // its branch/tag name or oid, url-encoded (ref names may contain '/', so 1925 // the route layer — which splits on '/' — stores them encoded). duped 1926 // into `aa`. 1927 value: []const u8, 1928 // the commit oid it points at 1929 oid: [hex_len]u8, 1930 1931 const Self = @This(); 1932 1933 // resolve a requested ref/oid (null = the repo's default branch) to a 1934 // concrete ref/oid plus the commit oid it points at. `requested_value` is 1935 // the url-encoded form (as it appears in the route). null when it doesn't 1936 // resolve (an unknown branch/tag, or a malformed/unknown oid). 1937 pub fn init( 1938 repo: *rp.Repo(repo_kind, repo_opts), 1939 io: std.Io, 1940 aa: std.mem.Allocator, 1941 requested_ref_or_oid: ?RoutablePage.RefOrOid, 1942 requested_value: []const u8, 1943 ) !?Self { 1944 var ref_or_oid: RoutablePage.RefOrOid = requested_ref_or_oid orelse .branch; 1945 // the decoded ref name / oid to look up. a named ref arrives url-encoded. 1946 var value: []const u8 = if (requested_ref_or_oid == null) 1947 requested_value 1948 else 1949 std.Uri.percentDecodeInPlace(try aa.dupe(u8, requested_value)); 1950 // no ref named: fall back to HEAD's branch (or its oid when detached). 1951 var head_buf: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1952 if (requested_ref_or_oid == null) { 1953 if (repo.head(io, &head_buf)) |head| switch (head) { 1954 .ref => |r| { 1955 ref_or_oid = .branch; 1956 value = r.name; 1957 }, 1958 .oid => |o| { 1959 ref_or_oid = .object; 1960 value = o; 1961 }, 1962 } else |_| {} 1963 } 1964 1965 var oid: [hex_len]u8 = undefined; 1966 switch (ref_or_oid) { 1967 .object => { 1968 if (value.len != hex_len) return null; 1969 @memcpy(&oid, value); 1970 }, 1971 .branch, .tag => { 1972 const ref_kind: rf.RefKind = if (ref_or_oid == .branch) .head else .tag; 1973 oid = (repo.readRef(io, .{ .kind = ref_kind, .name = value }) catch null) orelse return null; 1974 }, 1975 } 1976 // store url-encoded so the route layer can hold the value verbatim. 1977 return .{ .ref_or_oid = ref_or_oid, .value = try urlEncodeRef(aa, value), .oid = oid }; 1978 } 1979 }; 1980 } 1981 1982 // who a repo page is built for, which picks the actions it shows 1983 pub const Viewer = struct { 1984 role: evt.Repo.Role, 1985 email: []const u8, 1986 1987 // the session's viewer of a repo it holds `role` in, or null when logged 1988 // out. local mode owns everything. 1989 pub fn init(session: *Session, arena: *std.heap.ArenaAllocator, role: evt.Repo.Role) !?Viewer { 1990 if (session.data.host_kind == .local) return .{ .role = .owner, .email = "" }; 1991 const user_id = session.userId() orelse return null; 1992 const moment = session.haxy_moment orelse unreachable; 1993 const user = (try activeUser(moment, arena, user_id)) orelse return null; 1994 return .{ .role = role, .email = user.event.email }; 1995 } 1996 }; 1997 1998 // a repo a page shows, and what its viewer may do there 1999 pub const RepoHandle = struct { 2000 location: RoutablePage.RepoLocation, 2001 viewer: ?Viewer = null, 2002 2003 pub fn dupe(self: RepoHandle, allocator: std.mem.Allocator) !RepoHandle { 2004 var copy = self; 2005 copy.location = try self.location.dupe(allocator); 2006 return copy; 2007 } 2008 2009 // pushing, and seeing the events and undo tabs. local mode is always owner. 2010 pub fn canWrite(self: RepoHandle) bool { 2011 const viewer = self.viewer orelse return false; 2012 return viewer.role.atLeast(.write); 2013 } 2014 2015 pub fn canUndo(self: RepoHandle) bool { 2016 const viewer = self.viewer orelse return false; 2017 return viewer.role == .owner; 2018 } 2019 }; 2020 2021 // where a repo page reads its on-disk repo from 2022 pub const RepoSource = struct { 2023 path: []const u8, 2024 repo_kind: rp.RepoKind, 2025 // the user's global git config, so local mode picks up their identity and 2026 // their signing and ssh settings. null on the server paths, which must not 2027 // read whoever runs the server. 2028 global_config_path: ?[]const u8 = null, 2029 2030 pub fn localInitOpts(self: RepoSource) rp.InitOpts { 2031 return .{ .path = self.path, .global_config_path = self.global_config_path }; 2032 } 2033 2034 pub fn hasBranch(self: RepoSource, io: std.Io, allocator: std.mem.Allocator, branch: []const u8) !bool { 2035 if (!xit.ref.validateName(branch)) return false; 2036 return switch (self.repo_kind) { 2037 inline else => |repo_kind| blk: { 2038 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 2039 defer any_repo.deinit(io, allocator); 2040 break :blk switch (any_repo) { 2041 inline else => |*repo| (try repo.readRef(io, .{ .kind = .head, .name = branch })) != null, 2042 }; 2043 }, 2044 }; 2045 } 2046 2047 // whether the actor may change the event `id` names 2048 pub fn canModify( 2049 self: RepoSource, 2050 io: std.Io, 2051 allocator: std.mem.Allocator, 2052 actor: Actor, 2053 kind: evt.EventKind, 2054 id: *const [evt.event_id_size]u8, 2055 ) !bool { 2056 // a role that may change an event with no author needs no read 2057 if (actor.role.canModify(actor.author.email, null)) return true; 2058 2059 var arena = std.heap.ArenaAllocator.init(allocator); 2060 defer arena.deinit(); 2061 return switch (self.repo_kind) { 2062 inline else => |repo_kind| blk: { 2063 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, self.localInitOpts()); 2064 defer any_repo.deinit(io, allocator); 2065 break :blk switch (any_repo) { 2066 inline else => |*repo| actor.role.canModify(actor.author.email, try evt.readAuthorEmail(repo_kind, repo.self_repo_opts, io, allocator, &arena, repo, kind, id)), 2067 }; 2068 }, 2069 }; 2070 } 2071 }; 2072 2073 // who a write acts as and the role they hold in its repo 2074 pub const Actor = struct { 2075 author: evt.CommitAuthor, 2076 // local mode has no accounts, so it has no user id 2077 user_id: ?[evt.event_id_size]u8 = null, 2078 role: evt.Repo.Role, 2079 // the repo owner's id, which local mode lacks 2080 repo_user_id: ?[evt.event_id_size]u8 = null, 2081 }; 2082 2083 pub const Authorization = union(enum) { 2084 actor: Actor, 2085 login_required, 2086 repo_not_found, 2087 forbidden, 2088 }; 2089 2090 // the account `user_id` names, or null once it has been removed. a user id is 2091 // checked here wherever one enters, so the role lookups can trust theirs. 2092 pub fn activeUser( 2093 moment: evt.AdminDB.HashMap(.read_only), 2094 arena: *std.heap.ArenaAllocator, 2095 user_id: [evt.event_id_size]u8, 2096 ) !?evt.User.Record { 2097 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id)) orelse return null; 2098 return if (user.removed) null else user; 2099 } 2100 2101 // what the typed assignee resolves to 2102 pub const AssigneeLookup = union(enum) { 2103 email: []const u8, 2104 failure: Session.FormFeedback.AssignmentFailure, 2105 }; 2106 2107 // the email `text` assigns: itself when it holds an @, else the email of the 2108 // user it names. a null moment is local mode, which has no users to look up. 2109 pub fn assigneeEmail(moment_maybe: ?evt.AdminDB.HashMap(.read_only), arena: *std.heap.ArenaAllocator, text: []const u8) !AssigneeLookup { 2110 const trimmed = std.mem.trim(u8, text, " \t\r\n"); 2111 if (std.mem.indexOfScalar(u8, trimmed, '@') != null) return .{ .email = trimmed }; 2112 const moment = moment_maybe orelse return .{ .failure = .not_an_email }; 2113 const user_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, trimmed)) orelse return .{ .failure = .not_found }; 2114 const user = (try activeUser(moment, arena, user_id)) orelse return .{ .failure = .not_found }; 2115 return .{ .email = user.event.email }; 2116 } 2117 2118 // whether the user may write to the repo `identity` names with at least 2119 // `min_role`, and to the `thread_kind` tab when there is one 2120 pub fn authorizeUser( 2121 io: std.Io, 2122 moment: evt.AdminDB.HashMap(.read_only), 2123 arena: *std.heap.ArenaAllocator, 2124 users_dir: []const u8, 2125 user_id: [evt.event_id_size]u8, 2126 identity: []const u8, 2127 min_role: evt.Repo.Role, 2128 thread_kind: ?evt.EventKind, 2129 ) !Authorization { 2130 const user = (try activeUser(moment, arena, user_id)) orelse return .login_required; 2131 2132 const owner_repo = evt.parseOwnerRepoPath(identity) orelse return .repo_not_found; 2133 const repo = (try evt.readRepoByOwnerAndName(io, arena.child_allocator, arena, moment, users_dir, owner_repo.owner, owner_repo.name, user_id)) orelse return .repo_not_found; 2134 // a repo the user can't read doesn't exist to them 2135 const role = repo.role orelse return .repo_not_found; 2136 if (!role.atLeast(min_role)) return .forbidden; 2137 if (thread_kind) |kind| { 2138 // a tab that is off doesn't exist 2139 const tab_role = repo.repo.event.threadRole(kind) orelse return .repo_not_found; 2140 if (!role.atLeast(tab_role)) return .forbidden; 2141 } 2142 return .{ .actor = .{ 2143 .author = .{ .name = user.event.name, .email = user.event.email }, 2144 .user_id = user_id, 2145 .role = role, 2146 .repo_user_id = repo.repo.event.user_id[0..evt.event_id_size].*, 2147 } }; 2148 } 2149 2150 // events created in local mode have no account behind them, so the commit 2151 // author comes from the repo's config, which the global config feeds into. 2152 pub const local_author_fallback = evt.CommitAuthor{ .name = "haxy", .email = "user@haxy" }; 2153 2154 pub fn localAuthor( 2155 src: RepoSource, 2156 io: std.Io, 2157 allocator: std.mem.Allocator, 2158 arena: *std.heap.ArenaAllocator, 2159 ) !evt.CommitAuthor { 2160 switch (src.repo_kind) { 2161 inline else => |repo_kind| { 2162 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()) catch return local_author_fallback; 2163 defer any_repo.deinit(io, allocator); 2164 2165 switch (any_repo) { 2166 inline else => |*repo| { 2167 var config = try repo.listConfig(io, allocator); 2168 defer config.deinit(); 2169 2170 const user = config.sections.get("user") orelse return local_author_fallback; 2171 const name = user.get("name") orelse return local_author_fallback; 2172 const email = user.get("email") orelse return local_author_fallback; 2173 return .{ 2174 .name = try arena.allocator().dupe(u8, name), 2175 .email = try arena.allocator().dupe(u8, email), 2176 }; 2177 }, 2178 } 2179 }, 2180 } 2181 } 2182 2183 // per-connection mutable state. each SSH session / web session / local TUI 2184 // run gets its own. 2185 pub const Session = struct { 2186 data: Data = .{}, 2187 // session-lifetime allocations: login/user_id, persisted prefs, queued 2188 // actions. lives as long as the connection. 2189 arena: *std.heap.ArenaAllocator, 2190 // current page's allocations: page data and the page-scoped strings widgets 2191 // build (form actions, links). owned by Nav, swapped on each navigation, so 2192 // it doesn't accumulate over a long-lived session. on the web/wasm paths, 2193 // where a render is one-shot, this points at the same arena as `arena`. 2194 page_arena: *std.heap.ArenaAllocator, 2195 haxy_moment: ?evt.AdminDB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 2196 // filesystem io and the path to <server>/repos, for opening on-disk repos 2197 // during page construction. both null on wasm, which has no filesystem and 2198 // rebuilds pages from the serialized snapshot rather than from disk. 2199 io: ?std.Io = null, 2200 admin_repo: ?*rp.Repo(.xit, evt.admin_repo_opts) = null, 2201 users_dir: ?[]const u8 = null, 2202 // the single on-disk repo this session views, when running in local mode 2203 // (haxy invoked with no arguments inside a repo). null on the server paths, 2204 // which resolve repos from the admin db instead. 2205 local: ?RepoSource = null, 2206 // focus id -> the live TextInput, refreshed each frame by the views that own 2207 // inputs. web/wasm form handling looks widgets up here by focus id. 2208 text_inputs: std.AutoHashMapUnmanaged(usize, *wgt.TextInput) = .empty, 2209 back: enum { unavailable, available, requested } = .unavailable, 2210 refresh_requested: bool = false, // set by input (ctrl+r) 2211 // a requested forward navigation. set this (via navigate) to move to a new 2212 // page; Nav.sync builds it and then copies it into current_page, clearing 2213 // this back to null. setting current_page directly only updates the url and 2214 // does not navigate. 2215 next_page: ?RoutablePage = null, 2216 is_terminal: bool = false, // true on remote SSH and local TUI 2217 color: bool = true, // false when NO_COLOR is set, which also hides the art 2218 // the terminal's reported default background, which the art blends toward 2219 terminal_background: ?Grid.Color.Rgb = null, 2220 // port the web UI is served on, for the TUI/SSH footer's "http://localhost:<port>..." 2221 // url. null on the web itself (no footer there). 2222 web_port: ?u16 = null, 2223 // a host operation requested by a widget 2224 host_request: ?HostRequest = null, 2225 quit_requested: bool = false, 2226 2227 pub const HostRequest = union(enum) { 2228 show_copyable_text: []const u8, 2229 sync_events, 2230 undo: RoutablePage.RepoUndoRoute, 2231 }; 2232 2233 pub const FormFeedback = union(enum) { 2234 pub const LoginFailure = enum { unknown_user, wrong_password }; 2235 pub const ThreadFailure = enum { required_title }; 2236 pub const PatchFailure = enum { 2237 required_title, 2238 invalid_target_branch, 2239 invalid_source_branch, 2240 same_branch, 2241 unrelated_branches, 2242 2243 pub fn fromError(err: anyerror) ?PatchFailure { 2244 return switch (err) { 2245 error.InvalidTargetBranch => .invalid_target_branch, 2246 error.InvalidSourceBranch => .invalid_source_branch, 2247 error.SameBranch => .same_branch, 2248 error.UnrelatedBranches => .unrelated_branches, 2249 else => null, 2250 }; 2251 } 2252 }; 2253 2254 pub const RepoFailure = enum { 2255 required_name, 2256 invalid_name, 2257 name_taken, 2258 2259 pub fn fromError(err: anyerror) ?RepoFailure { 2260 return switch (err) { 2261 error.NameEmpty => .required_name, 2262 error.NameTooLong, error.InvalidName => .invalid_name, 2263 error.NameTaken => .name_taken, 2264 else => null, 2265 }; 2266 } 2267 }; 2268 2269 pub const RolesFailure = enum { not_found, already_added }; 2270 2271 pub const AssignmentFailure = enum { not_found, not_an_email, already_assigned }; 2272 2273 pub const UserFailure = enum { 2274 required_name, 2275 invalid_name, 2276 name_taken, 2277 required_email, 2278 email_taken, 2279 required_password, 2280 password_mismatch, 2281 wrong_password, 2282 2283 pub fn fromError(err: anyerror) ?UserFailure { 2284 return switch (err) { 2285 error.NameEmpty => .required_name, 2286 error.NameTooShort, error.NameTooLong, error.InvalidName => .invalid_name, 2287 error.NameTaken => .name_taken, 2288 error.EmailEmpty => .required_email, 2289 error.EmailTaken => .email_taken, 2290 error.PasswordEmpty => .required_password, 2291 error.PasswordMismatch => .password_mismatch, 2292 error.WrongPassword => .wrong_password, 2293 else => null, 2294 }; 2295 } 2296 }; 2297 2298 login: struct { 2299 failure: LoginFailure, 2300 username: []const u8, 2301 }, 2302 user: struct { 2303 failure: UserFailure, 2304 // passwords are never retained 2305 fields: struct { 2306 name: []const u8, 2307 email: []const u8, 2308 }, 2309 }, 2310 user_settings: struct { 2311 failure: UserFailure, 2312 // passwords are never retained 2313 fields: struct { email: []const u8 }, 2314 }, 2315 repo: struct { 2316 failure: RepoFailure, 2317 fields: struct { 2318 name: []const u8, 2319 description: []const u8, 2320 hash: hash.HashKind, 2321 access: evt.Repo.Access, 2322 }, 2323 }, 2324 repo_settings: struct { 2325 failure: RepoFailure, 2326 fields: struct { 2327 name: []const u8, 2328 description: []const u8, 2329 access: evt.Repo.Access, 2330 discuss_role: ?evt.Repo.Role, 2331 issue_role: ?evt.Repo.Role, 2332 patch_role: ?evt.Repo.Role, 2333 }, 2334 }, 2335 repo_roles: struct { 2336 failure: RolesFailure, 2337 name: []const u8, 2338 }, 2339 assignment: struct { 2340 failure: AssignmentFailure, 2341 text: []const u8, 2342 }, 2343 issue: struct { 2344 failure: ThreadFailure, 2345 fields: ?struct { 2346 title: []const u8, 2347 labels: []const u8, 2348 description: []const u8, 2349 } = null, 2350 }, 2351 patch: struct { 2352 failure: PatchFailure, 2353 fields: ?struct { 2354 title: []const u8, 2355 labels: []const u8, 2356 description: []const u8, 2357 target_branch: []const u8, 2358 source_branch: []const u8 = "", 2359 source_kind: Repo.Patches.Source.Kind = .fork, 2360 } = null, 2361 }, 2362 discussion: struct { 2363 failure: ThreadFailure, 2364 fields: ?struct { 2365 title: []const u8, 2366 labels: []const u8, 2367 description: []const u8, 2368 } = null, 2369 }, 2370 }; 2371 2372 const Self = @This(); 2373 2374 // serializable data sent down to web client 2375 pub const Data = struct { 2376 user_id: ?[]const u8 = null, 2377 // the logged-in user's name and email, for the views that show them 2378 user_name: ?[]const u8 = null, 2379 user_email: ?[]const u8 = null, 2380 // a failed form retained long enough to render it for correction 2381 form_feedback: ?FormFeedback = null, 2382 // a transient local event-sync error 2383 sync_failure: ?[]const u8 = null, 2384 // a failed terminal undo retained on the current tab 2385 undo_failure: ?[]const u8 = null, 2386 current_page: RoutablePage = .default, 2387 // the selected ANSI art. the server serializes one image for WASM so 2388 // the browser does not need the whole build-generated collection. 2389 ansi_art: []const u8 = "", 2390 // the bound git-service ports. absent in local mode, where remote urls 2391 // are not shown. 2392 git_http_port: ?u16 = null, 2393 git_ssh_port: ?u16 = null, 2394 git_ssh_prefix: []const u8 = "", 2395 // whether this session views a local repo or the server. unlike `local` 2396 // (the host-side filesystem source) this travels in the snapshot, so 2397 // the wasm side also parses elided link urls and hides the multi-user 2398 // chrome. 2399 host_kind: evt.HostKind = .server, 2400 }; 2401 2402 pub fn init( 2403 arena: *std.heap.ArenaAllocator, 2404 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2405 data: Data, 2406 ) !Self { 2407 var session = Self{ 2408 .data = data, 2409 .arena = arena, 2410 // until a host swaps in a page-scoped arena (Nav does this), page 2411 // allocations land in the session arena. on the web path that's the 2412 // intended behavior, since the whole arena is per-request. 2413 .page_arena = arena, 2414 .admin_repo = repo, 2415 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 2416 }; 2417 try session.loadUser(); 2418 return session; 2419 } 2420 2421 // load the logged-in user's name and persisted preferences from the db 2422 pub fn loadUser(self: *Self) !void { 2423 const user_id = self.userId() orelse return; 2424 const moment = self.haxy_moment orelse return; 2425 const user = (try activeUser(moment, self.arena, user_id)) orelse return self.logOut(); 2426 self.data.user_name = user.event.name; 2427 self.data.user_email = user.event.email; 2428 } 2429 2430 // forget the logged-in user, on a logout or once their account is removed 2431 pub fn logOut(self: *Self) void { 2432 self.data.user_id = null; 2433 self.data.user_name = null; 2434 self.data.user_email = null; 2435 } 2436 2437 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 2438 const bytes = self.data.user_id orelse return null; 2439 if (bytes.len != evt.event_id_size) return null; 2440 var id: [evt.event_id_size]u8 = undefined; 2441 @memcpy(&id, bytes); 2442 return id; 2443 } 2444 2445 pub fn formFeedback(self: *const Self, comptime tag: std.meta.Tag(FormFeedback)) ?@FieldType(FormFeedback, @tagName(tag)) { 2446 const feedback = self.data.form_feedback orelse return null; 2447 if (std.meta.activeTag(feedback) != tag) return null; 2448 return @field(feedback, @tagName(tag)); 2449 } 2450 2451 pub fn clearFormFeedback(self: *Self) void { 2452 self.data.form_feedback = null; 2453 } 2454 2455 // the actor for a write to the repo `identity` names, or null when the 2456 // session may not write there with at least `min_role` or in the 2457 // `thread_kind` tab. local mode always writes. 2458 pub fn authorize(self: *Self, identity: []const u8, min_role: evt.Repo.Role, thread_kind: ?evt.EventKind) !?Actor { 2459 if (self.data.host_kind == .local) { 2460 const author = blk: { 2461 const src = self.local orelse break :blk local_author_fallback; 2462 const io = self.io orelse break :blk local_author_fallback; 2463 break :blk try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 2464 }; 2465 return .{ .author = author, .role = .owner }; 2466 } 2467 const user_id = self.userId() orelse return null; 2468 // the session's moment predates this input, so read the roles afresh 2469 const admin_repo = self.admin_repo orelse return null; 2470 const io = self.io orelse return null; 2471 const users_dir = self.users_dir orelse return null; 2472 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2473 return switch (try authorizeUser(io, moment, self.page_arena, users_dir, user_id, identity, min_role, thread_kind)) { 2474 .actor => |actor| actor, 2475 .login_required, .repo_not_found, .forbidden => null, 2476 }; 2477 } 2478 2479 // where this session's event writes go. a server session always has a users dir 2480 pub fn eventHost(self: *const Self) evt.Host { 2481 return switch (self.data.host_kind) { 2482 .local => .local, 2483 .server => .{ .server = .{ .users_dir = self.users_dir orelse unreachable } }, 2484 }; 2485 } 2486 2487 // reload the moment from the admin repo and log out a removed user 2488 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 2489 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 2490 self.haxy_moment = moment; 2491 2492 const user_id = self.userId() orelse return; 2493 var arena = std.heap.ArenaAllocator.init(allocator); 2494 defer arena.deinit(); 2495 if (try activeUser(moment, &arena, user_id) == null) self.logOut(); 2496 } 2497 2498 // request a forward navigation to `route`; the host consumes next_page 2499 // (Nav.sync on the terminal, the wasm tick on the web). 2500 pub fn navigate(self: *Session, route: RoutablePage) !void { 2501 self.clearFormFeedback(); 2502 self.next_page = route; 2503 } 2504 }; 2505 2506 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2507 var nav = try Nav.init(allocator, session); 2508 defer nav.deinit(allocator); 2509 2510 var terminal = try term.Terminal.init(io, allocator); 2511 terminal.render_state.no_color = !session.color; 2512 var terminal_live = true; 2513 defer if (terminal_live) terminal.deinit(io); 2514 2515 // set term as active so it will be properly cooked 2516 // when a panic/segfault happens 2517 term.setActive(&terminal); 2518 defer term.setActive(null); 2519 2520 try terminal.queryBackground(); 2521 2522 while (!terminal.shouldQuit()) { 2523 const grid_changed = try terminal.render(&nav.root); 2524 2525 // process any inputs. 2526 // 2527 // if the grid didn't change, then first do a blocking 2528 // read, so the thread will sleep until further input. 2529 // after that, all remaining reads are non-blocking so 2530 // we can process the rest of the queued inputs. 2531 // 2532 // if the grid *did* change, then only do non-blocking 2533 // reads. we do not want to sleep the thread because 2534 // there may be an animation that requires more looping. 2535 var blocking = !grid_changed; 2536 while (try terminal.readKey(io, blocking)) |key| { 2537 blocking = false; 2538 try inputKey(allocator, &nav.root, key, session); 2539 } 2540 2541 if (session.host_request) |request| { 2542 session.host_request = null; 2543 switch (request) { 2544 .show_copyable_text => |copyable_text| { 2545 term.setActive(null); 2546 terminal.deinit(io); 2547 terminal_live = false; 2548 showCopyableText(io, copyable_text) catch |show_err| { 2549 terminal = try term.Terminal.init(io, allocator); 2550 terminal_live = true; 2551 term.setActive(&terminal); 2552 return show_err; 2553 }; 2554 terminal = try term.Terminal.init(io, allocator); 2555 terminal.render_state.no_color = !session.color; 2556 terminal_live = true; 2557 term.setActive(&terminal); 2558 }, 2559 .undo => |target| Repo.Undo.handleRequest(allocator, session, target), 2560 .sync_events => { 2561 try nav.root.build(allocator, .{ 2562 .min_size = .{ .width = null, .height = null }, 2563 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2564 }, nav.root.getFocus()); 2565 _ = try terminal.render(&nav.root); 2566 try Repo.Events.performSync(allocator, session); 2567 }, 2568 } 2569 } 2570 2571 // reload so the next navigation builds its page from the current moment 2572 if (repo_maybe) |repo| try session.reloadMoment(allocator, repo); 2573 2574 // reconcile navigation: forward to a new page, or back on escape. 2575 // the terminal background can change at any time, so recheck it on each page 2576 if (try nav.sync(allocator, session)) try terminal.queryBackground(); 2577 2578 // the quit button (on the quit tab) asks the host to tear down. 2579 if (session.quit_requested) terminal.requestQuit(); 2580 2581 try nav.root.build(allocator, .{ 2582 .min_size = .{ .width = null, .height = null }, 2583 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2584 }, nav.root.getFocus()); 2585 } 2586 } 2587 2588 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2589 const tty: ?std.Io.File = if (builtin.target.os.tag == .windows) 2590 null 2591 else 2592 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2593 defer if (tty) |file| file.close(io); 2594 2595 const input = if (tty) |file| file else std.Io.File.stdin(); 2596 const output = if (tty) |file| file else std.Io.File.stdout(); 2597 2598 var write_buf: [1024]u8 = undefined; 2599 var writer = output.writer(io, &write_buf); 2600 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}); 2601 try writer.interface.flush(); 2602 2603 var read_buf: [1]u8 = undefined; 2604 var reader = input.reader(io, &read_buf); 2605 while (true) { 2606 const byte = try reader.interface.takeByte(); 2607 if (byte == '\r' or byte == '\n') return; 2608 } 2609 } 2610 2611 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2612 const root_focus = root.getFocus(); 2613 const focused_before = root_focus.grandchild_id; 2614 var activate_in_page_link = false; 2615 switch (key) { 2616 // request a navigation pop; the host's Nav.sync goes back a page, or 2617 // quits when there's no history left. 2618 .escape => { 2619 session.back = .requested; 2620 return; 2621 }, 2622 .ctrl => |letter| switch (letter) { 2623 'r' => { 2624 session.refresh_requested = true; 2625 return; 2626 }, 2627 else => try root.input(allocator, key, root_focus), 2628 }, 2629 .enter => { 2630 if (root_focus.grandchild_id) |gid| { 2631 // follow a cross-page link 2632 if (crossPageLink(root_focus, gid, session.data)) |route| { 2633 return session.navigate(route); 2634 } 2635 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2636 } 2637 activate_in_page_link = true; 2638 try root.input(allocator, key, root_focus); 2639 }, 2640 .mouse => |mouse| { 2641 if (mouse.action == .press and mouse.action.press == .left) { 2642 if (cellPageLink(root, mouse.x, mouse.y, session.data)) |route| return session.navigate(route); 2643 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2644 const focus_id = hit.id; 2645 if (hit.kind == .background) { 2646 root_focus.setFocus(focus_id); 2647 return; 2648 } 2649 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2650 if (session.back == .available) session.back = .requested; 2651 return; 2652 } 2653 // follow a cross-page link 2654 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2655 return session.navigate(route); 2656 } 2657 if (currentPageLink(root_focus, focus_id, session.data)) return; 2658 if (rawLink(root_focus, focus_id)) |url| { 2659 if (!mouse.ctrl) try requestRawLink(session, url); 2660 return; 2661 } 2662 root_focus.setFocus(focus_id); 2663 activate_in_page_link = true; 2664 } 2665 // forward the press into the widget tree so buttons (and any 2666 // future click-aware widgets) can react. widgets that don't 2667 // care about presses ignore it. 2668 try root.input(allocator, key, root_focus); 2669 } else { 2670 try root.input(allocator, key, root_focus); 2671 } 2672 }, 2673 .event => |event| switch (event) { 2674 .background => |rgb| session.terminal_background = rgb, 2675 .resize => try root.input(allocator, key, root_focus), 2676 }, 2677 else => try root.input(allocator, key, root_focus), 2678 } 2679 2680 const focused_after = root_focus.grandchild_id; 2681 if (!activate_in_page_link and focused_before == focused_after) return; 2682 const focus_id = focused_after orelse return; 2683 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2684 } 2685 2686 // a link to bytes the server serves directly rather than to a page route, so 2687 // it never resolves to a RoutablePage 2688 pub const raw_link_prefix = "ax:"; 2689 2690 // a route already loaded into the current page; focusing it changes the url 2691 // without rebuilding the page 2692 pub const in_page_link_prefix = "ai:"; 2693 2694 // an in-page tab's base route, or the exact current route when it is selected 2695 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2696 const target = if (selected) session.data.current_page else route; 2697 return session.page_arena.allocator().print("{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2698 } 2699 2700 // a button the web renderer covers with a file picker that posts the chosen 2701 // file to the url after the prefix 2702 pub const file_input_prefix = "file:"; 2703 2704 // a submit button that overrides its enclosing form's action 2705 pub const submit_action_prefix = "submit:"; 2706 2707 // one option of a radio field, as `radio:<name>=<value>` 2708 pub const radio_prefix = "radio:"; 2709 2710 // the url an `ax:` link points at, or null for anything else. a host that 2711 // suppresses the anchor's own navigation needs this to follow the link. 2712 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2713 const child = root_focus.children.get(focus_id) orelse return null; 2714 const custom = switch (child.focus.kind) { 2715 .custom => |c| c, 2716 else => return null, 2717 }; 2718 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2719 return custom[raw_link_prefix.len..]; 2720 } 2721 2722 fn requestRawLink(session: *Session, url: []const u8) !void { 2723 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2724 } 2725 2726 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2727 // an absolute url is already complete 2728 if (!std.mem.startsWith(u8, path, "/")) return path; 2729 return if (session.web_port) |port| 2730 try allocator.print("http://localhost:{d}{s}", .{ port, path }) 2731 else 2732 path; 2733 } 2734 2735 // if the focus target at focus_id is an `a:` link that should navigate (a 2736 // different parent page, or a different files directory within the repo page), 2737 // return its route; otherwise null. lets a host turn a click / enter on such a 2738 // link into a navigation rather than a focus change. 2739 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2740 const current = data.current_page; 2741 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2742 // a link to a different parent page always navigates; within a page, a 2743 // files-directory / commits-page / list-window change navigates while tab 2744 // links stay in-page. 2745 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; 2746 return null; 2747 } 2748 2749 // the page the clicked cell links to, from a link inside a text box's content 2750 fn cellPageLink(root: *Widget, x: usize, y: usize, data: Session.Data) ?RoutablePage { 2751 const grid = root.getGrid() orelse return null; 2752 const cell = grid.cell(x, y) catch return null; 2753 const link = cell.link orelse return null; 2754 if (link.len == 0 or link[0] != '/') return null; 2755 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(link) else RoutablePage.fromUrl(link); 2756 } 2757 2758 // whether the `a:` focus target links to the page already shown, which a click 2759 // leaves alone rather than focusing 2760 pub fn currentPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) bool { 2761 const route = pageLink(root_focus, focus_id, data, "a:") orelse return false; 2762 return route.eql(data.current_page); 2763 } 2764 2765 // the route carried by an `ai:` focus target, or null for any other target 2766 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2767 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2768 } 2769 2770 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2771 const child = root_focus.children.get(focus_id) orelse return null; 2772 const custom = switch (child.focus.kind) { 2773 .custom => |c| c, 2774 else => return null, 2775 }; 2776 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2777 const path = custom[prefix.len..]; 2778 // local sessions build (and parse) links with the repo identity elided 2779 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2780 } 2781 2782 // a display author, resolved against the admin db at read time 2783 pub const Author = union(enum) { 2784 unknown, 2785 email: []const u8, // an email no user matches 2786 user_name: []const u8, 2787 2788 // the commit author line's email, resolved like initFromEmail 2789 pub fn init( 2790 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2791 arena: *std.heap.ArenaAllocator, 2792 author_line: []const u8, 2793 ) !Author { 2794 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2795 } 2796 2797 // from a record's stored author email: it resolves to the user currently 2798 // holding it, else stays the email. the result is allocated in `arena`, 2799 // so the email may borrow transient memory. 2800 pub fn initFromEmail( 2801 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2802 arena: *std.heap.ArenaAllocator, 2803 author_email: ?[]const u8, 2804 ) !Author { 2805 const email = author_email orelse return .unknown; 2806 if (haxy_moment) |moment| { 2807 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2808 return .{ .user_name = user.name }; 2809 } 2810 } 2811 return .{ .email = try arena.allocator().dupe(u8, email) }; 2812 } 2813 }; 2814 2815 // the "a:" link to user `name`'s page. 2816 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2817 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2818 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2819 return page_arena.allocator().print("a:{s}", .{url}); 2820 } 2821 2822 // a focusable " author " box showing `author`, linking to their user page 2823 // when it is a known user 2824 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2825 const text = switch (author) { 2826 .unknown => "", 2827 .email, .user_name => |t| t, 2828 }; 2829 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 2830 errdefer tb.deinit(allocator); 2831 tb.getFocus().mode = .all; 2832 switch (author) { 2833 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2834 else => {}, 2835 } 2836 return tb; 2837 } 2838 2839 // native-TUI navigation 2840 pub const Nav = struct { 2841 root: Widget, 2842 // backs the current page (its Page data and the page-scoped strings its 2843 // widgets build). owned here, swapped on each navigation. session.page_arena 2844 // tracks whichever of these is current so widgets allocate into it. 2845 arena: *std.heap.ArenaAllocator, 2846 // the login the current root was built for 2847 user_id: ?[evt.event_id_size]u8, 2848 history: std.ArrayList(Entry), 2849 2850 // each retained page keeps its own arena, freed when the page leaves the 2851 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2852 // long-lived session from accumulating every page it ever visited. 2853 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2854 2855 // cap on retained back-history so the chain can't grow memory without bound 2856 const max_history: usize = 16; 2857 2858 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2859 session.back = .unavailable; 2860 const arena = try allocator.create(std.heap.ArenaAllocator); 2861 arena.* = std.heap.ArenaAllocator.init(allocator); 2862 errdefer { 2863 arena.deinit(); 2864 allocator.destroy(arena); 2865 } 2866 2867 return .{ 2868 .root = try initCurrentRoot(allocator, arena, session), 2869 .arena = arena, 2870 .user_id = session.userId(), 2871 .history = .empty, 2872 }; 2873 } 2874 2875 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2876 self.root.deinit(allocator); 2877 freeArena(allocator, self.arena); 2878 for (self.history.items) |*entry| { 2879 entry.root.deinit(allocator); 2880 freeArena(allocator, entry.arena); 2881 } 2882 self.history.deinit(allocator); 2883 } 2884 2885 // rebuild the current page in place from a current moment 2886 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2887 if (session.haxy_moment == null and session.local == null) return; 2888 const arena = try allocator.create(std.heap.ArenaAllocator); 2889 arena.* = std.heap.ArenaAllocator.init(allocator); 2890 errdefer freeArena(allocator, arena); 2891 2892 const new_root = try initCurrentRoot(allocator, arena, session); 2893 2894 self.root.deinit(allocator); 2895 freeArena(allocator, self.arena); 2896 self.root = new_root; 2897 self.arena = arena; 2898 self.user_id = session.userId(); 2899 } 2900 2901 // build the current page's root in `arena`, or the not-found page's when 2902 // the route names nothing 2903 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 2904 session.page_arena = arena; 2905 const page = try arena.allocator().create(Page); 2906 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 2907 error.NotFound => blk: { 2908 session.data.current_page = .not_found; 2909 break :blk .not_found; 2910 }, 2911 else => |e| return e, 2912 }; 2913 return initRoot(allocator, page, session); 2914 } 2915 2916 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2917 arena.deinit(); 2918 allocator.destroy(arena); 2919 } 2920 2921 // reconcile the displayed root with the session's navigation state. a 2922 // forward nav (current_page moved to a different parent page) pushes the 2923 // current root and builds the new page in a fresh arena; a back request 2924 // frees the current page and restores the previous one. when escape is 2925 // pressed with no history left we switch to the quit tab instead of quitting. 2926 // returns whether the root was replaced. 2927 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 2928 // refresh: rebuild the current page in place from a current moment 2929 if (session.refresh_requested) { 2930 session.refresh_requested = false; 2931 try self.rebuild(allocator, session); 2932 return true; 2933 } 2934 2935 if (session.back == .requested) { 2936 if (self.history.pop()) |entry| { 2937 session.back = if (self.history.items.len == 0) .unavailable else .available; 2938 self.root.deinit(allocator); 2939 freeArena(allocator, self.arena); 2940 self.root = entry.root; 2941 self.arena = entry.arena; 2942 self.user_id = entry.user_id; 2943 session.page_arena = entry.arena; 2944 session.data.current_page = entry.route; 2945 // a page built under another login is rebuilt rather than restored 2946 if (!std.meta.eql(entry.user_id, session.userId())) { 2947 try self.rebuild(allocator, session); 2948 return true; 2949 } 2950 chooseAnsiArtForNavigation(session); 2951 return true; 2952 } 2953 session.back = .unavailable; 2954 // nothing to go back to; switch to the quit confirmation 2955 const root_focus = self.root.getFocus(); 2956 var iter = root_focus.children.iterator(); 2957 while (iter.next()) |entry| { 2958 switch (entry.value_ptr.focus.kind) { 2959 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2960 root_focus.setFocus(entry.key_ptr.*); 2961 // a Stack only builds its selected child, so the quit 2962 // button isn't in the focus tree until a build runs with 2963 // the quit tab selected. build once, then send a 2964 // synthetic arrow_down to drop focus from the tab onto 2965 // the button so it's ready to confirm. 2966 try self.root.build(allocator, .{ 2967 .min_size = .{ .width = null, .height = 40 }, 2968 .max_size = .{ .width = 80, .height = null }, 2969 }, root_focus); 2970 try self.root.input(allocator, .arrow_down, root_focus); 2971 break; 2972 }, 2973 else => {}, 2974 } 2975 } 2976 return false; 2977 } 2978 2979 // forward navigation: navigate() set next_page to the page to move to (a 2980 // cross-page link or tab change crossing pages) 2981 if (session.next_page) |route| { 2982 session.next_page = null; 2983 if (session.haxy_moment == null and session.local == null) return false; 2984 const previous_route = session.data.current_page; 2985 // the page we navigated to becomes the current page 2986 session.data.current_page = route; 2987 session.back = .available; 2988 2989 const arena = try allocator.create(std.heap.ArenaAllocator); 2990 arena.* = std.heap.ArenaAllocator.init(allocator); 2991 errdefer freeArena(allocator, arena); 2992 2993 const new_root = try initCurrentRoot(allocator, arena, session); 2994 2995 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 2996 // drop the oldest entry (freeing its widget tree and arena) once over cap 2997 if (self.history.items.len > max_history) { 2998 var oldest = self.history.orderedRemove(0); 2999 oldest.root.deinit(allocator); 3000 freeArena(allocator, oldest.arena); 3001 } 3002 self.root = new_root; 3003 self.arena = arena; 3004 self.user_id = session.userId(); 3005 return true; 3006 } 3007 return false; 3008 } 3009 }; 3010 3011 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3012 const page_widget: Widget = switch (page.*) { 3013 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3014 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3015 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3016 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3017 .not_found => .{ .not_found = try .init(allocator, session) }, 3018 }; 3019 3020 chooseAnsiArtForNavigation(session); 3021 3022 // on the TUI/SSH, the page sits above a one-row footer showing its web 3023 // url, when there's a web UI to point at 3024 var root = if (session.is_terminal) blk: { 3025 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3026 errdefer box.deinit(allocator); 3027 const bg_id = bg_blk: { 3028 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3029 errdefer background.deinit(allocator); 3030 const id = background.getFocus().id; 3031 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3032 break :bg_blk id; 3033 }; 3034 if (session.web_port != null) { 3035 var footer = try widget.Footer.init(allocator, session); 3036 errdefer footer.deinit(allocator); 3037 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3038 } 3039 box.getFocus().child_id = bg_id; 3040 break :blk Widget{ .box = box }; 3041 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3042 errdefer root.deinit(allocator); 3043 3044 // input-owning views build their TextInputs in init, so reset their 3045 // registrations here 3046 session.text_inputs.clearRetainingCapacity(); 3047 3048 try root.build(allocator, .{ 3049 .min_size = .{ .width = null, .height = 40 }, 3050 .max_size = .{ .width = 100, .height = null }, 3051 }, root.getFocus()); 3052 3053 return root; 3054 } 3055 3056 // native and server-side sessions choose from the embedded collection. WASM 3057 // keeps the art serialized by the server instead. 3058 fn chooseAnsiArtForNavigation(session: *Session) void { 3059 if (comptime ansi_art.art.len == 0) return; 3060 if (session.data.current_page == .not_found) { 3061 session.data.ansi_art = ansi_art.not_found; 3062 return; 3063 } 3064 const io = session.io orelse return; 3065 var random: usize = undefined; 3066 io.random(std.mem.asBytes(&random)); 3067 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3068 } 3069