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