╭ 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 diff3 = @import("../../diff3.zig"); 9 const Comment = @import("Comment.zig"); 10 const Attachment = @import("Attachment.zig"); 11 const thread = ui.widget.thread; 12 13 const wasm = builtin.target.cpu.arch == .wasm32; 14 15 // how many issues one window shows before a "next" link appears. 16 pub const page_size = 20; 17 18 // how many tags the tags view shows at most. 19 pub const max_tags = 1000; 20 21 // one issue from the repo's consumed event database, with its hex event id 22 // (the id lives in the event envelope, not the payload). 23 pub const IssueWithId = struct { 24 id: []const u8, 25 record: evt.Issue.Record, 26 // the creation commit's author 27 author: ui.Author = .unknown, 28 // whether the issue has an unresolved merge conflict 29 conflicted: bool = false, 30 comments: Comment.Window = .empty, 31 attachments: []const Attachment.WithId = &.{}, 32 }; 33 34 pub const Entry = IssueWithId; 35 36 // one side of a conflicted field: its value and who set it 37 pub const Side = struct { 38 text: []const u8, 39 author: ui.Author = .unknown, 40 }; 41 42 pub const FieldConflict = struct { 43 ours: Side, 44 theirs: Side, 45 }; 46 47 // the selected issue's conflict, read for the resolve view. ours is the live 48 // record; the chunks split the description against the merge base. 49 pub const Conflict = struct { 50 title: ?FieldConflict = null, 51 tags: ?FieldConflict = null, 52 description: ?struct { 53 chunks: []const diff3.Chunk, 54 ours_author: ui.Author = .unknown, 55 theirs_author: ui.Author = .unknown, 56 } = null, 57 }; 58 59 // one status's windowed listing. 60 pub const Window = struct { 61 items: []const IssueWithId, 62 // the id of the previous window's first issue, or null when this window 63 // is already the first. 64 prev_id: ?[]const u8, 65 // the id of the next window's first issue, or null when this is the last 66 // window. 67 next_id: ?[]const u8, 68 // how many issues the listing holds across all windows. 69 count: usize, 70 71 pub const empty: Window = .{ .items = &.{}, .prev_id = null, .next_id = null, .count = 0 }; 72 }; 73 74 // "owner/name", so the view can build /repo/owner/name/issues/... links. 75 identity: []const u8, 76 // the url-encoded tag the lists are filtered to ("" = unfiltered). 77 tag: []const u8, 78 // the hex event id of the issue its status's window is rooted at ("" = the 79 // first window), mirrored into the url. 80 selected_id: []const u8, 81 // the comment shown in the selected issue's detail pane. 82 comment_id: []const u8, 83 // the selected issue's comment window (0 = the first window). 84 comments_start: usize, 85 // the selected comment and its immediate replies. 86 comment_page: ?Comment.Permalink = null, 87 open: Window, 88 closed: Window, 89 // the conflicted issues' listing; its count also gates the conflicts tab. 90 conflicts: Window, 91 // the selected issue's conflict, set when the page shows the resolve view. 92 conflict: ?Conflict = null, 93 // the resolve view's comma-separated fields prefilled from their side, 94 // mirrored from the url. 95 theirs_picks: []const u8 = "", 96 // the view the page shows initially (a selected issue's status overrides the route's) 97 view: ui.RoutablePage.IssuesView, 98 // the /description page: the detail pane shows the selected issue's whole 99 // description behind a back link. 100 description_page: bool = false, 101 // every tag in the repo, in sorted order, for the tags view. 102 tags: []const []const u8, 103 // the on-disk repo this page was read from, for the terminal submit path 104 // (the web posts the new-issue form to the issue route instead). 105 repo_source: ?ui.RepoSource = null, 106 107 const Self = @This(); 108 109 pub const Event = evt.Issue; 110 pub const Status = evt.Issue.Status; 111 pub const ViewKind = ui.RoutablePage.IssuesView; 112 pub const thread_name = "issue"; 113 pub const header_widget_name = "repo_issues_header"; 114 115 pub fn listRoute(identity: []const u8, status: Status, tag: []const u8, selected: []const u8) ?ui.RoutablePage { 116 return ui.RoutablePage.repoIssuesRoute(identity, status, tag, selected); 117 } 118 119 pub fn conflictsRoute(identity: []const u8, selected: []const u8) ?ui.RoutablePage { 120 return ui.RoutablePage.repoIssuesConflictsRoute(identity, selected); 121 } 122 123 pub fn resolveRoute(identity: []const u8, selected: []const u8, picks: []const u8) ?ui.RoutablePage { 124 return ui.RoutablePage.repoIssuesResolveRoute(identity, selected, picks); 125 } 126 127 // `status`'s windowed listing. 128 pub fn window(self: *const Self, status: evt.Issue.Status) *const Window { 129 return switch (status) { 130 .open => &self.open, 131 .closed => &self.closed, 132 }; 133 } 134 135 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 136 // theirs: picks. 137 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 138 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 139 while (it.next()) |pick| { 140 if (std.mem.eql(u8, pick, field)) return true; 141 } 142 return false; 143 } 144 145 // the issue `selected_id` names, in whichever window holds it. 146 pub fn selectedThread(self: *const Self) ?*const IssueWithId { 147 if (self.selected_id.len == 0) return null; 148 for ([_]*const Window{ &self.open, &self.closed }) |win| { 149 for (win.items) |*entry| { 150 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 151 } 152 } 153 return null; 154 } 155 156 // an empty listing, for the wasm / no-repo paths. 157 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 { 158 return .{ 159 .identity = try aa.dupe(u8, identity), 160 .tag = try aa.dupe(u8, tag), 161 .selected_id = try aa.dupe(u8, selected_id), 162 .comment_id = try aa.dupe(u8, comment_id), 163 .comments_start = comments_start, 164 .open = .empty, 165 .closed = .empty, 166 .conflicts = .empty, 167 .theirs_picks = try aa.dupe(u8, theirs_picks), 168 // a description url shows a status list, the issue's own status 169 // picking it once the issue is read; conflicts and resolve urls also 170 // start there, upgraded by init when the conflict data exists. 171 .view = switch (view) { 172 .description, .conflicts, .resolve => .open, 173 else => view, 174 }, 175 .description_page = view == .description, 176 .tags = &.{}, 177 }; 178 } 179 180 // read one window per status of an opened repo's issues (filtered to `tag` 181 // when set), ordered by creation (newest first). the window of the issue 182 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 183 // event db next to it (synced from the events branch on each page build); a 184 // xit repo reads its own db. 185 pub fn init( 186 comptime repo_kind: rp.RepoKind, 187 comptime repo_opts: rp.RepoOpts(repo_kind), 188 arena: *std.heap.ArenaAllocator, 189 repo: *rp.Repo(repo_kind, repo_opts), 190 io: std.Io, 191 // the admin db's moment, for resolving author emails to user names (null 192 // in local mode, which has no users) 193 admin_moment: ?evt.AdminDB.HashMap(.read_only), 194 identity: []const u8, 195 tag: []const u8, 196 selected_id: []const u8, 197 comment_id: []const u8, 198 comments_start: usize, 199 theirs_picks: []const u8, 200 view: ui.RoutablePage.IssuesView, 201 ) !Self { 202 const empty = try emptyResult(arena.allocator(), identity, tag, selected_id, comment_id, comments_start, theirs_picks, view); 203 204 const aa = arena.allocator(); 205 const DB = evt.EventDB(repo_opts.hash); 206 const rooted = empty.selected_id.len != 0; 207 const tagged = empty.tag.len != 0; 208 // an explicitly named issue or tag that doesn't exist is a bad url 209 // (NotFound -> 404); the bare route falls through to an empty listing. 210 const strict = rooted or tagged; 211 212 // a repo with no consumed events has no moment yet. 213 const gpa = arena.child_allocator; 214 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; 215 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 216 const haxy_moment = (if (event_db_maybe) |*event_db| 217 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 218 else if (repo_kind == .git) 219 return empty 220 else 221 evt.currentMoment(repo_opts, repo)) catch { 222 if (strict) return error.NotFound; 223 return empty; 224 }; 225 226 // the sorted sets to window, per status: the tag's sets when filtered, 227 // else the top-level per-status sets. a missing set is an empty window. 228 var open_set: ?DB.SortedSet(.read_only) = null; 229 var closed_set: ?DB.SortedSet(.read_only) = null; 230 if (tagged) { 231 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; 232 const tag_to_issues = try DB.SortedMap(.read_only).init(tag_to_issues_cursor); 233 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.tag)); 234 open_set = try thread.tagStatusSet(Self, DB, tag_to_issues, decoded, .open); 235 closed_set = try thread.tagStatusSet(Self, DB, tag_to_issues, decoded, .closed); 236 // a tag no issue carries is a bad url 237 if (open_set == null and closed_set == null) return error.NotFound; 238 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.status_to_id_set_key))) |status_to_issues_cursor| { 239 const status_to_issues = try DB.SortedMap(.read_only).init(status_to_issues_cursor); 240 open_set = try thread.statusSet(Self, DB, status_to_issues, .open); 241 closed_set = try thread.statusSet(Self, DB, status_to_issues, .closed); 242 } else if (rooted) return error.NotFound; 243 244 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse { 245 if (strict) return error.NotFound; 246 return empty; 247 }; 248 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 249 250 // the conflicted issues' container: a set view for membership and 251 // windowing, a map view for the resolve entry, over the same cursor. 252 var conflict_set: ?DB.SortedSet(.read_only) = null; 253 var conflicts_map: ?DB.SortedMap(.read_only) = null; 254 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.conflicts_key))) |cursor| { 255 conflict_set = try DB.SortedSet(.read_only).init(cursor); 256 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 257 } 258 259 // a named issue roots its own status's window at itself (the conflicts 260 // view's window instead); the other windows start at the beginning. 261 var resolved_view = empty.view; 262 var open_root: ?[]const u8 = null; 263 var closed_root: ?[]const u8 = null; 264 var conflicts_root: ?[]const u8 = null; 265 var conflict_data: ?Conflict = null; 266 if (rooted) { 267 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 268 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 269 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 270 const issue_event = try evt.read(evt.Issue.Record, DB, repo_opts.hash, arena, issue_map); 271 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(issue_event.created_order, &id_bytes)); 272 273 if (view == .conflicts) { 274 // the id roots the conflicts window, so it must be conflicted 275 const set = conflict_set orelse return error.NotFound; 276 if (!try set.contains(order_key)) return error.NotFound; 277 conflicts_root = order_key; 278 } else { 279 // the named issue must be in its windowed set (a tag url can name 280 // an issue that doesn't carry the tag). 281 const set = (switch (issue_event.event.status) { 282 .open => open_set, 283 .closed => closed_set, 284 }) orelse return error.NotFound; 285 if (!try set.contains(order_key)) return error.NotFound; 286 287 switch (issue_event.event.status) { 288 .open => open_root = order_key, 289 .closed => closed_root = order_key, 290 } 291 // form urls keep their view; otherwise the issue's status picks it. 292 if (view != .edit and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (issue_event.event.status) { 293 .open => .open, 294 .closed => .closed, 295 }; 296 297 // a resolve url shows the resolve view only while the issue still 298 // has a conflict entry; otherwise the status list stands. the 299 // wasm client renders from the snapshot, so the diff machinery 300 // this pulls in is gated out of its build. 301 if (comptime !wasm) { 302 if (view == .resolve) { 303 if (conflicts_map) |map| { 304 if (try map.getCursor(order_key)) |conflict_cursor| { 305 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 306 conflict_data = try thread.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, issue_event, conflict_entry); 307 resolved_view = .resolve; 308 } 309 } 310 } 311 } 312 } 313 } 314 315 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 316 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); 317 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); 318 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); 319 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 320 321 const comment_page = if (empty.comment_id.len == 0) 322 null 323 else 324 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start); 325 326 const tags = try thread.loadTags(Self, repo_opts.hash, arena, haxy_moment); 327 328 return .{ 329 .identity = empty.identity, 330 .tag = empty.tag, 331 .selected_id = empty.selected_id, 332 .comment_id = empty.comment_id, 333 .comments_start = comments_start, 334 .comment_page = comment_page, 335 .open = open_window, 336 .closed = closed_window, 337 .conflicts = conflicts_window, 338 .conflict = conflict_data, 339 .theirs_picks = empty.theirs_picks, 340 .view = resolved_view, 341 .description_page = empty.description_page, 342 .tags = tags, 343 }; 344 } 345 346 pub const View = thread.View(.issue, Self); 347 pub const Detail = thread.Detail(.issue, Self); 348 pub const detail_widget_name = "repo_issue_detail"; 349 350 pub const Header = thread.Header; 351 352 // tabs switching between the issues page's views 353 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 354 var header = try Header.init(allocator); 355 errdefer header.deinit(allocator); 356 const aa = session.page_arena.allocator(); 357 const selected_index = View.viewIndex(data.view); 358 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_issues; 359 360 // a list tab per status, labeled with its listing's issue count 361 for ([_]evt.Issue.Status{ .open, .closed }, 0..) |status, index| { 362 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 363 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 364 var label_buf: [64]u8 = undefined; 365 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 366 try header.addTab(allocator, label, link, index); 367 } 368 369 // tags tab, labeled with the active tag filter 370 { 371 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.issue, data.identity, data.tag) orelse return error.RouteTooLong; 372 const tags_link = try ui.inPageTabLink(session, tags_route, page_selected and selected_index == View.viewIndex(.tags)); 373 const label = if (data.tag.len == 0) "tags" else blk: { 374 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 375 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 376 }; 377 try header.addTab(allocator, label, tags_link, View.viewIndex(.tags)); 378 } 379 380 // new-issue tab; an edit or resolve url shows its tab in this place 381 { 382 const route = switch (data.view) { 383 .edit => ui.RoutablePage.repoThreadEditRoute(.issue, data.identity, data.selected_id) orelse return error.RouteTooLong, 384 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 385 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 386 .remove => ui.RoutablePage.repoThreadRemoveRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 387 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 388 else => ui.RoutablePage.repoThreadNewRoute(.issue, data.identity) orelse return error.RouteTooLong, 389 }; 390 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 391 const label: []const u8 = switch (data.view) { 392 .edit => "edit", 393 .new_comment => "reply", 394 .edit_comment => "edit", 395 .remove => "remove", 396 .resolve => "resolve", 397 else => "new", 398 }; 399 try header.addTab(allocator, label, link, View.viewIndex(.new)); 400 } 401 402 // conflicts tab, labeled with the conflict count 403 if (data.conflicts.count > 0) { 404 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 405 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 406 var label_buf: [64]u8 = undefined; 407 const label = try std.fmt.bufPrint(&label_buf, "conflicts ({d})", .{data.conflicts.count}); 408 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 409 } 410 411 header.select(View.viewIndex(data.view)); 412 return header; 413 } 414