╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Roles.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
99 // creator, who holds no grant, is already added 100 .grant => { 101 const already_added = if (role) |held| held != .read else std.mem.eql(u8, &user_id, &owner_id); 102 if (already_added) return .already_added; 103 try evt.grantRole(io, allocator, users_dir, &owner_id, &repo_id, &user_id, .write, actor.author); 104 }, 105 .revoke => try evt.revokeRole(io, allocator, users_dir, &owner_id, &repo_id, &user_id, actor.author), 106 // only a listed grant changes role 107 .promote, .demote => { 108 if (role == null) return error.EventNotFound; 109 try evt.grantRole(io, allocator, users_dir, &owner_id, &repo_id, &user_id, if (action == .promote) .owner else .write, actor.author); 110 }, 111 } 112 return null; 113 } 114 115 // where an action lands: the same window of the list, or the files tab once 116 // the actor gave up their own owner role 117 pub fn landingRoute(identity: []const u8, start: usize, actor: ui.Actor, action: Action, name: []const u8) ?ui.RoutablePage { 118 const lost_ownership = (action == .revoke or action == .demote) and std.mem.eql(u8, name, actor.author.name); 119 if (lost_ownership) return ui.RoutablePage.repoFilesRoute(identity, null, "", "", 0); 120 return ui.RoutablePage.repoRolesRoute(identity, start); 121 } 122 123 pub const View = struct { 124 box: wgt.Box(ui.Widget), 125 data: *const Self, 126 session: *ui.Session, 127 128 const add_index = 0; 129 const list_index = 1; 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 { 152 var rows = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 153 errdefer rows.deinit(allocator); 154 if (data.start > 0) { 155 var previous = try Comment.linkBox(allocator, session, "← previous", ui.RoutablePage.repoRolesRoute(data.identity, data.start -| page_size) orelse return error.RouteTooLong); 156 errdefer previous.deinit(allocator); 157 try rows.children.put(allocator, previous.getFocus().id, .{ .widget = .{ .text_box = previous }, .rect = null, .min_size = null }); 158 } 159 for (data.rows) |row| { 160 var row_route = route; 161 row_route.repo_roles.user = ui.RoutablePage.Array(evt.User.name_max_len).from(row.name) orelse return error.RouteTooLong; 162 const row_url = try row_route.toUrl(session.page_arena); 163 164 var row_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 165 errdefer row_box.deinit(allocator); 166 row_box.getFocus().kind = .{ .custom = try aa.print("form:{s}", .{row_url}) }; 167 { 168 const user_route = ui.RoutablePage{ .user_repos = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(row.name) orelse return error.RouteTooLong } }; 169 var name = try Comment.linkBox(allocator, session, row.name, user_route); 170 errdefer name.deinit(allocator); 171 name.options.bottom_label.text = if (row.role == .owner) " owner " else " collaborator "; 172 try row_box.children.put(allocator, name.getFocus().id, .{ .widget = .{ .text_box = name }, .rect = null, .min_size = null }); 173 } 174 try addButton(allocator, &row_box, "remove", try aa.print("submit:{s}/revoke", .{row_url})); 175 if (row.role == .owner) 176 try addButton(allocator, &row_box, "demote to collaborator", try aa.print("submit:{s}/demote", .{row_url})) 177 else 178 try addButton(allocator, &row_box, "promote to owner", try aa.print("submit:{s}/promote", .{row_url})); 179 row_box.getFocus().child_id = row_box.children.keys()[0]; 180 try rows.children.put(allocator, row_box.getFocus().id, .{ .widget = .{ .box = row_box }, .rect = null, .min_size = null }); 181 } 182 if (data.next_start) |next_start| { 183 var next = try Comment.linkBox(allocator, session, "next →", ui.RoutablePage.repoRolesRoute(data.identity, next_start) orelse return error.RouteTooLong); 184 errdefer next.deinit(allocator); 185 try rows.children.put(allocator, next.getFocus().id, .{ .widget = .{ .text_box = next }, .rect = null, .min_size = null }); 186 } 187 if (rows.children.count() > 0) rows.getFocus().child_id = rows.children.keys()[0]; 188 var scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = rows }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 189 errdefer scroll.deinit(allocator); 190 try box.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = null }); 191 } 192 193 box.getFocus().child_id = box.children.keys()[add_index]; 194 return .{ .box = box, .data = data, .session = session }; 195 } 196 197 fn addButton(allocator: std.mem.Allocator, row: *wgt.Box(ui.Widget), text: []const u8, kind: []const u8) !void { 198 var button = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 199 errdefer button.deinit(allocator); 200 button.getFocus().mode = .all; 201 button.getFocus().kind = .{ .custom = kind }; 202 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 203 } 204 205 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 206 self.box.deinit(allocator); 207 } 208 209 fn addBox(self: *View) *wgt.Box(ui.Widget) { 210 return &self.box.children.values()[add_index].widget.box; 211 } 212 213 fn nameInput(self: *View) *wgt.TextInput { 214 return &self.addBox().children.values()[0].widget.text_input; 215 } 216 217 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 218 return &self.box.children.values()[list_index].widget.scroll; 219 } 220 221 fn addActive(self: *View) bool { 222 return self.box.getFocus().child_id == self.addBox().getFocus().id; 223 } 224 225 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 226 self.clearGrid(); 227 const name = self.nameInput(); 228 const failure = if (self.session.formFeedback(.repo_roles)) |saved| saved.failure else null; 229 name.options.top_label.text = if (failure) |value| switch (value) { 230 .not_found => " add a collaborator (not found) ", 231 .already_added => " add a collaborator (already added) ", 232 } else " add a collaborator "; 233 name.options.bottom_label.text = if (root_focus.grandchild_id == name.getFocus().id) " press enter " else ""; 234 // the web form handling finds the input by focus id 235 try self.session.text_inputs.put(self.session.arena.allocator(), name.getFocus().id, name); 236 try self.box.build(allocator, constraint, root_focus); 237 } 238 239 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 240 if (self.addActive()) { 241 switch (inp.vertDirection(key)) { 242 .down => root_focus.setFocus(self.listScroll().getFocus().id), 243 .up => {}, 244 .none => if (key == .enter) { 245 const name = try self.nameInput().text(allocator); 246 defer allocator.free(name); 247 try self.act(allocator, .grant, name); 248 } else try self.nameInput().input(allocator, key, root_focus), 249 } 250 return; 251 } 252 253 const rows = &self.listScroll().child.box; 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 if (delta < 0 and current == 0) 257 root_focus.setFocus(self.addBox().getFocus().id) 258 else 259 ui.widget.moveRowFocus(rows, self.listScroll(), root_focus, delta); 260 return; 261 } 262 switch (key) { 263 .arrow_left, .arrow_right => _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right), 264 else => { 265 // the previous link, when there is one, sits above the rows 266 const offset: usize = @intFromBool(self.data.start > 0); 267 if (current < offset or current - offset >= self.data.rows.len) return; 268 const row = self.data.rows[current - offset]; 269 const row_box = &rows.children.values()[current].widget.box; 270 const button_id = row_box.getFocus().child_id orelse return; 271 const pressed = switch (key) { 272 .enter => root_focus.grandchild_id == button_id, 273 .mouse => |mouse| inp.leftClickOn(root_focus, button_id, mouse), 274 else => false, 275 }; 276 if (!pressed) return; 277 const action: Action = switch (row_box.children.getIndex(button_id) orelse return) { 278 1 => .revoke, 279 2 => if (row.role == .owner) .demote else .promote, 280 else => return, 281 }; 282 try self.act(allocator, action, row.name); 283 }, 284 } 285 } 286 287 // the terminal performs the action itself; the web posts the form instead 288 fn act(self: *View, allocator: std.mem.Allocator, action: Action, name: []const u8) !void { 289 if (comptime wasm) return; 290 const io = self.session.io orelse return; 291 const users_dir = self.session.users_dir orelse return; 292 const admin_repo = self.session.admin_repo orelse return; 293 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 294 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 295 const failure = perform(io, allocator, moment, users_dir, actor, self.data.identity, action, name) catch |err| switch (err) { 296 // a stale row; the refresh below shows the list as it is 297 error.EventNotFound => null, 298 else => return err, 299 }; 300 if (failure) |value| { 301 self.session.data.form_feedback = .{ .repo_roles = .{ .failure = value, .name = try self.session.arena.allocator().dupe(u8, name) } }; 302 return; 303 } 304 try self.session.navigate(landingRoute(self.data.identity, self.data.start, actor, action, name) orelse return error.RouteTooLong); 305 } 306 307 pub fn clearGrid(self: *View) void { 308 self.box.clearGrid(); 309 } 310 311 pub fn getGrid(self: View) ?Grid { 312 return self.box.getGrid(); 313 } 314 315 pub fn getFocus(self: *View) *Focus { 316 return self.box.getFocus(); 317 } 318 319 // up leaves the view from the add row 320 pub fn atTop(self: *View) bool { 321 return self.addActive(); 322 } 323 }; 324