╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
136 comptime hash_kind: hash.HashKind, 137 haxy_moment: DB.HashMap(.read_only), 138 arena: *std.heap.ArenaAllocator, 139 user_id: []const u8, 140 ) !?@This() { 141 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 142 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 143 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 144 const user_map = try DB.HashMap(.read_only).init(user_cursor); 145 return try evt.read(@This(), DB, hash_kind, arena, user_map); 146 } 147 148 // read a user by name from the admin event store, or null if the admin repo or 149 // the user doesn't exist. field byte slices are allocated in `arena`. 150 pub fn readByName( 151 io: std.Io, 152 allocator: std.mem.Allocator, 153 admin_repo_path: []const u8, 154 arena: *std.heap.ArenaAllocator, 155 name: []const u8, 156 ) !?Self { 157 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 158 error.RepoNotFound => return null, 159 else => |e| return e, 160 }; 161 defer repo.deinit(io, allocator); 162 163 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 164 165 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 166 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 167 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 168 var user_id: [evt.event_id_size]u8 = undefined; 169 _ = try user_id_cursor.readBytes(&user_id); 170 171 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 172 } 173 174 // flip a user's ANSI-art preference by re-emitting their User event with 175 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 176 pub fn toggleAnsi( 177 comptime repo_opts: rp.RepoOpts(.xit), 178 io: std.Io, 179 allocator: std.mem.Allocator, 180 repo: *rp.Repo(.xit, repo_opts), 181 user_id: []const u8, 182 ) !void { 183 const DB = rp.Repo(.xit, repo_opts).DB; 184 var arena = std.heap.ArenaAllocator.init(allocator); 185 defer arena.deinit(); 186 187 const moment = try evt.currentMoment(repo_opts, repo); 188 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 189 190 var updated = user; 191 updated.enable_ansi = !user.enable_ansi; 192 try evt.commitAndConsume(repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 193 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 194 .event = .{ .user = updated }, 195 }}); 196 } 197