╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
224 comptime DB: type, 225 comptime hash_kind: hash.HashKind, 226 haxy_moment: DB.HashMap(.read_only), 227 arena: *std.heap.ArenaAllocator, 228 email: []const u8, 229 ) !?Record { 230 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 231 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 232 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 233 var user_id: [evt.event_id_size]u8 = undefined; 234 _ = try user_id_cursor.readBytes(&user_id); 235 return try readById(DB, hash_kind, haxy_moment, arena, &user_id); 236 } 237 238 // read a user by name from the admin event store, or null if the admin repo or 239 // the user doesn't exist. field byte slices are allocated in `arena`. 240 pub fn readByName( 241 io: std.Io, 242 allocator: std.mem.Allocator, 243 admin_repo_path: []const u8, 244 arena: *std.heap.ArenaAllocator, 245 name: []const u8, 246 ) !?Record { 247 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 248 error.RepoNotFound => return null, 249 else => |e| return e, 250 }; 251 defer repo.deinit(io, allocator); 252 253 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 254 255 const name_to_user_id_cursor = try moment.getCursor(hash.hashInt(evt.admin_repo_opts.hash, "name->user-id")) orelse return null; 256 const name_to_user_id = try evt.AdminDB.HashMap(.read_only).init(name_to_user_id_cursor); 257 const user_id_cursor = try name_to_user_id.getCursor(hash.hashInt(evt.admin_repo_opts.hash, name)) orelse return null; 258 var user_id: [evt.event_id_size]u8 = undefined; 259 _ = try user_id_cursor.readBytes(&user_id); 260 261 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 262 } 263 264 // flip a user's ANSI-art preference by re-emitting their User event with 265 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 266 pub fn toggleAnsi( 267 comptime repo_opts: rp.RepoOpts(.xit), 268 io: std.Io, 269 allocator: std.mem.Allocator, 270 repo: *rp.Repo(.xit, repo_opts), 271 user_id: []const u8, 272 ) !void { 273 const DB = rp.Repo(.xit, repo_opts).DB; 274 var arena = std.heap.ArenaAllocator.init(allocator); 275 defer arena.deinit(); 276 277 const moment = try evt.currentMoment(repo_opts, repo); 278 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 279 280 var updated = user.event; 281 updated.enable_ansi = !updated.enable_ansi; 282 try evt.consume(.admin, .xit, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 283 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 284 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 285 .author = .{ .name = user.event.name, .email = user.event.email }, 286 .event = .{ .user = updated }, 287 }}); 288 } 289