╭ changed ────────────╮ │src/event/Comment.zig│ ╰─────────────────────╯ ╭───────────╮ │← all files│ ╰───────────╯ ╭────────────────────────────────────────╮ │1 const std = @import("std"); │ │2 const evt = @import("../event.zig"); │ │3 const xit = @import("xit"); │ │4 +const rp = xit.repo; │ │5 const hash = xit.hash; │ │6 │ │7 thread_id: [evt.event_id_size * 2]u8,│ ╰────────────────────────────────────────╯ ╭─────────────────────────────────────────────────────────────────────────────────────────╮ │120 const replies = try DB.SortedSet(.read_write).init(replies_cursor); │ │121 try replies.put(&thread_order_key); │ │122 } │ │123 + │ │124 +// create a comment and return its event id. `repo` must be writable. │ │125 +pub fn create( │ │126 + comptime repo_kind: rp.RepoKind, │ │127 + comptime repo_opts: rp.RepoOpts(repo_kind), │ │128 + io: std.Io, │ │129 + allocator: std.mem.Allocator, │ │130 + repo: *rp.Repo(repo_kind, repo_opts), │ │131 + thread_id: *const [evt.event_id_size * 2]u8, │ │132 + parent_id: *const [evt.event_id_size * 2]u8, │ │133 + body: []const u8, │ │134 + author_email: []const u8, │ │135 +) ![evt.event_id_size * 2]u8 { │ │136 + if (!fieldsValid(body)) return error.InvalidFields; │ │137 │ │138 + var id_bytes: [evt.event_id_size]u8 = undefined; │ │139 + io.random(&id_bytes); │ │140 + const event_id = std.fmt.bytesToHex(id_bytes, .lower); │ │141 + try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{.{│ │142 + .id = event_id, │ │143 + .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), │ │144 + .author_email = author_email, │ │145 + .event = .{ .comment = .{ │ │146 + .thread_id = thread_id.*, │ │147 + .parent_id = parent_id.*, │ │148 + .body = body, │ │149 + } }, │ │150 + }}); │ │151 + return event_id; │ │152 +} │ │153 + │ │154 // read a comment by event id, or null if the id isn't a known comment. field │ │155 // byte slices are allocated in `arena`. │ │156 pub fn readById( │ ╰─────────────────────────────────────────────────────────────────────────────────────────╯