╭ Discussions.zig ──────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const xit = @import("xit"); 6 const rp = xit.repo; 7 const hash = xit.hash; 8 const Comment = @import("Comment.zig"); 9 const Attachment = @import("Attachment.zig"); 10 const thread = ui.widget.thread; 11 12 const wasm = builtin.target.cpu.arch == .wasm32; 13 14 // how many discussions one window shows before a "next" link appears. 15 pub const page_size = 20; 16 17 // how many labels the labels view shows at most. 18 pub const max_labels = 1000; 19 20 // one discussion from the repo's consumed event database, with its hex event id 21 // (the id lives in the event envelope, not the payload). 22 pub const DiscussionWithId = struct { 23 id: []const u8, 24 record: evt.Discussion.Record, 25 author: ui.Author = .unknown, 26 comments: Comment.Window = .empty, 27 attachments: []const Attachment.WithId = &.{}, 28 }; 29 30 pub const Entry = DiscussionWithId; 31 32 pub const Window = struct { 33 items: []const DiscussionWithId, 34 prev_id: ?[]const u8, 35 next_id: ?[]const u8, 36 count: usize, 37 // whether a search stopped counting at its cap, so `count` is a floor. 38 more: bool = false, 39 40 pub const empty: Window = .{ .items = &.{}, .prev_id = null, .next_id = null, .count = 0 }; 41 }; 42 43 identity: []const u8, 44 label: []const u8, 45 // the decoded query the list is filtered to, or null when unfiltered. 46 search: ?[]const u8 = null, 47 selected_id: []const u8, 48 comment_id: []const u8, 49 comments_start: usize, 50 comment_page: ?Comment.Permalink = null, 51 recent: Window, 52 view: ui.RoutablePage.DiscussionsView, 53 description_page: bool = false, 54 labels: []const []const u8, 55 repo_source: ?ui.RepoSource = null, 56 57 const Self = @This(); 58 59 pub const Event = evt.Discussion; 60 pub const Status = enum { recent }; 61 pub const ViewKind = ui.RoutablePage.DiscussionsView; 62 pub const thread_name = "discussion"; 63 pub const header_widget_name = "repo_discussions_header"; 64 65 pub fn listRoute(identity: []const u8, _: Status, label: []const u8, selected: []const u8) ?ui.RoutablePage { 66 return ui.RoutablePage.repoDiscussionsRoute(identity, label, selected); 67 } 68 69 pub fn selectedThread(self: *const Self) ?*const DiscussionWithId { 70 if (self.selected_id.len == 0) return null; 71 for (self.recent.items) |*entry| { 72 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 73 } 74 return null; 75 } 76 77 pub fn window(self: *const Self, _: Status) *const Window { 78 return &self.recent; 79 } 80 81 pub fn emptyResult( 82 aa: std.mem.Allocator, 83 identity: []const u8, 84 label: []const u8, 85 search: []const u8, 86 selected_id: []const u8, 87 comment_id: []const u8, 88 comments_start: usize, 89 view: ui.RoutablePage.DiscussionsView, 90 ) !Self { 91 return .{ 92 .identity = try aa.dupe(u8, identity), 93 .label = try aa.dupe(u8, label), 94 .search = if (search.len == 0) null else std.Uri.percentDecodeInPlace(try aa.dupe(u8, search)), 95 .selected_id = try aa.dupe(u8, selected_id), 96 .comment_id = try aa.dupe(u8, comment_id), 97 .comments_start = comments_start, 98 .recent = .empty, 99 .view = if (view == .description) .recent else view, 100 .description_page = view == .description, 101 .labels = &.{}, 102 }; 103 } 104 105 pub fn init( 106 comptime repo_kind: rp.RepoKind, 107 comptime repo_opts: rp.RepoOpts(repo_kind), 108 arena: *std.heap.ArenaAllocator, 109 repo: *rp.Repo(repo_kind, repo_opts), 110 io: std.Io, 111 admin_moment: ?evt.AdminDB.HashMap(.read_only), 112 identity: []const u8, 113 label: []const u8, 114 search: []const u8, 115 selected_id: []const u8, 116 comment_id: []const u8, 117 comments_start: usize, 118 view: ui.RoutablePage.DiscussionsView, 119 ) !Self { 120 const empty = try emptyResult(arena.allocator(), identity, label, search, selected_id, comment_id, comments_start, view); 121 const aa = arena.allocator(); 122 const DB = evt.EventDB(repo_opts.hash); 123 const rooted = empty.selected_id.len != 0; 124 const labeled = empty.label.len != 0; 125 const strict = rooted or labeled; 126 127 const gpa = arena.child_allocator; 128 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, gpa, repo.core.repo_dir) else null; 129 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 130 const haxy_moment = (if (event_db_maybe) |*event_db| 131 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 132 else if (repo_kind == .git) 133 return empty 134 else 135 evt.currentMoment(repo_opts, repo)) catch { 136 if (strict) return error.NotFound; 137 return empty; 138 }; 139 140 const set_maybe: ?DB.SortedSet(.read_only) = if (labeled) blk: { 141 const labels_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Discussion.label_to_id_set_key)) orelse return error.NotFound; 142 const label_sets = try DB.SortedMap(.read_only).init(labels_cursor); 143 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.label)); 144 const cursor = try label_sets.getCursor(decoded) orelse return error.NotFound; 145 break :blk try DB.SortedSet(.read_only).init(cursor); 146 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Discussion.active_id_set_key))) |cursor| 147 try DB.SortedSet(.read_only).init(cursor) 148 else 149 null; 150 151 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Discussion.record_map_key)) orelse { 152 if (strict) return error.NotFound; 153 return empty; 154 }; 155 const records = try DB.HashMap(.read_only).init(records_cursor); 156 157 var root_key: ?[]const u8 = null; 158 if (rooted) { 159 const id = evt.parseEventId(empty.selected_id) catch return error.NotFound; 160 const record_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &id)) orelse return error.NotFound; 161 const record = try evt.read(evt.Discussion.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(record_cursor)); 162 if (record.removed) return error.NotFound; 163 const activity_order = try evt.Discussion.activityOrder(DB, repo_opts.hash, haxy_moment, &id); 164 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(activity_order, &id)); 165 const set = set_maybe orelse return error.NotFound; 166 if (!try set.contains(order_key)) return error.NotFound; 167 root_key = order_key; 168 } 169 170 const discussion_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 171 const loaded_window = try thread.loadWindow(Self, .discuss, repo_opts.hash, arena, admin_moment, haxy_moment, records, set_maybe, root_key, null, empty.selected_id, discussion_comments_start, empty.search); 172 const comment_page = if (empty.comment_id.len == 0) 173 null 174 else 175 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start); 176 177 var label_names: std.ArrayList([]const u8) = .empty; 178 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Discussion.label_to_id_set_key))) |labels_cursor| { 179 const label_sets = try DB.SortedMap(.read_only).init(labels_cursor); 180 var label_iter = try label_sets.iterator(); 181 while (try label_iter.next()) |entry_cursor| { 182 if (label_names.items.len == max_labels) break; 183 var entry = entry_cursor; 184 const pair = try entry.readKeyValuePair(); 185 try label_names.append(aa, try pair.key_cursor.readBytesAlloc(aa, null)); 186 } 187 } 188 189 return .{ 190 .identity = empty.identity, 191 .label = empty.label, 192 .search = empty.search, 193 .selected_id = empty.selected_id, 194 .comment_id = empty.comment_id, 195 .comments_start = comments_start, 196 .comment_page = comment_page, 197 .recent = loaded_window, 198 .view = empty.view, 199 .description_page = empty.description_page, 200 .labels = label_names.items, 201 }; 202 } 203 204 pub const View = thread.View(.discuss, Self); 205 pub const Detail = thread.Detail(.discuss, Self); 206 pub const detail_widget_name = "repo_discussion_detail"; 207 208 pub const Header = thread.Header; 209 210 const recent_tab_label = "recent"; 211 const labels_tab_label = "labels"; 212 const edit_tab_label = "edit"; 213 const reply_tab_label = "reply"; 214 const remove_tab_label = "remove"; 215 const new_tab_label = "new"; 216 217 // tabs switching between the discussions page's views 218 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 219 var header = try Header.init(allocator, session, data.search); 220 errdefer header.deinit(allocator); 221 const aa = session.page_arena.allocator(); 222 const selected_index = View.viewIndex(data.view); 223 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_discussions; 224 225 // recent discussions 226 { 227 const route = try thread.searchRoute(ui.RoutablePage.repoDiscussionsRoute(data.identity, data.label, ""), data.search); 228 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == 0); 229 var label_buf: [64]u8 = undefined; 230 const label = try thread.countLabel(&label_buf, recent_tab_label, data.recent); 231 try header.addTab(allocator, label, link, 0); 232 } 233 234 // labels tab, showing the active label filter 235 { 236 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.discuss, data.identity, data.label), data.search); 237 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 238 const label = if (data.label.len == 0) labels_tab_label else blk: { 239 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.label)); 240 break :blk try std.fmt.allocPrint(aa, labels_tab_label ++ " ({s})", .{decoded}); 241 }; 242 try header.addTab(allocator, label, labels_link, View.viewIndex(.labels)); 243 } 244 245 // new-discussion tab; an edit or comment url shows its tab in this place 246 { 247 const route = switch (data.view) { 248 .edit => ui.RoutablePage.repoThreadEditRoute(.discuss, data.identity, data.selected_id) orelse return error.RouteTooLong, 249 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.discuss, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 250 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.discuss, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 251 .remove => ui.RoutablePage.repoThreadRemoveRoute(.discuss, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 252 else => ui.RoutablePage.repoThreadNewRoute(.discuss, data.identity) orelse return error.RouteTooLong, 253 }; 254 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 255 const label: []const u8 = switch (data.view) { 256 .edit => edit_tab_label, 257 .new_comment => reply_tab_label, 258 .edit_comment => edit_tab_label, 259 .remove => remove_tab_label, 260 else => new_tab_label, 261 }; 262 try header.addTab(allocator, label, link, View.viewIndex(.new)); 263 } 264 265 header.select(View.viewIndex(data.view)); 266 return header; 267 } 268