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