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