╭ 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.Comment.readById(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 145 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, identity: []const u8, entry: CommentWithId) !Item { 146 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 147 errdefer box.deinit(allocator); 148 149 { 150 var bar = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 151 errdefer bar.deinit(allocator); 152 153 var author = try ui.authorBox(allocator, session.page_arena, entry.author); 154 errdefer author.deinit(allocator); 155 try bar.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null, .shrink = true }); 156 157 var reply = try linkBox(allocator, session, "new reply", ui.RoutablePage.repoCommentNewRoute(identity, &entry.comment.event.thread_id, &entry.id) orelse return error.RouteTooLong); 158 errdefer reply.deinit(allocator); 159 try bar.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = .{ .width = "new reply".len + 2, .height = null } }); 160 161 var permalink = try linkBox(allocator, session, "permalink", ui.RoutablePage.repoCommentsRoute(identity, &entry.comment.event.thread_id, &entry.id, 0) orelse return error.RouteTooLong); 162 errdefer permalink.deinit(allocator); 163 try bar.children.put(allocator, permalink.getFocus().id, .{ .widget = .{ .text_box = permalink }, .rect = null, .min_size = .{ .width = "permalink".len + 2, .height = null } }); 164 165 if (!std.mem.eql(u8, &entry.comment.event.parent_id, &entry.comment.event.thread_id)) { 166 const parent_text = if (entry.parent_author) |parent_author| switch (parent_author) { 167 .unknown => "", 168 .email, .user_name => |text| text, 169 } else ""; 170 var parent = try wgt.TextBox(ui.Widget).init(allocator, parent_text, .{ 171 .border_style = .single, 172 .rounded_corners = true, 173 .wrap_kind = .none, 174 .label = " replying to ", 175 }); 176 errdefer parent.deinit(allocator); 177 parent.getFocus().focusable = true; 178 const route = ui.RoutablePage.repoCommentsRoute(identity, &entry.comment.event.thread_id, &entry.comment.event.parent_id, 0) orelse return error.RouteTooLong; 179 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try route.toUrl(session.page_arena)}) }; 180 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 } }); 181 } 182 183 bar.getFocus().child_id = bar.children.keys()[0]; 184 try box.children.put(allocator, bar.getFocus().id, .{ .widget = .{ .box = bar }, .rect = null, .min_size = null }); 185 } 186 187 const body_text = if (entry.comment.deleted) "(deleted)" else entry.comment.event.body; 188 var body_box = try wgt.TextBox(ui.Widget).init(allocator, body_text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word }); 189 errdefer body_box.deinit(allocator); 190 body_box.getFocus().focusable = true; 191 try box.children.put(allocator, body_box.getFocus().id, .{ .widget = .{ .text_box = body_box }, .rect = null, .min_size = null }); 192 box.getFocus().child_id = box.children.keys()[metadata_index]; 193 194 return .{ .box = box }; 195 } 196 197 fn metadata(self: *Item) *wgt.Box(ui.Widget) { 198 return &self.box.children.values()[metadata_index].widget.box; 199 } 200 201 fn body(self: *Item) *wgt.TextBox(ui.Widget) { 202 return &self.box.children.values()[body_index].widget.text_box; 203 } 204 205 pub fn bodyFocused(self: *Item) bool { 206 return self.box.getFocus().child_id == self.body().getFocus().id; 207 } 208 209 pub fn focusMetadata(self: *Item, root_focus: *Focus) void { 210 const bar = self.metadata(); 211 root_focus.setFocus(bar.getFocus().child_id orelse bar.children.keys()[0]); 212 } 213 214 pub fn focusBody(self: *Item, root_focus: *Focus) void { 215 root_focus.setFocus(self.body().getFocus().id); 216 } 217 218 pub fn moveHorizontal(self: *Item, root_focus: *Focus, right: bool) bool { 219 if (self.bodyFocused()) return false; 220 const bar = self.metadata(); 221 const selected = bar.getFocus().child_id orelse return false; 222 const selected_index = bar.children.getIndex(selected) orelse return false; 223 const target = if (right) selected_index + 1 else selected_index -| 1; 224 if (target == selected_index or target >= bar.children.count()) return false; 225 root_focus.setFocus(bar.children.keys()[target]); 226 return true; 227 } 228 229 pub fn rowRect(self: *Item, body_row: bool) ?layout.IRect { 230 return self.box.children.values()[if (body_row) body_index else metadata_index].rect; 231 } 232 233 pub fn deinit(self: *Item, allocator: std.mem.Allocator) void { 234 self.box.deinit(allocator); 235 } 236 237 pub fn build(self: *Item, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 238 try self.box.build(allocator, constraint, root_focus); 239 } 240 241 pub fn input(self: *Item, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 242 try self.box.input(allocator, key, root_focus); 243 } 244 245 pub fn clearGrid(self: *Item) void { 246 self.box.clearGrid(); 247 } 248 249 pub fn getGrid(self: Item) ?Grid { 250 return self.box.getGrid(); 251 } 252 253 pub fn getFocus(self: *Item) *Focus { 254 return self.box.getFocus(); 255 } 256 }; 257 258 // append one comment's metadata bar and body. 259 pub fn appendComment(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, identity: []const u8, entry: CommentWithId) !void { 260 var item = try Item.init(allocator, session, identity, entry); 261 errdefer item.deinit(allocator); 262 try box.children.put(allocator, item.getFocus().id, .{ .widget = .{ .repo_comment = item }, .rect = null, .min_size = null }); 263 } 264 265 pub fn appendCount(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), count: usize, singular: []const u8, plural: []const u8, arena: std.mem.Allocator) !void { 266 const text = try std.fmt.allocPrint(arena, "{d} {s}", .{ count, if (count == 1) singular else plural }); 267 var count_box = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .wrap_kind = .none }); 268 errdefer count_box.deinit(allocator); 269 try box.children.put(allocator, count_box.getFocus().id, .{ .widget = .{ .text_box = count_box }, .rect = null, .min_size = null }); 270 } 271 272 pub fn appendWindowNav(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, identity: []const u8, thread_id: []const u8, selected_id: ?[]const u8, window: Window) !void { 273 const has_prev = window.start > 0; 274 const has_more = window.start < window.count and window.count - window.start > page_size; 275 if (!has_prev and !has_more) return; 276 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 277 errdefer row.deinit(allocator); 278 if (has_prev) { 279 const start = window.start -| page_size; 280 const route = if (selected_id) |id| 281 ui.RoutablePage.repoCommentsRoute(identity, thread_id, id, start) 282 else 283 ui.RoutablePage.repoIssueCommentsRoute(identity, thread_id, start); 284 var previous = try linkBox(allocator, session, "← previous", route orelse return error.RouteTooLong); 285 errdefer previous.deinit(allocator); 286 try row.children.put(allocator, previous.getFocus().id, .{ .widget = .{ .text_box = previous }, .rect = null, .min_size = null }); 287 } 288 if (has_more) { 289 const start = window.start + page_size; 290 const route = if (selected_id) |id| 291 ui.RoutablePage.repoCommentsRoute(identity, thread_id, id, start) 292 else 293 ui.RoutablePage.repoIssueCommentsRoute(identity, thread_id, start); 294 var next = try linkBox(allocator, session, "next →", route orelse return error.RouteTooLong); 295 errdefer next.deinit(allocator); 296 try row.children.put(allocator, next.getFocus().id, .{ .widget = .{ .text_box = next }, .rect = null, .min_size = null }); 297 } 298 row.getFocus().child_id = row.children.keys()[0]; 299 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 300 } 301 302 pub fn linkBox(allocator: std.mem.Allocator, session: *ui.Session, text: []const u8, route: ui.RoutablePage) !wgt.TextBox(ui.Widget) { 303 var box = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 304 errdefer box.deinit(allocator); 305 box.getFocus().focusable = true; 306 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try route.toUrl(session.page_arena)}) }; 307 return box; 308 } 309