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