╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const xit = @import("xit"); 6 const rp = xit.repo; 7 const hash = xit.hash; 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const Key = xitui.input.Key; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 const inp = @import("../input.zig"); 15 const diff3 = @import("../../diff3.zig"); 16 const Comment = @import("Comment.zig"); 17 const Attachment = @import("Attachment.zig"); 18 const Threads = @import("Threads.zig"); 19 20 const wasm = builtin.target.cpu.arch == .wasm32; 21 22 // how much of a description the detail pane shows before linking to the 23 // /description page, the same limit commit messages use. 24 pub const max_description_size = 2 * 1024; 25 26 // how many issues one window shows before a "next" link appears. 27 pub const page_size = 20; 28 29 // how many tags the tags view shows at most. 30 pub const max_tags = 1000; 31 32 // one issue from the repo's consumed event database, with its hex event id 33 // (the id lives in the event envelope, not the payload). 34 pub const IssueWithId = struct { 35 id: []const u8, 36 record: evt.Issue.Record, 37 // the creation commit's author 38 author: ui.Author = .unknown, 39 // whether the issue has an unresolved merge conflict 40 conflicted: bool = false, 41 comments: Comment.Window = .empty, 42 attachments: []const Attachment.WithId = &.{}, 43 }; 44 45 pub const Entry = IssueWithId; 46 47 // one side of a conflicted field: its value and who set it 48 pub const Side = struct { 49 text: []const u8, 50 author: ui.Author = .unknown, 51 }; 52 53 pub const FieldConflict = struct { 54 ours: Side, 55 theirs: Side, 56 }; 57 58 // the selected issue's conflict, read for the resolve view. ours is the live 59 // record; the chunks split the description against the merge base. 60 pub const Conflict = struct { 61 title: ?FieldConflict = null, 62 tags: ?FieldConflict = null, 63 description: ?struct { 64 chunks: []const diff3.Chunk, 65 ours_author: ui.Author = .unknown, 66 theirs_author: ui.Author = .unknown, 67 } = null, 68 }; 69 70 // one status's windowed listing. 71 pub const Window = struct { 72 items: []const IssueWithId, 73 // the id of the previous window's first issue, or null when this window 74 // is already the first. 75 prev_id: ?[]const u8, 76 // the id of the next window's first issue, or null when this is the last 77 // window. 78 next_id: ?[]const u8, 79 // how many issues the listing holds across all windows. 80 count: usize, 81 82 pub const empty: Window = .{ .items = &.{}, .prev_id = null, .next_id = null, .count = 0 }; 83 }; 84 85 // "owner/name", so the view can build /repo/owner/name/issues/... links. 86 identity: []const u8, 87 // the url-encoded tag the lists are filtered to ("" = unfiltered). 88 tag: []const u8, 89 // the hex event id of the issue its status's window is rooted at ("" = the 90 // first window), mirrored into the url. 91 selected_id: []const u8, 92 // the comment shown in the selected issue's detail pane. 93 comment_id: []const u8, 94 // the selected issue's comment window (0 = the first window). 95 comments_start: usize, 96 // the selected comment and its immediate replies. 97 comment_page: ?Comment.Permalink = null, 98 open: Window, 99 closed: Window, 100 // the conflicted issues' listing; its count also gates the conflicts tab. 101 conflicts: Window, 102 // the selected issue's conflict, set when the page shows the resolve view. 103 conflict: ?Conflict = null, 104 // the resolve view's comma-separated fields prefilled from their side, 105 // mirrored from the url. 106 theirs_picks: []const u8 = "", 107 // the view the page shows initially (a selected issue's status overrides the route's) 108 view: ui.RoutablePage.IssuesView, 109 // the /description page: the detail pane shows the selected issue's whole 110 // description behind a back link. 111 description_page: bool = false, 112 // every tag in the repo, in sorted order, for the tags view. 113 tags: []const []const u8, 114 // the on-disk repo this page was read from, for the terminal submit path 115 // (the web posts the new-issue form to the issue route instead). 116 repo_source: ?ui.RepoSource = null, 117 118 const Self = @This(); 119 120 pub const Event = evt.Issue; 121 pub const Status = evt.Issue.Status; 122 pub const ViewKind = ui.RoutablePage.IssuesView; 123 124 // `status`'s windowed listing. 125 pub fn window(self: *const Self, status: evt.Issue.Status) *const Window { 126 return switch (status) { 127 .open => &self.open, 128 .closed => &self.closed, 129 }; 130 } 131 132 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 133 // theirs: picks. 134 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 135 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 136 while (it.next()) |pick| { 137 if (std.mem.eql(u8, pick, field)) return true; 138 } 139 return false; 140 } 141 142 // the issue `selected_id` names, in whichever window holds it. 143 pub fn selectedThread(self: *const Self) ?*const IssueWithId { 144 if (self.selected_id.len == 0) return null; 145 for ([_]*const Window{ &self.open, &self.closed }) |win| { 146 for (win.items) |*entry| { 147 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 148 } 149 } 150 return null; 151 } 152 153 // an empty listing, for the wasm / no-repo paths. 154 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, tag: []const u8, selected_id: []const u8, comment_id: []const u8, comments_start: usize, theirs_picks: []const u8, view: ui.RoutablePage.IssuesView) !Self { 155 return .{ 156 .identity = try aa.dupe(u8, identity), 157 .tag = try aa.dupe(u8, tag), 158 .selected_id = try aa.dupe(u8, selected_id), 159 .comment_id = try aa.dupe(u8, comment_id), 160 .comments_start = comments_start, 161 .open = .empty, 162 .closed = .empty, 163 .conflicts = .empty, 164 .theirs_picks = try aa.dupe(u8, theirs_picks), 165 // a description url shows a status list, the issue's own status 166 // picking it once the issue is read; conflicts and resolve urls also 167 // start there, upgraded by init when the conflict data exists. 168 .view = switch (view) { 169 .description, .conflicts, .resolve => .open, 170 else => view, 171 }, 172 .description_page = view == .description, 173 .tags = &.{}, 174 }; 175 } 176 177 // read one window per status of an opened repo's issues (filtered to `tag` 178 // when set), ordered by creation (newest first). the window of the issue 179 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 180 // event db next to it (synced from the events branch on each page build); a 181 // xit repo reads its own db. 182 pub fn init( 183 comptime repo_kind: rp.RepoKind, 184 comptime repo_opts: rp.RepoOpts(repo_kind), 185 arena: *std.heap.ArenaAllocator, 186 repo: *rp.Repo(repo_kind, repo_opts), 187 io: std.Io, 188 // the admin db's moment, for resolving author emails to user names (null 189 // in local mode, which has no users) 190 admin_moment: ?evt.AdminDB.HashMap(.read_only), 191 identity: []const u8, 192 tag: []const u8, 193 selected_id: []const u8, 194 comment_id: []const u8, 195 comments_start: usize, 196 theirs_picks: []const u8, 197 view: ui.RoutablePage.IssuesView, 198 ) !Self { 199 const empty = try emptyResult(arena.allocator(), identity, tag, selected_id, comment_id, comments_start, theirs_picks, view); 200 201 const aa = arena.allocator(); 202 const DB = evt.EventDB(repo_opts.hash); 203 const rooted = empty.selected_id.len != 0; 204 const tagged = empty.tag.len != 0; 205 // an explicitly named issue or tag that doesn't exist is a bad url 206 // (NotFound -> 404); the bare route falls through to an empty listing. 207 const strict = rooted or tagged; 208 209 // a repo with no consumed events has no moment yet. 210 const gpa = arena.child_allocator; 211 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, gpa, repo.core.repo_dir) else null; 212 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 213 const haxy_moment = (if (event_db_maybe) |*event_db| 214 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 215 else if (repo_kind == .git) 216 return empty 217 else 218 evt.currentMoment(repo_opts, repo)) catch { 219 if (strict) return error.NotFound; 220 return empty; 221 }; 222 223 // the sorted sets to window, per status: the tag's sets when filtered, 224 // else the top-level per-status sets. a missing set is an empty window. 225 var open_set: ?DB.SortedSet(.read_only) = null; 226 var closed_set: ?DB.SortedSet(.read_only) = null; 227 if (tagged) { 228 const tag_to_issues_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag+status->issue-id-set")) orelse return error.NotFound; 229 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 230 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.tag)); 231 open_set = try tagStatusSet(DB, tag_to_issues, decoded, .open); 232 closed_set = try tagStatusSet(DB, tag_to_issues, decoded, .closed); 233 // a tag no issue carries is a bad url 234 if (open_set == null and closed_set == null) return error.NotFound; 235 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "status->issue-id-set"))) |status_to_issues_cursor| { 236 const status_to_issues = try DB.SortedMap(.read_only).init(status_to_issues_cursor); 237 open_set = try statusSet(DB, status_to_issues, .open); 238 closed_set = try statusSet(DB, status_to_issues, .closed); 239 } else if (rooted) return error.NotFound; 240 241 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse { 242 if (strict) return error.NotFound; 243 return empty; 244 }; 245 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 246 247 // the conflicted issues' container: a set view for membership and 248 // windowing, a map view for the resolve entry, over the same cursor. 249 var conflict_set: ?DB.SortedSet(.read_only) = null; 250 var conflicts_map: ?DB.SortedMap(.read_only) = null; 251 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.conflicts_key))) |cursor| { 252 conflict_set = try DB.SortedSet(.read_only).init(cursor); 253 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 254 } 255 256 // a named issue roots its own status's window at itself (the conflicts 257 // view's window instead); the other windows start at the beginning. 258 var resolved_view = empty.view; 259 var open_root: ?[]const u8 = null; 260 var closed_root: ?[]const u8 = null; 261 var conflicts_root: ?[]const u8 = null; 262 var conflict_data: ?Conflict = null; 263 if (rooted) { 264 if (empty.selected_id.len != evt.event_id_size * 2) return error.NotFound; 265 var id_bytes: [evt.event_id_size]u8 = undefined; 266 _ = std.fmt.hexToBytes(&id_bytes, empty.selected_id) catch return error.NotFound; 267 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 268 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 269 const issue_event = try evt.read(evt.Issue.Record, DB, repo_opts.hash, arena, issue_map); 270 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(issue_event.created_order, &id_bytes)); 271 272 if (view == .conflicts) { 273 // the id roots the conflicts window, so it must be conflicted 274 const set = conflict_set orelse return error.NotFound; 275 if (!try set.contains(order_key)) return error.NotFound; 276 conflicts_root = order_key; 277 } else { 278 // the named issue must be in its windowed set (a tag url can name 279 // an issue that doesn't carry the tag). 280 const set = (switch (issue_event.event.status) { 281 .open => open_set, 282 .closed => closed_set, 283 }) orelse return error.NotFound; 284 if (!try set.contains(order_key)) return error.NotFound; 285 286 switch (issue_event.event.status) { 287 .open => open_root = order_key, 288 .closed => closed_root = order_key, 289 } 290 // form urls keep their view; otherwise the issue's status picks it. 291 if (view != .edit and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (issue_event.event.status) { 292 .open => .open, 293 .closed => .closed, 294 }; 295 296 // a resolve url shows the resolve view only while the issue still 297 // has a conflict entry; otherwise the status list stands. the 298 // wasm client renders from the snapshot, so the diff machinery 299 // this pulls in is gated out of its build. 300 if (comptime !wasm) { 301 if (view == .resolve) { 302 if (conflicts_map) |map| { 303 if (try map.getCursor(order_key)) |conflict_cursor| { 304 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 305 conflict_data = try readConflict(repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, issue_event, conflict_entry); 306 resolved_view = .resolve; 307 } 308 } 309 } 310 } 311 } 312 } 313 314 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 315 const open_window = try loadWindow(repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, open_set, open_root, conflict_set, empty.selected_id, thread_comments_start); 316 const closed_window = try loadWindow(repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, closed_set, closed_root, conflict_set, empty.selected_id, thread_comments_start); 317 const conflicts_window = try loadWindow(repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, conflict_set, conflicts_root, conflict_set, empty.selected_id, thread_comments_start); 318 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 319 320 const comment_page = if (empty.comment_id.len == 0) 321 null 322 else 323 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start); 324 325 // every tag in the repo, in the tag map's sorted order. the keys are 326 // "tag,status", so a tag's entries are adjacent and dedup by prefix. 327 var tags: std.ArrayList([]const u8) = .empty; 328 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag+status->issue-id-set"))) |tag_to_issues_cursor| { 329 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 330 var tag_iter = try tag_to_issues.iterator(); 331 while (try tag_iter.next()) |kv_pair_cursor| { 332 if (tags.items.len == max_tags) break; 333 var kv_cursor = kv_pair_cursor; 334 const kv_pair = try kv_cursor.readKeyValuePair(); 335 const key = try kv_pair.key_cursor.readBytesAlloc(aa, null); 336 const space = std.mem.indexOfScalar(u8, key, ' ') orelse continue; 337 const key_tag = key[0..space]; 338 if (tags.getLastOrNull()) |last| { 339 if (std.mem.eql(u8, last, key_tag)) continue; 340 } 341 try tags.append(aa, key_tag); 342 } 343 } 344 345 return .{ 346 .identity = empty.identity, 347 .tag = empty.tag, 348 .selected_id = empty.selected_id, 349 .comment_id = empty.comment_id, 350 .comments_start = comments_start, 351 .comment_page = comment_page, 352 .open = open_window, 353 .closed = closed_window, 354 .conflicts = conflicts_window, 355 .conflict = conflict_data, 356 .theirs_picks = empty.theirs_picks, 357 .view = resolved_view, 358 .description_page = empty.description_page, 359 .tags = tags.items, 360 }; 361 } 362 363 // `status`'s sorted set within `statuses` (null when the status has no issues) 364 fn statusSet( 365 comptime DB: type, 366 statuses: DB.SortedMap(.read_only), 367 status: evt.Issue.Status, 368 ) !?DB.SortedSet(.read_only) { 369 const cursor = (try statuses.getCursor(@tagName(status))) orelse return null; 370 return try DB.SortedSet(.read_only).init(cursor); 371 } 372 373 // the set at `tag_to_issues`'s "tag,status" key (null when no issue carries 374 // `tag` with `status`, or the tag is too long to exist) 375 fn tagStatusSet( 376 comptime DB: type, 377 tag_to_issues: DB.SortedMap(.read_only), 378 tag: []const u8, 379 status: evt.Issue.Status, 380 ) !?DB.SortedSet(.read_only) { 381 var key_buffer: evt.Issue.TagStatusKey = undefined; 382 const key = evt.Issue.tagStatusKey(&key_buffer, tag, status) catch return null; 383 const cursor = (try tag_to_issues.getCursor(key)) orelse return null; 384 return try DB.SortedSet(.read_only).init(cursor); 385 } 386 387 // read one window of `set_maybe` (null = an empty listing) starting at 388 // `root_key` (null = the beginning) 389 fn loadWindow( 390 comptime hash_kind: hash.HashKind, 391 arena: *std.heap.ArenaAllocator, 392 admin_moment: ?evt.AdminDB.HashMap(.read_only), 393 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 394 event_id_to_issue: evt.EventDB(hash_kind).HashMap(.read_only), 395 set_maybe: ?evt.EventDB(hash_kind).SortedSet(.read_only), 396 root_key: ?[]const u8, 397 conflict_set: ?evt.EventDB(hash_kind).SortedSet(.read_only), 398 selected_id: []const u8, 399 comments_start: usize, 400 ) !Window { 401 const set = set_maybe orelse return .empty; 402 const DB = evt.EventDB(hash_kind); 403 const aa = arena.allocator(); 404 405 // seek once to the window start: the root key, or the set's first entry. 406 var prev_id: ?[]const u8 = null; 407 var iter = if (root_key) |key| blk: { 408 // the previous window starts page_size ranks back ("" = the first 409 // window, linked as the bare list route). 410 const rank = try set.rank(key); 411 if (rank > 0 and rank <= page_size) { 412 prev_id = ""; 413 } else if (rank > page_size) { 414 const kv = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 415 var prev_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 416 _ = try kv.key_cursor.readBytes(&prev_key); 417 const prev_hex = std.fmt.bytesToHex(prev_key[@sizeOf(u64)..].*, .lower); 418 prev_id = try aa.dupe(u8, &prev_hex); 419 } 420 break :blk try set.iteratorFrom(key); 421 } else try set.iteratorFromIndex(0); 422 423 // collect this window's issues, plus a peek at the one after it (its id is 424 // the next window's start). the trailing bytes of each set key are the 425 // issue event id. 426 var issues: std.ArrayList(IssueWithId) = .empty; 427 var next_id: ?[]const u8 = null; 428 while (try iter.next()) |id_cursor_val| { 429 var id_cursor = id_cursor_val; 430 const id_kv = try id_cursor.readKeyValuePair(); 431 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 432 _ = try id_kv.key_cursor.readBytes(&order_key); 433 const id_hex = std.fmt.bytesToHex(order_key[@sizeOf(u64)..].*, .lower); 434 if (issues.items.len == page_size) { 435 next_id = try aa.dupe(u8, &id_hex); 436 break; 437 } 438 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(hash_kind, order_key[@sizeOf(u64)..])) orelse continue; 439 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 440 const issue_event = try evt.read(evt.Issue.Record, DB, hash_kind, arena, issue_map); 441 try issues.append(aa, .{ 442 .id = try aa.dupe(u8, &id_hex), 443 .record = issue_event, 444 .author = try ui.Author.initFromEmail(admin_moment, arena, issue_event.author_email), 445 .conflicted = if (conflict_set) |cs| try cs.contains(&order_key) else false, 446 .comments = try Comment.loadWindow( 447 hash_kind, 448 arena, 449 admin_moment, 450 haxy_moment, 451 evt.Comment.thread_id_to_comment_id_set_key, 452 &id_hex, 453 if (std.mem.eql(u8, &id_hex, selected_id)) comments_start else 0, 454 ), 455 .attachments = try Attachment.load(hash_kind, arena, haxy_moment, &id_hex), 456 }); 457 } 458 459 return .{ 460 .items = issues.items, 461 .prev_id = prev_id, 462 .next_id = next_id, 463 .count = @intCast(try set.count()), 464 }; 465 } 466 467 // the selected issue's conflict entry, shaped for the resolve view. `ours` is 468 // the live record. 469 fn readConflict( 470 comptime repo_kind: rp.RepoKind, 471 comptime repo_opts: rp.RepoOpts(repo_kind), 472 arena: *std.heap.ArenaAllocator, 473 repo: *rp.Repo(repo_kind, repo_opts), 474 io: std.Io, 475 admin_moment: ?evt.AdminDB.HashMap(.read_only), 476 haxy_moment: evt.EventDB(repo_opts.hash).HashMap(.read_only), 477 id_bytes: *const [evt.event_id_size]u8, 478 ours: evt.Issue.Record, 479 conflict_entry: evt.EventDB(repo_opts.hash).HashMap(.read_only), 480 ) !Conflict { 481 const DB = evt.EventDB(repo_opts.hash); 482 const aa = arena.allocator(); 483 const gpa = arena.child_allocator; 484 485 const fields_cursor = (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key))) orelse return error.NotFound; 486 const conflicted_fields = try fields_cursor.readBytesAlloc(aa, null); 487 488 const their_cursor = (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key))) orelse return error.NotFound; 489 const theirs = try evt.read(evt.Issue.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(their_cursor)); 490 491 // absent when both sides created the issue independently 492 var base: ?evt.Issue.Record = null; 493 if (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.base_record_key))) |base_cursor| { 494 base = try evt.read(evt.Issue.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(base_cursor)); 495 } 496 497 // each side's field->oid map, for attributing the versions; a field a 498 // merge left unattributed reads as an unknown author 499 var their_field_oids: ?DB.SortedMap(.read_only) = null; 500 if (try conflict_entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_field_to_oid_key))) |cursor| { 501 their_field_oids = try DB.SortedMap(.read_only).init(cursor); 502 } 503 var our_field_oids: ?DB.SortedMap(.read_only) = null; 504 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.id_to_field_to_oid_key))) |map_cursor| { 505 const id_to_field_to_oid = try DB.HashMap(.read_only).init(map_cursor); 506 if (try id_to_field_to_oid.getCursor(hash.hashInt(repo_opts.hash, id_bytes))) |cursor| { 507 our_field_oids = try DB.SortedMap(.read_only).init(cursor); 508 } 509 } 510 511 var conflict = Conflict{}; 512 var field_iter = std.mem.splitScalar(u8, conflicted_fields, ' '); 513 while (field_iter.next()) |field| { 514 // status can't conflict (two changes of a two-value field agree) 515 const known = std.meta.stringToEnum(enum { title, tags, description }, field) orelse continue; 516 const our_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, our_field_oids, field)); 517 const their_author = try oidAuthor(repo_kind, repo_opts, arena, repo, io, admin_moment, try readFieldOid(repo_opts.hash, their_field_oids, field)); 518 switch (known) { 519 .title => conflict.title = .{ 520 .ours = .{ .text = ours.event.title, .author = our_author }, 521 .theirs = .{ .text = theirs.event.title, .author = their_author }, 522 }, 523 .tags => conflict.tags = .{ 524 .ours = .{ .text = ours.event.tags, .author = our_author }, 525 .theirs = .{ .text = theirs.event.tags, .author = their_author }, 526 }, 527 .description => conflict.description = .{ 528 .chunks = try diff3.chunks(io, gpa, arena, if (base) |b| b.event.description else "", ours.event.description, theirs.event.description), 529 .ours_author = our_author, 530 .theirs_author = their_author, 531 }, 532 } 533 } 534 return conflict; 535 } 536 537 // the oid `field` maps to in a field->oid map, or null 538 fn readFieldOid( 539 comptime hash_kind: hash.HashKind, 540 field_oids_maybe: ?evt.EventDB(hash_kind).SortedMap(.read_only), 541 field: []const u8, 542 ) !?[hash.byteLen(hash_kind)]u8 { 543 const field_oids = field_oids_maybe orelse return null; 544 const cursor = (try field_oids.getCursor(field)) orelse return null; 545 var oid: [hash.byteLen(hash_kind)]u8 = undefined; 546 _ = try cursor.readBytes(&oid); 547 return oid; 548 } 549 550 // the author of the event commit `oid_maybe` names 551 fn oidAuthor( 552 comptime repo_kind: rp.RepoKind, 553 comptime repo_opts: rp.RepoOpts(repo_kind), 554 arena: *std.heap.ArenaAllocator, 555 repo: *rp.Repo(repo_kind, repo_opts), 556 io: std.Io, 557 admin_moment: ?evt.AdminDB.HashMap(.read_only), 558 oid_maybe: ?[hash.byteLen(repo_opts.hash)]u8, 559 ) !ui.Author { 560 const oid = oid_maybe orelse return .unknown; 561 const gpa = arena.child_allocator; 562 var start_oids = [_][hash.hexLen(repo_opts.hash)]u8{std.fmt.bytesToHex(oid, .lower)}; 563 var commit_iter = repo.log(io, gpa, start_oids[0..1]) catch return .unknown; 564 defer commit_iter.deinit(); 565 const commit_object = (commit_iter.next(gpa) catch return .unknown) orelse return .unknown; 566 defer commit_object.deinit(); 567 return try ui.Author.init(admin_moment, arena, commit_object.content.commit.metadata.author orelse ""); 568 } 569 570 pub const View = Threads.View(.issue, Self); 571 572 // tabs switching between the issues page's views. 573 pub const Header = struct { 574 box: wgt.Box(ui.Widget), 575 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 576 577 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 578 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 579 errdefer box.deinit(allocator); 580 581 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 582 errdefer tab_ids.deinit(allocator); 583 584 const aa = session.page_arena.allocator(); 585 586 // a list tab per status, labeled with its listing's issue count 587 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 588 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 589 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 590 var label_buf: [64]u8 = undefined; 591 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 592 try addTab(allocator, &box, &tab_ids, label, link); 593 } 594 595 // tags tab, labeled with the active tag filter 596 { 597 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.issue, data.identity, data.tag) orelse return error.RouteTooLong; 598 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 599 const label = if (data.tag.len == 0) "tags" else blk: { 600 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 601 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 602 }; 603 try addTab(allocator, &box, &tab_ids, label, tags_link); 604 } 605 606 // new-issue tab; an edit or resolve url shows its tab in this place 607 { 608 const route = switch (data.view) { 609 .edit => ui.RoutablePage.repoThreadEditRoute(.issue, data.identity, data.selected_id) orelse return error.RouteTooLong, 610 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 611 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 612 .remove => ui.RoutablePage.repoThreadRemoveRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 613 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 614 else => ui.RoutablePage.repoThreadNewRoute(.issue, data.identity) orelse return error.RouteTooLong, 615 }; 616 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 617 const label: []const u8 = switch (data.view) { 618 .edit => "edit", 619 .new_comment => "reply", 620 .edit_comment => "edit", 621 .remove => "remove", 622 .resolve => "resolve", 623 else => "new", 624 }; 625 try addTab(allocator, &box, &tab_ids, label, link); 626 } 627 628 // conflicts tab, labeled with the conflict count 629 if (data.conflicts.count > 0) { 630 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 631 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 632 var label_buf: [64]u8 = undefined; 633 const label = try std.fmt.bufPrint(&label_buf, "conflicts ({d})", .{data.conflicts.count}); 634 try addTab(allocator, &box, &tab_ids, label, link); 635 } 636 637 var self = Header{ .box = box, .tab_ids = tab_ids }; 638 // the tab matching the page's view is selected initially. 639 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 640 return self; 641 } 642 643 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 644 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 645 errdefer text_box.deinit(allocator); 646 text_box.getFocus().focusable = true; 647 text_box.getFocus().kind = .{ .custom = link }; 648 try tab_ids.put(allocator, text_box.getFocus().id, {}); 649 try box.children.put(allocator, text_box.getFocus().id, .{ 650 .widget = .{ .text_box = text_box }, 651 .rect = null, 652 .min_size = .{ .width = label.len + 2, .height = null }, 653 }); 654 } 655 656 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 657 self.box.deinit(allocator); 658 self.tab_ids.deinit(allocator); 659 } 660 661 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 662 self.clearGrid(); 663 // only the selected tab shows its border 664 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 665 switch (child.widget) { 666 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 667 else => {}, 668 } 669 } 670 try self.box.build(allocator, constraint, root_focus); 671 } 672 673 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 674 _ = allocator; 675 const current_tab = self.currentTabIndex() orelse return; 676 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 677 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 678 } 679 } 680 681 pub fn clearGrid(self: *Header) void { 682 self.box.clearGrid(); 683 } 684 685 pub fn getGrid(self: Header) ?Grid { 686 return self.box.getGrid(); 687 } 688 689 pub fn getFocus(self: *Header) *Focus { 690 return self.box.getFocus(); 691 } 692 693 pub fn getSelectedIndex(self: Header) ?usize { 694 return self.currentTabIndex(); 695 } 696 697 fn currentTabIndex(self: Header) ?usize { 698 const child_id = self.box.focus.child_id orelse return null; 699 return self.tab_ids.getIndex(child_id); 700 } 701 }; 702