╭ changed ────────────╮ │src/event/Comment.zig│ ╰─────────────────────╯ ╭───────────╮ │← all files│ ╰───────────╯ ╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │149 } }, │ │150 }}); │ │151 return event_id; │ │152 +} │ │153 + │ │154 +// replace a comment's body while preserving its thread and parent. `repo` must │ │155 +// be writable. │ │156 +pub fn update( │ │157 + comptime repo_kind: rp.RepoKind, │ │158 + comptime repo_opts: rp.RepoOpts(repo_kind), │ │159 + io: std.Io, │ │160 + allocator: std.mem.Allocator, │ │161 + repo: *rp.Repo(repo_kind, repo_opts), │ │162 + thread_id: *const [evt.event_id_size * 2]u8, │ │163 + comment_id: *const [evt.event_id_size]u8, │ │164 + body: []const u8, │ │165 + author_email: []const u8, │ │166 +) !void { │ │167 + if (!fieldsValid(body)) return error.InvalidFields; │ │168 + │ │169 + var arena = std.heap.ArenaAllocator.init(allocator); │ │170 + defer arena.deinit(); │ │171 + │ │172 + const DB = evt.EventDB(repo_opts.hash); │ │173 + 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;│ │174 + defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); │ │175 + const moment = (if (event_db_maybe) |*event_db| │ │176 + evt.currentMomentFromDb(repo_opts.hash, event_db.db) │ │177 + else if (repo_kind == .git) │ │178 + return error.NotFound │ │179 + else │ │180 + evt.currentMoment(repo_opts, repo)) catch return error.NotFound; │ │181 + const comment = (try readById(DB, repo_opts.hash, moment, &arena, comment_id)) orelse return error.NotFound; │ │182 + if (!std.mem.eql(u8, &comment.event.thread_id, thread_id)) return error.NotFound; │ │183 + │ │184 + var updated = comment.event; │ │185 + updated.body = body; │ │186 + try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{.{ │ │187 + .id = std.fmt.bytesToHex(comment_id.*, .lower), │ │188 + .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), │ │189 + .author_email = author_email, │ │190 + .event = .{ .comment = updated }, │ │191 + }}); │ │192 } │ │193 │ │194 // read a comment by event id, or null if the id isn't a known comment. field │ ╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯