╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Comment.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
115 ) ![evt.event_id_size * 2]u8 { 116 if (!fieldsValid(body)) return error.InvalidFields; 117 118 // a reply's parent is the thread itself or one of its comments 119 const thread_id_bytes = evt.parseEventId(thread_id) catch return error.NotFound; 120 const parent_id_bytes = evt.parseEventId(parent_id) catch return error.NotFound; 121 const parent: ?evt.ThreadChild = if (std.mem.eql(u8, thread_id, parent_id)) null else .{ .comment = &parent_id_bytes }; 122 if (!try evt.threadHolds(repo_kind, repo_opts, io, allocator, repo, thread_kind, &thread_id_bytes, parent)) return error.NotFound; 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(host, .repo, 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 host: evt.Host, 144 comptime repo_kind: rp.RepoKind, 145 comptime repo_opts: rp.RepoOpts(repo_kind), 146 io: std.Io, 147 allocator: std.mem.Allocator, 148 repo: *rp.Repo(repo_kind, repo_opts), 149 thread_kind: evt.EventKind, 150 thread_id: *const [evt.event_id_size * 2]u8, 151 comment_id: *const [evt.event_id_size]u8, 152 body: []const u8, 153 author: evt.CommitAuthor, 154 ) !void { 155 if (!fieldsValid(body)) return error.InvalidFields; 156 157 const thread_id_bytes = evt.parseEventId(thread_id) catch return error.NotFound; 158 if (!try evt.threadHolds(repo_kind, repo_opts, io, allocator, repo, thread_kind, &thread_id_bytes, .{ .comment = comment_id })) return error.NotFound; 159 160 var arena = std.heap.ArenaAllocator.init(allocator); 161 defer arena.deinit(); 162 163 const comment = blk: { 164 const DB = evt.EventDB(repo_opts.hash); 165 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; 166 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 167 const moment = (if (event_db_maybe) |*event_db| 168 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 169 else if (repo_kind == .git) 170 return error.NotFound 171 else 172 evt.currentMoment(repo_opts, repo)) catch return error.NotFound; 173 break :blk (try readById(DB, repo_opts.hash, moment, &arena, comment_id)) orelse return error.NotFound; 174 }; 175 if (comment.removed) return error.NotFound; 176 177 var updated = comment.event; 178 updated.body = body; 179 try evt.consume(host, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{.{ 180 .id = std.fmt.bytesToHex(comment_id.*, .lower), 181 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 182 .author = author, 183 .event = .{ .comment = updated }, 184 }}); 185 } 186 187 // read a comment by event id, or null if the id isn't a known comment. field 188 // byte slices are allocated in `arena`. 189 pub fn readById( 190 comptime DB: type, 191 comptime hash_kind: hash.HashKind, 192 haxy_moment: DB.HashMap(.read_only), 193 arena: *std.heap.ArenaAllocator, 194 comment_id: []const u8, 195 ) !?Record { 196 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 197 const records = try DB.HashMap(.read_only).init(records_cursor); 198 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, comment_id)) orelse return null; 199 const record_map = try DB.HashMap(.read_only).init(record_cursor); 200 return try evt.read(Record, DB, hash_kind, arena, record_map); 201 } 202