╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
30 repos: []const evt.Repo.Record, 31 repos_start: usize, // the repos window this page was built with, mirrored into the url 32 repos_next_start: ?usize, // the `start` for the "next" row, or null on the last window 33 forks: []const ForkItem, 34 forks_start: usize, 35 forks_next_start: ?usize, 36 settings: Settings, 37 auth: Auth, 38 quit: Quit, 39 route_name: ui.RoutablePage.Array(evt.User.name_max_len), 40 41 const Self = @This(); 42 43 pub fn init( 44 arena: *std.heap.ArenaAllocator, 45 session: *ui.Session, 46 haxy_moment: evt.AdminDB.HashMap(.read_only), 47 name: ui.RoutablePage.Array(evt.User.name_max_len), 48 repos_start: usize, 49 forks_start: usize, 50 ) !Self { 51 const DB = evt.AdminDB; 52 const hash_kind = evt.admin_repo_opts.hash; 53 54 // a route identifies a user by name; resolve it to the user's event id via 55 // the name->user-id index, which everything below keys off of. 56 const name_to_user_id_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "name->user-id")) orelse return error.NotFound; 57 const name_to_user_id = try DB.HashMap(.read_only).init(name_to_user_id_cursor); 58 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(hash_kind, name.slice())) orelse return error.NotFound; 59 var user_id_buf: [evt.event_id_size]u8 = undefined; 60 _ = try user_id_cursor.readBytes(&user_id_buf); 61 const user_id: []const u8 = &user_id_buf; 62 63 const user = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, user_id)) orelse return error.NotFound; 64 65 var repos: std.ArrayList(evt.Repo.Record) = .empty; 66 var repos_next_start: ?usize = null; 67 68 // the user-id->repo-id-set index maps each user to a set of their repo event 69 // ids ordered by creation (newest first); it only exists once a repo has 70 // been consumed, so a user with no repos simply yields an empty list. 71 if (try haxy_moment.getCursor(hash.hashInt(hash_kind, "user-id->repo-id-set"))) |user_id_to_repo_id_set_cursor| { 72 const user_id_to_repo_id_set = try DB.HashMap(.read_only).init(user_id_to_repo_id_set_cursor); 73 if (try user_id_to_repo_id_set.getCursor(hash.hashInt(hash_kind, user_id))) |user_repos_cursor| { 74 const user_repos = try DB.SortedSet(.read_only).init(user_repos_cursor); 75 const count = try user_repos.count(); 76 77 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return error.NotFound; 78 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 79 80 // read the window [start, start+page_size) with one seek to the start 81 // rank, then a sequential walk. 82 const end = @min(repos_start + page_size, count); 83 var repos_iter = try user_repos.iteratorFromIndex(repos_start); 84 var i = repos_start; 85 while (i < end) : (i += 1) { 86 var kv_cursor = (try repos_iter.next()) orelse break; 87 const kv = try kv_cursor.readKeyValuePair(); 88 // the set key is orderKey = [created-order][event-id]; its trailing 89 // bytes are the repo event id. 90 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 91 _ = try kv.key_cursor.readBytes(&order_key); 92 const event_id = order_key[@sizeOf(u64)..]; 93 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, event_id)) orelse continue; 94 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 95 const repo_event = try evt.read(evt.Repo.Record, DB, hash_kind, arena, repo_map); 96 try repos.append(arena.allocator(), repo_event); 97 } 98 repos_next_start = if (end < count) end else null; 99 } 100 } 101 102 var forks: std.ArrayList(ForkItem) = .empty; 103 var forks_next_start: ?usize = null; 104 if (try haxy_moment.getCursor(hash.hashInt(hash_kind, evt.Fork.user_id_to_fork_id_set_key))) |by_user_cursor| { 105 const by_user = try DB.HashMap(.read_only).init(by_user_cursor); 106 if (try by_user.getCursor(hash.hashInt(hash_kind, user_id))) |user_forks_cursor| { 107 const user_forks = try DB.SortedSet(.read_only).init(user_forks_cursor); 108 const repos_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, evt.Repo.record_map_key)) orelse return error.NotFound; 109 const repo_records = try DB.HashMap(.read_only).init(repos_cursor); 110 const count = try user_forks.count(); 111 const end = @min(forks_start + page_size, count); 112 var iter = try user_forks.iteratorFromIndex(forks_start); 113 var i = forks_start; 114 while (i < end) : (i += 1) { 115 const cursor = (try iter.next()) orelse break; 116 var kv_cursor = cursor; 117 const kv = try kv_cursor.readKeyValuePair(); 118 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 119 _ = try kv.key_cursor.readBytes(&order_key); 120 const fork_id = order_key[@sizeOf(u64)..]; 121 const record = (try evt.Fork.readById(DB, hash_kind, haxy_moment, arena, fork_id)) orelse continue; 122 123 const repo_cursor = try repo_records.getCursor(hash.hashInt(hash_kind, record.event.repo_id)) orelse continue; 124 const target_repo = try evt.read(evt.Repo.Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(repo_cursor)); 125 const owner = (try evt.User.readById(DB, hash_kind, haxy_moment, arena, target_repo.event.user_id)) orelse continue; 126 const target = try std.fmt.allocPrint(arena.allocator(), "{s}/{s}", .{ owner.event.name, target_repo.event.name }); 127 128 var title: []const u8 = "(unavailable)"; 129 if (session.io) |io| if (session.repos_dir) |repos_dir| { 130 const id_hex = std.fmt.bytesToHex(fork_id.*, .lower); 131 const path = try fork.forkPath(arena.allocator(), repos_dir, &id_hex); 132 if (rp.Repo(.xit, .{}).open(io, arena.child_allocator, .{ .path = path, .require_repo_root = true })) |opened| { 133 var fork_repo = opened; 134 defer fork_repo.deinit(io, arena.child_allocator); 135 if (evt.currentMoment(.{}, &fork_repo)) |moment| { 136 if (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, arena, fork_id)) |patch| title = patch.event.title; 137 } else |_| {} 138 } else |_| {} 139 }; 140 try forks.append(arena.allocator(), .{ .target = target, .title = title }); 141 } 142 forks_next_start = if (end < count) end else null; 143 } 144 } 145 146 return .{ 147 .header = try Header.init(arena, user.event.name), 148 .user = evt.project(evt.User.Public, user.event), 149 .repos = repos.items, 150 .repos_start = repos_start, 151 .repos_next_start = repos_next_start, 152 .forks = forks.items, 153 .forks_start = forks_start, 154 .forks_next_start = forks_next_start, 155 .settings = Settings.init(), 156 .auth = Auth.init(), 157 .quit = Quit.init(), 158 .route_name = name, 159 }; 160 } 161 162 pub const View = struct { 163 box: wgt.Box(ui.Widget), 164 data: *const Self, 165 session: *ui.Session, 166 167 const header_index: usize = 0; 168 const stack_index: usize = 1; 169 170 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 171 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 172 errdefer box.deinit(allocator); 173 174 // build the header first so we can grab the repos-tab id for the auth 175 // view (it focuses there after login). 176 { 177 var header_view = try Header.View.init(allocator, &data.header, session); 178 errdefer header_view.deinit(allocator); 179 try box.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .user_header = header_view }, .rect = null, .min_size = null }); 180 } 181 182 { 183 var stack = try wgt.Stack(ui.Widget).init(allocator); 184 errdefer stack.deinit(allocator); 185 186 // repos list — the default tab 187 { 188 var list = try ui.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 189 errdefer list.deinit(allocator); 190 191 var arena = std.heap.ArenaAllocator.init(allocator); 192 defer arena.deinit(); 193 const aa = arena.allocator(); 194 195 // a leading "previous" row off the first window, one row per repo, 196 // then a trailing "next" row when more remain. each window row 197 // navigates to the adjacent window of this user's repos. 198 var items: std.ArrayList(ui.FlowBox.Item) = .empty; 199 if (data.repos_start > 0) 200 try items.append(aa, .{ .text = "← previous", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/repos/start:{d}", .{ data.user.name, data.repos_start -| page_size }) }); 201 for (data.repos) |repo| 202 // clicking a repo opens its page; the "a:" prefix makes the web 203 // renderer emit an <a href="/repo/alice/foo"> anchor. 204 try items.append(aa, .{ 205 .text = try std.fmt.allocPrint(aa, "{s} - {s}", .{ repo.event.name, repo.event.description }), 206 .link = try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}", .{ data.user.name, repo.event.name }), 207 }); 208 if (data.repos_next_start) |next_start| 209 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/repos/start:{d}", .{ data.user.name, next_start }) }); 210 try list.setItems(allocator, items.items); 211 212 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 213 } 214 215 // forks list 216 { 217 var list = try ui.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 218 errdefer list.deinit(allocator); 219 220 var item_arena = std.heap.ArenaAllocator.init(allocator); 221 defer item_arena.deinit(); 222 const aa = item_arena.allocator(); 223 var items: std.ArrayList(ui.FlowBox.Item) = .empty; 224 if (data.forks_start > 0) 225 try items.append(aa, .{ .text = "← previous", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/forks/start:{d}", .{ data.user.name, data.forks_start -| page_size }) }); 226 for (data.forks) |fork_item| 227 try items.append(aa, .{ .text = try std.fmt.allocPrint(aa, "{s}\n{s}", .{ fork_item.target, fork_item.title }), .link = "" }); 228 if (data.forks_next_start) |next_start| 229 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:/user/{s}/forks/start:{d}", .{ data.user.name, next_start }) }); 230 try list.setItems(allocator, items.items); 231 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 232 } 233 234 // the header has no settings tab without a login, so keep the 235 // stack's children 1:1 with the tabs by skipping the view too 236 if (session.data.user_id != null) { 237 var settings_view = try Settings.View.init(allocator, session); 238 errdefer settings_view.deinit(allocator); 239 try stack.children.put(allocator, settings_view.getFocus().id, .{ .home_settings = settings_view }); 240 } 241 242 { 243 var auth_view = try Auth.View.init(allocator, &data.auth, session); 244 errdefer auth_view.deinit(allocator); 245 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 246 } 247 248 if (session.is_terminal) { 249 var quit_view = try Quit.View.init(allocator, session); 250 errdefer quit_view.deinit(allocator); 251 try stack.children.put(allocator, quit_view.getFocus().id, .{ .quit = quit_view }); 252 } 253 254 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 255 } 256 257 var self = View{ .box = box, .data = data, .session = session }; 258 self.getFocus().child_id = box.children.keys()[header_index]; 259 return self; 260 } 261 262 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 263 self.box.deinit(allocator); 264 } 265 266 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 267 self.clearGrid(); 268 const header = &self.box.children.values()[header_index].widget.user_header; 269 const stack = &self.box.children.values()[stack_index].widget.stack; 270 271 // each header tab maps 1:1 to a stack child by position. mirror the 272 // selection into current_page so the host can push the matching url; 273 // all user tabs share the .user parent, so this stays on the page 274 // rather than navigating. 275 if (header.getSelectedIndex()) |index| { 276 stack.getFocus().child_id = stack.children.keys()[index]; 277 const name = self.data.route_name; 278 switch (index) { 279 0 => self.session.data.current_page = .{ .user_repos = .{ .name = name, .start = self.data.repos_start } }, 280 1 => self.session.data.current_page = .{ .user_forks = .{ .name = name, .start = self.data.forks_start } }, 281 else => switch (std.meta.activeTag(stack.children.values()[index])) { 282 .home_settings => self.session.data.current_page = .{ .user_settings = name }, 283 .home_auth => self.session.data.current_page = .{ .user_auth = name }, 284 // the quit tab is tty-only and not a route, so leave current_page 285 // alone (nothing to mirror into the url). 286 .quit => {}, 287 else => {}, 288 }, 289 } 290 } 291 try self.box.build(allocator, constraint, root_focus); 292 } 293 294 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 295 if (self.getFocus().child_id) |child_id| { 296 if (self.box.children.getIndex(child_id)) |current_index| { 297 const child = &self.box.children.values()[current_index].widget; 298 var index = current_index; 299 300 const direction = inp.vertDirection(key); 301 302 switch (direction) { 303 .up => { 304 switch (child.*) { 305 .user_header => { 306 try child.input(allocator, key, root_focus); 307 }, 308 .stack => { 309 if (child.stack.getSelected()) |selected_widget| { 310 const at_top = switch (selected_widget.*) { 311 .flow_box_scroll => |*v| v.getSelectedIndex() == 0, 312 .home_settings => |*v| v.getSelectedIndex() == 0, 313 .home_auth => |*v| v.getSelectedIndex() == 0, 314 .quit => |*v| v.getSelectedIndex() == 0, 315 else => false, 316 }; 317 if (at_top) { 318 index = header_index; 319 } else { 320 try child.input(allocator, key, root_focus); 321 } 322 } 323 }, 324 else => {}, 325 } 326 }, 327 .down => { 328 switch (child.*) { 329 .user_header => { 330 index = stack_index; 331 }, 332 .stack => { 333 try child.input(allocator, key, root_focus); 334 }, 335 else => {}, 336 } 337 }, 338 .none => { 339 try child.input(allocator, key, root_focus); 340 }, 341 } 342 343 if (index != current_index) { 344 root_focus.setFocus(self.box.children.keys()[index]); 345 } 346 } 347 } 348 } 349 350 pub fn clearGrid(self: *View) void { 351 self.box.clearGrid(); 352 } 353 354 pub fn getGrid(self: View) ?Grid { 355 return self.box.getGrid(); 356 } 357 358 pub fn getFocus(self: *View) *Focus { 359 return self.box.getFocus(); 360 } 361 }; 362