╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Delete.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
43 // delete `target` as `user_id`, its main owner, when `typed` is its confirm 44 // text and `password` is theirs. returns why nothing was deleted, or null 45 // once deleted 46 pub fn perform( 47 io: std.Io, 48 allocator: std.mem.Allocator, 49 users_dir: []const u8, 50 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 51 user_id: [evt.event_id_size]u8, 52 target: Target, 53 typed: []const u8, 54 password: []const u8, 55 ) !?ui.Session.FormFeedback.DeleteFailure { 56 var arena = std.heap.ArenaAllocator.init(allocator); 57 defer arena.deinit(); 58 const user = (try ui.activeUser(try evt.currentMoment(evt.admin_repo_opts, admin_repo), &arena, user_id)) orelse return error.NotFound; 59 60 // the typed text is checked before the slow password check 61 if (!std.mem.eql(u8, typed, target.confirmText())) return .wrong_confirm; 62 if (!evt.User.verifyPassword(user.event.password_hash, password)) return .wrong_password; 63 64 // anyone but the main owner finds nothing to delete 65 const author = evt.CommitAuthor{ .name = user.event.name, .email = user.event.email }; 66 switch (target) { 67 .repo => |identity| { 68 const parsed = evt.parseOwnerRepoPath(identity) orelse return error.NotFound; 69 if (!std.mem.eql(u8, parsed.owner, user.event.name)) return error.NotFound; 70 const repo_id = blk: { 71 var user_repo = (try evt.openUserRepo(io, allocator, users_dir, &user_id)) orelse return error.NotFound; 72 defer user_repo.deinit(io, allocator); 73 const moment = (try evt.userMoment(&user_repo)) orelse return error.NotFound; 74 const found = (try evt.Repo.readByName(evt.UserDB, evt.user_repo_opts.hash, moment, &arena, parsed.name)) orelse return error.NotFound; 75 break :blk found.event_id; 76 }; 77 try evt.deleteRepo(io, allocator, users_dir, &user_id, &repo_id, author); 78 }, 79 .fork => |f| { 80 const parsed = evt.parseOwnerRepoPath(f.identity) orelse return error.NotFound; 81 if (!std.mem.eql(u8, parsed.owner, user.event.name)) return error.NotFound; 82 const fork_id = evt.parseEventId(&f.id) catch return error.NotFound; 83 const record = (try evt.readForkById(io, allocator, &arena, users_dir, &user_id, &fork_id)) orelse return error.NotFound; 84 if (record.removed) return error.NotFound; 85 try fork.remove(io, allocator, users_dir, &user_id, &f.id, author); 86 }, 87 .user => |name| { 88 if (!std.mem.eql(u8, name, user.event.name)) return error.NotFound; 89 try evt.deleteUser(io, allocator, users_dir, admin_repo, &user_id, author); 90 }, 91 } 92 return null; 93 } 94 95 // the session's failed delete, when it was of `target`'s kind 96 fn savedFeedback(session: *const ui.Session, target: Target) ?@FieldType(ui.Session.FormFeedback, "delete") { 97 const saved = session.formFeedback(.delete) orelse return null; 98 return if (saved.target == std.meta.activeTag(target)) saved else null; 99 } 100 101 pub const View = struct { 102 // the centered form, scrolled when it outgrows the window 103 scroll: wgt.Scroll(ui.Widget), 104 session: *ui.Session, 105 target: Target, 106 // the confirm input's label, plain and after a mismatch 107 confirm_label: []const u8, 108 mismatch_label: []const u8, 109 // the form's controls in order: the confirm text, the password, then submit 110 control_ids: [3]usize, 111 112 // the form's children, in the same order as its controls 113 const confirm_index = 0; 114 const password_index = 1; 115 const submit_index = 2; 116 117 // `route` is the delete tab's route, which the form posts under 118 pub fn init(allocator: std.mem.Allocator, data: Self, session: *ui.Session, route: ui.RoutablePage) !View { 119 const aa = session.page_arena.allocator(); 120 const url = try route.toUrl(session.page_arena); 121 const confirm_label = try aa.print(" type `{s}` here ", .{data.target.confirmText()}); 122 const mismatch_label = try aa.print(" type `{s}` here (doesn't match) ", .{data.target.confirmText()}); 123 // wide enough to show the whole label 124 const visible_width = @max(30, mismatch_label.len); 125 126 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 127 errdefer box.deinit(allocator); 128 box.getFocus().kind = .{ .custom = try aa.print("form:{s}/delete", .{url}) }; 129 130 var control_ids: [3]usize = undefined; 131 { 132 var confirm = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = confirm_label }, .name = "confirm", .visible_width = visible_width, .round_corners = true, .render_content = session.is_terminal }); 133 errdefer confirm.deinit(allocator); 134 confirm.getFocus().mode = .all; 135 if (savedFeedback(session, data.target)) |saved| try confirm.setContent(allocator, saved.confirm); 136 control_ids[confirm_index] = confirm.getFocus().id; 137 try box.children.put(allocator, confirm.getFocus().id, .{ .widget = .{ .text_input = confirm }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 138 } 139 { 140 var password = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = " password " }, .password = true, .name = "password", .visible_width = visible_width, .round_corners = true, .render_content = session.is_terminal }); 141 errdefer password.deinit(allocator); 142 password.getFocus().mode = .all; 143 control_ids[password_index] = password.getFocus().id; 144 try box.children.put(allocator, password.getFocus().id, .{ .widget = .{ .text_input = password }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 145 } 146 { 147 var submit = try ui.widget.SubmitButton.initLabeled(allocator, try aa.print("permanently delete this {s}", .{@tagName(data.target)})); 148 errdefer submit.deinit(allocator); 149 submit.setBorder(.{ .kind = .single, .style = .{ .fg = .{ .ansi = .red } } }); 150 control_ids[submit_index] = submit.buttonId(); 151 try box.children.put(allocator, submit.getFocus().id, .{ .widget = .{ .submit_button = submit }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 152 } 153 box.getFocus().child_id = control_ids[confirm_index]; 154 155 var center = try ui.widget.Center.init(allocator, .{ .box = box }); 156 errdefer center.deinit(allocator); 157 return .{ 158 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .center = center }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }), 159 .session = session, 160 .target = data.target, 161 .confirm_label = confirm_label, 162 .mismatch_label = mismatch_label, 163 .control_ids = control_ids, 164 }; 165 } 166 167 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 168 self.scroll.deinit(allocator); 169 } 170 171 fn formBox(self: *View) *wgt.Box(ui.Widget) { 172 return &self.scroll.child.center.child.box; 173 } 174 175 fn textInput(self: *View, index: usize) *wgt.TextInput { 176 return &self.formBox().children.values()[index].widget.text_input; 177 } 178 179 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 180 self.clearGrid(); 181 const failure = if (savedFeedback(self.session, self.target)) |saved| saved.failure else null; 182 self.textInput(confirm_index).options.top_label.text = if (failure == .wrong_confirm) self.mismatch_label else self.confirm_label; 183 self.textInput(password_index).options.top_label.text = if (failure == .wrong_password) " password (wrong) " else " password "; 184 // the web form handling finds the inputs by focus id 185 for ([_]usize{ confirm_index, password_index }) |index| { 186 try self.session.text_inputs.put(self.session.arena.allocator(), self.control_ids[index], self.textInput(index)); 187 } 188 // the window's height as a minimum centers a form that fits 189 try self.scroll.build(allocator, .{ 190 .min_size = .{ .width = constraint.min_size.width, .height = constraint.max_size.height orelse constraint.min_size.height }, 191 .max_size = constraint.max_size, 192 }, root_focus); 193 } 194 195 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 196 const focused = root_focus.grandchild_id orelse return; 197 const cur = std.mem.indexOfScalar(usize, &self.control_ids, focused) orelse return; 198 const submit_id = self.control_ids[submit_index]; 199 const on_submit = focused == submit_id; 200 const direction = inp.vertDirection(key); 201 const up = direction == .up or key == .back_tab; 202 if (up or direction == .down or key == .tab) { 203 // up from the first control is the sub header's, through `atTop` 204 if (up and cur == 0) return; 205 const next = if (up) cur - 1 else cur + 1; 206 if (next == self.control_ids.len) return; 207 root_focus.setFocus(self.control_ids[next]); 208 if (self.session.is_terminal) if (self.formBox().children.values()[next].rect) |rect| ui.widget.scrollToCenteredRect(&self.scroll, rect); 209 return; 210 } 211 switch (key) { 212 .enter => if (on_submit) return self.submitForm(allocator), 213 .mouse => |mouse| if (inp.leftClickOn(root_focus, submit_id, mouse)) return self.submitForm(allocator), 214 else => {}, 215 } 216 if (!on_submit) try self.textInput(cur).input(allocator, key, root_focus); 217 } 218 219 // delete the target. this is the terminal path; the web posts the form to 220 // the delete route 221 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 222 if (comptime wasm) return; 223 const io = self.session.io orelse return; 224 const users_dir = self.session.users_dir orelse return; 225 const admin_repo = self.session.admin_repo orelse return; 226 const user_id = self.session.userId() orelse return; 227 228 const confirm = try self.textInput(confirm_index).text(allocator); 229 defer allocator.free(confirm); 230 const password = try self.textInput(password_index).text(allocator); 231 defer allocator.free(password); 232 233 const failure = perform(io, allocator, users_dir, admin_repo, user_id, self.target, confirm, password) catch |err| switch (err) { 234 // only the main owner is offered the form 235 error.NotFound => return, 236 else => return err, 237 }; 238 if (failure) |value| { 239 self.session.data.form_feedback = .{ .delete = .{ .failure = value, .target = std.meta.activeTag(self.target), .confirm = try self.session.arena.allocator().dupe(u8, confirm) } }; 240 return; 241 } 242 243 switch (self.target) { 244 .repo, .fork => { 245 const name = self.session.data.user_name orelse unreachable; 246 try self.session.navigate(.{ .user_repos = .{ .name = ui.RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong } }); 247 }, 248 .user => { 249 self.session.haxy_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 250 try self.session.forgetSshKey(); 251 self.session.logOut(); 252 try self.session.navigate(.default); 253 }, 254 } 255 } 256 257 pub fn clearGrid(self: *View) void { 258 self.scroll.clearGrid(); 259 } 260 261 pub fn getGrid(self: View) ?Grid { 262 return self.scroll.getGrid(); 263 } 264 265 pub fn getFocus(self: *View) *Focus { 266 return self.scroll.getFocus(); 267 } 268 269 // up leaves the view from the first control 270 pub fn atTop(self: *View, root_focus: *Focus) bool { 271 return root_focus.grandchild_id == self.control_ids[confirm_index]; 272 } 273 }; 274