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