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