╭ 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 assignees: []const ui.Assignee = &.{}, 29 // whether the viewer may change the thread, computed here so the web ui has it 30 can_modify: bool = false, 31 // the viewer's notify button, null when hidden 32 notify_action: ?ui.NotifyAction = null, 33 }; 34 35 pub const Entry = DiscussionWithId; 36 37 pub const Window = struct { 38 items: []const DiscussionWithId, 39 prev_id: ?[]const u8, 40 next_id: ?[]const u8, 41 count: usize, 42 // whether a search stopped counting at its cap, so `count` is a floor. 43 more: bool = false, 44 45 pub const empty: Window = .{ .items = &.{}, .prev_id = null, .next_id = null, .count = 0 }; 46 }; 47 48 identity: []const u8, 49 label: []const u8, 50 // the decoded query the list is filtered to, or null when unfiltered. 51 search: ?[]const u8 = null, 52 selected_id: []const u8, 53 comment_id: []const u8, 54 comments_start: usize, 55 comment_page: ?Comment.Permalink = null, 56 // who the page is built for, null when logged out 57 viewer: ?ui.Viewer = null, 58 recent: Window, 59 view: ui.RoutablePage.DiscussionsView, 60 description_page: bool = false, 61 labels: []const []const u8, 62 repo_source: ?ui.RepoSource = null, 63 64 const Self = @This(); 65 66 pub const Event = evt.Discussion; 67 pub const Status = enum { recent }; 68 pub const ViewKind = ui.RoutablePage.DiscussionsView; 69 pub const thread_name = "discussion"; 70 pub const header_widget_name = "repo_discussions_header"; 71 72 pub fn listRoute(identity: []const u8, _: Status, label: []const u8, selected: []const u8) ?ui.RoutablePage { 73 return ui.RoutablePage.repoDiscussionsRoute(identity, label, selected); 74 } 75 76 pub fn selectedThread(self: *const Self) ?*const DiscussionWithId { 77 if (self.selected_id.len == 0) return null; 78 for (self.recent.items) |*entry| { 79 if (std.mem.eql(u8, entry.id, self.selected_id)) return entry; 80 } 81 return null; 82 } 83 84 pub fn window(self: *const Self, _: Status) *const Window { 85 return &self.recent; 86 } 87 88 pub fn emptyResult( 89 aa: std.mem.Allocator, 90 identity: []const u8, 91 label: []const u8, 92 search: []const u8, 93 selected_id: []const u8, 94 comment_id: []const u8, 95 comments_start: usize, 96 view: ui.RoutablePage.DiscussionsView, 97 ) !Self { 98 return .{ 99 .identity = try aa.dupe(u8, identity), 100 .label = try aa.dupe(u8, label), 101 .search = if (search.len == 0) null else std.Uri.percentDecodeInPlace(try aa.dupe(u8, search)), 102 .selected_id = try aa.dupe(u8, selected_id), 103 .comment_id = try aa.dupe(u8, comment_id), 104 .comments_start = comments_start, 105 .recent = .empty, 106 .view = if (view == .description) .recent else view, 107 .description_page = view == .description, 108 .labels = &.{}, 109 }; 110 } 111 112 pub fn init( 113 comptime repo_kind: rp.RepoKind, 114 comptime repo_opts: rp.RepoOpts(repo_kind), 115 arena: *std.heap.ArenaAllocator, 116 repo: *rp.Repo(repo_kind, repo_opts), 117 io: std.Io, 118 admin_moment: ?evt.AdminDB.HashMap(.read_only), 119 identity: []const u8, 120 label: []const u8, 121 search: []const u8, 122 selected_id: []const u8, 123 comment_id: []const u8, 124 comments_start: usize, 125 view: ui.RoutablePage.DiscussionsView, 126 viewer: ?ui.Viewer, 127 ) !Self { 128 const empty = try emptyResult(arena.allocator(), identity, label, search, selected_id, comment_id, comments_start, view); 129 const aa = arena.allocator(); 130 const DB = evt.EventDB(repo_opts.hash); 131 const rooted = empty.selected_id.len != 0; 132 const labeled = empty.label.len != 0; 133 const strict = rooted or labeled; 134 135 const gpa = arena.child_allocator; 136 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; 137 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 138 const haxy_moment = (if (event_db_maybe) |*event_db| 139 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 140 else if (repo_kind == .git) 141 return empty 142 else 143 evt.currentMoment(repo_opts, repo)) catch { 144 if (strict) return error.NotFound; 145 return empty; 146 }; 147 148 const set_maybe: ?DB.SortedSet(.read_only) = if (labeled) blk: { 149 const labels_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Discussion.label_to_id_set_key)) orelse return error.NotFound; 150 const label_sets = try DB.SortedMap(.read_only).init(labels_cursor); 151 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, empty.label)); 152 const cursor = try label_sets.getCursor(decoded) orelse return error.NotFound; 153 break :blk try DB.SortedSet(.read_only).init(cursor); 154 } else if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Discussion.active_id_set_key))) |cursor| 155 try DB.SortedSet(.read_only).init(cursor) 156 else 157 null; 158 159 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Discussion.record_map_key)) orelse { 160 if (strict) return error.NotFound; 161 return empty; 162 }; 163 const records = try DB.HashMap(.read_only).init(records_cursor); 164 165 var root_key: ?[]const u8 = null; 166 if (rooted) { 167 const id = evt.parseEventId(empty.selected_id) catch return error.NotFound; 168 const record_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &id)) orelse return error.NotFound; 169 const record = try evt.read(evt.Discussion.Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(record_cursor)); 170 if (record.removed) return error.NotFound; 171 const activity_order = try evt.Discussion.activityOrder(DB, repo_opts.hash, haxy_moment, &id); 172 const order_key = try aa.dupe(u8, &evt.orderKeyDesc(activity_order, &id)); 173 const set = set_maybe orelse return error.NotFound; 174 if (!try set.contains(order_key)) return error.NotFound; 175 root_key = order_key; 176 } 177 178 const discussion_comments_start = if (empty.comment_id.len == 0) comments_start else 0; 179 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, null, viewer); 180 const comment_page = if (empty.comment_id.len == 0) 181 null 182 else 183 try Comment.init(repo_opts.hash, arena, admin_moment, haxy_moment, empty.selected_id, empty.comment_id, comments_start, viewer); 184 185 var label_names: std.ArrayList([]const u8) = .empty; 186 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Discussion.label_to_id_set_key))) |labels_cursor| { 187 const label_sets = try DB.SortedMap(.read_only).init(labels_cursor); 188 var label_iter = try label_sets.iterator(); 189 while (try label_iter.next()) |entry_cursor| { 190 if (label_names.items.len == max_labels) break; 191 var entry = entry_cursor; 192 const pair = try entry.readKeyValuePair(); 193 try label_names.append(aa, try pair.key_cursor.readBytesAlloc(aa, null)); 194 } 195 } 196 197 return .{ 198 .identity = empty.identity, 199 .label = empty.label, 200 .search = empty.search, 201 .selected_id = empty.selected_id, 202 .comment_id = empty.comment_id, 203 .comments_start = comments_start, 204 .comment_page = comment_page, 205 .viewer = viewer, 206 .recent = loaded_window, 207 .view = empty.view, 208 .description_page = empty.description_page, 209 .labels = label_names.items, 210 }; 211 } 212 213 pub const View = thread.View(.discuss, Self); 214 pub const Detail = thread.Detail(.discuss, Self); 215 pub const detail_widget_name = "repo_discussion_detail"; 216 217 pub const Header = thread.Header; 218 219 const recent_tab_label = "recent"; 220 const labels_tab_label = "labels"; 221 const edit_tab_label = "edit"; 222 const reply_tab_label = "reply"; 223 const remove_tab_label = "remove"; 224 const new_tab_label = "new"; 225 226 // tabs switching between the discussions page's views 227 pub fn initHeader(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 228 var header = try Header.init(allocator, session, data.search, null); 229 errdefer header.deinit(allocator); 230 const selected_index = View.viewIndex(data.view); 231 const page_selected = std.meta.activeTag(session.data.current_page) == .repo_discussions; 232 233 // recent discussions 234 { 235 const route = try thread.searchRoute(ui.RoutablePage.repoDiscussionsRoute(data.identity, data.label, ""), data.search); 236 const link = try ui.inPageTabLink(session, route, page_selected and selected_index == 0); 237 var label_buf: [64]u8 = undefined; 238 const label = try thread.countLabel(&label_buf, recent_tab_label, data.recent); 239 try header.addTab(allocator, label, link, 0); 240 } 241 242 // labels tab 243 { 244 const labels_route = try thread.searchRoute(ui.RoutablePage.repoThreadLabelsRoute(.discuss, data.identity, data.label), data.search); 245 const labels_link = try ui.inPageTabLink(session, labels_route, page_selected and selected_index == View.viewIndex(.labels)); 246 try header.addTab(allocator, labels_tab_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