╭ Members.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const inp = @import("../input.zig"); 6 const Comment = @import("./Comment.zig"); 7 const xit = @import("xit"); 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const Key = xitui.input.Key; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 15 const wasm = builtin.target.cpu.arch == .wasm32; 16 17 pub const page_size = 20; 18 19 // the repo's members, which only its owners see 20 identity: []const u8, 21 start: usize, 22 next_start: ?usize = null, 23 rows: []const Row, 24 // the role change the route asks to confirm, which stands in for the list 25 confirm: ?Confirm = null, 26 27 const Self = @This(); 28 29 pub const Confirm = struct { action: Action, name: []const u8 }; 30 31 pub const Row = struct { 32 name: []const u8, 33 role: evt.Repo.Role, 34 }; 35 36 pub const Action = enum { grant, revoke, promote, demote }; 37 38 // one window of the repo's grants, newest first, without read grants, the 39 // creator or removed users 40 pub fn init( 41 io: std.Io, 42 allocator: std.mem.Allocator, 43 arena: *std.heap.ArenaAllocator, 44 admin_moment: evt.AdminDB.HashMap(.read_only), 45 users_dir: []const u8, 46 identity: []const u8, 47 owner_id: *const [evt.event_id_size]u8, 48 repo_id: *const [evt.event_id_size]u8, 49 start: usize, 50 confirm: ?Confirm, 51 ) !Self { 52 const aa = arena.allocator(); 53 const grants = try evt.readRepoGrants(io, allocator, arena, users_dir, owner_id, repo_id); 54 var rows: std.ArrayList(Row) = .empty; 55 var skipped: usize = 0; 56 var next_start: ?usize = null; 57 for (grants) |grant| { 58 // the creator owns the repo with or without a grant 59 const user_id = try evt.parseEventId(&grant.user_id); 60 if (grant.role == .read or std.mem.eql(u8, &user_id, owner_id)) continue; 61 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, arena, &user_id)) orelse continue; 62 if (user.removed) continue; 63 if (skipped < start) { 64 skipped += 1; 65 continue; 66 } 67 if (rows.items.len == page_size) { 68 next_start = start + page_size; 69 break; 70 } 71 try rows.append(aa, .{ .name = user.event.name, .role = grant.role }); 72 } 73 return .{ 74 .identity = try aa.dupe(u8, identity), 75 .start = start, 76 .next_start = next_start, 77 .rows = rows.items, 78 .confirm = if (confirm) |value| .{ .action = value.action, .name = try aa.dupe(u8, value.name) } else null, 79 }; 80 } 81 82 // the confirmation of taking `name`'s role away 83 fn confirmRoute(identity: []const u8, start: usize, name: []const u8, action: Action) !ui.RoutablePage { 84 var route = ui.RoutablePage.repoMembersRoute(identity, start) orelse return error.RouteTooLong; 85 route.repo_members.user = ui.RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 86 route.repo_members.confirm = action; 87 return route; 88 } 89 90 // act on `name`'s role in the repo `identity` names as `actor`, one of its 91 // owners. returns why an add wrote nothing, or null once written 92 pub fn perform( 93 io: std.Io, 94 allocator: std.mem.Allocator, 95 admin_moment: evt.AdminDB.HashMap(.read_only), 96 users_dir: []const u8, 97 actor: ui.Actor, 98 identity: []const u8, 99 action: Action, 100 name: []const u8, 101 ) !?ui.Session.FormFeedback.MembersFailure { 102 const owner_id = actor.repo_user_id orelse return error.NotFound; 103 const parsed = ui.RoutablePage.RepoIdentity.parse(identity) orelse return error.NotFound; 104 const user_id = (try evt.User.readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, name)) orelse 105 return if (action == .grant) .not_found else error.EventNotFound; 106 107 var arena = std.heap.ArenaAllocator.init(allocator); 108 defer arena.deinit(); 109 const repo_id, const role = blk: { 110 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, &owner_id)) orelse return error.NotFound; 111 defer user_repo.deinit(io, allocator); 112 const moment = (try evt.userMoment(&user_repo)) orelse return error.NotFound; 113 const found = (try evt.Repo.readByName(evt.UserDB, evt.user_repo_opts.hash, moment, &arena, parsed.name)) orelse return error.NotFound; 114 break :blk .{ found.event_id, try evt.Grant.readRole(evt.UserDB, evt.user_repo_opts.hash, moment, &arena, &found.event_id, &user_id) }; 115 }; 116 117 switch (action) { 118 // a reader becomes a member; anyone else with a role, and the 119 // creator, who holds no grant, is already added 120 .grant => { 121 const already_added = if (role) |held| held != .read else std.mem.eql(u8, &user_id, &owner_id); 122 if (already_added) return .already_added; 123 try evt.grantRole(io, allocator, users_dir, &owner_id, &repo_id, &user_id, .write, actor.author); 124 }, 125 .revoke => try evt.revokeRole(io, allocator, users_dir, &owner_id, &repo_id, &user_id, actor.author), 126 // only a listed grant changes role 127 .promote, .demote => { 128 if (role == null) return error.EventNotFound; 129 try evt.grantRole(io, allocator, users_dir, &owner_id, &repo_id, &user_id, if (action == .promote) .owner else .write, actor.author); 130 }, 131 } 132 return null; 133 } 134 135 // where an action lands: the same window of the list, or the files tab once 136 // the actor gave up their own owner role 137 pub fn landingRoute(identity: []const u8, start: usize, actor: ui.Actor, action: Action, name: []const u8) ?ui.RoutablePage { 138 const lost_ownership = (action == .revoke or action == .demote) and std.mem.eql(u8, name, actor.author.name); 139 if (lost_ownership) return ui.RoutablePage.repoFilesRoute(identity, null, "", "", 0); 140 return ui.RoutablePage.repoMembersRoute(identity, start); 141 } 142 143 pub const View = struct { 144 scroll: wgt.Scroll(ui.Widget), 145 data: *const Self, 146 session: *ui.Session, 147 148 const add_index = 0; 149 150 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 151 const aa = session.page_arena.allocator(); 152 if (data.confirm) |confirm| { 153 const url = try (try confirmRoute(data.identity, data.start, confirm.name, confirm.action)).toUrl(session.page_arena); 154 var center = try ui.widget.initConfirmationForm(allocator, try aa.print("form:{s}", .{url}), &.{.{ 155 .label = if (confirm.action == .demote) "demote to member" else "remove user", 156 .bottom_label = try aa.print(" {s} ", .{confirm.name}), 157 }}); 158 errdefer center.deinit(allocator); 159 return .{ 160 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 161 .data = data, 162 .session = session, 163 }; 164 } 165 166 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 167 errdefer box.deinit(allocator); 168 const route = ui.RoutablePage.repoMembersRoute(data.identity, data.start) orelse return error.RouteTooLong; 169 const url = try route.toUrl(session.page_arena); 170 171 { 172 // the gap leaves a blank line between the text and the input 173 var add = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert, .gap = 1 }); 174 errdefer add.deinit(allocator); 175 add.getFocus().kind = .{ .custom = try aa.print("form:{s}/grant", .{url}) }; 176 { 177 var about = try ui.widget.RichText.init(allocator, &.{ 178 .{ .text = "members", .style = .{ .bold = true } }, 179 .{ .text = " can push to this repo.\n" }, 180 .{ .text = "owners", .style = .{ .bold = true } }, 181 .{ .text = " can add and remove members." }, 182 }); 183 errdefer about.deinit(allocator); 184 try add.children.put(allocator, about.getFocus().id, .{ .widget = .{ .rich_text = about }, .rect = null, .min_size = null }); 185 } 186 var name = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " add a member " }, .name = "name", .visible_width = 30, .round_corners = true, .render_content = session.is_terminal }); 187 errdefer name.deinit(allocator); 188 name.getFocus().mode = .all; 189 if (session.formFeedback(.repo_members)) |saved| try name.setContent(allocator, saved.name); 190 add.getFocus().child_id = name.getFocus().id; 191 try add.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_input = name }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 192 try box.children.put(allocator, add.getFocus().id, .{ .widget = .{ .box = add }, .rect = null, .min_size = .{ .width = null, .height = 6 } }); 193 } 194 195 // the user rows follow the add row in the same box 196 if (data.start > 0) { 197 var previous = try Comment.linkBox(allocator, session, "← previous", ui.RoutablePage.repoMembersRoute(data.identity, data.start -| page_size) orelse return error.RouteTooLong); 198 errdefer previous.deinit(allocator); 199 try box.children.put(allocator, previous.getFocus().id, .{ .widget = .{ .text_box = previous }, .rect = null, .min_size = null }); 200 } 201 for (data.rows) |row| { 202 var row_route = route; 203 row_route.repo_members.user = ui.RoutablePage.Array(evt.User.name_max_len).from(row.name) orelse return error.RouteTooLong; 204 const row_url = try row_route.toUrl(session.page_arena); 205 206 var row_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 207 errdefer row_box.deinit(allocator); 208 row_box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{row_url}) }; 209 { 210 const user_route = ui.RoutablePage{ .user_repos = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(row.name) orelse return error.RouteTooLong } }; 211 var name = try Comment.linkBox(allocator, session, row.name, user_route); 212 errdefer name.deinit(allocator); 213 name.options.bottom_label.text = if (row.role == .owner) " owner " else " member "; 214 try row_box.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_box = name }, .rect = null, .min_size = null }); 215 } 216 // taking a role away asks first 217 if (row.role == .owner) { 218 const demote_route = try confirmRoute(data.identity, data.start, row.name, .demote); 219 try addButton(allocator, &row_box, "demote to member", try aa.print("a:{s}", .{try demote_route.toUrl(session.page_arena)})); 220 } else try addButton(allocator, &row_box, "promote to owner", try aa.print("submit:{s}/promote", .{row_url})); 221 { 222 const revoke_route = try confirmRoute(data.identity, data.start, row.name, .revoke); 223 var remove = try wgt.TextBox.init(allocator, "✕", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 224 errdefer remove.deinit(allocator); 225 remove.getFocus().mode = .all; 226 remove.getFocus().kind = .{ .custom = try aa.print("a:{s}", .{try revoke_route.toUrl(session.page_arena)}) }; 227 try row_box.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 228 } 229 row_box.getFocus().child_id = row_box.children.keys()[0]; 230 try box.children.put(allocator, row_box.getFocus().id, .{ .widget = .{ .box = row_box }, .rect = null, .min_size = null }); 231 } 232 if (data.next_start) |next_start| { 233 var next = try Comment.linkBox(allocator, session, "next →", ui.RoutablePage.repoMembersRoute(data.identity, next_start) orelse return error.RouteTooLong); 234 errdefer next.deinit(allocator); 235 try box.children.put(allocator, next.getFocus().id, .{ .widget = .{ .text_box = next }, .rect = null, .min_size = null }); 236 } 237 238 box.getFocus().child_id = box.children.keys()[add_index]; 239 var center = try ui.widget.Center.init(allocator, .{ .box = box }); 240 errdefer center.deinit(allocator); 241 return .{ 242 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 243 .data = data, 244 .session = session, 245 }; 246 } 247 248 fn addButton(allocator: std.mem.Allocator, row: *wgt.Box(ui.Widget), text: []const u8, kind: []const u8) !void { 249 var button = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 250 errdefer button.deinit(allocator); 251 button.getFocus().mode = .all; 252 button.getFocus().kind = .{ .custom = kind }; 253 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 254 } 255 256 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 257 self.scroll.deinit(allocator); 258 } 259 260 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 261 return &self.scroll.child.center.child.box; 262 } 263 264 fn addBox(self: *View) *wgt.Box(ui.Widget) { 265 return &self.contentBox().children.values()[add_index].widget.box; 266 } 267 268 fn nameInput(self: *View) *wgt.TextInput { 269 return &self.addBox().children.values()[1].widget.text_input; 270 } 271 272 fn addActive(self: *View) bool { 273 return self.contentBox().getFocus().child_id == self.addBox().getFocus().id; 274 } 275 276 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 277 self.clearGrid(); 278 if (self.data.confirm == null) try self.labelAddInput(root_focus); 279 // the window's height as a minimum centers rows that fit 280 try self.scroll.build(allocator, .{ 281 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 282 .max_size = constraint.max_size, 283 }, root_focus); 284 } 285 286 // label the add input for this frame and register it for the web form 287 // handling 288 fn labelAddInput(self: *View, root_focus: *Focus) !void { 289 const name = self.nameInput(); 290 const failure = if (self.session.formFeedback(.repo_members)) |saved| saved.failure else null; 291 name.options.top_label.text = if (failure) |value| switch (value) { 292 .not_found => " add a member (not found) ", 293 .already_added => " add a member (already added) ", 294 } else " add a member "; 295 name.options.bottom_label.text = if (root_focus.grandchild_id == name.getFocus().id) " press enter " else ""; 296 try self.session.text_inputs.put(self.session.arena.allocator(), name.getFocus().id, name); 297 } 298 299 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 300 if (self.data.confirm) |confirm| { 301 const button_id = self.contentBox().getFocus().child_id orelse return; 302 if (inp.activated(root_focus, button_id, key)) try self.act(allocator, confirm.action, confirm.name); 303 return; 304 } 305 // the add row keeps every key but the vertical ones, which move between rows 306 if (self.addActive() and inp.vertDirection(key) == .none) { 307 if (key == .enter) { 308 const name = try self.nameInput().text(allocator); 309 defer allocator.free(name); 310 try self.act(allocator, .grant, name); 311 } else try self.nameInput().input(allocator, key, root_focus); 312 return; 313 } 314 315 const rows = self.contentBox(); 316 const current = rows.children.getIndex(rows.getFocus().child_id orelse return) orelse return; 317 if (inp.rowDelta(key, @intCast(rows.children.count()))) |delta| { 318 ui.widget.moveRowFocus(rows, &self.scroll, root_focus, delta); 319 return; 320 } 321 switch (key) { 322 .arrow_left, .arrow_right => _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right), 323 else => { 324 // the add row and the previous link, when there is one, sit above the rows 325 const offset: usize = add_index + 1 + @intFromBool(self.data.start > 0); 326 if (current < offset or current - offset >= self.data.rows.len) return; 327 const row = self.data.rows[current - offset]; 328 const row_box = &rows.children.values()[current].widget.box; 329 // remove and demote are links to their confirmations, which the host follows 330 if (row.role == .owner) return; 331 const button_id = row_box.getFocus().child_id orelse return; 332 if (row_box.children.getIndex(button_id) != 1 or !inp.activated(root_focus, button_id, key)) return; 333 try self.act(allocator, .promote, row.name); 334 }, 335 } 336 } 337 338 // the terminal performs the action itself; the web posts the form instead 339 fn act(self: *View, allocator: std.mem.Allocator, action: Action, name: []const u8) !void { 340 if (comptime wasm) return; 341 const io = self.session.io orelse return; 342 const users_dir = self.session.users_dir orelse return; 343 const admin_repo = self.session.admin_repo orelse return; 344 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 345 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 346 const failure = perform(io, allocator, moment, users_dir, actor, self.data.identity, action, name) catch |err| switch (err) { 347 // a stale row; the refresh below shows the list as it is 348 error.EventNotFound => null, 349 else => return err, 350 }; 351 if (failure) |value| { 352 self.session.data.form_feedback = .{ .repo_members = .{ .failure = value, .name = try self.session.arena.allocator().dupe(u8, name) } }; 353 return; 354 } 355 try self.session.navigate(landingRoute(self.data.identity, self.data.start, actor, action, name) orelse return error.RouteTooLong); 356 } 357 358 pub fn clearGrid(self: *View) void { 359 self.scroll.clearGrid(); 360 } 361 362 pub fn getGrid(self: View) ?Grid { 363 return self.scroll.getGrid(); 364 } 365 366 pub fn getFocus(self: *View) *Focus { 367 return self.scroll.getFocus(); 368 } 369 370 // up leaves the view from the add row or the confirmation 371 pub fn atTop(self: *View) bool { 372 return self.data.confirm != null or self.addActive(); 373 } 374 }; 375