╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Members.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
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 about_index = 0; 149 const add_index = 1; 150 151 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 152 const aa = session.page_arena.allocator(); 153 if (data.confirm) |confirm| { 154 const url = try (try confirmRoute(data.identity, data.start, confirm.name, confirm.action)).toUrl(session.page_arena); 155 var center = try ui.widget.initConfirmationForm(allocator, try aa.print("form:{s}", .{url}), &.{.{ 156 .label = if (confirm.action == .demote) "demote to member" else "remove user", 157 .bottom_label = try aa.print(" {s} ", .{confirm.name}), 158 }}); 159 errdefer center.deinit(allocator); 160 return .{ 161 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 162 .data = data, 163 .session = session, 164 }; 165 } 166 167 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 168 errdefer box.deinit(allocator); 169 const route = ui.RoutablePage.repoMembersRoute(data.identity, data.start) orelse return error.RouteTooLong; 170 const url = try route.toUrl(session.page_arena); 171 172 { 173 var about = try ui.widget.initAbout(allocator, &.{ 174 .{ .text = "members", .style = .{ .bold = true } }, 175 .{ .text = " can push to this repo.\n" }, 176 .{ .text = "owners", .style = .{ .bold = true } }, 177 .{ .text = " can add and remove members." }, 178 }); 179 errdefer about.deinit(allocator); 180 try box.children.put(allocator, about.getFocus().id, .{ .widget = .{ .box = about }, .rect = null, .min_size = null }); 181 } 182 { 183 var add = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 184 errdefer add.deinit(allocator); 185 add.getFocus().kind = .{ .custom = try aa.print("form:{s}/grant", .{url}) }; 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 = 3 } }); 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()[about_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()[0].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 fn aboutBox(self: *View) *wgt.Box(ui.Widget) { 277 return &self.contentBox().children.values()[about_index].widget.box; 278 } 279 280 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 281 self.clearGrid(); 282 if (self.data.confirm == null) { 283 try self.labelAddInput(root_focus); 284 ui.widget.markAboutFocused(self.aboutBox(), root_focus); 285 } 286 // the window's height as a minimum centers rows that fit 287 try self.scroll.build(allocator, .{ 288 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 289 .max_size = constraint.max_size, 290 }, root_focus); 291 } 292 293 // label the add input for this frame and register it for the web form 294 // handling 295 fn labelAddInput(self: *View, root_focus: *Focus) !void { 296 const name = self.nameInput(); 297 const failure = if (self.session.formFeedback(.repo_members)) |saved| saved.failure else null; 298 name.options.top_label.text = if (failure) |value| switch (value) { 299 .not_found => " add a member (not found) ", 300 .already_added => " add a member (already added) ", 301 } else " add a member "; 302 name.options.bottom_label.text = if (root_focus.grandchild_id == name.getFocus().id) " press enter " else ""; 303 try self.session.text_inputs.put(self.session.arena.allocator(), name.getFocus().id, name); 304 } 305 306 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 307 if (self.data.confirm) |confirm| { 308 const button_id = self.contentBox().getFocus().child_id orelse return; 309 if (inp.activated(root_focus, button_id, key)) try self.act(allocator, confirm.action, confirm.name); 310 return; 311 } 312 // the add row keeps every key but the vertical ones, which move between rows 313 if (self.addActive() and inp.vertDirection(key) == .none) { 314 if (key == .enter) { 315 const name = try self.nameInput().text(allocator); 316 defer allocator.free(name); 317 try self.act(allocator, .grant, name); 318 } else try self.nameInput().input(allocator, key, root_focus); 319 return; 320 } 321 322 const rows = self.contentBox(); 323 const current = rows.children.getIndex(rows.getFocus().child_id orelse return) orelse return; 324 if (inp.rowDelta(key, @intCast(rows.children.count()))) |delta| { 325 ui.widget.moveRowFocus(rows, &self.scroll, root_focus, delta); 326 return; 327 } 328 switch (key) { 329 .arrow_left, .arrow_right => _ = ui.widget.moveInSelectedRow(rows, root_focus, key == .arrow_right), 330 else => { 331 // the text, the add row and the previous link, when there is one, sit above the rows 332 const offset: usize = add_index + 1 + @as(usize, @intFromBool(self.data.start > 0)); 333 if (current < offset or current - offset >= self.data.rows.len) return; 334 const row = self.data.rows[current - offset]; 335 const row_box = &rows.children.values()[current].widget.box; 336 // remove and demote are links to their confirmations, which the host follows 337 if (row.role == .owner) return; 338 const button_id = row_box.getFocus().child_id orelse return; 339 if (row_box.children.getIndex(button_id) != 1 or !inp.activated(root_focus, button_id, key)) return; 340 try self.act(allocator, .promote, row.name); 341 }, 342 } 343 } 344 345 // the terminal performs the action itself; the web posts the form instead 346 fn act(self: *View, allocator: std.mem.Allocator, action: Action, name: []const u8) !void { 347 if (comptime wasm) return; 348 const io = self.session.io orelse return; 349 const users_dir = self.session.users_dir orelse return; 350 const admin_repo = self.session.admin_repo orelse return; 351 const actor = (try self.session.authorize(self.data.identity, .owner, null)) orelse return; 352 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 353 const failure = perform(io, allocator, moment, users_dir, actor, self.data.identity, action, name) catch |err| switch (err) { 354 // a stale row; the refresh below shows the list as it is 355 error.EventNotFound => null, 356 else => return err, 357 }; 358 if (failure) |value| { 359 self.session.data.form_feedback = .{ .repo_members = .{ .failure = value, .name = try self.session.arena.allocator().dupe(u8, name) } }; 360 return; 361 } 362 try self.session.navigate(landingRoute(self.data.identity, self.data.start, actor, action, name) orelse return error.RouteTooLong); 363 } 364 365 pub fn clearGrid(self: *View) void { 366 self.scroll.clearGrid(); 367 } 368 369 pub fn getGrid(self: View) ?Grid { 370 return self.scroll.getGrid(); 371 } 372 373 pub fn getFocus(self: *View) *Focus { 374 return self.scroll.getFocus(); 375 } 376 377 // up leaves the view from the explanatory text or the confirmation 378 pub fn atTop(self: *View) bool { 379 return self.data.confirm != null or self.contentBox().getFocus().child_id == self.aboutBox().getFocus().id; 380 } 381 }; 382