╭ Comment.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const ui = @import("../../ui.zig"); 4 const xit = @import("xit"); 5 const hash = xit.hash; 6 const xitui = xit.xitui; 7 const wgt = xitui.widget; 8 const layout = xitui.layout; 9 const Key = xitui.input.Key; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 13 pub const page_size = 10; 14 15 pub const CommentWithId = struct { 16 id: [evt.event_id_size * 2]u8, 17 comment: evt.Comment.Record, 18 author: ui.Author = .unknown, 19 parent_author: ?ui.Author = null, 20 }; 21 22 pub const Window = struct { 23 comments: []const CommentWithId, 24 start: usize, 25 count: usize, 26 27 pub const empty: Window = .{ .comments = &.{}, .start = 0, .count = 0 }; 28 }; 29 30 pub const Permalink = struct { 31 selected: CommentWithId, 32 replies: Window, 33 }; 34 35 // read a comment permalink and one window of its immediate replies. 36 pub fn init( 37 comptime hash_kind: hash.HashKind, 38 arena: *std.heap.ArenaAllocator, 39 admin_moment: ?evt.AdminDB.HashMap(.read_only), 40 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 41 thread_id: []const u8, 42 selected_id: []const u8, 43 start: usize, 44 ) !Permalink { 45 const selected = (try readOne(hash_kind, arena, admin_moment, haxy_moment, selected_id)) orelse return error.NotFound; 46 if (!std.mem.eql(u8, &selected.comment.event.thread_id, thread_id)) return error.NotFound; 47 48 return .{ 49 .selected = selected, 50 .replies = try loadWindow(hash_kind, arena, admin_moment, haxy_moment, evt.Comment.parent_id_to_comment_id_set_key, selected_id, start), 51 }; 52 } 53 54 // read one chronological comment window from an id-to-comment-set index. 55 pub fn loadWindow( 56 comptime hash_kind: hash.HashKind, 57 arena: *std.heap.ArenaAllocator, 58 admin_moment: ?evt.AdminDB.HashMap(.read_only), 59 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 60 index_key: []const u8, 61 owner_id: []const u8, 62 start: usize, 63 ) !Window { 64 const DB = evt.EventDB(hash_kind); 65 const index_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, index_key)) orelse return .{ 66 .comments = &.{}, 67 .start = start, 68 .count = 0, 69 }; 70 const index = try DB.HashMap(.read_only).init(index_cursor); 71 const set_cursor = try index.getCursor(hash.hashInt(hash_kind, owner_id)) orelse return .{ 72 .comments = &.{}, 73 .start = start, 74 .count = 0, 75 }; 76 const set = try DB.SortedSet(.read_only).init(set_cursor); 77 const count: usize = @intCast(try set.count()); 78 const end = start + @min(page_size, count -| start); 79 var iter = try set.iteratorFromIndex(start); 80 var comments: std.ArrayList(CommentWithId) = .empty; 81 var i = start; 82 while (i < end) : (i += 1) { 83 var cursor = (try iter.next()) orelse break; 84 const kv = try cursor.readKeyValuePair(); 85 var order_key: [@sizeOf(u64) + evt.event_id_size]u8 = undefined; 86 _ = try kv.key_cursor.readBytes(&order_key); 87 const id_bytes = order_key[@sizeOf(u64)..].*; 88 const entry = (try readOneBytes(hash_kind, arena, admin_moment, haxy_moment, &id_bytes)) orelse continue; 89 try comments.append(arena.allocator(), entry); 90 } 91 return .{ 92 .comments = comments.items, 93 .start = start, 94 .count = count, 95 }; 96 } 97 98 fn readOne( 99 comptime hash_kind: hash.HashKind, 100 arena: *std.heap.ArenaAllocator, 101 admin_moment: ?evt.AdminDB.HashMap(.read_only), 102 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 103 id: []const u8, 104 ) !?CommentWithId { 105 const bytes = idBytes(id) orelse return null; 106 return readOneBytes(hash_kind, arena, admin_moment, haxy_moment, &bytes); 107 } 108 109 fn readOneBytes( 110 comptime hash_kind: hash.HashKind, 111 arena: *std.heap.ArenaAllocator, 112 admin_moment: ?evt.AdminDB.HashMap(.read_only), 113 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 114 id_bytes: *const [evt.event_id_size]u8, 115 ) !?CommentWithId { 116 const DB = evt.EventDB(hash_kind); 117 const comment = (try evt.Comment.readById(DB, hash_kind, haxy_moment, arena, id_bytes)) orelse return null; 118 const parent_author: ?ui.Author = if (!std.mem.eql(u8, &comment.event.parent_id, &comment.event.thread_id)) blk: { 119 const parent_bytes = idBytes(&comment.event.parent_id) orelse break :blk null; 120 const parent = (try evt.readRecordSubset(evt.Comment, struct { author_email: ?[]const u8 }, DB, hash_kind, haxy_moment, arena, &parent_bytes)) orelse break :blk null; 121 break :blk try ui.Author.initFromEmail(admin_moment, arena, parent.author_email); 122 } else null; 123 return .{ 124 .id = std.fmt.bytesToHex(id_bytes.*, .lower), 125 .comment = comment, 126 .author = try ui.Author.initFromEmail(admin_moment, arena, comment.author_email), 127 .parent_author = parent_author, 128 }; 129 } 130 131 fn idBytes(id: []const u8) ?[evt.event_id_size]u8 { 132 if (id.len != evt.event_id_size * 2) return null; 133 var bytes: [evt.event_id_size]u8 = undefined; 134 _ = std.fmt.hexToBytes(&bytes, id) catch return null; 135 if (!std.mem.eql(u8, id, &std.fmt.bytesToHex(bytes, .lower))) return null; 136 return bytes; 137 } 138 139 pub const Item = struct { 140 box: wgt.Box(ui.Widget), 141 142 const metadata_index: usize = 0; 143 const body_index: usize = 1; 144 const gap_index: usize = 2; 145 const metadata_first_link_index: usize = 1; 146 147 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, identity: []const u8, thread_kind: evt.EventKind, entry: CommentWithId) !Item { 148 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 149 errdefer box.deinit(allocator); 150 151 { 152 var bar = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 153 errdefer bar.deinit(allocator); 154 155 var spacer = try ui.widget.Spacer.init(allocator); 156 errdefer spacer.deinit(allocator); 157 try bar.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 158 159 var author = try ui.authorBox(allocator, session.page_arena, entry.author); 160 errdefer author.deinit(allocator); 161 try bar.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null, .flex = .shrink }); 162 163 var reply = try linkBox(allocator, session, "new reply", commentNewRoute(thread_kind, identity, &entry.comment.event.thread_id, &entry.id) orelse return error.RouteTooLong); 164 errdefer reply.deinit(allocator); 165 try bar.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = .{ .width = "new reply".len + 2, .height = null } }); 166 167 if (!entry.comment.removed) { 168 var edit = try linkBox(allocator, session, "edit comment", commentEditRoute(thread_kind, identity, &entry.comment.event.thread_id, &entry.id) orelse return error.RouteTooLong); 169 errdefer edit.deinit(allocator); 170 try bar.children.put(allocator, edit.getFocus().id, .{ .widget = .{ .text_box = edit }, .rect = null, .min_size = .{ .width = "edit comment".len + 2, .height = null } }); 171 } 172 173 var permalink = try linkBox(allocator, session, "permalink", commentsRoute(thread_kind, identity, &entry.comment.event.thread_id, &entry.id, 0) orelse return error.RouteTooLong); 174 errdefer permalink.deinit(allocator); 175 try bar.children.put(allocator, permalink.getFocus().id, .{ .widget = .{ .text_box = permalink }, .rect = null, .min_size = .{ .width = "permalink".len + 2, .height = null } }); 176 177 if (!std.mem.eql(u8, &entry.comment.event.parent_id, &entry.comment.event.thread_id)) { 178 const parent_text = if (entry.parent_author) |parent_author| switch (parent_author) { 179 .unknown => "", 180 .email, .user_name => |text| text, 181 } else ""; 182 var parent = try wgt.TextBox.init(allocator, parent_text, .{ 183 .border_style = .single, 184 .round_corners = true, 185 .wrap_kind = .none, 186 .label = " replying to ", 187 }); 188 errdefer parent.deinit(allocator); 189 parent.getFocus().mode = .all; 190 const route = commentsRoute(thread_kind, identity, &entry.comment.event.thread_id, &entry.comment.event.parent_id, 0) orelse return error.RouteTooLong; 191 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try route.toUrl(session.page_arena)}) }; 192 try bar.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = @max(parent_text.len, " replying to ".len) + 2, .height = null } }); 193 } 194 195 if (!entry.comment.removed) { 196 var remove = try linkBox(allocator, session, "✕", removeRoute(thread_kind, identity, &entry.comment.event.thread_id, &entry.id) orelse return error.RouteTooLong); 197 errdefer remove.deinit(allocator); 198 try bar.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 199 } 200 201 bar.getFocus().child_id = bar.children.keys()[metadata_first_link_index]; 202 try box.children.put(allocator, bar.getFocus().id, .{ .widget = .{ .box = bar }, .rect = null, .min_size = null }); 203 } 204 205 const body_text = if (entry.comment.removed) "(removed)" else entry.comment.event.body; 206 var body_box = try wgt.TextBox.init(allocator, body_text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .word }); 207 errdefer body_box.deinit(allocator); 208 body_box.getFocus().mode = .all; 209 try box.children.put(allocator, body_box.getFocus().id, .{ .widget = .{ .text_box = body_box }, .rect = null, .min_size = null }); 210 211 var gap = try wgt.Text.init(allocator, ""); 212 errdefer gap.deinit(allocator); 213 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 214 215 box.getFocus().child_id = box.children.keys()[metadata_index]; 216 217 return .{ .box = box }; 218 } 219 220 fn metadata(self: *Item) *wgt.Box(ui.Widget) { 221 return &self.box.children.values()[metadata_index].widget.box; 222 } 223 224 fn body(self: *Item) *wgt.TextBox { 225 return &self.box.children.values()[body_index].widget.text_box; 226 } 227 228 pub fn bodyFocused(self: *Item) bool { 229 return self.box.getFocus().child_id == self.body().getFocus().id; 230 } 231 232 pub fn focusMetadata(self: *Item, root_focus: *Focus) void { 233 const bar = self.metadata(); 234 root_focus.setFocus(bar.getFocus().child_id orelse bar.children.keys()[metadata_first_link_index]); 235 } 236 237 pub fn focusBody(self: *Item, root_focus: *Focus) void { 238 root_focus.setFocus(self.body().getFocus().id); 239 } 240 241 pub fn moveHorizontal(self: *Item, root_focus: *Focus, right: bool) bool { 242 if (self.bodyFocused()) return false; 243 const bar = self.metadata(); 244 const selected = bar.getFocus().child_id orelse return false; 245 const selected_index = bar.children.getIndex(selected) orelse return false; 246 const target = if (right) selected_index + 1 else selected_index -| 1; 247 if (target < metadata_first_link_index or target >= bar.children.count()) return false; 248 root_focus.setFocus(bar.children.keys()[target]); 249 return true; 250 } 251 252 pub fn rowRect(self: *Item, body_row: bool) ?layout.IRect { 253 if (!body_row) return self.box.children.values()[metadata_index].rect; 254 var rect = self.box.children.values()[body_index].rect orelse return null; 255 if (self.box.children.values()[gap_index].rect) |gap| rect.size.height += gap.size.height; 256 return rect; 257 } 258 259 pub fn deinit(self: *Item, allocator: std.mem.Allocator) void { 260 self.box.deinit(allocator); 261 } 262 263 pub fn build(self: *Item, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 264 try self.box.build(allocator, constraint, root_focus); 265 } 266 267 pub fn input(self: *Item, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 268 try self.box.input(allocator, key, root_focus); 269 } 270 271 pub fn clearGrid(self: *Item) void { 272 self.box.clearGrid(); 273 } 274 275 pub fn getGrid(self: Item) ?Grid { 276 return self.box.getGrid(); 277 } 278 279 pub fn getFocus(self: *Item) *Focus { 280 return self.box.getFocus(); 281 } 282 }; 283 284 // append one comment's metadata bar and body. 285 pub fn appendComment(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, identity: []const u8, thread_kind: evt.EventKind, entry: CommentWithId) !void { 286 var item = try Item.init(allocator, session, identity, thread_kind, entry); 287 errdefer item.deinit(allocator); 288 try box.children.put(allocator, item.getFocus().id, .{ .widget = .{ .repo_comment = item }, .rect = null, .min_size = null }); 289 } 290 291 pub fn appendCount(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), count: usize, singular: []const u8, plural: []const u8) !void { 292 var text_buf: [64]u8 = undefined; 293 const text = try std.fmt.bufPrint(&text_buf, "{d} {s}", .{ count, if (count == 1) singular else plural }); 294 var count_box = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .wrap_kind = .none }); 295 errdefer count_box.deinit(allocator); 296 try box.children.put(allocator, count_box.getFocus().id, .{ .widget = .{ .text_box = count_box }, .rect = null, .min_size = null }); 297 } 298 299 pub fn appendWindowNav(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, identity: []const u8, thread_kind: evt.EventKind, thread_id: []const u8, selected_id: ?[]const u8, window: Window) !void { 300 const has_prev = window.start > 0; 301 const has_more = window.start < window.count and window.count - window.start > page_size; 302 if (!has_prev and !has_more) return; 303 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 304 errdefer row.deinit(allocator); 305 if (has_prev) { 306 const start = window.start -| page_size; 307 const route = commentsRoute(thread_kind, identity, thread_id, selected_id, start); 308 var previous = try linkBox(allocator, session, "← previous", route orelse return error.RouteTooLong); 309 errdefer previous.deinit(allocator); 310 try row.children.put(allocator, previous.getFocus().id, .{ .widget = .{ .text_box = previous }, .rect = null, .min_size = null }); 311 } 312 if (has_more) { 313 const start = window.start + page_size; 314 const route = commentsRoute(thread_kind, identity, thread_id, selected_id, start); 315 var next = try linkBox(allocator, session, "next →", route orelse return error.RouteTooLong); 316 errdefer next.deinit(allocator); 317 try row.children.put(allocator, next.getFocus().id, .{ .widget = .{ .text_box = next }, .rect = null, .min_size = null }); 318 } 319 row.getFocus().child_id = row.children.keys()[0]; 320 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 321 } 322 323 fn commentsRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, selected_id: ?[]const u8, start: usize) ?ui.RoutablePage { 324 return if (selected_id) |id| 325 ui.RoutablePage.repoThreadCommentRoute(kind, identity, thread_id, id, start) 326 else 327 ui.RoutablePage.repoThreadCommentsRoute(kind, identity, thread_id, start); 328 } 329 330 fn commentNewRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, parent_id: []const u8) ?ui.RoutablePage { 331 return ui.RoutablePage.repoThreadCommentNewRoute(kind, identity, thread_id, parent_id); 332 } 333 334 fn commentEditRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?ui.RoutablePage { 335 return ui.RoutablePage.repoThreadCommentEditRoute(kind, identity, thread_id, comment_id); 336 } 337 338 fn removeRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?ui.RoutablePage { 339 return ui.RoutablePage.repoThreadRemoveRoute(kind, identity, thread_id, comment_id); 340 } 341 342 pub fn linkBox(allocator: std.mem.Allocator, session: *ui.Session, text: []const u8, route: ui.RoutablePage) !wgt.TextBox { 343 var box = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 344 errdefer box.deinit(allocator); 345 box.getFocus().mode = .all; 346 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try route.toUrl(session.page_arena)}) }; 347 return box; 348 } 349