╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const pch = @import("../patch.zig"); 4 const notify = @import("../notify.zig"); 5 const ui = @import("../ui.zig"); 6 const xit = @import("xit"); 7 const rp = xit.repo; 8 const hash = xit.hash; 9 const xitui = xit.xitui; 10 const wgt = xitui.widget; 11 const layout = xitui.layout; 12 const Key = xitui.input.Key; 13 const Grid = xitui.grid.Grid; 14 const Focus = xitui.focus.Focus; 15 const inp = @import("./input.zig"); 16 17 pub const Header = @import("./Repo/Header.zig"); 18 pub const Files = @import("./Repo/Files.zig"); 19 pub const Markdown = @import("./Repo/Markdown.zig"); 20 pub const Commits = @import("./Repo/Commits.zig"); 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 Members = @import("./Repo/Members.zig"); 31 pub const Branches = @import("./Repo/Branches.zig"); 32 pub const Quit = @import("./Quit.zig"); 33 34 header: Header, 35 repo: evt.Repo.Record, 36 handle: ui.RepoHandle, 37 files: Files, 38 changes: Changes, 39 refs: Refs, 40 discussions: Discussions, 41 issues: Issues, 42 patches: Patches, 43 events: Events, 44 undo: ?Undo = null, 45 settings: ?Settings = null, 46 user_notifications: ui.UserNotifications, 47 quit: Quit, 48 49 const Self = @This(); 50 51 const Changes = union(enum) { 52 commits: Commits, 53 diff: Diff, 54 }; 55 56 pub fn init( 57 arena: *std.heap.ArenaAllocator, 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 .read_access = repo.event.read_access, 282 }; 283 284 // open the repo once for every tab. files and changes share a ref or revision. 285 // no filesystem (wasm), nowhere to look, or a failed open: empty tabs. 286 // the events tab needs write access, like undo 287 if (route == .repo_events and !handle.canWrite()) return error.NotFound; 288 // a tab that is off doesn't exist 289 const route_thread_kind: ?evt.EventKind = switch (route) { 290 .repo_discussions => .discuss, 291 .repo_issues => .issue, 292 .repo_patches => .patch, 293 else => null, 294 }; 295 if (route_thread_kind) |kind| if (repo.event.threadRole(kind) == null) return error.NotFound; 296 const undo_index = if (route == .repo_undo) route.repo_undo.index else null; 297 // only xit repos have undo history, so it's read in their branch below. 298 // local mode leaves undo to xit itself. 299 var undo_data: ?Undo = null; 300 // only the owner sees the settings, which show the repo's hash kind 301 var settings: ?Settings = null; 302 const files, const changes, const refs, var discussions, var issues, var patches, const events = blk: { 303 read: { 304 const io = session.io orelse break :read; 305 const src = source orelse break :read; 306 const gpa = arena.child_allocator; 307 switch (src.repo_kind) { 308 inline else => |repo_kind| { 309 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, src.localInitOpts()) catch break :read; 310 defer any_repo.deinit(io, gpa); 311 switch (any_repo) { 312 inline else => |*opened| { 313 // local mode: bring the event db up to date with the events branch 314 if (session.local != null) { 315 try evt.consume(.local, .repo, repo_kind, opened.self_repo_opts, io, gpa, opened, evt.events_ref, &.{}); 316 try pch.refreshBranches(.local, repo_kind, opened.self_repo_opts, io, gpa, opened, null, null); 317 } 318 if (session.data.host_kind == .server and handle.canUndo()) { 319 // a server page always resolved its repo id 320 const repo_id = repo_id_maybe orelse unreachable; 321 const identity = try arena.allocator().dupe(u8, repo_identity.identity); 322 // only the repo's creator may delete it, never a granted owner 323 const creator = if (session.userId()) |user_id| std.mem.eql(u8, &user_id, &owner_id) else false; 324 settings = .{ 325 .identity = identity, 326 .name = repo.event.name, 327 .description = repo.event.description, 328 .access = repo.event.read_access, 329 .discuss_role = repo.event.discuss_role, 330 .issue_role = repo.event.issue_role, 331 .patch_role = repo.event.patch_role, 332 .hash_kind = opened.self_repo_opts.hash, 333 .view = switch (route) { 334 .repo_members => .members, 335 .repo_branches => .branches, 336 .repo_delete => .delete, 337 else => .settings, 338 }, 339 .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), 340 .branches = try Branches.init(arena, repo_identity.identity, repo.event), 341 .delete = if (creator) .{ .target = .{ .repo = identity } } else null, 342 }; 343 } 344 // tabs switch in-page, so every tab's data is read here 345 if (repo_kind == .xit and session.data.host_kind == .server and handle.canWrite()) { 346 undo_data = Undo.init(opened.self_repo_opts, arena, opened, session.haxy_moment, repo_identity.identity, undo_index, handle) catch |err| switch (err) { 347 error.OutOfMemory => return err, 348 else => .{ .identity = try arena.allocator().dupe(u8, repo_identity.identity), .handle = handle, .failure = @errorName(err) }, 349 }; 350 } 351 // a merge: route stands for the log of what the merge brought in 352 const ref_or_oid: ?ui.RoutablePage.RefOrOid, const ref_value, const base_oid = if (commits_merge.len == 0) 353 .{ requested_ref_or_oid, requested_ref_value, commits_base_oid } 354 else merged: { 355 const merged = try Commits.resolveMerge(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, commits_merge); 356 break :merged .{ .object, merged.oid, merged.base_oid }; 357 }; 358 const files_data = if (patchrev_id.len != 0) 359 try Files.initPatchRev(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, handle, patchrev_id, files_dir, files_line) 360 else 361 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); 362 const changes_data: Changes = if (route == .repo_diff) 363 .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, route.repo_diff) } 364 else if (patchrev_id.len != 0) diff: { 365 const diff_route = ui.RoutablePage.repoPatchRevDiffRoute(repo_identity.identity, patchrev_id, 0, "") orelse return error.RouteTooLong; 366 break :diff .{ .diff = try Diff.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, diff_route.repo_diff) }; 367 } 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) }; 368 const target_branch = if (files_data.ref_or_oid == .branch) files_data.ref_or_oid_value else ""; 369 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)); 370 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)); 371 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)); 372 if (repo_kind == .xit and session.data.host_kind == .server) { 373 // a server page always resolved its repo id 374 const repo_id = repo_id_maybe orelse unreachable; 375 readNotify(opened.self_repo_opts, session, arena, opened, &owner_id, &repo_id, viewed_thread, &discussions_data, &issues_data, &patches_data) catch |err| { 376 std.log.warn("failed to read thread notifications: {s}", .{@errorName(err)}); 377 }; 378 } 379 break :blk .{ 380 files_data, 381 changes_data, 382 try Refs.init(repo_kind, opened.self_repo_opts, arena, opened, io, gpa, repo_identity.identity, refs_kind, refs_from, refs_search), 383 discussions_data, 384 issues_data, 385 patches_data, 386 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), 387 }; 388 }, 389 } 390 }, 391 } 392 } 393 const aa = arena.allocator(); 394 break :blk .{ 395 try Files.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, files_dir), 396 Changes{ .commits = try Commits.emptyResult(aa, handle, requested_ref_or_oid orelse .branch, requested_ref_value, commits_base_oid) }, 397 try Refs.emptyResult(arena, repo_identity.identity, refs_kind, refs_from, refs_search), 398 try Discussions.emptyResult(aa, repo_identity.identity, discussions_search, discussions_selected, discussions_comment, discussions_comments_start, discussions_view), 399 try Issues.emptyResult(aa, repo_identity.identity, issues_search, issues_selected, issues_comment, issues_comments_start, issues_theirs, issues_view), 400 try Patches.emptyResult(aa, repo_identity.identity, patches_search, patches_selected, patches_comment, patches_comments_start, patches_theirs, patches_view), 401 try Events.empty(aa, repo_identity.identity, events_view, session.local != null, session.data.sync_failure), 402 }; 403 }; 404 if (route == .repo_undo and undo_data == null) return error.NotFound; 405 if ((route == .repo_settings or route == .repo_members or route == .repo_branches or route == .repo_delete) and settings == null) return error.NotFound; 406 if (settings) |value| if (route == .repo_delete and value.delete == null) return error.NotFound; 407 discussions.repo_source = source; 408 issues.repo_source = source; 409 patches.repo_source = source; 410 patches.repo_id = repo_id_maybe; 411 412 return .{ 413 // use the files tab's resolved ref for the header 414 .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), 415 .repo = repo, 416 .handle = handle, 417 .files = files, 418 .changes = changes, 419 .refs = refs, 420 .discussions = discussions, 421 .issues = issues, 422 .patches = patches, 423 .events = events, 424 .undo = undo_data, 425 .settings = settings, 426 // after the shown thread's notif is marked read 427 .user_notifications = try ui.UserNotifications.init(arena, session, route.userTab()), 428 .quit = Quit.init(), 429 }; 430 } 431 432 // set the viewer's notify button on every listed thread and mark the viewed thread's notif read 433 fn readNotify( 434 comptime repo_opts: rp.RepoOpts(.xit), 435 session: *ui.Session, 436 arena: *std.heap.ArenaAllocator, 437 repo: *rp.Repo(.xit, repo_opts), 438 owner_id: *const [evt.event_id_size]u8, 439 repo_id: *const [evt.event_id_size]u8, 440 viewed_thread: []const u8, 441 discussions: *Discussions, 442 issues: *Issues, 443 patches: *Patches, 444 ) !void { 445 const user_id = session.userId() orelse return; 446 const io = session.io orelse return error.NoMoment; 447 const users_dir = session.users_dir orelse return error.NoMoment; 448 const gpa = arena.child_allocator; 449 450 { 451 // the owner's plain data, which holds the tier index 452 var owner_repo = (try evt.openUserRepo(io, gpa, users_dir, owner_id)) orelse return error.NotFound; 453 defer owner_repo.deinit(io, gpa); 454 const owner_moment = try notify.latestMoment(&owner_repo); 455 456 try setNotifyActions(arena, &discussions.recent, owner_moment, repo_id, &user_id); 457 for ([_]*Issues.Window{ &issues.open, &issues.closed, &issues.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 458 for ([_]*Patches.Window{ &patches.open, &patches.closed, &patches.merged, &patches.conflicts }) |window| try setNotifyActions(arena, window, owner_moment, repo_id, &user_id); 459 } 460 461 if (viewed_thread.len == 0) return; 462 var user_repo = (try evt.openUserRepo(io, gpa, users_dir, &user_id)) orelse return; 463 defer user_repo.deinit(io, gpa); 464 const latest = (try notify.latestMoment(&user_repo)) orelse return; 465 const notif_id = notify.idOf(repo_id, &try evt.parseEventId(viewed_thread)); 466 const item = (try notify.readItem(latest, arena, &notif_id)) orelse return; 467 if (!item.unread) return; 468 // the page reads this moment or a newer one, so a child in it is shown 469 const moment = try evt.currentMoment(repo_opts, repo); 470 const kinds_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.event_index_key)) orelse return error.CursorNotFound; 471 const kinds = try evt.EventDB(repo_opts.hash).HashMap(.read_only).init(kinds_cursor); 472 // a child the page can't show leaves it unread 473 if (try evt.readEventKind(repo_opts.hash, kinds, &item.notif.last_child_id) == null) return; 474 try notify.markRead(io, gpa, users_dir, &user_id, &notif_id, &item.notif.last_child_id); 475 } 476 477 // set the viewer's notify button on each of a window's threads 478 fn setNotifyActions( 479 arena: *std.heap.ArenaAllocator, 480 window: anytype, 481 owner_moment: ?evt.UserDB.HashMap(.read_only), 482 repo_id: *const [evt.event_id_size]u8, 483 user_id: *const [evt.event_id_size]u8, 484 ) !void { 485 const items = try arena.allocator().dupe(std.meta.Elem(@TypeOf(window.items)), window.items); 486 for (items) |*entry| { 487 const notif_id = notify.idOf(repo_id, &try evt.parseEventId(entry.id)); 488 // muting drops the user from every tier, so membership alone decides 489 const notified = if (owner_moment) |moment| try notify.isNotified(moment, &notif_id, user_id) else false; 490 entry.notify_action = if (notified) .mute else .subscribe; 491 } 492 window.items = items; 493 } 494 495 // the viewer a tab's comment bar sees, which is none below the tab's role 496 fn tabViewer(handle: ui.RepoHandle, repo: evt.Repo.Record, kind: evt.EventKind) ?ui.Viewer { 497 const viewer = handle.viewer orelse return null; 498 const tab_role = repo.event.threadRole(kind) orelse return null; 499 return if (viewer.role.atLeast(tab_role)) viewer else null; 500 } 501 502 pub const View = struct { 503 box: wgt.Box(ui.Widget), 504 505 const header_index: usize = 0; 506 const stack_index: usize = 1; 507 508 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 509 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .round_corners = true, .direction = .vert }); 510 errdefer box.deinit(allocator); 511 512 // build the header first so we can grab the files-tab id for the user 513 // view (it focuses there after login). 514 { 515 var header_view = try Header.View.init(allocator, data, session); 516 errdefer header_view.deinit(allocator); 517 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_header = header_view }, .rect = null, .min_size = null }); 518 } 519 520 { 521 var stack = try wgt.Stack(ui.Widget).init(allocator); 522 errdefer stack.deinit(allocator); 523 524 // files — the default tab: the current directory's listing. 525 { 526 var files_view = try Files.View.init(allocator, &data.files, session); 527 errdefer files_view.deinit(allocator); 528 try stack.children.put(allocator, files_view.getFocus().id, .{ .repo_files = files_view }); 529 } 530 531 switch (data.changes) { 532 .diff => |*diff| { 533 var diff_view = try Diff.View.init(allocator, diff, session); 534 errdefer diff_view.deinit(allocator); 535 try stack.children.put(allocator, diff_view.getFocus().id, .{ .diff_view = diff_view }); 536 }, 537 .commits => |*commits| { 538 var commits_view = try Commits.View.init(allocator, commits, session); 539 errdefer commits_view.deinit(allocator); 540 try stack.children.put(allocator, commits_view.getFocus().id, .{ .repo_commits = commits_view }); 541 }, 542 } 543 544 // refs — the repo's branches and tags. 545 { 546 var refs_view = try Refs.View.init(allocator, &data.refs, session); 547 errdefer refs_view.deinit(allocator); 548 try stack.children.put(allocator, refs_view.getFocus().id, .{ .repo_refs = refs_view }); 549 } 550 551 // discussions and their comment permalinks. 552 if (data.repo.event.discuss_role != null) { 553 var discussions_view = try Discussions.View.init(allocator, &data.discussions, session); 554 errdefer discussions_view.deinit(allocator); 555 try stack.children.put(allocator, discussions_view.getFocus().id, .{ .repo_discussions = discussions_view }); 556 } 557 558 // issues — the repo's issue tracker and comment permalinks. 559 if (data.repo.event.issue_role != null) { 560 var issues_view = try Issues.View.init(allocator, &data.issues, session); 561 errdefer issues_view.deinit(allocator); 562 try stack.children.put(allocator, issues_view.getFocus().id, .{ .repo_issues = issues_view }); 563 } 564 565 // patches and their comment permalinks. 566 if (data.repo.event.patch_role != null) { 567 var patches_view = try Patches.View.init(allocator, &data.patches, session); 568 errdefer patches_view.deinit(allocator); 569 try stack.children.put(allocator, patches_view.getFocus().id, .{ .repo_patches = patches_view }); 570 } 571 572 if (data.handle.canWrite()) { 573 var events_view = try Events.View.init(allocator, &data.events, session); 574 errdefer events_view.deinit(allocator); 575 try stack.children.put(allocator, events_view.getFocus().id, .{ .repo_events = events_view }); 576 } 577 578 if (data.undo) |*undo| { 579 var undo_view = try Undo.View.init(allocator, undo, session); 580 errdefer undo_view.deinit(allocator); 581 try stack.children.put(allocator, undo_view.getFocus().id, .{ .repo_undo = undo_view }); 582 } 583 584 if (data.settings) |*settings| { 585 var settings_view = try Settings.View.init(allocator, settings, session); 586 errdefer settings_view.deinit(allocator); 587 try stack.children.put(allocator, settings_view.getFocus().id, .{ .repo_settings = settings_view }); 588 } 589 590 // the header shows new repo with a login, and new user without one 591 // and user only outside local mode, so keep the stack's children 592 // 1:1 with the tabs by skipping the same views. 593 const identity = try session.page_arena.allocator().print("{s}:{s}", .{ data.header.owner_name, data.header.name }); 594 if (session.data.user_id != null) { 595 const route = ui.RoutablePage{ .repo_repo_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 596 var new_repo_view = try ui.NewRepo.View.init(allocator, session, route); 597 errdefer new_repo_view.deinit(allocator); 598 try stack.children.put(allocator, new_repo_view.getFocus().id, .{ .new_repo = new_repo_view }); 599 } else if (session.data.host_kind == .server) { 600 const route = ui.RoutablePage{ .repo_user_new = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong }; 601 var new_user_view = try ui.NewUser.View.init(allocator, session, route); 602 errdefer new_user_view.deinit(allocator); 603 try stack.children.put(allocator, new_user_view.getFocus().id, .{ .new_user = new_user_view }); 604 } 605 606 if (session.data.host_kind == .server) { 607 const route = ui.RoutablePage{ .repo_user = .{ .name = ui.RoutablePage.Array(ui.RoutablePage.repo_route_max_len).from(identity) orelse return error.RouteTooLong } }; 608 var user_view = try ui.UserSettings.initView(allocator, session, &data.user_notifications, route); 609 errdefer user_view.deinit(allocator); 610 try stack.children.put(allocator, user_view.getFocus().id, user_view); 611 } 612 613 if (session.is_terminal) { 614 var quit_view = try Quit.View.init(allocator, session); 615 errdefer quit_view.deinit(allocator); 616 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 617 } 618 619 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 620 } 621 622 var self = View{ .box = box }; 623 // a page opens on its tabs, except that search results open in the 624 // tab's own search box. 625 const results = data.files.find != null or 626 (data.changes == .commits and data.changes.commits.search != null) or 627 data.discussions.search != null or data.issues.search != null or data.patches.search != null; 628 self.getFocus().child_id = box.children.keys()[if (results) stack_index else header_index]; 629 return self; 630 } 631 632 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 633 self.box.deinit(allocator); 634 } 635 636 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 637 self.clearGrid(); 638 const header = &self.box.children.values()[header_index].widget.repo_header; 639 const stack = &self.box.children.values()[stack_index].widget.stack; 640 641 // each header tab maps 1:1 to a stack child by position 642 if (header.getSelectedIndex()) |index| 643 stack.getFocus().child_id = stack.children.keys()[index]; 644 try self.box.build(allocator, constraint, root_focus); 645 } 646 647 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 648 const stack = &self.box.children.values()[stack_index].widget.stack; 649 if (self.getFocus().child_id) |child_id| { 650 if (self.box.children.getIndex(child_id)) |current_index| { 651 const child = &self.box.children.values()[current_index].widget; 652 var index = current_index; 653 654 const direction = inp.vertDirection(key); 655 656 switch (direction) { 657 .up => { 658 switch (child.*) { 659 .repo_header => { 660 try child.input(allocator, key, root_focus); 661 }, 662 .stack => { 663 if (child.stack.getSelected()) |selected_widget| { 664 if (selected_widget.atTop(root_focus)) { 665 index = header_index; 666 } else { 667 try child.input(allocator, key, root_focus); 668 } 669 } 670 }, 671 else => {}, 672 } 673 }, 674 .down => { 675 switch (child.*) { 676 .repo_header => { 677 if (stack.getSelected()) |selected_widget| switch (selected_widget.*) { 678 .repo_files => |*v| if (v.focusHeader(root_focus)) return, 679 .repo_commits => |*v| if (v.focusHeader(root_focus)) return, 680 .repo_refs => |*v| if (v.focusHeader(root_focus)) return, 681 .repo_events => |*v| if (v.focusHeader(root_focus)) return, 682 .repo_undo => |*v| if (v.focusHeader(root_focus)) return, 683 .repo_settings => |*v| if (v.focusHeader(root_focus)) return, 684 .user_notifications => |*v| if (v.focusHeader(root_focus)) return, 685 else => {}, 686 }; 687 index = stack_index; 688 }, 689 .stack => { 690 try child.input(allocator, key, root_focus); 691 }, 692 else => {}, 693 } 694 }, 695 .none => { 696 try child.input(allocator, key, root_focus); 697 }, 698 } 699 700 if (index != current_index) { 701 root_focus.setFocus(self.box.children.keys()[index]); 702 } 703 } 704 } 705 } 706 707 pub fn clearGrid(self: *View) void { 708 self.box.clearGrid(); 709 } 710 711 pub fn getGrid(self: View) ?Grid { 712 return self.box.getGrid(); 713 } 714 715 pub fn getFocus(self: *View) *Focus { 716 return self.box.getFocus(); 717 } 718 }; 719