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