╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3021 3022 var thread_id: [evt.event_id_size * 2]u8 = undefined; 3023 @memcpy(&thread_id, self.data.selected_id); 3024 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 3025 var parent: [evt.event_id_size * 2]u8 = undefined; 3026 @memcpy(&parent, self.data.comment_id); 3027 break :blk parent; 3028 }; 3029 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 3030 const editing = self.data.view == .edit_comment; 3031 if (editing) { 3032 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3033 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 3034 } 3035 switch (src.repo_kind) { 3036 inline else => |repo_kind| { 3037 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3038 defer any_repo.deinit(io, allocator); 3039 switch (any_repo) { 3040 inline else => |*repo| if (editing) { 3041 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 3042 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 3043 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 3044 } else { 3045 event_id_hex = try evt.Comment.create(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &parent_id, body, author); 3046 }, 3047 } 3048 }, 3049 } 3050 3051 body_input.clear(allocator); 3052 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 3053 try self.session.navigate(route); 3054 } 3055 3056 // terminal form submissions; the web posts to the forms' routes instead 3057 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 3058 switch (self.data.view) { 3059 .new_comment, .edit_comment => try self.submitComment(allocator), 3060 .edit => try self.submitEditedThread(allocator), 3061 else => try self.submitNewThread(allocator), 3062 } 3063 } 3064 3065 // commit the new thread to the repo's events branch and navigate to it. 3066 // this is the terminal path; the web posts the form to the thread route, 3067 // so the wasm side never runs (or compiles) the repo access below. 3068 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 3069 if (comptime wasm) return; 3070 const io = self.session.io orelse return; 3071 const src = self.data.repo_source orelse return; 3072 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3073 3074 const form = self.threadForm() orelse return; 3075 const title_input = try formField(form, "title"); 3076 const labels_input = try formField(form, "labels"); 3077 const description_input = try formField(form, "description"); 3078 3079 const title = try title_input.text(allocator); 3080 defer allocator.free(title); 3081 const labels = try labels_input.text(allocator); 3082 defer allocator.free(labels); 3083 const description = try description_input.text(allocator); 3084 defer allocator.free(description); 3085 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3086 defer if (comptime supports_drafts) allocator.free(target_branch); 3087 3088 if (!Event.fieldsValid(title, labels)) { 3089 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3090 return; 3091 } 3092 3093 if (comptime supports_drafts) { 3094 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 3095 const source_branch = switch (source.selectedKind()) { 3096 .fork => null, 3097 .branch => try source.field().text(allocator), 3098 }; 3099 defer if (source_branch) |branch| allocator.free(branch); 3100 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 3101 const failure = FeedbackFailure.fromError(err) orelse return err; 3102 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3103 return; 3104 }; 3105 title_input.clear(allocator); 3106 labels_input.clear(allocator); 3107 description_input.clear(allocator); 3108 (try formField(form, "target_branch")).clear(allocator); 3109 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 3110 try self.session.navigate(route); 3111 return; 3112 } 3113 3114 var id_bytes: [evt.event_id_size]u8 = undefined; 3115 io.random(&id_bytes); 3116 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 3117 3118 const event = evt.EventWithId{ 3119 .id = event_id_hex, 3120 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 3121 .author = actor.author, 3122 .event = @unionInit(evt.Event, @tagName(kind), Event{ 3123 .title = title, 3124 .description = description, 3125 .labels = labels, 3126 }), 3127 }; 3128 3129 switch (src.repo_kind) { 3130 inline else => |repo_kind| { 3131 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3132 defer any_repo.deinit(io, allocator); 3133 switch (any_repo) { 3134 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 3135 } 3136 }, 3137 } 3138 3139 // wipe the form so a return visit starts fresh 3140 title_input.clear(allocator); 3141 labels_input.clear(allocator); 3142 description_input.clear(allocator); 3143 3144 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), &event_id_hex) orelse return; 3145 try self.session.navigate(route); 3146 } 3147 3148 // re-emit the selected thread's event with the form's content (status 3149 // preserved), then reload the thread's page. this is the terminal path; 3150 // the web posts the form to the edit route. 3151 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3152 if (comptime wasm) return; 3153 const io = self.session.io orelse return; 3154 const entry = self.data.selectedThread() orelse return; 3155 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3156 const author = actor.author; 3157 3158 const form = self.threadForm() orelse return; 3159 const title_input = try formField(form, "title"); 3160 const labels_input = try formField(form, "labels"); 3161 const description_input = try formField(form, "description"); 3162 3163 const title = try title_input.text(allocator); 3164 defer allocator.free(title); 3165 const labels = try labels_input.text(allocator); 3166 defer allocator.free(labels); 3167 const description = try description_input.text(allocator); 3168 defer allocator.free(description); 3169 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3170 defer if (comptime supports_drafts) allocator.free(target_branch); 3171 3172 if (!Event.fieldsValid(title, labels)) { 3173 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3174 return; 3175 } 3176 const src = self.data.repo_source orelse return; 3177 3178 if (comptime supports_drafts) { 3179 if (entryDraft(entry.*)) { 3180 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3181 const failure = FeedbackFailure.fromError(err) orelse return err; 3182 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3183 return; 3184 }; 3185 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3186 try self.session.navigate(route); 3187 return; 3188 } 3189 } 3190 3191 const id_bytes = try evt.parseEventId(entry.id); 3192 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3193 3194 switch (src.repo_kind) { 3195 inline else => |repo_kind| { 3196 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3197 defer any_repo.deinit(io, allocator); 3198 switch (any_repo) { 3199 inline else => |*repo| if (supports_drafts) { 3200 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3201 .title = title, 3202 .labels = labels, 3203 .description = description, 3204 .target_branch = target_branch, 3205 } }, author) catch |err| { 3206 const failure = FeedbackFailure.fromError(err) orelse return err; 3207 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3208 return; 3209 }; 3210 } else if (has_status) 3211 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3212 .title = title, 3213 .labels = labels, 3214 .description = description, 3215 } }, author) 3216 else 3217 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3218 } 3219 }, 3220 } 3221 3222 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3223 try self.session.navigate(route); 3224 } 3225 3226 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3227 const aa = self.session.arena.allocator(); 3228 const source = if (supports_drafts) blk: { 3229 const form = self.threadForm() orelse break :blk null; 3230 break :blk formWidget(form, .patch_source); 3231 } else null; 3232 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3233 .failure = failure, 3234 .fields = if (comptime supports_drafts) .{ 3235 .title = try aa.dupe(u8, title), 3236 .labels = try aa.dupe(u8, labels), 3237 .description = try aa.dupe(u8, description), 3238 .target_branch = try aa.dupe(u8, target_branch), 3239 .source_branch = if (source) |value| try value.field().text(aa) else "", 3240 .source_kind = if (source) |value| value.selectedKind() else .fork, 3241 } else .{ 3242 .title = try aa.dupe(u8, title), 3243 .labels = try aa.dupe(u8, labels), 3244 .description = try aa.dupe(u8, description), 3245 }, 3246 }); 3247 } 3248 3249 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3250 switch (confirmation(self.data.view) orelse return) { 3251 .publish => if (comptime supports_drafts) { 3252 if (comptime wasm) return; 3253 const entry = self.data.selectedThread() orelse return; 3254 if (!entryDraft(entry.*)) return; 3255 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3256 try self.data.publishDraft(self.session, allocator, author, entry.id); 3257 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), entry.id) orelse return; 3258 try self.session.navigate(route); 3259 } else unreachable, 3260 .merge => if (comptime supports_merge) { 3261 if (comptime wasm) return; 3262 const entry = self.data.selectedThread() orelse return; 3263 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3264 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3265 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3266 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3267 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3268 const route = listRoute(self.data.identity, entryStatus(entry.*), entry.id) orelse return; 3269 try self.session.navigate(route); 3270 return; 3271 }, 3272 else => return err, 3273 }; 3274 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3275 const route = listRoute(self.data.identity, merged, entry.id) orelse return; 3276 try self.session.navigate(route); 3277 } else unreachable, 3278 .remove => try self.submitRemove(allocator), 3279 } 3280 } 3281 3282 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3283 const comment = self.data.comment_id.len != 0; 3284 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3285 const id = evt.parseEventId(event_id) catch return; 3286 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3287 } 3288 3289 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3290 if (comptime wasm) return; 3291 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3292 const author = actor.author; 3293 3294 if (comptime supports_drafts) { 3295 const selected = self.data.selectedThread() orelse return; 3296 if (event_kind == kind and entryDraft(selected.*)) { 3297 try Data.removeDraft(self.session, allocator, author, &id); 3298 const route = draftsRoute(self.data.identity) orelse return; 3299 try self.session.navigate(route); 3300 return; 3301 } 3302 } 3303 3304 const io = self.session.io orelse return; 3305 const src = self.data.repo_source orelse return; 3306 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3307 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3308 3309 switch (src.repo_kind) { 3310 inline else => |repo_kind| { 3311 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3312 defer any_repo.deinit(io, allocator); 3313 switch (any_repo) { 3314 inline else => |*repo| try evt.removeInThread(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, event_kind, &id, author), 3315 } 3316 }, 3317 } 3318 3319 const id_hex = std.fmt.bytesToHex(id, .lower); 3320 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3321 try self.session.navigate(route); 3322 } 3323 3324 // enter the detail pane, landing on the way back to the list when it shows 3325 fn focusDetail(self: *This, index: usize, root_focus: *Focus) void { 3326 if (widget.listBackId(self.detailFrame(index)) != null) return widget.focusListBack(self.detailFrame(index), root_focus); 3327 _ = self.detail(index).focusFirst(root_focus); 3328 } 3329 3330 // return to the list. 3331 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3332 root_focus.setFocus(self.listScroll(index).getFocus().id); 3333 } 3334 3335 // cross to the header tabs above the stack. 3336 fn focusHeader(self: *This, root_focus: *Focus) void { 3337 root_focus.setFocus(self.box.children.keys()[header_index]); 3338 } 3339 3340 pub fn clearGrid(self: *This) void { 3341 self.box.clearGrid(); 3342 } 3343 3344 pub fn getGrid(self: This) ?Grid { 3345 return self.box.getGrid(); 3346 } 3347 3348 pub fn getFocus(self: *This) *Focus { 3349 return self.box.getFocus(); 3350 } 3351 3352 // for the parent's "scroll up at the top jumps to the header" check: at the 3353 // top only while the header tabs hold focus, so up from the split 3354 // crosses into the tabs first. 3355 pub fn atTop(self: *This) bool { 3356 return self.headerActive(); 3357 } 3358 }; 3359 } 3360