╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
21 pub const Diff = @import("./Repo/Diff.zig"); 22 pub const Refs = @import("./Repo/Refs.zig"); 23 pub const Discussions = @import("./Repo/Discussions.zig"); 24 pub const Issues = @import("./Repo/Issues.zig"); 25 pub const Patches = @import("./Repo/Patches.zig"); 26 pub const Comment = @import("./Repo/Comment.zig"); 27 pub const Undo = @import("./Repo/Undo.zig"); 28 pub const Events = @import("./Repo/Events.zig"); 29 pub const Settings = @import("./Repo/Settings.zig"); 30 pub const Roles = @import("./Repo/Roles.zig"); 31 pub const Quit = @import("./Quit.zig"); 32 33 header: Header, 34 repo: evt.Repo.Record, 35 handle: ui.RepoHandle, 36 files: Files, 37 changes: Changes, 38 refs: Refs, 39 discussions: Discussions, 40 issues: Issues, 41 patches: Patches, 42 events: Events, 43 undo: ?Undo = null, 44 settings: ?Settings = null, 45 user_notifications: ui.UserNotifications, 46 quit: Quit, 47 48 const Self = @This(); 49 50 const Changes = union(enum) { 51 commits: Commits, 52 diff: Diff, 53 }; 54 55 pub fn init( 56 arena: *std.heap.ArenaAllocator, 57 session: *ui.Session, 58 route: ui.RoutablePage, 59 ) !Self { 60 const DB = evt.AdminDB; 61 const hash_kind = evt.admin_repo_opts.hash; 62 63 // every repo route stores its identity as "owner:name" (or elides it in local mode) 64 const name_str = route.repoIdentity() orelse return error.UnexpectedRoute; 65 const repo_identity = ui.RoutablePage.RepoIdentity.parse(name_str) orelse return error.NotFound; 66 // files, commits and diff share the requested ref, or the default branch. 67 // directories and hunk windows only apply to their own tab. 68 const requested_ref_or_oid: ?ui.RoutablePage.RefOrOid = switch (route) { 69 .repo_files => |f| f.ref_kind, 70 .repo_commits => |c| c.ref_or_oid, 71 .repo_diff => |d| d.ref_or_oid, 72 else => null, 73 }; 74 const requested_ref_value: []const u8 = switch (route) { 75 .repo_files => |*f| f.ref_value.slice(), 76 .repo_commits => |*c| c.value.slice(), 77 .repo_diff => |*d| d.value.slice(), 78 else => "", 79 }; 80 const patchrev_id: []const u8 = switch (route) { 81 .repo_files => |*f| f.patchrev_id.slice(), 82 .repo_diff => |*d| d.patchrev_id.slice(), 83 else => "", 84 }; 85 const files_dir = switch (route) { 86 .repo_files => |*f| f.path.slice(), 87 else => "", 88 }; 89 const files_line = switch (route) { 90 .repo_files => |f| f.line, 91 else => 0, 92 }; 93 const files_find: []const u8 = switch (route) { 94 .repo_files => |*f| f.find.slice(), 95 else => "", 96 }; 97 // the commits view's pane shows the whole message of the commit it walks from 98 const commits_message = route == .repo_commits and route.repo_commits.message; 99 const commits_base_oid: []const u8 = switch (route) { 100 .repo_commits => |*c| c.base_oid.slice(), 101 else => "", 102 }; 103 const commits_search: []const u8 = switch (route) { 104 .repo_commits => |*c| c.search.slice(), 105 else => "", 106 }; 107 const commits_from: []const u8 = switch (route) { 108 .repo_commits => |*c| c.from.slice(), 109 else => "", 110 }; 111 const commits_merge: []const u8 = switch (route) { 112 .repo_commits => |*c| c.merge.slice(), 113 else => "", 114 }; 115 // the refs tab windows one column at a time: `refs_from` (a url-encoded 116 // ref name) roots `refs_kind`'s column, the other stays at its first window. 117 const refs_kind: ui.RoutablePage.RefKind = switch (route) { 118 .repo_refs => |r| r.kind, 119 else => .branch, 120 }; 121 const refs_from: []const u8 = switch (route) { 122 .repo_refs => |*r| r.from.slice(), 123 else => "", 124 }; 125 const refs_search: []const u8 = switch (route) { 126 .repo_refs => |*r| r.search.slice(), 127 else => "", 128 }; 129 const discussions_search: []const u8 = switch (route) { 130 .repo_discussions => |*t| t.search.slice(), 131 else => "", 132 }; 133 const discussions_selected: []const u8 = switch (route) { 134 .repo_discussions => |*t| t.selected.slice(), 135 else => "", 136 }; 137 const discussions_comment: []const u8 = switch (route) { 138 .repo_discussions => |*t| t.comment.slice(), 139 else => "", 140 }; 141 const discussions_view: ui.RoutablePage.DiscussionsView = switch (route) { 142 .repo_discussions => |t| t.view, 143 else => .recent, 144 }; 145 const discussions_comments_start: usize = switch (route) { 146 .repo_discussions => |t| t.comments_start, 147 else => 0, 148 }; 149 // the issues tab's search, the issue its window is rooted at, and the view 150 // it shows. 151 const issues_search: []const u8 = switch (route) { 152 .repo_issues => |*i| i.search.slice(), 153 else => "", 154 }; 155 const issues_selected: []const u8 = switch (route) { 156 .repo_issues => |*i| i.selected.slice(), 157 else => "", 158 }; 159 const issues_comment: []const u8 = switch (route) { 160 .repo_issues => |*i| i.comment.slice(), 161 else => "", 162 }; 163 const issues_theirs: []const u8 = switch (route) { 164 .repo_issues => |*i| i.theirs.slice(), 165 else => "", 166 }; 167 const issues_view: ui.RoutablePage.IssuesView = switch (route) { 168 .repo_issues => |i| i.view, 169 else => .open, 170 }; 171 const issues_comments_start: usize = switch (route) { 172 .repo_issues => |i| i.comments_start, 173 else => 0, 174 }; 175 const patches_search: []const u8 = switch (route) { 176 .repo_patches => |*p| p.search.slice(), 177 else => "", 178 }; 179 const patches_selected: []const u8 = switch (route) { 180 .repo_patches => |*p| p.selected.slice(), 181 else => "", 182 }; 183 const patches_comment: []const u8 = switch (route) { 184 .repo_patches => |*p| p.comment.slice(), 185 else => "", 186 }; 187 const patches_theirs: []const u8 = switch (route) { 188 .repo_patches => |*p| p.theirs.slice(), 189 else => "", 190 }; 191 const patches_view: ui.RoutablePage.PatchesView = switch (route) { 192 .repo_patches => |p| p.view, 193 else => .open, 194 }; 195 const patches_comments_start: usize = switch (route) { 196 .repo_patches => |p| p.comments_start, 197 else => 0, 198 }; 199 const events_kind: ?evt.EventKind = switch (route) { 200 .repo_events => |e| e.kind, 201 else => null, 202 }; 203 const events_view: ui.RoutablePage.EventsView = switch (route) { 204 .repo_events => |e| e.view, 205 else => .active, 206 }; 207 const events_moment: ?u64 = switch (route) { 208 .repo_events => |e| e.moment, 209 else => null, 210 }; 211 const events_selected: []const u8 = switch (route) { 212 .repo_events => |*e| e.selected.slice(), 213 else => "", 214 }; 215 const roles_start: usize = switch (route) { 216 .repo_roles => |r| r.start, 217 else => 0, 218 }; 219 // the thread the route shows, whose notif viewing it reads 220 const viewed_thread: []const u8 = switch (route) { 221 .repo_issues => |*i| if (i.view == .conflicts) "" else i.selected.slice(), 222 .repo_patches => |*p| if (p.view == .conflicts) "" else p.selected.slice(), 223 .repo_discussions => |*t| t.selected.slice(), 224 else => "", 225 }; 226 227 // where the on-disk repo lives (null keeps the views' empty fallback), plus 228 // the repo and owner-name metadata the header shows. local mode already 229 // knows all three; the server paths resolve them from the admin db. 230 var source: ?ui.RepoSource = null; 231 var repo_id_maybe: ?[evt.event_id_size]u8 = null; 232 var repo: evt.Repo.Record = undefined; 233 // local mode owns its repo 234 var role: evt.Repo.Role = .owner; 235 var owner_name: []const u8 = undefined; 236 if (session.local) |local| { 237 source = local; 238 // local routes elide the identity, so the display name comes from the 239 // repo's directory rather than the route. 240 repo = .{ 241 .event = .{ 242 .user_id = "", 243 .name = try arena.allocator().dupe(u8, std.Io.Dir.path.basename(local.path)), 244 .description = "", 245 // local mode shows every tab 246 .discuss_role = .read, 247 .issue_role = .read, 248 .patch_role = .read, 249 }, 250 }; 251 owner_name = ""; 252 } else { 253 const haxy_moment = session.haxy_moment orelse return error.NoMoment; 254 const io = session.io orelse return error.NoMoment; 255 const users_dir = session.users_dir orelse return error.NoMoment; 256 const found = (try evt.readRepoByOwnerAndName(io, arena.child_allocator, arena, haxy_moment, users_dir, repo_identity.owner, repo_identity.name, session.userId())) orelse return error.NotFound; 257 // a repo the session can't read doesn't exist to it 258 role = found.role orelse return error.NotFound; 259 repo = found.repo; 260 repo_id_maybe = found.event_id; 261 262 // resolve the creating user so the header can show their name to the left 263 // of the repo title. 264 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo.event.user_id)) orelse return error.NotFound; 265 owner_name = owner.event.name; 266 267 // the repo's working copy lives in its owner's dir. 268 source = .{ 269 .path = try evt.repoPath(arena.allocator(), users_dir, repo.event.user_id, &found.event_id), 270 .repo_kind = .xit, 271 }; 272 } 273 274 const handle = ui.RepoHandle{ 275 .location = .{ .repo = try arena.allocator().dupe(u8, repo_identity.identity) }, 276 .viewer = try ui.Viewer.init(session, arena, role), 277 }; 278 279 // open the repo once for every tab. files and changes share a ref or revision. 280 // no filesystem (wasm), nowhere to look, or a failed open: empty tabs. 281 // the events tab needs write access, like undo 282 if (route == .repo_events and !handle.canWrite()) return error.NotFound; 283 // a tab that is off doesn't exist 284 const route_thread_kind: ?evt.EventKind = switch (route) { 285 .repo_discussions => .discuss, 286 .repo_issues => .issue, 287 .repo_patches => .patch, 288 else => null, 289 }; 290 if (route_thread_kind) |kind| if (repo.event.threadRole(kind) == null) return error.NotFound; 291 const undo_index = if (route == .repo_undo) route.repo_undo.index else null; 292 const undo_clear = route == .repo_undo and route.repo_undo.clear; 293 // only xit repos have undo history, so it's read in their branch below 294 var undo_data: ?Undo = null; 295 // only the owner sees the settings, which show the repo's hash kind 296 var settings: ?Settings = null; 297 const files, const changes, const refs, var discussions, var issues, var patches, const events = blk: { 298 read: { 299 const io = session.io orelse break :read; 300 const src = source orelse break :read; 301 const gpa = arena.child_allocator; 302 switch (src.repo_kind) { 303 inline else => |repo_kind| { 304 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 305 defer any_repo.deinit(io, gpa); 306 switch (any_repo) { 307 inline else => |*opened| { 308 // local mode: bring the event db up to date with the events branch 309 if (session.local != null) { 310 try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 311 try pch.refreshBranches(.local, repo_kind, opened.self_repo_opts, io, gpa, opened, null, null); 312 } 313 if (session.data.host_kind == .server and handle.canUndo()) { 314 // a server page always resolved its repo id 315 const repo_id = repo_id_maybe orelse unreachable; 316 settings = .{ 317 .identity = try arena.allocator().dupe(u8, repo_identity.identity), 318 .name = repo.event.name, 319 .description = repo.event.description, 320 .access = repo.event.read_access, 321 .discuss_role = repo.event.discuss_role, 322 .issue_role = repo.event.issue_role, 323 .patch_role = repo.event.patch_role, 324 .hash_kind = opened.self_repo_opts.hash, 325 .view = if (route == .repo_roles) .roles else .settings, 326 .roles = try Roles.init(io, gpa, arena, session.haxy_moment orelse return error.NoMoment, session.users_dir orelse return error.NoMoment, repo_identity.identity, repo.event.user_id[0..evt.event_id_size], &repo_id, roles_start), 327 }; 328 } 329 // tabs switch in-page, so every tab's data is read here 330 if (repo_kind == .xit and handle.canWrite()) { 331 undo_data = Undo.init(opened.self_repo_opts, arena, opened, session.haxy_moment, repo_identity.identity, undo_index, handle) catch |err| switch (err) { 332 error.OutOfMemory => return err, 333 else => .{ .identity = try arena.allocator().dupe(u8, repo_identity.identity), .handle = handle, .failure = @errorName(err) }, 334 }; 335 } 336 // a merge: route stands for the log of what the merge brought in 337 const ref_or_oid: ?ui.RoutablePage.RefOrOid, const ref_value, const base_oid = if (commits_merge.len == 0) 338 .{ requested_ref_or_oid, requested_ref_value, commits_base_oid } 339 else merged: { 340 const merged = try Commits.resolveMerge(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, commits_merge); 341 break :merged .{ .object, merged.oid, merged.base_oid }; 342 }; 343 const files_data = if (patchrev_id.len != 0) 344 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, patchrev_id, files_dir, files_line) 345 else 346 try Files.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, ref_or_oid, ref_value, files_dir, files_line, files_find); 347 const changes_data: Changes = if (route == .repo_diff) 348 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 349 else if (patchrev_id.len != 0) diff: { 350 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 351 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 352 } else .{ .commits = try Commits.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, session.haxy_moment, handle, ref_or_oid, ref_value, commits_message, base_oid, commits_search, commits_from) }; 353 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 354 var discussions_data = try Discussions.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view, tabViewer(handle, repo, .discuss)); 355 var issues_data = try Issues.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view, tabViewer(handle, repo, .issue)); 356 var patches_data = try Patches.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, session, repo_id_maybe, repo_identity.identity, target_branch, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view, tabViewer(handle, repo, .patch)); 357 if (repo_kind == .xit and session.data.host_kind == .server) { 358 // a server page always resolved its repo id 359 const repo_id = repo_id_maybe orelse unreachable; 360 readNotify(opened.self_repo_opts, session, arena, opened, repo.event.user_id[0..evt.event_id_size], &repo_id, viewed_thread, &discussions_data, &issues_data, &patches_data) catch |err| { 361 std.log.warn("failed to read thread notifications: {s}", .{@errorName(err)}); 362 }; 363 } 364 break :blk .{ 365 files_data, 366 changes_data, 367 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from, refs_search), 368 discussions_data, 369 issues_data, 370 patches_data, 371 try Events.init(repo_kind, opened.self_repo_opts, arena, opened, io, session.haxy_moment, repo_identity.identity, repo.event, events_view, events_kind, events_selected, events_moment, session.local != null, session.data.sync_failure), 372 }; 373 }, 374 } 375 }, 376 } 377 } 378 const aa = arena.allocator(); 379 break :blk .{ 380 try Files.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 381 Changes{ .commits = try Commits.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, commits_base_oid) }, 382 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 383 try Discussions.emptyResult(aa, repo_identity.identity, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 384 try Issues.emptyResult(aa, repo_identity.identity, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 385 try Patches.emptyResult(aa, repo_identity.identity, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 386 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 387 }; 388 }; 389 if (route == .repo_undo and undo_data == null) return error.NotFound; 390 if ((route == .repo_settings or route == .repo_roles) and settings == null) return error.NotFound; 391 if (undo_data) |*undo| undo.clear = undo_clear; 392 discussions.repo_source = source; 393 issues.repo_source = source; 394 patches.repo_source = source; 395 patches.repo_id = repo_id_maybe; 396 397 return .{ 398 // use the files tab's resolved ref for the header 399 .header = try Header.init(arena, repo.event.name, owner_name, files.ref_or_oid, files.ref_or_oid_value, discussions.search, issues.search, patches.search), 400 .repo = repo, 401 .handle = handle, 402 .files = files, 403 .changes = changes, 404 .refs = refs, 405 .discussions = discussions, 406 .issues = issues, 407 .patches = patches, 408 .events = events, 409 .undo = undo_data, 410 .settings = settings, 411 // after the shown thread's notif is marked read 412 .user_notifications = try ui.UserNotifications.init(arena, session, route.userTab()), 413 .quit = Quit.init(), 414 }; 415 } 416 417 // set the viewer's notify button on every listed thread and mark the viewed thread's notif read 418 fn readNotify( 419 comptime repo_opts: rp.RepoOpts(.xit), 420 session: *ui.Session, 421 arena: *std.heap.ArenaAllocator, 422 repo: *rp.Repo(.xit, repo_opts), 423 owner_id: *const [evt.event_id_size]u8, 424 repo_id: *const [evt.event_id_size]u8, 425 viewed_thread: []const u8, 426 discussions: *Discussions, 427 issues: *Issues, 428 patches: *Patches, 429 ) !void { 430 const user_id = session.userId() orelse return; 431 const io = session.io orelse return error.NoMoment; 432 const users_dir = session.users_dir orelse return error.NoMoment; 433 const gpa = arena.child_allocator; 434 435 { 436 // the owner's plain data, which holds the subscriber index 437 var owner_repo = (try evt.openUserRepo(io, gpa, users_dir, owner_id)) orelse return error.NotFound; 438 defer owner_repo.deinit(io, gpa); 439 const owner_moment = try notify.latestMoment(&owner_repo); 440 441 try setNotifyActions(arena, &discussions.recent, owner_moment, repo_id, &user_id); 442 for ([_]*Issues.Window{ &issues.open, &issues.closed, &issues.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 443 for ([_]*Patches.Window{ &patches.open, &patches.closed, &patches.merged, &patches.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 444 } 445 446 if (viewed_thread.len == 0) return; 447 var user_repo = (try evt.openUserRepo(io, gpa, users_dir, &user_id)) orelse return; 448 defer user_repo.deinit(io, gpa); 449 const latest = (try notify.latestMoment(&user_repo)) orelse return; 450 const notif_id = notify.idOf(repo_id, &try evt.parseEventId(viewed_thread)); 451 const item = (try notify.readItem(latest, arena, &notif_id)) orelse return; 452 if (!item.unread) return; 453 // the page reads this moment or a newer one, so a child in it is shown 454 const moment = try evt.currentMoment(repo_opts, repo); 455 const kinds_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.event_index_key)) orelse return error.CursorNotFound; 456 const kinds = try evt.EventDB(repo_opts.hash).HashMap(.read_only).init(kinds_cursor); 457 // a child the page can't show leaves it unread 458 if (try evt.readEventKind(repo_opts.hash, kinds, &item.notif.last_child_id) == null) return; 459 try notify.markRead(io, gpa, users_dir, &user_id, &notif_id, &item.notif.last_child_id); 460 } 461 462 // set the viewer's notify button on each of a window's threads 463 fn setNotifyActions( 464 arena: *std.heap.ArenaAllocator, 465 window: anytype, 466 owner_moment: ?evt.UserDB.HashMap(.read_only), 467 repo_id: *const [evt.event_id_size]u8, 468 user_id: *const [evt.event_id_size]u8, 469 ) !void { 470 const items = try arena.allocator().dupe(std.meta.Elem(@TypeOf(window.items)), window.items); 471 for (items) |*entry| { 472 const thread_id = try evt.parseEventId(entry.id); 473 // muting drops the user from the index, so membership alone decides 474 const subscribed = if (owner_moment) |moment| try notify.isSubscribed(moment, &notify.idOf(repo_id, &thread_id), user_id) else false; 475 entry.notify_action = if (subscribed) .mute else .subscribe; 476 } 477 window.items = items; 478 } 479 480 // the viewer a tab's comment bar sees, which is none below the tab's role 481 fn tabViewer(handle: ui.RepoHandle, repo: evt.Repo.Record, kind: evt.EventKind) ?ui.Viewer { 482 const viewer = handle.viewer orelse return null; 483 const tab_role = repo.event.threadRole(kind) orelse return null; 484 return if (viewer.role.atLeast(tab_role)) viewer else null; 485 } 486 487 pub const View = struct { 488 box: wgt.Box(ui.Widget), 489 490 const header_index: usize = 0; 491 const stack_index: usize = 1; 492 493 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 494 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 495 errdefer box.deinit(allocator); 496 497 // build the header first so we can grab the files-tab id for the user 498 // view (it focuses there after login). 499 { 500 var header_view = try Header.View.init(allocator, data, session); 501 errdefer header_view.deinit(allocator); 502 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 503 } 504 505 { 506 var stack = try wgt.Stack(ui.Widget).init(allocator); 507 errdefer stack.deinit(allocator); 508 509 // files — the default tab: the current directory's listing. 510 { 511 var files_view = try Files.View.init(allocator, &data.files, session); 512 errdefer files_view.deinit(allocator); 513 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 514 } 515 516 switch (data.changes) { 517 .diff => |*diff| { 518 var diff_view = try Diff.View.init(allocator, diff, session); 519 errdefer diff_view.deinit(allocator); 520 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 521 }, 522 .commits => |*commits| { 523 var commits_view = try Commits.View.init(allocator, commits, session); 524 errdefer commits_view.deinit(allocator); 525 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 526 }, 527 } 528 529 // refs — the repo's branches and tags. 530 { 531 var refs_view = try Refs.View.init(allocator, &data.refs, session); 532 errdefer refs_view.deinit(allocator); 533 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 534 } 535 536 // discussions and their comment permalinks. 537 if (data.repo.event.discuss_role != null) { 538 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 539 errdefer discussions_view.deinit(allocator); 540 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 541 } 542 543 // issues — the repo's issue tracker and comment permalinks. 544 if (data.repo.event.issue_role != null) { 545 var issues_view = try Issues.View.init(allocator, &data.issues, session); 546 errdefer issues_view.deinit(allocator); 547 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 548 } 549 550 // patches and their comment permalinks. 551 if (data.repo.event.patch_role != null) { 552 var patches_view = try Patches.View.init(allocator, &data.patches, session); 553 errdefer patches_view.deinit(allocator); 554 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 555 } 556 557 if (data.handle.canWrite()) { 558 var events_view = try Events.View.init(allocator, &data.events, session); 559 errdefer events_view.deinit(allocator); 560 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 561 } 562 563 if (data.undo) |*undo| { 564 var undo_view = try Undo.View.init(allocator, undo, session); 565 errdefer undo_view.deinit(allocator); 566 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 567 } 568 569 if (data.settings) |*settings| { 570 var settings_view = try Settings.View.init(allocator, settings, session); 571 errdefer settings_view.deinit(allocator); 572 try stack.children.put(allocator, settings_view.getFocus().id, .{ .repo_settings = settings_view }); 573 } 574 575 // the header shows new repo with a login, and new user without one 576 // and user only outside local mode, so keep the stack's children 577 // 1:1 with the tabs by skipping the same views. 578 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.owner_name, data.header.name }); 579 if (session.data.user_id != null) { 580 const route = ui.RoutablePage{ .repo_repo_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 581 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 582 errdefer new_repo_view.deinit(allocator); 583 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 584 } else if (session.data.host_kind == .server) { 585 const route = ui.RoutablePage{ .repo_user_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 586 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 587 errdefer new_user_view.deinit(allocator); 588 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 589 } 590 591 if (session.data.host_kind == .server) { 592 const route = ui.RoutablePage{ .repo_user = .{ .name = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong } }; 593 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, route); 594 errdefer user_view.deinit(allocator); 595 try stack.children.put(allocator, user_view.getFocus().id, user_view); 596 } 597 598 if (session.is_terminal) { 599 var quit_view = try Quit.View.init(allocator, session); 600 errdefer quit_view.deinit(allocator); 601 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 602 } 603 604 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 605 } 606 607 var self = View{ .box = box }; 608 // a page opens on its tabs, except that search results open in the 609 // tab's own search box. 610 const results = data.files.find != null or 611 (data.changes == .commits and data.changes.commits.search != null) or 612 data.discussions.search != null or data.issues.search != null or data.patches.search != null; 613 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 614 return self; 615 } 616 617 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 618 self.box.deinit(allocator); 619 } 620 621 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 622 self.clearGrid(); 623 const header = &self.box.children.values()[header_index].widget.repo_header; 624 const stack = &self.box.children.values()[stack_index].widget.stack; 625 626 // each header tab maps 1:1 to a stack child by position 627 if (header.getSelectedIndex()) |index| 628 stack.getFocus().child_id = stack.children.keys()[index]; 629 try self.box.build(allocator, constraint, root_focus); 630 } 631 632 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 633 const stack = &self.box.children.values()[stack_index].widget.stack; 634 if (self.getFocus().child_id) |child_id| { 635 if (self.box.children.getIndex(child_id)) |current_index| { 636 const child = &self.box.children.values()[current_index].widget; 637 var index = current_index; 638 639 const direction = inp.vertDirection(key); 640 641 switch (direction) { 642 .up => { 643 switch (child.*) { 644 .repo_header => { 645 try child.input(allocator, key, root_focus); 646 }, 647 .stack => { 648 if (child.stack.getSelected()) |selected_widget| { 649 if (selected_widget.atTop(root_focus)) { 650 index = header_index; 651 } else { 652 try child.input(allocator, key, root_focus); 653 } 654 } 655 }, 656 else => {}, 657 } 658 }, 659 .down => { 660 switch (child.*) { 661 .repo_header => { 662 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 663 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 664 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 665 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 666 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 667 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 668 .repo_settings => |*v| if (v.focusHeader(root_focus)) return, 669 .user_notifications => |*v| if (v.focusHeader(root_focus)) return, 670 else => {}, 671 }; 672 index = stack_index; 673 }, 674 .stack => { 675 try child.input(allocator, key, root_focus); 676 }, 677 else => {}, 678 } 679 }, 680 .none => { 681 try child.input(allocator, key, root_focus); 682 }, 683 } 684 685 if (index != current_index) { 686 root_focus.setFocus(self.box.children.keys()[index]); 687 } 688 } 689 } 690 } 691 692 pub fn clearGrid(self: *View) void { 693 self.box.clearGrid(); 694 } 695 696 pub fn getGrid(self: View) ?Grid { 697 return self.box.getGrid(); 698 } 699 700 pub fn getFocus(self: *View) *Focus { 701 return self.box.getFocus(); 702 } 703 }; 704