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