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