╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Comment.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 // references use the same lower-case hex form as event ids in json 70 var thread_id: [evt.event_id_size]u8 = undefined; 71 _ = try std.fmt.hexToBytes(&thread_id, &record_to_write.event.thread_id); 72 if (!std.mem.eql(u8, &record_to_write.event.thread_id, &std.fmt.bytesToHex(thread_id, .lower))) return error.InvalidEventId; 73 var parent_id: [evt.event_id_size]u8 = undefined; 74 _ = try std.fmt.hexToBytes(&parent_id, &record_to_write.event.parent_id); 75 if (!std.mem.eql(u8, &record_to_write.event.parent_id, &std.fmt.bytesToHex(parent_id, .lower))) return error.InvalidEventId; 76 77 if (existing_record_maybe) |existing_record| { 78 // updates preserve the original creation order and author 79 record_to_write.created_order = existing_record.created_order; 80 record_to_write.author_email = existing_record.author_email; 81 82 // a comment cannot move between threads or positions in the thread 83 if (!std.mem.eql(u8, &existing_record.event.thread_id, &record_to_write.event.thread_id)) return error.ThreadChanged; 84 if (!std.mem.eql(u8, &existing_record.event.parent_id, &record_to_write.event.parent_id)) return error.ParentChanged; 85 } 86 87 const comment_cursor = try event_id_to_comment.putCursor(comment_key); 88 const comment = try DB.HashMap(.read_write).init(comment_cursor); 89 try evt.upsert(Record, DB, hash_kind, comment, record_to_write); 90 try evt.indexEvent(DB, hash_kind, haxy_moment, event_id, .comment, record_to_write.created_order, record_to_write.removed); 91 92 if (existing_cursor_maybe == null) { 93 const comment_id_set_cursor = try haxy_moment.putCursor(hash.hashInt(hash_kind, id_set_key)); 94 const comment_id_set = try DB.SortedSet(.read_write).init(comment_id_set_cursor); 95 try comment_id_set.put(&evt.orderKeyDesc(record_to_write.created_order, event_id)); 96 try evt.touchThread(DB, hash_kind, haxy_moment, &thread_id, record_to_write.created_order, arena); 97 } 98 99 // removed comments stay in the thread so their replies remain connected 100 const thread_order_key = evt.orderKey(record_to_write.created_order, event_id); 101 const thread_comments_cursor = try thread_id_to_comment_id_set.putCursor(hash.hashInt(hash_kind, &record_to_write.event.thread_id)); 102 const thread_comments = try DB.SortedSet(.read_write).init(thread_comments_cursor); 103 try thread_comments.put(&thread_order_key); 104 105 const replies_cursor = try parent_id_to_comment_id_set.putCursor(hash.hashInt(hash_kind, &record_to_write.event.parent_id)); 106 const replies = try DB.SortedSet(.read_write).init(replies_cursor); 107 try replies.put(&thread_order_key); 108 } 109 110 // create a comment and return its event id. `repo` must be writable. 111 pub fn create( 112 comptime repo_kind: rp.RepoKind, 113 comptime repo_opts: rp.RepoOpts(repo_kind), 114 io: std.Io, 115 allocator: std.mem.Allocator, 116 repo: *rp.Repo(repo_kind, repo_opts), 117 thread_id: *const [evt.event_id_size * 2]u8, 118 parent_id: *const [evt.event_id_size * 2]u8, 119 body: []const u8, 120 author: evt.CommitAuthor, 121 ) ![evt.event_id_size * 2]u8 { 122 if (!fieldsValid(body)) return error.InvalidFields; 123 124 var id_bytes: [evt.event_id_size]u8 = undefined; 125 io.random(&id_bytes); 126 const event_id = std.fmt.bytesToHex(id_bytes, .lower); 127 try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{.{ 128 .id = event_id, 129 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 130 .author = author, 131 .event = .{ .comment = .{ 132 .thread_id = thread_id.*, 133 .parent_id = parent_id.*, 134 .body = body, 135 } }, 136 }}); 137 return event_id; 138 } 139 140 // replace a comment's body while preserving its thread and parent. `repo` must 141 // be writable. 142 pub fn update( 143 comptime repo_kind: rp.RepoKind, 144 comptime repo_opts: rp.RepoOpts(repo_kind), 145 io: std.Io, 146 allocator: std.mem.Allocator, 147 repo: *rp.Repo(repo_kind, repo_opts), 148 thread_id: *const [evt.event_id_size * 2]u8, 149 comment_id: *const [evt.event_id_size]u8, 150 body: []const u8, 151 author: evt.CommitAuthor, 152 ) !void { 153 if (!fieldsValid(body)) return error.InvalidFields; 154 155 var arena = std.heap.ArenaAllocator.init(allocator); 156 defer arena.deinit(); 157 158 const DB = evt.EventDB(repo_opts.hash); 159 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; 160 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 161 const moment = (if (event_db_maybe) |*event_db| 162 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 163 else if (repo_kind == .git) 164 return error.NotFound 165 else 166 evt.currentMoment(repo_opts, repo)) catch return error.NotFound; 167 const comment = (try readById(DB, repo_opts.hash, moment, &arena, comment_id)) orelse return error.NotFound; 168 if (!std.mem.eql(u8, &comment.event.thread_id, thread_id)) return error.NotFound; 169 if (comment.removed) return error.NotFound; 170 171 var updated = comment.event; 172 updated.body = body; 173 try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{.{ 174 .id = std.fmt.bytesToHex(comment_id.*, .lower), 175 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 176 .author = author, 177 .event = .{ .comment = updated }, 178 }}); 179 } 180 181 // read a comment by event id, or null if the id isn't a known comment. field 182 // byte slices are allocated in `arena`. 183 pub fn readById( 184 comptime DB: type, 185 comptime hash_kind: hash.HashKind, 186 haxy_moment: DB.HashMap(.read_only), 187 arena: *std.heap.ArenaAllocator, 188 comment_id: []const u8, 189 ) !?Record { 190 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 191 const records = try DB.HashMap(.read_only).init(records_cursor); 192 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, comment_id)) orelse return null; 193 const record_map = try DB.HashMap(.read_only).init(record_cursor); 194 return try evt.read(Record, DB, hash_kind, arena, record_map); 195 } 196