╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2870 // terminal path; the web posts the form to its comment route. 2871 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2872 if (comptime wasm) return; 2873 const io = self.session.io orelse return; 2874 const src = self.data.repo_source orelse return; 2875 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2876 const author = actor.author; 2877 const form = self.formBox() orelse return; 2878 const body_input = try formField(form, "body"); 2879 const body = try body_input.text(allocator); 2880 defer allocator.free(body); 2881 if (!evt.Comment.fieldsValid(body)) return; 2882 2883 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2884 @memcpy(&thread_id, self.data.selected_id); 2885 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2886 var parent: [evt.event_id_size * 2]u8 = undefined; 2887 @memcpy(&parent, self.data.comment_id); 2888 break :blk parent; 2889 }; 2890 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2891 const editing = self.data.view == .edit_comment; 2892 if (editing) { 2893 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2894 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 2895 } 2896 switch (src.repo_kind) { 2897 inline else => |repo_kind| { 2898 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2899 defer any_repo.deinit(io, allocator); 2900 switch (any_repo) { 2901 inline else => |*repo| if (editing) { 2902 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2903 try evt.Comment.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, kind, &thread_id, &comment_id, body, author); 2904 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2905 } else { 2906 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); 2907 }, 2908 } 2909 }, 2910 } 2911 2912 body_input.clear(allocator); 2913 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2914 try self.session.navigate(route); 2915 } 2916 2917 // terminal form submissions; the web posts to the forms' routes instead 2918 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2919 switch (self.data.view) { 2920 .new_comment, .edit_comment => try self.submitComment(allocator), 2921 .edit => try self.submitEditedThread(allocator), 2922 else => try self.submitNewThread(allocator), 2923 } 2924 } 2925 2926 // commit the new thread to the repo's events branch and navigate to it. 2927 // this is the terminal path; the web posts the form to the thread route, 2928 // so the wasm side never runs (or compiles) the repo access below. 2929 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2930 if (comptime wasm) return; 2931 const io = self.session.io orelse return; 2932 const src = self.data.repo_source orelse return; 2933 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 2934 2935 const form = self.threadForm() orelse return; 2936 const title_input = try formField(form, "title"); 2937 const labels_input = try formField(form, "labels"); 2938 const description_input = try formField(form, "description"); 2939 2940 const title = try title_input.text(allocator); 2941 defer allocator.free(title); 2942 const labels = try labels_input.text(allocator); 2943 defer allocator.free(labels); 2944 const description = try description_input.text(allocator); 2945 defer allocator.free(description); 2946 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2947 defer if (comptime supports_drafts) allocator.free(target_branch); 2948 2949 if (!Event.fieldsValid(title, labels)) { 2950 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2951 return; 2952 } 2953 2954 if (comptime supports_drafts) { 2955 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2956 const source_branch = switch (source.selectedKind()) { 2957 .fork => null, 2958 .branch => try source.field().text(allocator), 2959 }; 2960 defer if (source_branch) |branch| allocator.free(branch); 2961 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 2962 const failure = FeedbackFailure.fromError(err) orelse return err; 2963 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2964 return; 2965 }; 2966 title_input.clear(allocator); 2967 labels_input.clear(allocator); 2968 description_input.clear(allocator); 2969 (try formField(form, "target_branch")).clear(allocator); 2970 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2971 try self.session.navigate(route); 2972 return; 2973 } 2974 2975 var id_bytes: [evt.event_id_size]u8 = undefined; 2976 io.random(&id_bytes); 2977 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2978 2979 const event = evt.EventWithId{ 2980 .id = event_id_hex, 2981 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2982 .author = actor.author, 2983 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2984 .title = title, 2985 .description = description, 2986 .labels = labels, 2987 }), 2988 }; 2989 2990 switch (src.repo_kind) { 2991 inline else => |repo_kind| { 2992 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2993 defer any_repo.deinit(io, allocator); 2994 switch (any_repo) { 2995 inline else => |*repo| try evt.consume(self.session.eventHost(), .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2996 } 2997 }, 2998 } 2999 3000 // wipe the form so a return visit starts fresh 3001 title_input.clear(allocator); 3002 labels_input.clear(allocator); 3003 description_input.clear(allocator); 3004 3005 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", &event_id_hex) orelse return; 3006 try self.session.navigate(route); 3007 } 3008 3009 // re-emit the selected thread's event with the form's content (status 3010 // preserved), then reload the thread's page. this is the terminal path; 3011 // the web posts the form to the edit route. 3012 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 3013 if (comptime wasm) return; 3014 const io = self.session.io orelse return; 3015 const entry = self.data.selectedThread() orelse return; 3016 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3017 const author = actor.author; 3018 3019 const form = self.threadForm() orelse return; 3020 const title_input = try formField(form, "title"); 3021 const labels_input = try formField(form, "labels"); 3022 const description_input = try formField(form, "description"); 3023 3024 const title = try title_input.text(allocator); 3025 defer allocator.free(title); 3026 const labels = try labels_input.text(allocator); 3027 defer allocator.free(labels); 3028 const description = try description_input.text(allocator); 3029 defer allocator.free(description); 3030 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 3031 defer if (comptime supports_drafts) allocator.free(target_branch); 3032 3033 if (!Event.fieldsValid(title, labels)) { 3034 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 3035 return; 3036 } 3037 const src = self.data.repo_source orelse return; 3038 3039 if (comptime supports_drafts) { 3040 if (entryDraft(entry.*)) { 3041 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 3042 const failure = FeedbackFailure.fromError(err) orelse return err; 3043 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3044 return; 3045 }; 3046 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3047 try self.session.navigate(route); 3048 return; 3049 } 3050 } 3051 3052 const id_bytes = try evt.parseEventId(entry.id); 3053 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 3054 3055 switch (src.repo_kind) { 3056 inline else => |repo_kind| { 3057 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3058 defer any_repo.deinit(io, allocator); 3059 switch (any_repo) { 3060 inline else => |*repo| if (supports_drafts) { 3061 Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3062 .title = title, 3063 .labels = labels, 3064 .description = description, 3065 .target_branch = target_branch, 3066 } }, author) catch |err| { 3067 const failure = FeedbackFailure.fromError(err) orelse return err; 3068 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 3069 return; 3070 }; 3071 } else if (has_status) 3072 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 3073 .title = title, 3074 .labels = labels, 3075 .description = description, 3076 } }, author) 3077 else 3078 try Event.update(self.session.eventHost(), repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 3079 } 3080 }, 3081 } 3082 3083 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3084 try self.session.navigate(route); 3085 } 3086 3087 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 3088 const aa = self.session.arena.allocator(); 3089 const source = if (supports_drafts) blk: { 3090 const form = self.threadForm() orelse break :blk null; 3091 break :blk formWidget(form, .patch_source); 3092 } else null; 3093 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 3094 .failure = failure, 3095 .fields = if (comptime supports_drafts) .{ 3096 .title = try aa.dupe(u8, title), 3097 .labels = try aa.dupe(u8, labels), 3098 .description = try aa.dupe(u8, description), 3099 .target_branch = try aa.dupe(u8, target_branch), 3100 .source_branch = if (source) |value| try value.field().text(aa) else "", 3101 .source_kind = if (source) |value| value.selectedKind() else .fork, 3102 } else .{ 3103 .title = try aa.dupe(u8, title), 3104 .labels = try aa.dupe(u8, labels), 3105 .description = try aa.dupe(u8, description), 3106 }, 3107 }); 3108 } 3109 3110 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 3111 switch (confirmation(self.data.view) orelse return) { 3112 .publish => if (comptime supports_drafts) { 3113 if (comptime wasm) return; 3114 const entry = self.data.selectedThread() orelse return; 3115 if (!entryDraft(entry.*)) return; 3116 const author = ((try self.session.authorize(self.data.identity, .read, kind)) orelse return).author; 3117 try self.data.publishDraft(self.session, allocator, author, entry.id); 3118 const route = listRoute(self.data.identity, @fromBackingInt(@intCast(0)), "", entry.id) orelse return; 3119 try self.session.navigate(route); 3120 } else unreachable, 3121 .merge => if (comptime supports_merge) { 3122 if (comptime wasm) return; 3123 const entry = self.data.selectedThread() orelse return; 3124 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 3125 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 3126 const author = ((try self.session.authorize(self.data.identity, .write, kind)) orelse return).author; 3127 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 3128 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 3129 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 3130 try self.session.navigate(route); 3131 return; 3132 }, 3133 else => return err, 3134 }; 3135 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 3136 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 3137 try self.session.navigate(route); 3138 } else unreachable, 3139 .remove => try self.submitRemove(allocator), 3140 } 3141 } 3142 3143 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 3144 const comment = self.data.comment_id.len != 0; 3145 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 3146 const id = evt.parseEventId(event_id) catch return; 3147 try self.removeEvent(allocator, if (comment) .comment else kind, id); 3148 } 3149 3150 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 3151 if (comptime wasm) return; 3152 const actor = (try self.session.authorize(self.data.identity, .read, kind)) orelse return; 3153 const author = actor.author; 3154 3155 if (comptime supports_drafts) { 3156 const selected = self.data.selectedThread() orelse return; 3157 if (event_kind == kind and entryDraft(selected.*)) { 3158 try Data.removeDraft(self.session, allocator, author, &id); 3159 const route = draftsRoute(self.data.identity) orelse return; 3160 try self.session.navigate(route); 3161 return; 3162 } 3163 } 3164 3165 const io = self.session.io orelse return; 3166 const src = self.data.repo_source orelse return; 3167 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 3168 const thread_id = evt.parseEventId(self.data.selected_id) catch return; 3169 3170 switch (src.repo_kind) { 3171 inline else => |repo_kind| { 3172 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 3173 defer any_repo.deinit(io, allocator); 3174 switch (any_repo) { 3175 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), 3176 } 3177 }, 3178 } 3179 3180 const id_hex = std.fmt.bytesToHex(id, .lower); 3181 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 3182 try self.session.navigate(route); 3183 } 3184 3185 // return to the list. 3186 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 3187 root_focus.setFocus(self.listScroll(index).getFocus().id); 3188 } 3189 3190 // cross to the header tabs above the stack. 3191 fn focusHeader(self: *This, root_focus: *Focus) void { 3192 root_focus.setFocus(self.box.children.keys()[header_index]); 3193 } 3194 3195 pub fn clearGrid(self: *This) void { 3196 self.box.clearGrid(); 3197 } 3198 3199 pub fn getGrid(self: This) ?Grid { 3200 return self.box.getGrid(); 3201 } 3202 3203 pub fn getFocus(self: *This) *Focus { 3204 return self.box.getFocus(); 3205 } 3206 3207 // for the parent's "scroll up at the top jumps to the header" check: at the 3208 // top only while the header tabs hold focus, so up from the split 3209 // crosses into the tabs first. 3210 pub fn atTop(self: *This) bool { 3211 return self.headerActive(); 3212 } 3213 }; 3214 } 3215