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