╭ 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 Threads = @import("Threads.zig"); 12 13 const wasm = builtin.target.cpu.arch == .wasm32; 14 15 // how much of a description the detail pane shows before linking to the 16 // /description page, the same limit commit messages use. 17 pub const max_description_size = 2 * 1024; 18 19 // how many issues one window shows before a "next" link appears. 20 pub const page_size = 20; 21 22 // how many tags the tags view shows at most. 23 pub const max_tags = 1000; 24 25 // one issue from the repo's consumed event database, with its hex event id 26 // (the id lives in the event envelope, not the payload). 27 pub const IssueWithId = struct { 28 id: []const u8, 29 record: evt.Issue.Record, 30 // the creation commit's author 31 author: ui.Author = .unknown, 32 // whether the issue has an unresolved merge conflict 33 conflicted: bool = false, 34 comments: Comment.Window = .empty, 35 attachments: []const Attachment.WithId = &.{}, 36 }; 37 38 pub const Entry = IssueWithId; 39 40 // one side of a conflicted field: its value and who set it 41 pub const Side = struct { 42 text: []const u8, 43 author: ui.Author = .unknown, 44 }; 45 46 pub const FieldConflict = struct { 47 ours: Side, 48 theirs: Side, 49 }; 50 51 // the selected issue's conflict, read for the resolve view. ours is the live 52 // record; the chunks split the description against the merge base. 53 pub const Conflict = struct { 54 title: ?FieldConflict = null, 55 tags: ?FieldConflict = null, 56 description: ?struct { 57 chunks: []const diff3.Chunk, 58 ours_author: ui.Author = .unknown, 59 theirs_author: ui.Author = .unknown, 60 } = null, 61 }; 62 63 // one status's windowed listing. 64 pub const Window = struct { 65 items: []const IssueWithId, 66 // the id of the previous window's first issue, or null when this window 67 // is already the first. 68 prev_id: ?[]const u8, 69 // the id of the next window's first issue, or null when this is the last 70 // window. 71 next_id: ?[]const u8, 72 // how many issues the listing holds across all windows. 73 count: usize, 74 75 pub const empty: Window = .{ .items = &.{}, .prev_id = null, .next_id = null, .count = 0 }; 76 }; 77 78 // "owner/name", so the view can build /repo/owner/name/issues/... links. 79 identity: []const u8, 80 // the url-encoded tag the lists are filtered to ("" = unfiltered). 81 tag: []const u8, 82 // the hex event id of the issue its status's window is rooted at ("" = the 83 // first window), mirrored into the url. 84 selected_id: []const u8, 85 // the comment shown in the selected issue's detail pane. 86 comment_id: []const u8, 87 // the selected issue's comment window (0 = the first window). 88 comments_start: usize, 89 // the selected comment and its immediate replies. 90 comment_page: ?Comment.Permalink = null, 91 open: Window, 92 closed: Window, 93 // the conflicted issues' listing; its count also gates the conflicts tab. 94 conflicts: Window, 95 // the selected issue's conflict, set when the page shows the resolve view. 96 conflict: ?Conflict = null, 97 // the resolve view's comma-separated fields prefilled from their side, 98 // mirrored from the url. 99 theirs_picks: []const u8 = "", 100 // the view the page shows initially (a selected issue's status overrides the route's) 101 view: ui.RoutablePage.IssuesView, 102 // the /description page: the detail pane shows the selected issue's whole 103 // description behind a back link. 104 description_page: bool = false, 105 // every tag in the repo, in sorted order, for the tags view. 106 tags: []const []const u8, 107 // the on-disk repo this page was read from, for the terminal submit path 108 // (the web posts the new-issue form to the issue route instead). 109 repo_source: ?ui.RepoSource = null, 110 111 const Self = @This(); 112 113 pub const Event = evt.Issue; 114 pub const Status = evt.Issue.Status; 115 pub const ViewKind = ui.RoutablePage.IssuesView; 116 pub const thread_name = "issue"; 117 pub const header_widget_name = "repo_issues_header"; 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 Threads.tagStatusSet(Self, DB, tag_to_issues, decoded, .open); 239 closed_set = try Threads.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 Threads.statusSet(Self, DB, status_to_issues, .open); 245 closed_set = try Threads.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 Threads.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 Threads.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 Threads.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 Threads.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 Threads.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 = Threads.View(.issue, Self); 351 352 pub const Header = Threads.Header; 353 354 // tabs switching between the issues page's views 355 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 356 var header = try Header.init(allocator); 357 errdefer header.deinit(allocator); 358 const aa = session.page_arena.allocator(); 359 const selected_index = View.viewIndex(data.view); 360 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_issues; 361 362 // a list tab per status, labeled with its listing's issue count 363 for ([_]evt.Issue.Status{ .open, .closed }, 0..) |status, index| { 364 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 365 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 366 var label_buf: [64]u8 = undefined; 367 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 368 try header.addTab(allocator, label, link, index); 369 } 370 371 // tags tab, labeled with the active tag filter 372 { 373 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.issue, data.identity, data.tag) orelse return error.RouteTooLong; 374 const tags_link = try ui.inPageTabLink(session, tags_route, page_selected and selected_index == View.viewIndex(.tags)); 375 const label = if (data.tag.len == 0) "tags" else blk: { 376 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 377 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 378 }; 379 try header.addTab(allocator, label, tags_link, View.viewIndex(.tags)); 380 } 381 382 // new-issue tab; an edit or resolve url shows its tab in this place 383 { 384 const route = switch (data.view) { 385 .edit => ui.RoutablePage.repoThreadEditRoute(.issue, data.identity, data.selected_id) orelse return error.RouteTooLong, 386 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 387 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 388 .remove => ui.RoutablePage.repoThreadRemoveRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 389 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 390 else => ui.RoutablePage.repoThreadNewRoute(.issue, data.identity) orelse return error.RouteTooLong, 391 }; 392 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 393 const label: []const u8 = switch (data.view) { 394 .edit => "edit", 395 .new_comment => "reply", 396 .edit_comment => "edit", 397 .remove => "remove", 398 .resolve => "resolve", 399 else => "new", 400 }; 401 try header.addTab(allocator, label, link, View.viewIndex(.new)); 402 } 403 404 // conflicts tab, labeled with the conflict count 405 if (data.conflicts.count > 0) { 406 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 407 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 408 var label_buf: [64]u8 = undefined; 409 const label = try std.fmt.bufPrint(&label_buf, "conflicts ({d})", .{data.conflicts.count}); 410 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 411 } 412 413 header.select(View.viewIndex(data.view)); 414 return header; 415 } 416