╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 const selected_index = View.viewIndex(data.view); 346 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_issues; 347 348 // a list tab per status, labeled with its listing's issue count 349 for ([_]evt.Issue.Status{ .open, .closed }, 0..) |status, index| { 350 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 351 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 352 var label_buf: [64]u8 = undefined; 353 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 354 try header.addTab(allocator, label, link, index); 355 } 356 357 // tags tab, labeled with the active tag filter 358 { 359 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.issue, data.identity, data.tag) orelse return error.RouteTooLong; 360 const tags_link = try ui.inPageTabLink(session, tags_route, page_selected and selected_index == View.viewIndex(.tags)); 361 const label = if (data.tag.len == 0) "tags" else blk: { 362 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 363 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 364 }; 365 try header.addTab(allocator, label, tags_link, View.viewIndex(.tags)); 366 } 367 368 // new-issue tab; an edit or resolve url shows its tab in this place 369 { 370 const route = switch (data.view) { 371 .edit => ui.RoutablePage.repoThreadEditRoute(.issue, data.identity, data.selected_id) orelse return error.RouteTooLong, 372 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 373 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 374 .remove => ui.RoutablePage.repoThreadRemoveRoute(.issue, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 375 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 376 else => ui.RoutablePage.repoThreadNewRoute(.issue, data.identity) orelse return error.RouteTooLong, 377 }; 378 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 379 const label: []const u8 = switch (data.view) { 380 .edit => "edit", 381 .new_comment => "reply", 382 .edit_comment => "edit", 383 .remove => "remove", 384 .resolve => "resolve", 385 else => "new", 386 }; 387 try header.addTab(allocator, label, link, View.viewIndex(.new)); 388 } 389 390 // conflicts tab, labeled with the conflict count 391 if (data.conflicts.count > 0) { 392 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 393 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 394 var label_buf: [64]u8 = undefined; 395 const label = try std.fmt.bufPrint(&label_buf, "conflicts ({d})", .{data.conflicts.count}); 396 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 397 } 398 399 header.select(View.viewIndex(data.view)); 400 return header; 401 } 402