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