╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Comment.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
20 21 // the moment keys `evt.merge` reads and writes for this kind 22 pub const merge_policy: evt.MergePolicy = .target_wins; 23 pub const record_map_key = "event-id->comment"; 24 pub const id_set_key = "comment-id-set"; 25 26 // the indexes the thread reads 27 pub const thread_id_to_comment_id_set_key = "thread-id->comment-id-set"; 28 pub const parent_id_to_comment_id_set_key = "parent-id->comment-id-set"; 29 30 pub fn fieldsValid(body: []const u8) bool { 31 return body.len != 0; 32 } 33 34 pub fn consume( 35 comptime DB: type, 36 comptime hash_kind: hash.HashKind, 37 haxy_moment: DB.HashMap(.read_write), 38 event_id: *const [evt.event_id_size]u8, 39 record_maybe: ?Record, 40 arena: *std.heap.ArenaAllocator, 41 _: ?[]const u8, 42 ) !void { 43 const comment_key = hash.hashInt(hash_kind, event_id); 44 45 const event_id_to_comment_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, record_map_key)); 46 const event_id_to_comment = try DB.HashMap(.read_write).init(event_id_to_comment_cursor); 47 48 const thread_id_to_comment_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, thread_id_to_comment_id_set_key)); 49 const thread_id_to_comment_id_set = try DB.HashMap(.read_write).init(thread_id_to_comment_id_set_cursor); 50 51 const parent_id_to_comment_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, parent_id_to_comment_id_set_key)); 52 const parent_id_to_comment_id_set = try DB.HashMap(.read_write).init(parent_id_to_comment_id_set_cursor); 53 54 var existing_record_maybe: ?Record = null; 55 const existing_cursor_maybe = try event_id_to_comment.getCursor(comment_key); 56 if (existing_cursor_maybe) |existing_cursor| { 57 const existing_comment = try DB.HashMap(.read_only).init(existing_cursor); 58 existing_record_maybe = try evt.read(Record, DB, hash_kind, arena, existing_comment); 59 } 60 61 var record_to_write = if (record_maybe) |record| 62 record 63 else blk: { 64 var record = existing_record_maybe orelse return error.EventNotFound; 65 record.removed = true; 66 break :blk record; 67 }; 68 69 const thread_id = try evt.parseEventId(&record_to_write.event.thread_id); 70 _ = try evt.parseEventId(&record_to_write.event.parent_id); 71 72 if (existing_record_maybe) |existing_record| { 73 // updates preserve the original creation order and author 74 record_to_write.created_order = existing_record.created_order; 75 record_to_write.author_email = existing_record.author_email; 76 77 // a comment cannot move between threads or positions in the thread 78 if (!std.mem.eql(u8, &existing_record.event.thread_id, &record_to_write.event.thread_id)) return error.ThreadChanged; 79 if (!std.mem.eql(u8, &existing_record.event.parent_id, &record_to_write.event.parent_id)) return error.ParentChanged; 80 } 81 82 const comment_cursor = try event_id_to_comment.putCursor(comment_key); 83 const comment = try DB.HashMap(.read_write).init(comment_cursor); 84 try evt.upsert(Record, DB, hash_kind, comment, record_to_write); 85 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .comment, record_to_write.created_order, record_to_write.removed); 86 87 if (existing_cursor_maybe == null) { 88 const comment_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key)); 89 const comment_id_set = try DB.SortedSet(.read_write).init(comment_id_set_cursor); 90 try comment_id_set.put(&evt.orderKeyDesc(record_to_write.created_order, event_id)); 91 try evt.touchThread(DB, hash_kind, haxy_moment, &thread_id, record_to_write.created_order, arena); 92 } 93 94 // removed comments stay in the thread so their replies remain connected 95 const thread_order_key = evt.orderKey(record_to_write.created_order, event_id); 96 const thread_comments_cursor = try thread_id_to_comment_id_set.putCursor(hash.hashInt(hash_kind, &record_to_write.event.thread_id)); 97 const thread_comments = try DB.SortedSet(.read_write).init(thread_comments_cursor); 98 try thread_comments.put(&thread_order_key); 99 100 const replies_cursor = try parent_id_to_comment_id_set.putCursor(hash.hashInt(hash_kind, &record_to_write.event.parent_id)); 101 const replies = try DB.SortedSet(.read_write).init(replies_cursor); 102 try replies.put(&thread_order_key); 103 } 104 105 // create a comment and return its event id. `repo` must be writable. 106 pub fn create( 107 comptime repo_kind: rp.RepoKind, 108 comptime repo_opts: rp.RepoOpts(repo_kind), 109 io: std.Io, 110 allocator: std.mem.Allocator, 111 repo: *rp.Repo(repo_kind, repo_opts), 112 thread_id: *const [evt.event_id_size * 2]u8, 113 parent_id: *const [evt.event_id_size * 2]u8, 114 body: []const u8, 115 author: evt.CommitAuthor, 116 ) ![evt.event_id_size * 2]u8 { 117 if (!fieldsValid(body)) return error.InvalidFields; 118 119 var id_bytes: [evt.event_id_size]u8 = undefined; 120 io.random(&id_bytes); 121 const event_id = std.fmt.bytesToHex(id_bytes, .lower); 122 try evt.consume(.repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{.{ 123 .id = event_id, 124 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 125 .author = author, 126 .event = .{ .comment = .{ 127 .thread_id = thread_id.*, 128 .parent_id = parent_id.*, 129 .body = body, 130 } }, 131 }}); 132 return event_id; 133 } 134 135 // replace a comment's body while preserving its thread and parent. `repo` must 136 // be writable. 137 pub fn update( 138 comptime repo_kind: rp.RepoKind, 139 comptime repo_opts: rp.RepoOpts(repo_kind), 140 io: std.Io, 141 allocator: std.mem.Allocator, 142 repo: *rp.Repo(repo_kind, repo_opts), 143 thread_id: *const [evt.event_id_size * 2]u8, 144 comment_id: *const [evt.event_id_size]u8, 145 body: []const u8, 146 author: evt.CommitAuthor, 147 ) !void { 148 if (!fieldsValid(body)) return error.InvalidFields; 149 150 var arena = std.heap.ArenaAllocator.init(allocator); 151 defer arena.deinit(); 152 153 const DB = evt.EventDB(repo_opts.hash); 154 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 155 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 156 const moment = (if (event_db_maybe) |*event_db| 157 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 158 else if (repo_kind == .git) 159 return error.NotFound 160 else 161 evt.currentMoment(repo_opts, repo)) catch return error.NotFound; 162 const comment = (try readById(DB, repo_opts.hash, moment, &arena, comment_id)) orelse return error.NotFound; 163 if (!std.mem.eql(u8, &comment.event.thread_id, thread_id)) return error.NotFound; 164 if (comment.removed) return error.NotFound; 165 166 var updated = comment.event; 167 updated.body = body; 168 try evt.consume(.repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{.{ 169 .id = std.fmt.bytesToHex(comment_id.*, .lower), 170 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 171 .author = author, 172 .event = .{ .comment = updated }, 173 }}); 174 } 175 176 // read a comment by event id, or null if the id isn't a known comment. field 177 // byte slices are allocated in `arena`. 178 pub fn readById( 179 comptime DB: type, 180 comptime hash_kind: hash.HashKind, 181 haxy_moment: DB.HashMap(.read_only), 182 arena: *std.heap.ArenaAllocator, 183 comment_id: []const u8, 184 ) !?Record { 185 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 186 const records = try DB.HashMap(.read_only).init(records_cursor); 187 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, comment_id)) orelse return null; 188 const record_map = try DB.HashMap(.read_only).init(record_cursor); 189 return try evt.read(Record, DB, hash_kind, arena, record_map); 190 } 191