╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Discussions.zig ──────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 // who the page is built for, null when logged out 52 viewer: ?ui.Viewer = null, 53 recent: Window, 54 view: ui.RoutablePage.DiscussionsView, 55 description_page: bool = false, 56 labels: []const []const u8, 57 repo_source: ?ui.RepoSource = null, 58 59 const Self = @This(); 60 61 pub const Event = evt.Discussion; 62 pub const Status = enum { recent }; 63 pub const ViewKind = ui.RoutablePage.DiscussionsView; 64 pub const thread_name = "discussion"; 65 pub const header_widget_name = "repo_discussions_header"; 66 67 pub fn listRoute(identity: []const u8, _: Status, label: []const u8, selected: []const u8) ?ui.RoutablePage { 68 return ui.RoutablePage.repoDiscussionsRoute(identity, label, selected); 69 } 70 71 pub fn selectedThread(self: *const Self) ?*const DiscussionWithId { 72 if (self.selected_id.len == 0) return null; 73 for (self.recent.items) |*entry| { 74 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 75 } 76 return null; 77 } 78 79 pub fn window(self: *const Self, _: Status) *const Window { 80 return &self.recent; 81 } 82 83 pub fn emptyResult( 84 aa: std.mem.Allocator, 85 identity: []const u8, 86 label: []const u8, 87 search: []const u8, 88 selected_id: []const u8, 89 comment_id: []const u8, 90 comments_start: usize, 91 view: ui.RoutablePage.DiscussionsView, 92 ) !Self { 93 return .{ 94 .identity = try aa.dupe(u8, identity), 95 .label = try aa.dupe(u8, label), 96 .search = if (search.len == 0) null else std.Uri.percentDecodeInPlace(try aa.dupe(u8, search)), 97 .selected_id = try aa.dupe(u8, selected_id), 98 .comment_id = try aa.dupe(u8, comment_id), 99 .comments_start = comments_start, 100 .recent = .empty, 101 .view = if (view == .description) .recent else view, 102 .description_page = view == .description, 103 .labels = &.{}, 104 }; 105 } 106 107 pub fn init( 108 comptime repo_kind: rp.RepoKind, 109 comptime repo_opts: rp.RepoOpts(repo_kind), 110 arena: *std.heap.ArenaAllocator, 111 repo: *rp.Repo(repo_kind, repo_opts), 112 io: std.Io, 113 admin_moment: ?evt.AdminDB.HashMap(.read_only), 114 identity: []const u8, 115 label: []const u8, 116 search: []const u8, 117 selected_id: []const u8, 118 comment_id: []const u8, 119 comments_start: usize, 120 view: ui.RoutablePage.DiscussionsView, 121 viewer: ?ui.Viewer, 122 ) !Self { 123 const empty = try emptyResult(arena.allocator(), identity, label, search, selected_id, comment_id, comments_start, view); 124 const aa = arena.allocator(); 125 const DB = evt.EventDB(repo_opts.hash); 126 const rooted = empty.selected_id.len != 0; 127 const labeled = empty.label.len != 0; 128 const strict = rooted or labeled; 129 130 const gpa = arena.child_allocator; 131 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; 132 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 133 const haxy_moment = (if (event_db_maybe) |*event_db| 134 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 135 else if (repo_kind == .git) 136 return empty 137 else 138 evt.currentMoment(repo_opts, repo)) catch { 139 if (strict) return error.NotFound; 140 return empty; 141 }; 142 143 const set_maybe: ?DB.SortedSet(.read_only) = if (labeled) blk: { 144 const labels_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Discussion.label_to_id_set_key)) orelse return error.NotFound; 145 const label_sets = try DB.SortedMap(.read_only).init(labels_cursor); 146 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.label)); 147 const cursor = try label_sets.getCursor(decoded) orelse return error.NotFound; 148 break :blk try DB.SortedSet(.read_only).init(cursor); 149 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Discussion.active_id_set_key))) |cursor| 150 try DB.SortedSet(.read_only).init(cursor) 151 else 152 null; 153 154 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Discussion.record_map_key)) orelse { 155 if (strict) return error.NotFound; 156 return empty; 157 }; 158 const records = try DB.HashMap(.read_only).init(records_cursor); 159 160 var root_key: ?[]const u8 = null; 161 if (rooted) { 162 const id = evt.parseEventId(empty.selected_id) catch return error.NotFound; 163 const record_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &id)) orelse return error.NotFound; 164 const record = try evt.read(evt.Discussion.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(record_cursor)); 165 if (record.removed) return error.NotFound; 166 const activity_order = try evt.Discussion.activityOrder(DB, repo_opts.hash, haxy_moment, &id); 167 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(activity_order, &id)); 168 const set = set_maybe orelse return error.NotFound; 169 if (!try set.contains(order_key)) return error.NotFound; 170 root_key = order_key; 171 } 172 173 const discussion_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 174 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, viewer); 175 const comment_page = if (empty.comment_id.len == 0) 176 null 177 else 178 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start, viewer); 179 180 var label_names: std.ArrayList([]const u8) = .empty; 181 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Discussion.label_to_id_set_key))) |labels_cursor| { 182 const label_sets = try DB.SortedMap(.read_only).init(labels_cursor); 183 var label_iter = try label_sets.iterator(); 184 while (try label_iter.next()) |entry_cursor| { 185 if (label_names.items.len == max_labels) break; 186 var entry = entry_cursor; 187 const pair = try entry.readKeyValuePair(); 188 try label_names.append(aa, try pair.key_cursor.readBytesAlloc(aa, null)); 189 } 190 } 191 192 return .{ 193 .identity = empty.identity, 194 .label = empty.label, 195 .search = empty.search, 196 .selected_id = empty.selected_id, 197 .comment_id = empty.comment_id, 198 .comments_start = comments_start, 199 .comment_page = comment_page, 200 .viewer = viewer, 201 .recent = loaded_window, 202 .view = empty.view, 203 .description_page = empty.description_page, 204 .labels = label_names.items, 205 }; 206 } 207 208 pub const View = thread.View(.discuss, Self); 209 pub const Detail = thread.Detail(.discuss, Self); 210 pub const detail_widget_name = "repo_discussion_detail"; 211 212 pub const Header = thread.Header; 213 214 const recent_tab_label = "recent"; 215 const labels_tab_label = "labels"; 216 const edit_tab_label = "edit"; 217 const reply_tab_label = "reply"; 218 const remove_tab_label = "remove"; 219 const new_tab_label = "new"; 220 221 // tabs switching between the discussions page's views 222 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 223 var header = try Header.init(allocator, session, data.search); 224 errdefer header.deinit(allocator); 225 const aa = session.page_arena.allocator(); 226 const selected_index = View.viewIndex(data.view); 227 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_discussions; 228 229 // recent discussions 230 { 231 const route = try thread.searchRoute(ui.RoutablePage.repoDiscussionsRoute(data.identity, data.label, ""), data.search); 232 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == 0); 233 var label_buf: [64]u8 = undefined; 234 const label = try thread.countLabel(&label_buf, recent_tab_label, data.recent); 235 try header.addTab(allocator, label, link, 0); 236 } 237 238 // labels tab, showing the active label filter 239 { 240 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.discuss, data.identity, data.label), data.search); 241 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 242 const label = if (data.label.len == 0) labels_tab_label else blk: { 243 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.label)); 244 break :blk try std.fmt.allocPrint(aa, labels_tab_label ++ " ({s})", .{decoded}); 245 }; 246 try header.addTab(allocator, label, labels_link, View.viewIndex(.labels)); 247 } 248 249 // new-discussion tab; an edit or comment url shows its tab in this place 250 { 251 const route = switch (data.view) { 252 .edit => ui.RoutablePage.repoThreadEditRoute(.discuss, data.identity, data.selected_id) orelse return error.RouteTooLong, 253 .new_comment => ui.RoutablePage.repoThreadCommentNewRoute(.discuss, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 254 .edit_comment => ui.RoutablePage.repoThreadCommentEditRoute(.discuss, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 255 .remove => ui.RoutablePage.repoThreadRemoveRoute(.discuss, data.identity, data.selected_id, data.comment_id) orelse return error.RouteTooLong, 256 else => ui.RoutablePage.repoThreadNewRoute(.discuss, data.identity) orelse return error.RouteTooLong, 257 }; 258 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == View.viewIndex(.new)); 259 const label: []const u8 = switch (data.view) { 260 .edit => edit_tab_label, 261 .new_comment => reply_tab_label, 262 .edit_comment => edit_tab_label, 263 .remove => remove_tab_label, 264 else => new_tab_label, 265 }; 266 try header.addTab(allocator, label, link, View.viewIndex(.new)); 267 } 268 269 header.select(View.viewIndex(data.view)); 270 return header; 271 } 272