╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Repo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
21 22 pub const Access = enum { 23 private, 24 public, 25 }; 26 27 // what a user may do in a repo, ordered so a higher role includes the lower ones 28 pub const Role = enum { 29 none, 30 read, 31 write, 32 owner, 33 34 pub fn atLeast(self: Role, min: Role) bool { 35 return @intFromEnum(self) >= @intFromEnum(min); 36 } 37 38 // whether a user may change an event: anyone's with the write role, 39 // otherwise only one they authored 40 pub fn canModify(self: Role, user_email: []const u8, author_email_maybe: ?[]const u8) bool { 41 if (self.atLeast(.write)) return true; 42 const author_email = author_email_maybe orelse return false; 43 return self.atLeast(.read) and std.mem.eql(u8, user_email, author_email); 44 } 45 }; 46 47 pub const name_max_len = 32; 48 49 // the role `user_id_maybe` holds in the repo: its creator owns it, and anyone 50 // else gets the higher of the base role and their grant. `user_moment` is the 51 // owner's, which holds the grants. null is a logged-out user. 52 pub fn roleOf( 53 comptime DB: type, 54 comptime hash_kind: hash.HashKind, 55 user_moment: DB.HashMap(.read_only), 56 arena: *std.heap.ArenaAllocator, 57 record: Record, 58 repo_id: *const [evt.event_id_size]u8, 59 user_id_maybe: ?[evt.event_id_size]u8, 60 ) !Role { 61 // what the access fields give everyone, logged in or not 62 const base: Role = if (record.event.write_access == .public) .write else if (record.event.read_access == .public) .read else .none; 63 const user_id = user_id_maybe orelse return base; 64 if (std.mem.eql(u8, record.event.user_id, &user_id)) return .owner; 65 66 const granted = (try evt.Grant.readRole(DB, hash_kind, user_moment, arena, repo_id, &user_id)) orelse return base; 67 return if (granted.atLeast(base)) granted else base; 68 } 69 70 // the moment keys `evt.merge` reads and writes for this kind 71 pub const merge_policy: evt.MergePolicy = .target_wins; 72 pub const record_map_key = "event-id->repo"; 73 pub const all_id_set_key = "repo-id-set"; 74 pub const name_index_key = "name->repo-id"; 75 76 pub fn validateName(name: []const u8) !void { 77 if (name.len == 0) return error.NameEmpty; 78 if (name.len > name_max_len) return error.NameTooLong; 79 if (std.mem.eql(u8, name, ".") or std.mem.eql(u8, name, "..")) return error.InvalidName; 80 81 for (name) |c| { 82 if (!std.ascii.isAlphanumeric(c) and c != '-' and c != '_' and c != '.') 83 return error.InvalidName; 84 } 85 } 86 87 pub fn consume( 88 comptime DB: type, 89 comptime hash_kind: hash.HashKind, 90 haxy_moment: DB.HashMap(.read_write), 91 event_id: *const [evt.event_id_size]u8, 92 record_maybe: ?Record, 93 arena: *std.heap.ArenaAllocator, 94 _: ?[]const u8, 95 ) !void { 96 const repo_key = hash.hashInt(hash_kind, event_id); 97 98 const event_id_to_repo_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, "event-id->repo")); 99 const event_id_to_repo = try DB.HashMap(.read_write).init(event_id_to_repo_cursor); 100 101 const name_to_repo_id_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, name_index_key)); 102 const name_to_repo_id = try DB.SortedMap(.read_write).init(name_to_repo_id_cursor); 103 104 var existing_record_maybe: ?Record = null; 105 const existing_cursor_maybe = try event_id_to_repo.getCursor(repo_key); 106 if (existing_cursor_maybe) |existing_cursor| { 107 const existing_repo = try DB.HashMap(.read_only).init(existing_cursor); 108 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_repo); 109 } 110 111 var record_to_write = record_maybe orelse try evt.removedRecord(Record, DB, hash_kind, haxy_moment.readOnly(), existing_record_maybe); 112 113 if (!record_to_write.removed) try validateName(record_to_write.event.name); 114 115 if (existing_record_maybe) |existing_record| { 116 // updates preserve the original creation metadata 117 record_to_write.created_order = existing_record.created_order; 118 119 // drop the old active index; active values are re-added below 120 if (!existing_record.removed) { 121 _ = try name_to_repo_id.remove(existing_record.event.name); 122 } 123 } 124 125 const repo_cursor = try event_id_to_repo.putCursor(repo_key); 126 const repo = try DB.HashMap(.read_write).init(repo_cursor); 127 try evt.upsert(Record, DB, hash_kind, repo, record_to_write); 128 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .repo, existing_record_maybe, record_to_write); 129 130 const order_key = evt.orderKeyDesc(record_to_write.created_order, event_id); 131 132 // the id set retains removed records so merges can carry removals 133 if (existing_cursor_maybe == null) { 134 const repo_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, all_id_set_key)); 135 const repo_id_set = try DB.SortedSet(.read_write).init(repo_id_set_cursor); 136 try repo_id_set.put(&order_key); 137 } 138 139 if (!record_to_write.removed) { 140 try name_to_repo_id.put(record_to_write.event.name, .{ .bytes = event_id }); 141 } 142 } 143 144 // a repo plus its event id (the id is the on-disk repo directory name, so the 145 // caller can locate the working repo under <server>/repos/<hex event id>). 146 pub const RepoWithId = struct { 147 repo: Record, 148 event_id: [evt.event_id_size]u8, 149 }; 150 151 // a repo as one viewer sees it 152 pub const RepoWithRole = struct { 153 repo: Record, 154 event_id: [evt.event_id_size]u8, 155 role: Role, 156 }; 157 158 // read a repo by its name from its owner's user repo 159 pub fn readByName( 160 comptime DB: type, 161 comptime hash_kind: hash.HashKind, 162 user_moment: DB.HashMap(.read_only), 163 arena: *std.heap.ArenaAllocator, 164 name: []const u8, 165 ) !?RepoWithId { 166 const name_to_repo_id_cursor = try user_moment.getCursor(hash.hashInt(hash_kind, name_index_key)) orelse return null; 167 const name_to_repo_id = try DB.SortedMap(.read_only).init(name_to_repo_id_cursor); 168 const repo_id_cursor = try name_to_repo_id.getCursor(name) orelse return null; 169 var repo_id: [evt.event_id_size]u8 = undefined; 170 _ = try repo_id_cursor.readBytes(&repo_id); 171 const repo = (try readById(DB, hash_kind, user_moment, arena, &repo_id)) orelse return null; 172 return .{ .repo = repo, .event_id = repo_id }; 173 } 174 175 pub fn readById( 176 comptime DB: type, 177 comptime hash_kind: hash.HashKind, 178 haxy_moment: DB.HashMap(.read_only), 179 arena: *std.heap.ArenaAllocator, 180 id: []const u8, 181 ) !?Record { 182 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 183 const records = try DB.HashMap(.read_only).init(records_cursor); 184 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 185 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 186 } 187