╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ User.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
184 haxy_moment: DB.HashMap(.read_only), 185 arena: *std.heap.ArenaAllocator, 186 email: []const u8, 187 ) !?Public { 188 const email_to_user_id_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, email_to_user_id_key))) orelse return null; 189 const email_to_user_id = try DB.HashMap(.read_only).init(email_to_user_id_cursor); 190 const user_id_cursor = (try email_to_user_id.getCursor(hash.hashInt(hash_kind, email))) orelse return null; 191 var user_id: [evt.event_id_size]u8 = undefined; 192 _ = try user_id_cursor.readBytes(&user_id); 193 const user_map = try userMap(DB, hash_kind, haxy_moment, &user_id) orelse return null; 194 const record = try evt.read(struct { event: Public }, DB, hash_kind, arena, user_map); 195 return record.event; 196 } 197 198 // a user's id via the name->user-id index 199 pub fn readIdByName( 200 comptime DB: type, 201 comptime hash_kind: hash.HashKind, 202 haxy_moment: DB.HashMap(.read_only), 203 name: []const u8, 204 ) !?[evt.event_id_size]u8 { 205 const index_cursor = (try haxy_moment.getCursor(hash.hashInt(hash_kind, name_index_key))) orelse return null; 206 const index = try DB.SortedMap(.read_only).init(index_cursor); 207 const user_id_cursor = (try index.getCursor(name)) orelse return null; 208 var user_id: [evt.event_id_size]u8 = undefined; 209 _ = try user_id_cursor.readBytes(&user_id); 210 return user_id; 211 } 212 213 // a user's record map via the event-id->user index 214 fn userMap( 215 comptime DB: type, 216 comptime hash_kind: hash.HashKind, 217 haxy_moment: DB.HashMap(.read_only), 218 user_id: []const u8, 219 ) !?DB.HashMap(.read_only) { 220 const event_id_to_user_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->user")) orelse return null; 221 const event_id_to_user = try DB.HashMap(.read_only).init(event_id_to_user_cursor); 222 const user_cursor = try event_id_to_user.getCursor(hash.hashInt(hash_kind, user_id)) orelse return null; 223 return try DB.HashMap(.read_only).init(user_cursor); 224 } 225 226 // read a user by name from the admin event store, or null if the admin repo or 227 // the user doesn't exist. field byte slices are allocated in `arena`. 228 pub fn readByName( 229 io: std.Io, 230 allocator: std.mem.Allocator, 231 admin_repo_path: []const u8, 232 arena: *std.heap.ArenaAllocator, 233 name: []const u8, 234 ) !?Record { 235 var repo = rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_repo_path }) catch |err| switch (err) { 236 error.RepoNotFound => return null, 237 else => |e| return e, 238 }; 239 defer repo.deinit(io, allocator); 240 241 const moment = try evt.currentMoment(evt.admin_repo_opts, &repo); 242 243 const user_id = try readIdByName(evt.AdminDB, evt.admin_repo_opts.hash, moment, name) orelse return null; 244 245 return try readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &user_id); 246 } 247 248 // flip a user's ANSI-art preference by re-emitting their User event with 249 // enable_ansi negated. a no-op for an unknown user. `repo` must be writable. 250 pub fn toggleAnsi( 251 comptime repo_opts: rp.RepoOpts(.xit), 252 io: std.Io, 253 allocator: std.mem.Allocator, 254 repo: *rp.Repo(.xit, repo_opts), 255 users_dir: []const u8, 256 user_id: []const u8, 257 ) !void { 258 const DB = rp.Repo(.xit, repo_opts).DB; 259 var arena = std.heap.ArenaAllocator.init(allocator); 260 defer arena.deinit(); 261 262 const moment = try evt.currentMoment(repo_opts, repo); 263 const user = (try readById(DB, repo_opts.hash, moment, &arena, user_id)) orelse return; 264 265 var updated = user.event; 266 updated.enable_ansi = !updated.enable_ansi; 267 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, repo_opts, io, allocator, repo, evt.events_ref, &[_]evt.EventWithId{.{ 268 .id = std.fmt.bytesToHex(user_id[0..evt.event_id_size].*, .lower), 269 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 270 .author = .{ .name = user.event.name, .email = user.event.email }, 271 .event = .{ .user = updated }, 272 }}); 273 } 274