╭ Patches.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 const fork = @import("../../fork.zig"); 13 const pch = @import("../../patch.zig"); 14 15 const wasm = builtin.target.cpu.arch == .wasm32; 16 17 // how much of a description the detail pane shows before linking to the 18 // /description page, the same limit commit messages use. 19 pub const max_description_size = 2 * 1024; 20 21 // how many patches one window shows before a "next" link appears. 22 pub const page_size = 20; 23 24 // how many tags the tags view shows at most. 25 pub const max_tags = 1000; 26 27 // one patch from the repo's consumed event database, with its hex event id 28 // (the id lives in the event envelope, not the payload). 29 pub const PatchWithId = struct { 30 id: []const u8, 31 record: evt.Patch.Record, 32 // the creation commit's author 33 author: ui.Author = .unknown, 34 // whether the patch has an unresolved merge conflict 35 conflicted: bool = false, 36 comments: Comment.Window = .empty, 37 attachments: []const Attachment.WithId = &.{}, 38 draft: bool = false, 39 revision_ready: bool = false, 40 }; 41 42 pub const Entry = PatchWithId; 43 44 // one side of a conflicted field: its value and who set it 45 pub const Side = struct { 46 text: []const u8, 47 author: ui.Author = .unknown, 48 }; 49 50 pub const FieldConflict = struct { 51 ours: Side, 52 theirs: Side, 53 }; 54 55 // the selected patch's conflict, read for the resolve view. ours is the live 56 // record; the chunks split the description against the merge base. 57 pub const Conflict = struct { 58 title: ?FieldConflict = null, 59 tags: ?FieldConflict = null, 60 status: ?FieldConflict = null, 61 revision: ?FieldConflict = null, 62 description: ?struct { 63 chunks: []const diff3.Chunk, 64 ours_author: ui.Author = .unknown, 65 theirs_author: ui.Author = .unknown, 66 } = null, 67 }; 68 69 // one status's windowed listing. 70 pub const Window = struct { 71 items: []const PatchWithId, 72 // the id of the previous window's first patch, or null when this window 73 // is already the first. 74 prev_id: ?[]const u8, 75 // the id of the next window's first patch, or null when this is the last 76 // window. 77 next_id: ?[]const u8, 78 // how many patches the listing holds across all windows. 79 count: usize, 80 81 pub const empty: Window = .{ .items = &.{}, .prev_id = null, .next_id = null, .count = 0 }; 82 }; 83 84 // "owner/name", so the view can build /repo/owner/name/patches/... links. 85 identity: []const u8, 86 // the url-encoded tag the lists are filtered to ("" = unfiltered). 87 tag: []const u8, 88 // the hex event id of the patch its status's window is rooted at ("" = the 89 // first window), mirrored into the url. 90 selected_id: []const u8, 91 // the comment shown in the selected patch's detail pane. 92 comment_id: []const u8, 93 // the selected patch's comment window (0 = the first window). 94 comments_start: usize, 95 // the selected comment and its immediate replies. 96 comment_page: ?Comment.Permalink = null, 97 open: Window, 98 closed: Window, 99 merged: Window, 100 drafts: Window, 101 // the conflicted patches' listing; its count also gates the conflicts tab. 102 conflicts: Window, 103 // the selected patch's conflict, set when the page shows the resolve view. 104 conflict: ?Conflict = null, 105 // the resolve view's comma-separated fields prefilled from their side, 106 // mirrored from the url. 107 theirs_picks: []const u8 = "", 108 // the view the page shows initially (a selected patch's status overrides the route's) 109 view: ui.RoutablePage.PatchesView, 110 // the /description page: the detail pane shows the selected patch's whole 111 // description behind a back link. 112 description_page: bool = false, 113 // every tag in the repo, in sorted order, for the tags view. 114 tags: []const []const u8, 115 // the on-disk repo this page was read from, for the terminal submit path 116 // (the web posts the new-patch form to the patch route instead). 117 repo_source: ?ui.RepoSource = null, 118 repo_id: ?[evt.event_id_size]u8 = null, 119 target_branch: []const u8 = "", 120 121 const Self = @This(); 122 123 pub const Event = evt.Patch; 124 pub const Status = evt.Patch.Status; 125 pub const ViewKind = ui.RoutablePage.PatchesView; 126 127 pub fn createDraft( 128 data: *const Self, 129 session: *ui.Session, 130 allocator: std.mem.Allocator, 131 title: []const u8, 132 tags: []const u8, 133 description: []const u8, 134 ) ![evt.event_id_size * 2]u8 { 135 if (!evt.Patch.fieldsValid(title, tags)) return error.InvalidFields; 136 const io = session.io orelse return error.NotFound; 137 const repos_dir = session.repos_dir orelse return error.NotFound; 138 const admin_repo = session.admin_repo orelse return error.NotFound; 139 const user_id_slice = session.data.user_id orelse return error.NotFound; 140 if (user_id_slice.len != evt.event_id_size) return error.NotFound; 141 const repo_id = data.repo_id orelse return error.NotFound; 142 const author = (try session.eventAuthor()) orelse return error.NotFound; 143 var id_bytes: [evt.event_id_size]u8 = undefined; 144 io.random(&id_bytes); 145 const id = std.fmt.bytesToHex(id_bytes, .lower); 146 var user_id: [evt.event_id_size]u8 = undefined; 147 @memcpy(&user_id, user_id_slice); 148 const path = try fork.create(.{}, io, allocator, repos_dir, admin_repo, .{ 149 .id = id, 150 .user_id = user_id, 151 .repo_id = repo_id, 152 .title = title, 153 .description = description, 154 .tags = tags, 155 .author = author, 156 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 157 }); 158 allocator.free(path); 159 return id; 160 } 161 162 pub fn postDraft(data: *const Self, session: *ui.Session, allocator: std.mem.Allocator, id: []const u8) !void { 163 const io = session.io orelse return error.NotFound; 164 const repos_dir = session.repos_dir orelse return error.NotFound; 165 const admin_repo = session.admin_repo orelse return error.NotFound; 166 const repo_source = data.repo_source orelse return error.NotFound; 167 const repo_id = data.repo_id orelse return error.NotFound; 168 const user_id_slice = session.data.user_id orelse return error.NotFound; 169 if (user_id_slice.len != evt.event_id_size) return error.NotFound; 170 const patch_id = evt.parseEventId(id) catch return error.NotFound; 171 const author = (try session.eventAuthor()) orelse return error.NotFound; 172 173 var user_id: [evt.event_id_size]u8 = undefined; 174 @memcpy(&user_id, user_id_slice); 175 const id_hex = std.fmt.bytesToHex(patch_id, .lower); 176 const fork_path = try fork.forkPath(allocator, repos_dir, &id_hex); 177 defer allocator.free(fork_path); 178 179 var target_repo = try rp.Repo(.xit, .{}).open(io, allocator, repo_source.localInitOpts()); 180 defer target_repo.deinit(io, allocator); 181 try pch.post(.{}, io, allocator, admin_repo, &target_repo, fork_path, .{ 182 .id = id_hex, 183 .user_id = user_id, 184 .repo_id = repo_id, 185 .author = author, 186 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 187 }); 188 } 189 190 // `status`'s windowed listing. 191 pub fn window(self: *const Self, status: evt.Patch.Status) *const Window { 192 return switch (status) { 193 .open => &self.open, 194 .closed => &self.closed, 195 .merged => &self.merged, 196 }; 197 } 198 199 // whether `field` (a field name or "d<n>" hunk name) is listed in the url's 200 // theirs: picks. 201 pub fn theirsPicked(self: *const Self, field: []const u8) bool { 202 var it = std.mem.splitScalar(u8, self.theirs_picks, ','); 203 while (it.next()) |pick| { 204 if (std.mem.eql(u8, pick, field)) return true; 205 } 206 return false; 207 } 208 209 // the patch `selected_id` names, in whichever window holds it. 210 pub fn selectedThread(self: *const Self) ?*const PatchWithId { 211 if (self.selected_id.len == 0) return null; 212 for ([_]*const Window{ &self.open, &self.closed, &self.merged, &self.drafts }) |win| { 213 for (win.items) |*entry| { 214 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 215 } 216 } 217 return null; 218 } 219 220 // an empty listing, for the wasm / no-repo paths. 221 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.PatchesView) !Self { 222 return .{ 223 .identity = try aa.dupe(u8, identity), 224 .tag = try aa.dupe(u8, tag), 225 .selected_id = try aa.dupe(u8, selected_id), 226 .comment_id = try aa.dupe(u8, comment_id), 227 .comments_start = comments_start, 228 .open = .empty, 229 .closed = .empty, 230 .merged = .empty, 231 .drafts = .empty, 232 .conflicts = .empty, 233 .theirs_picks = try aa.dupe(u8, theirs_picks), 234 // a description url shows a status list, the patch's own status 235 // picking it once the patch is read; conflicts and resolve urls also 236 // start there, upgraded by init when the conflict data exists. 237 .view = switch (view) { 238 .description, .conflicts, .resolve => .open, 239 else => view, 240 }, 241 .description_page = view == .description, 242 .tags = &.{}, 243 }; 244 } 245 246 // read one window per status of an opened repo's patches (filtered to `tag` 247 // when set), ordered by creation (newest first). the window of the patch 248 // `selected_id` names starts at it ("" = the beginning). a git repo reads the 249 // event db next to it (synced from the events branch on each page build); a 250 // xit repo reads its own db. 251 pub fn init( 252 comptime repo_kind: rp.RepoKind, 253 comptime repo_opts: rp.RepoOpts(repo_kind), 254 arena: *std.heap.ArenaAllocator, 255 repo: *rp.Repo(repo_kind, repo_opts), 256 io: std.Io, 257 // the admin db's moment, for resolving author emails to user names (null 258 // in local mode, which has no users) 259 admin_moment: ?evt.AdminDB.HashMap(.read_only), 260 session: *ui.Session, 261 repo_id_maybe: ?[evt.event_id_size]u8, 262 identity: []const u8, 263 tag: []const u8, 264 selected_id: []const u8, 265 comment_id: []const u8, 266 comments_start: usize, 267 theirs_picks: []const u8, 268 view: ui.RoutablePage.PatchesView, 269 ) !Self { 270 const allowed_view: ui.RoutablePage.PatchesView = if (session.local != null and (view == .new or view == .drafts)) .open else view; 271 var empty = try emptyResult(arena.allocator(), identity, tag, selected_id, comment_id, comments_start, theirs_picks, allowed_view); 272 273 const aa = arena.allocator(); 274 const DB = evt.EventDB(repo_opts.hash); 275 const rooted = empty.selected_id.len != 0; 276 const tagged = empty.tag.len != 0; 277 const drafts_window = if (admin_moment) |admin| 278 if (repo_id_maybe) |repo_id| 279 if (session.data.user_id != null and session.repos_dir != null) 280 try loadDraftWindow(arena, session, admin, &repo_id, empty.selected_id) 281 else 282 Window.empty 283 else 284 Window.empty 285 else 286 Window.empty; 287 var draft_selected = false; 288 if (rooted) for (drafts_window.items) |item| { 289 if (std.mem.eql(u8, item.id, empty.selected_id)) { 290 draft_selected = true; 291 break; 292 } 293 }; 294 empty.drafts = drafts_window; 295 if (draft_selected) empty.view = .drafts; 296 297 // an explicitly named posted patch or tag that doesn't exist is a bad url 298 // (NotFound -> 404); drafts and bare routes can use the empty fallback. 299 const strict = tagged or (rooted and !draft_selected); 300 301 // a repo with no consumed events has no moment yet. 302 const gpa = arena.child_allocator; 303 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; 304 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 305 const haxy_moment = (if (event_db_maybe) |*event_db| 306 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 307 else if (repo_kind == .git) 308 return empty 309 else 310 evt.currentMoment(repo_opts, repo)) catch { 311 if (strict) return error.NotFound; 312 return empty; 313 }; 314 315 // the sorted sets to window, per status: the tag's sets when filtered, 316 // else the top-level per-status sets. a missing set is an empty window. 317 var open_set: ?DB.SortedSet(.read_only) = null; 318 var closed_set: ?DB.SortedSet(.read_only) = null; 319 var merged_set: ?DB.SortedSet(.read_only) = null; 320 if (tagged) { 321 const tag_to_patches_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.tag_status_to_id_set_key)) orelse return error.NotFound; 322 const tag_to_patches = try DB.SortedMap(.read_only).init(tag_to_patches_cursor); 323 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.tag)); 324 open_set = try Threads.tagStatusSet(Self, DB, tag_to_patches, decoded, .open); 325 closed_set = try Threads.tagStatusSet(Self, DB, tag_to_patches, decoded, .closed); 326 merged_set = try Threads.tagStatusSet(Self, DB, tag_to_patches, decoded, .merged); 327 // a tag no patch carries is a bad url 328 if (open_set == null and closed_set == null and merged_set == null) return error.NotFound; 329 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key))) |status_to_patches_cursor| { 330 const status_to_patches = try DB.SortedMap(.read_only).init(status_to_patches_cursor); 331 open_set = try Threads.statusSet(Self, DB, status_to_patches, .open); 332 closed_set = try Threads.statusSet(Self, DB, status_to_patches, .closed); 333 merged_set = try Threads.statusSet(Self, DB, status_to_patches, .merged); 334 } else if (strict) return error.NotFound; 335 336 const event_id_to_patch_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse { 337 if (strict) return error.NotFound; 338 return empty; 339 }; 340 const event_id_to_patch = try DB.HashMap(.read_only).init(event_id_to_patch_cursor); 341 342 // the conflicted patches' container: a set view for membership and 343 // windowing, a map view for the resolve entry, over the same cursor. 344 var conflict_set: ?DB.SortedSet(.read_only) = null; 345 var conflicts_map: ?DB.SortedMap(.read_only) = null; 346 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 347 conflict_set = try DB.SortedSet(.read_only).init(cursor); 348 conflicts_map = try DB.SortedMap(.read_only).init(cursor); 349 } 350 351 // a named patch roots its own status's window at itself (the conflicts 352 // view's window instead); the other windows start at the beginning. 353 var resolved_view = empty.view; 354 var open_root: ?[]const u8 = null; 355 var closed_root: ?[]const u8 = null; 356 var merged_root: ?[]const u8 = null; 357 var conflicts_root: ?[]const u8 = null; 358 var conflict_data: ?Conflict = null; 359 if (rooted and !draft_selected) { 360 const id_bytes = evt.parseEventId(empty.selected_id) catch return error.NotFound; 361 const patch_cursor = try event_id_to_patch.getCursor(hash.hashInt(repo_opts.hash, &id_bytes)) orelse return error.NotFound; 362 const patch_map = try DB.HashMap(.read_only).init(patch_cursor); 363 const patch_event = try evt.read(evt.Patch.Record, DB, repo_opts.hash, arena, patch_map); 364 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(patch_event.created_order, &id_bytes)); 365 366 if (view == .conflicts) { 367 // the id roots the conflicts window, so it must be conflicted 368 const set = conflict_set orelse return error.NotFound; 369 if (!try set.contains(order_key)) return error.NotFound; 370 conflicts_root = order_key; 371 } else { 372 // the named patch must be in its windowed set (a tag url can name 373 // an patch that doesn't carry the tag). 374 const set = (switch (patch_event.event.status) { 375 .open => open_set, 376 .closed => closed_set, 377 .merged => merged_set, 378 }) orelse return error.NotFound; 379 if (!try set.contains(order_key)) return error.NotFound; 380 381 switch (patch_event.event.status) { 382 .open => open_root = order_key, 383 .closed => closed_root = order_key, 384 .merged => merged_root = order_key, 385 } 386 // form urls keep their view; otherwise the patch's status picks it. 387 if (view != .edit and view != .new_comment and view != .edit_comment and view != .remove) resolved_view = switch (patch_event.event.status) { 388 .open => .open, 389 .closed => .closed, 390 .merged => .merged, 391 }; 392 393 // a resolve url shows the resolve view only while the patch still 394 // has a conflict entry; otherwise the status list stands. the 395 // wasm client renders from the snapshot, so the diff machinery 396 // this pulls in is gated out of its build. 397 if (comptime !wasm) { 398 if (view == .resolve) { 399 if (conflicts_map) |map| { 400 if (try map.getCursor(order_key)) |conflict_cursor| { 401 const conflict_entry = try DB.HashMap(.read_only).init(conflict_cursor); 402 conflict_data = try Threads.readConflict(Self, repo_kind, repo_opts, arena, repo, io, admin_moment, haxy_moment, &id_bytes, patch_event, conflict_entry); 403 resolved_view = .resolve; 404 } 405 } 406 } 407 } 408 } 409 } 410 const thread_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 411 const open_window = try Threads.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, open_set, open_root, conflict_set, empty.selected_id, thread_comments_start); 412 const closed_window = try Threads.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, closed_set, closed_root, conflict_set, empty.selected_id, thread_comments_start); 413 const merged_window = try Threads.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, merged_set, merged_root, conflict_set, empty.selected_id, thread_comments_start); 414 const conflicts_window = try Threads.loadWindow(Self, repo_opts.hash, arena, admin_moment, haxy_moment, event_id_to_patch, conflict_set, conflicts_root, conflict_set, empty.selected_id, thread_comments_start); 415 if (view == .conflicts and conflicts_window.count > 0) resolved_view = .conflicts; 416 417 const comment_page = if (empty.comment_id.len == 0) 418 null 419 else 420 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start); 421 422 const tags = try Threads.loadTags(Self, repo_opts.hash, arena, haxy_moment); 423 424 return .{ 425 .identity = empty.identity, 426 .tag = empty.tag, 427 .selected_id = empty.selected_id, 428 .comment_id = empty.comment_id, 429 .comments_start = comments_start, 430 .comment_page = comment_page, 431 .open = open_window, 432 .closed = closed_window, 433 .merged = merged_window, 434 .drafts = drafts_window, 435 .conflicts = conflicts_window, 436 .conflict = conflict_data, 437 .theirs_picks = empty.theirs_picks, 438 .view = resolved_view, 439 .description_page = empty.description_page, 440 .tags = tags, 441 }; 442 } 443 444 fn loadDraftWindow( 445 arena: *std.heap.ArenaAllocator, 446 session: *ui.Session, 447 admin_moment: evt.AdminDB.HashMap(.read_only), 448 repo_id: *const [evt.event_id_size]u8, 449 selected_id: []const u8, 450 ) !Window { 451 const user_id = session.data.user_id orelse return .empty; 452 if (user_id.len != evt.event_id_size) return .empty; 453 const repos_dir = session.repos_dir orelse return .empty; 454 const io = session.io orelse return .empty; 455 const key = evt.Fork.draftKey(repo_id, user_id); 456 const drafts_cursor = try admin_moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, evt.Fork.repo_user_to_draft_id_set_key)) orelse return .empty; 457 const drafts = try evt.AdminDB.HashMap(.read_only).init(drafts_cursor); 458 const set_cursor = try drafts.getCursor(hash.hashInt(evt.admin_repo_opts.hash, &key)) orelse return .empty; 459 const set = try evt.AdminDB.SortedSet(.read_only).init(set_cursor); 460 const aa = arena.allocator(); 461 462 var root_key: ?[]const u8 = null; 463 if (selected_id.len != 0) { 464 const id = evt.parseEventId(selected_id) catch return error.NotFound; 465 if (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, arena, &id)) |record| { 466 if (!record.removed and record.event.stage == .draft) { 467 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(record.created_order, &id)); 468 if (!try set.contains(order_key)) return error.NotFound; 469 root_key = order_key; 470 } 471 } 472 } 473 474 var prev_id: ?[]const u8 = null; 475 var iter = if (root_key) |root| blk: { 476 const rank = try set.rank(root); 477 if (rank > 0 and rank <= page_size) { 478 prev_id = ""; 479 } else if (rank > page_size) { 480 const pair = try set.getIndexKeyValuePair(@intCast(rank - page_size)) orelse return error.NotFound; 481 var previous: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 482 _ = try pair.key_cursor.readBytes(&previous); 483 const hex = std.fmt.bytesToHex(previous[@sizeOf(u64)..].*, .lower); 484 prev_id = try aa.dupe(u8, &hex); 485 } 486 break :blk try set.iteratorFrom(root); 487 } else try set.iteratorFromIndex(0); 488 489 var items: std.ArrayList(PatchWithId) = .empty; 490 var next_id: ?[]const u8 = null; 491 while (try iter.next()) |cursor| { 492 const id = try evt.readOrderKeyId(evt.AdminDB, cursor); 493 const id_hex = std.fmt.bytesToHex(id, .lower); 494 if (items.items.len == page_size) { 495 next_id = try aa.dupe(u8, &id_hex); 496 break; 497 } 498 const path = try fork.forkPath(aa, repos_dir, &id_hex); 499 var fork_repo = rp.Repo(.xit, .{}).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true }) catch continue; 500 defer fork_repo.deinit(io, arena.child_allocator); 501 const moment = evt.currentMoment(.{}, &fork_repo) catch continue; 502 const patch = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, arena, &id)) orelse continue; 503 var revision_ready = false; 504 if (patch.event.revision) |revision| { 505 const revision_id = evt.parseEventId(&revision.id) catch continue; 506 if (try evt.PatchRev.readById(evt.EventDB(.sha1), .sha1, moment, arena, &revision_id)) |record| 507 revision_ready = revision.matches(record); 508 } 509 try items.append(aa, .{ 510 .id = try aa.dupe(u8, &id_hex), 511 .record = patch, 512 .author = try ui.Author.initFromEmail(admin_moment, arena, patch.author_email), 513 .draft = true, 514 .revision_ready = revision_ready, 515 }); 516 } 517 return .{ 518 .items = items.items, 519 .prev_id = prev_id, 520 .next_id = next_id, 521 .count = @intCast(try set.count()), 522 }; 523 } 524 525 pub const View = Threads.View(.patch, Self); 526 527 pub const Header = Threads.Header; 528 529 // tabs switching between the patches page's views 530 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 531 var header = try Header.init(allocator); 532 errdefer header.deinit(allocator); 533 const aa = session.page_arena.allocator(); 534 const selected_index = View.viewIndex(data.view); 535 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_patches; 536 537 // a list tab per status, labeled with its listing's patch count 538 for ([_]evt.Patch.Status{ .open, .closed, .merged }, 0..) |status, index| { 539 const route = ui.RoutablePage.repoPatchesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 540 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == index); 541 var label_buf: [64]u8 = undefined; 542 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 543 try header.addTab(allocator, label, link, index); 544 } 545 546 // tags tab, labeled with the active tag filter 547 { 548 const tags_route = ui.RoutablePage.repoThreadTagsRoute(.patch, data.identity, data.tag) orelse return error.RouteTooLong; 549 const tags_link = try ui.inPageTabLink(session, tags_route, page_selected and selected_index == View.viewIndex(.tags)); 550 const label = if (data.tag.len == 0) "tags" else blk: { 551 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 552 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 553 }; 554 try header.addTab(allocator, label, tags_link, View.viewIndex(.tags)); 555 } 556 557 // new-patch tab; an edit or resolve url shows its tab in this place 558 if (session.local == null) { 559 const route = switch (data.view) { 560 .edit => ui.RoutablePage.repoThreadEditRoute(.patch, data.identity, data.selected_id) orelse return error.RouteTooLong, 561 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 562 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 563 .remove => ui.RoutablePage.repoThreadRemoveRoute(.patch, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 564 .resolve => ui.RoutablePage.repoPatchesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 565 else => ui.RoutablePage.repoThreadNewRoute(.patch, data.identity) orelse return error.RouteTooLong, 566 }; 567 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 568 const label: []const u8 = switch (data.view) { 569 .edit => "edit", 570 .new_comment => "reply", 571 .edit_comment => "edit", 572 .remove => "remove", 573 .resolve => "resolve", 574 else => "new", 575 }; 576 try header.addTab(allocator, label, link, View.viewIndex(.new)); 577 } 578 579 // drafts belong to the logged-in user and are not available locally 580 if (session.local == null) { 581 const route = ui.RoutablePage.repoPatchesDraftsRoute(data.identity) orelse return error.RouteTooLong; 582 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.drafts)); 583 var label_buf: [64]u8 = undefined; 584 const label = try std.fmt.bufPrint(&label_buf, "drafts ({d})", .{data.drafts.count}); 585 try header.addTab(allocator, label, link, View.viewIndex(.drafts)); 586 } 587 588 // conflicts tab, labeled with the conflict count 589 if (data.conflicts.count > 0) { 590 const route = ui.RoutablePage.repoPatchesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 591 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.conflicts)); 592 var label_buf: [64]u8 = undefined; 593 const label = try std.fmt.bufPrint(&label_buf, "conflicts ({d})", .{data.conflicts.count}); 594 try header.addTab(allocator, label, link, View.viewIndex(.conflicts)); 595 } 596 597 header.select(View.viewIndex(data.view)); 598 return header; 599 } 600