╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
87 selected_id: []const u8, 88 // the comment shown in the selected issue's detail pane. 89 comment_id: []const u8, 90 // the selected issue's comment window (0 = the first window). 91 comments_start: usize, 92 // the selected comment and its immediate replies. 93 comment_page: ?Comment.Permalink = null, 94 // who the page is built for, null when logged out 95 viewer: ?ui.Viewer = null, 96 open: Window, 97 closed: Window, 98 // the conflicted issues' listing; its count also gates the conflicts tab. 99 conflicts: Window, 100 // the selected issue's conflict, set when the page shows the resolve view. 101 conflict: ?Conflict = null, 102 // the resolve view's comma-separated fields prefilled from their side, 103 // mirrored from the url. 104 theirs_picks: []const u8 = "", 105 // the view the page shows initially (a selected issue's status overrides the route's) 106 view: ui.RoutablePage.IssuesView, 107 // the /description page: the detail pane shows the selected issue's whole 108 // description behind a back link. 109 description_page: bool = false, 110 // every label in the repo, in sorted order, for the labels view. 111 labels: []const []const u8, 112 // the on-disk repo this page was read from, for the terminal submit path 113 // (the web posts the new-issue form to the issue route instead). 114 repo_source: ?ui.RepoSource = null, 115 116 const Self = @This(); 117 118 pub const Event = evt.Issue; 119 pub const Status = evt.Issue.Status; 120 pub const ViewKind = ui.RoutablePage.IssuesView; 121 pub const thread_name = "issue"; 122 pub const header_widget_name = "repo_issues_header"; 123 124 pub fn statusKind(event: Event) Status { 125 return event.status; 126 } 127 128 pub fn listRoute(identity: []const u8, status: Status, label: []const u8, selected: []const u8) ?ui.RoutablePage { 129 return ui.RoutablePage.repoIssuesRoute(identity, status, label, selected); 130 } 131 132 pub fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 133 return ui.RoutablePage.repoIssuesConflictsRoute(identity, selected); 134 } 135 136 pub fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 137 return ui.RoutablePage.repoIssuesResolveRoute(identity, selected, picks); 138 } 139 140 // `status`'s windowed listing. 141 pub fn window(self: *const Self, status: evt.Issue.Status) *const Window { 142 return switch (status) { 143 .open => &self.open, 144 .closed => &self.closed, 145 }; 146 } 147 148 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 149 // theirs: picks. 150 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 151 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 152 while (it.next()) |pick| { 153 if (std.mem.eql(u8, pick, field)) return true; 154 } 155 return false; 156 } 157 158 // the issue `selected_id` names, in whichever window holds it. 159 pub fn selectedThread(self: *const Self) ?*const IssueWithId { 160 if (self.selected_id.len == 0) return null; 161 for ([_]*const Window{ &self.open, &self.closed }) |win| { 162 for (win.items) |*entry| { 163 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 164 } 165 } 166 return null; 167 } 168 169 // an empty listing, for the wasm / no-repo paths. 170 pub fn emptyResult(aa: std.mem.Allocator, identity: []const u8, label: []const u8, search: []const u8, selected_id: []const u8, comment_id: []const u8, comments_start: usize, theirs_picks: []const u8, view: ui.RoutablePage.IssuesView) !Self { 171 return .{ 172 .identity = try aa.dupe(u8, identity), 173 .label = try aa.dupe(u8, label), 174 .search = if (search.len == 0) null else std.Uri.percentDecodeInPlace(try aa.dupe(u8, search)), 175 .selected_id = try aa.dupe(u8, selected_id), 176 .comment_id = try aa.dupe(u8, comment_id), 177 .comments_start = comments_start, 178 .open = .empty, 179 .closed = .empty, 180 .conflicts = .empty, 181 .theirs_picks = try aa.dupe(u8, theirs_picks), 182 // a description url shows a status list, the issue's own status 183 // picking it once the issue is read; conflicts and resolve urls also 184 // start there, upgraded by init when the conflict data exists. 185 .view = switch (view) { 186 .description, .conflicts, .resolve => .open, 187 else => view, 188 }, 189 .description_page = view == .description, 190 .labels = &.{}, 191 }; 192 } 193 194 // read one window per status of an opened repo's issues (filtered to `label` 195 // when set), ordered by creation (newest first). the window of the issue 196 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 197 // event db next to it (synced from the events branch on each page build); a 198 // xit repo reads its own db. 199 pub fn init( 200 comptime repo_kind: rp.RepoKind, 201 comptime repo_opts: rp.RepoOpts(repo_kind), 202 arena: *std.heap.ArenaAllocator, 203 repo: *rp.Repo(repo_kind, repo_opts), 204 io: std.Io, 205 // the admin db's moment, for resolving author emails to user names (null 206 // in local mode, which has no users) 207 admin_moment: ?evt.AdminDB.HashMap(.read_only), 208 identity: []const u8, 209 label: []const u8, 210 search: []const u8, 211 selected_id: []const u8, 212 comment_id: []const u8, 213 comments_start: usize, 214 theirs_picks: []const u8, 215 view: ui.RoutablePage.IssuesView, 216 viewer: ?ui.Viewer, 217 ) !Self { 218 const empty = try emptyResult(arena.allocator(), identity, label, search, selected_id, comment_id, comments_start, theirs_picks, view); 219 220 const aa = arena.allocator(); 221 const DB = evt.EventDB(repo_opts.hash); 222 const rooted = empty.selected_id.len != 0; 223 const labeled = empty.label.len != 0; 224 // an explicitly named issue or label that doesn't exist is a bad url 225 // (NotFound -> 404); the bare route falls through to an empty listing. 226 const strict = rooted or labeled; 227 228 // a repo with no consumed events has no moment yet. 229 const gpa = arena.child_allocator; 230 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; 231 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 232 const haxy_moment = (if (event_db_maybe) |*event_db| 233 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 234 else if (repo_kind == .git) 235 return empty 236 else 237 evt.currentMoment(repo_opts, repo)) catch { 238 if (strict) return error.NotFound; 239 return empty; 240 }; 241 242 // the sorted sets to window, per status: the label's sets when filtered, 243 // else the top-level per-status sets. a missing set is an empty window. 244 var open_set: ?DB.SortedSet(.read_only) = null; 245 var closed_set: ?DB.SortedSet(.read_only) = null; 246 if (labeled) { 247 const label_to_issues_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.label_status_to_id_set_key)) orelse return error.NotFound; 248 const label_to_issues = try DB.SortedMap(.read_only).init(label_to_issues_cursor); 249 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.label)); 250 open_set = try thread.labelStatusSet(Self, DB, label_to_issues, decoded, .open); 251 closed_set = try thread.labelStatusSet(Self, DB, label_to_issues, decoded, .closed); 252 // a label no issue carries is a bad url 253 if (open_set == null and closed_set == null) return error.NotFound; 254 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.status_to_id_set_key))) |status_to_issues_cursor| { 255 const status_to_issues = try DB.SortedMap(.read_only).init(status_to_issues_cursor); 256 open_set = try thread.statusSet(Self, DB, status_to_issues, .open); 257 closed_set = try thread.statusSet(Self, DB, status_to_issues, .closed); 258 } else if (rooted) return error.NotFound; 259 260 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse { 261 if (strict) return error.NotFound; 262 return empty; 263 }; 264 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 265 266 // the conflicted issues' container: a set view for membership and 267 // windowing, a map view for the resolve entry, over the same cursor. 268 var conflict_set: ?DB.SortedSet(.read_only) = null; 269 var conflicts_map: ?DB.SortedMap(.read_only) = null; 270 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.conflicts_key))) |cursor| { 271 conflict_set = try DB.SortedSet(.read_only).init(cursor); 272 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 273 } 274 275 // a named issue roots its own status's window at itself (the conflicts 276 // view's window instead); the other windows start at the beginning. 277 var resolved_view = empty.view; 278 var open_root: ?[]const u8 = null; 279 var closed_root: ?[]const u8 = null; 280 var conflicts_root: ?[]const u8 = null; 281 var conflict_data: ?Conflict = null; 282 if (rooted) { 283 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 284 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 285 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 286 const issue_event = try evt.read(evt.Issue.Record, DB, repo_opts.hash, arena, issue_map); 287 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(issue_event.created_order, &id_bytes)); 288 289 if (view == .conflicts) { 290 // the id roots the conflicts window, so it must be conflicted 291 const set = conflict_set orelse return error.NotFound; 292 if (!try set.contains(order_key)) return error.NotFound; 293 conflicts_root = order_key; 294 } else { 295 // the named issue must be in its windowed set (a label url can name 296 // an issue that doesn't carry the label). 297 const set = (switch (issue_event.event.status) { 298 .open => open_set, 299 .closed => closed_set, 300 }) orelse return error.NotFound; 301 if (!try set.contains(order_key)) return error.NotFound; 302 303 switch (issue_event.event.status) { 304 .open => open_root = order_key, 305 .closed => closed_root = order_key, 306 } 307 // form urls keep their view; otherwise the issue's status picks it. 308 if (view != .edit and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (issue_event.event.status) { 309 .open => .open, 310 .closed => .closed, 311 }; 312 313 // a resolve url shows the resolve view only while the issue still 314 // has a conflict entry; otherwise the status list stands. the 315 // wasm client renders from the snapshot, so the diff machinery 316 // this pulls in is gated out of its build. 317 if (comptime !wasm) { 318 if (view == .resolve) { 319 if (conflicts_map) |map| { 320 if (try map.getCursor(order_key)) |conflict_cursor| { 321 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 322 conflict_data = try thread.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, issue_event, conflict_entry); 323 resolved_view = .resolve; 324 } 325 } 326 } 327 } 328 } 329 } 330 331 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 332 const open_window = try thread.loadWindow(Self, .issue, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, open_set, open_root, conflict_set, empty.selected_id, thread_comments_start, empty.search, viewer); 333 const closed_window = try thread.loadWindow(Self, .issue, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, closed_set, closed_root, conflict_set, empty.selected_id, thread_comments_start, empty.search, viewer); 334 const conflicts_window = try thread.loadWindow(Self, .issue, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_issue, conflict_set, conflicts_root, conflict_set, empty.selected_id, thread_comments_start, null, viewer); 335 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 336 337 const comment_page = if (empty.comment_id.len == 0) 338 null 339 else 340 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start, viewer); 341 342 const labels = try thread.loadLabels(Self, repo_opts.hash, arena, haxy_moment); 343 344 return .{ 345 .identity = empty.identity, 346 .label = empty.label, 347 .search = empty.search, 348 .selected_id = empty.selected_id, 349 .comment_id = empty.comment_id, 350 .comments_start = comments_start, 351 .comment_page = comment_page, 352 .viewer = viewer, 353 .open = open_window, 354 .closed = closed_window, 355 .conflicts = conflicts_window, 356 .conflict = conflict_data, 357 .theirs_picks = empty.theirs_picks, 358 .view = resolved_view, 359 .description_page = empty.description_page, 360 .labels = labels, 361 }; 362 } 363 364 pub const View = thread.View(.issue, Self); 365 pub const Detail = thread.Detail(.issue, Self); 366 pub const detail_widget_name = "repo_issue_detail"; 367 368 pub const Header = thread.Header; 369 370 const open_tab_label = "open"; 371 const closed_tab_label = "closed"; 372 const labels_tab_label = "labels"; 373 const edit_tab_label = "edit"; 374 const reply_tab_label = "reply"; 375 const remove_tab_label = "remove"; 376 const resolve_tab_label = "resolve"; 377 const new_tab_label = "new"; 378 const conflicts_tab_label = "conflicts"; 379 380 // tabs switching between the issues page's views 381 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 382 var header = try Header.init(allocator, session, data.search); 383 errdefer header.deinit(allocator); 384 const selected_index = View.viewIndex(data.view); 385 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_issues; 386 387 // a list tab per status, labeled with its listing's issue count 388 const status_labels = [_][]const u8{ open_tab_label, closed_tab_label }; 389 for ([_]evt.Issue.Status{ .open, .closed }, status_labels, 0..) |status, status_label, index| { 390 const route = try thread.searchRoute(ui.RoutablePage.repoIssuesRoute(data.identity, status, data.label, ""), data.search); 391 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 392 var label_buf: [64]u8 = undefined; 393 const label = try thread.countLabel(&label_buf, status_label, data.window(status).*); 394 try header.addTab(allocator, label, link, index); 395 } 396 397 // labels tab 398 { 399 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.issue, data.identity, data.label), data.search); 400 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 401 try header.addTab(allocator, labels_tab_label, labels_link, View.viewIndex(.labels)); 402 } 403 404 // new-issue tab; an edit or resolve url shows its tab in this place 405 { 406 const route = switch (data.view) { 407 .edit => ui.RoutablePage.repoThreadEditRoute(.issue, data.identity, data.selected_id) orelse return error.RouteTooLong, 408 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 409 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 410 .remove => ui.RoutablePage.repoThreadRemoveRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 411 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 412 else => ui.RoutablePage.repoThreadNewRoute(.issue, data.identity) orelse return error.RouteTooLong, 413 }; 414 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 415 const label: []const u8 = switch (data.view) { 416 .edit => edit_tab_label, 417 .new_comment => reply_tab_label, 418 .edit_comment => edit_tab_label, 419 .remove => remove_tab_label, 420 .resolve => resolve_tab_label, 421 else => new_tab_label, 422 }; 423 try header.addTab(allocator, label, link, View.viewIndex(.new)); 424 } 425 426 // conflicts tab, labeled with the conflict count 427 if (data.conflicts.count > 0) { 428 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 429 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 430 var label_buf: [64]u8 = undefined; 431 const label = try std.fmt.bufPrint(&label_buf, conflicts_tab_label ++ " ({d})", .{data.conflicts.count}); 432 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 433 } 434 435 header.select(View.viewIndex(data.view)); 436 return header; 437 } 438