╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ thread.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2617 inline else => |repo_kind| { 2618 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2619 defer any_repo.deinit(io, allocator); 2620 switch (any_repo) { 2621 inline else => |*repo| { 2622 var resolution: Event.Resolve = .{ 2623 .title = title, 2624 .labels = labels, 2625 .hunks = hunks.items, 2626 }; 2627 if (comptime @hasField(Event.Resolve, "theirs")) resolution.theirs = self.data.theirs_picks; 2628 const change = Event.Update{ .resolve = resolution }; 2629 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, change, author) catch |err| switch (err) { 2630 // leave the form up for correction 2631 error.InvalidFields => return, 2632 else => |e| return e, 2633 }; 2634 }, 2635 } 2636 }, 2637 } 2638 2639 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2640 try self.session.navigate(route); 2641 } 2642 2643 // commit a new or edited comment and navigate to its permalink. this is the 2644 // terminal path; the web posts the form to its comment route. 2645 fn submitComment(self: *This, allocator: std.mem.Allocator) !void { 2646 if (comptime wasm) return; 2647 const io = self.session.io orelse return; 2648 const src = self.data.repo_source orelse return; 2649 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2650 const author = actor.author; 2651 const form = self.formBox() orelse return; 2652 const body_input = try formField(form, "body"); 2653 const body = try body_input.text(allocator); 2654 defer allocator.free(body); 2655 if (!evt.Comment.fieldsValid(body)) return; 2656 2657 var thread_id: [evt.event_id_size * 2]u8 = undefined; 2658 @memcpy(&thread_id, self.data.selected_id); 2659 const parent_id = if (self.data.comment_id.len == 0) thread_id else blk: { 2660 var parent: [evt.event_id_size * 2]u8 = undefined; 2661 @memcpy(&parent, self.data.comment_id); 2662 break :blk parent; 2663 }; 2664 var event_id_hex: [evt.event_id_size * 2]u8 = undefined; 2665 const editing = self.data.view == .edit_comment; 2666 if (editing) { 2667 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2668 if (!try src.canModify(io, allocator, actor, .comment, &comment_id)) return; 2669 } 2670 switch (src.repo_kind) { 2671 inline else => |repo_kind| { 2672 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2673 defer any_repo.deinit(io, allocator); 2674 switch (any_repo) { 2675 inline else => |*repo| if (editing) { 2676 const comment_id = evt.parseEventId(self.data.comment_id) catch return; 2677 try evt.Comment.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &comment_id, body, author); 2678 event_id_hex = std.fmt.bytesToHex(comment_id, .lower); 2679 } else { 2680 event_id_hex = try evt.Comment.create(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &thread_id, &parent_id, body, author); 2681 }, 2682 } 2683 }, 2684 } 2685 2686 body_input.clear(allocator); 2687 const route = ui.RoutablePage.repoThreadCommentRoute(kind, self.data.identity, &thread_id, &event_id_hex, 0) orelse return; 2688 try self.session.navigate(route); 2689 } 2690 2691 // terminal form submissions; the web posts to the forms' routes instead 2692 fn submitForm(self: *This, allocator: std.mem.Allocator) !void { 2693 switch (self.data.view) { 2694 .new_comment, .edit_comment => try self.submitComment(allocator), 2695 .edit => try self.submitEditedThread(allocator), 2696 else => try self.submitNewThread(allocator), 2697 } 2698 } 2699 2700 // commit the new thread to the repo's events branch and navigate to it. 2701 // this is the terminal path; the web posts the form to the thread route, 2702 // so the wasm side never runs (or compiles) the repo access below. 2703 fn submitNewThread(self: *This, allocator: std.mem.Allocator) !void { 2704 if (comptime wasm) return; 2705 const io = self.session.io orelse return; 2706 const src = self.data.repo_source orelse return; 2707 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2708 2709 const form = self.threadForm() orelse return; 2710 const title_input = try formField(form, "title"); 2711 const labels_input = try formField(form, "labels"); 2712 const description_input = try formField(form, "description"); 2713 2714 const title = try title_input.text(allocator); 2715 defer allocator.free(title); 2716 const labels = try labels_input.text(allocator); 2717 defer allocator.free(labels); 2718 const description = try description_input.text(allocator); 2719 defer allocator.free(description); 2720 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2721 defer if (comptime supports_drafts) allocator.free(target_branch); 2722 2723 if (!Event.fieldsValid(title, labels)) { 2724 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2725 return; 2726 } 2727 2728 if (comptime supports_drafts) { 2729 const source = formWidget(form, .patch_source) orelse return error.MissingFormField; 2730 const source_branch = switch (source.selectedKind()) { 2731 .fork => null, 2732 .branch => try source.field().text(allocator), 2733 }; 2734 defer if (source_branch) |branch| allocator.free(branch); 2735 const event_id_hex = Data.create(self.data, self.session, allocator, actor, title, labels, description, target_branch, source_branch) catch |err| { 2736 const failure = FeedbackFailure.fromError(err) orelse return err; 2737 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2738 return; 2739 }; 2740 title_input.clear(allocator); 2741 labels_input.clear(allocator); 2742 description_input.clear(allocator); 2743 (try formField(form, "target_branch")).clear(allocator); 2744 const route = ui.RoutablePage.repoThreadCommentsRoute(kind, self.data.identity, &event_id_hex, 0) orelse return; 2745 try self.session.navigate(route); 2746 return; 2747 } 2748 2749 var id_bytes: [evt.event_id_size]u8 = undefined; 2750 io.random(&id_bytes); 2751 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 2752 2753 const event = evt.EventWithId{ 2754 .id = event_id_hex, 2755 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 2756 .author = actor.author, 2757 .event = @unionInit(evt.Event, @tagName(kind), Event{ 2758 .title = title, 2759 .description = description, 2760 .labels = labels, 2761 }), 2762 }; 2763 2764 switch (src.repo_kind) { 2765 inline else => |repo_kind| { 2766 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2767 defer any_repo.deinit(io, allocator); 2768 switch (any_repo) { 2769 inline else => |*repo| try evt.consume(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 2770 } 2771 }, 2772 } 2773 2774 // wipe the form so a return visit starts fresh 2775 title_input.clear(allocator); 2776 labels_input.clear(allocator); 2777 description_input.clear(allocator); 2778 2779 const route = listRoute(self.data.identity, @enumFromInt(0), "", &event_id_hex) orelse return; 2780 try self.session.navigate(route); 2781 } 2782 2783 // re-emit the selected thread's event with the form's content (status 2784 // preserved), then reload the thread's page. this is the terminal path; 2785 // the web posts the form to the edit route. 2786 fn submitEditedThread(self: *This, allocator: std.mem.Allocator) !void { 2787 if (comptime wasm) return; 2788 const io = self.session.io orelse return; 2789 const entry = self.data.selectedThread() orelse return; 2790 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2791 const author = actor.author; 2792 2793 const form = self.threadForm() orelse return; 2794 const title_input = try formField(form, "title"); 2795 const labels_input = try formField(form, "labels"); 2796 const description_input = try formField(form, "description"); 2797 2798 const title = try title_input.text(allocator); 2799 defer allocator.free(title); 2800 const labels = try labels_input.text(allocator); 2801 defer allocator.free(labels); 2802 const description = try description_input.text(allocator); 2803 defer allocator.free(description); 2804 const target_branch = if (comptime supports_drafts) try (try formField(form, "target_branch")).text(allocator) else ""; 2805 defer if (comptime supports_drafts) allocator.free(target_branch); 2806 2807 if (!Event.fieldsValid(title, labels)) { 2808 if (!evt.titleValid(title)) try self.rememberThreadFeedback(.required_title, title, labels, description, target_branch); 2809 return; 2810 } 2811 const src = self.data.repo_source orelse return; 2812 2813 if (comptime supports_drafts) { 2814 if (entryDraft(entry.*)) { 2815 self.data.editDraft(self.session, allocator, author, entry.id, title, labels, description, target_branch) catch |err| { 2816 const failure = FeedbackFailure.fromError(err) orelse return err; 2817 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2818 return; 2819 }; 2820 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2821 try self.session.navigate(route); 2822 return; 2823 } 2824 } 2825 2826 const id_bytes = try evt.parseEventId(entry.id); 2827 if (!try src.canModify(io, allocator, actor, kind, &id_bytes)) return; 2828 2829 switch (src.repo_kind) { 2830 inline else => |repo_kind| { 2831 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2832 defer any_repo.deinit(io, allocator); 2833 switch (any_repo) { 2834 inline else => |*repo| if (supports_drafts) { 2835 Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2836 .title = title, 2837 .labels = labels, 2838 .description = description, 2839 .target_branch = target_branch, 2840 } }, author) catch |err| { 2841 const failure = FeedbackFailure.fromError(err) orelse return err; 2842 try self.rememberThreadFeedback(failure, title, labels, description, target_branch); 2843 return; 2844 }; 2845 } else if (has_status) 2846 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 2847 .title = title, 2848 .labels = labels, 2849 .description = description, 2850 } }, author) 2851 else 2852 try Event.update(self.session.data.host_kind, repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, title, labels, description, author), 2853 } 2854 }, 2855 } 2856 2857 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2858 try self.session.navigate(route); 2859 } 2860 2861 fn rememberThreadFeedback(self: *This, failure: FeedbackFailure, title: []const u8, labels: []const u8, description: []const u8, target_branch: []const u8) !void { 2862 const aa = self.session.arena.allocator(); 2863 const source = if (supports_drafts) blk: { 2864 const form = self.threadForm() orelse break :blk null; 2865 break :blk formWidget(form, .patch_source); 2866 } else null; 2867 self.session.data.form_feedback = @unionInit(ui.Session.FormFeedback, @tagName(feedback_tag), .{ 2868 .failure = failure, 2869 .fields = if (comptime supports_drafts) .{ 2870 .title = try aa.dupe(u8, title), 2871 .labels = try aa.dupe(u8, labels), 2872 .description = try aa.dupe(u8, description), 2873 .target_branch = try aa.dupe(u8, target_branch), 2874 .source_branch = if (source) |value| try value.field().text(aa) else "", 2875 .source_kind = if (source) |value| value.selectedKind() else .fork, 2876 } else .{ 2877 .title = try aa.dupe(u8, title), 2878 .labels = try aa.dupe(u8, labels), 2879 .description = try aa.dupe(u8, description), 2880 }, 2881 }); 2882 } 2883 2884 fn submitConfirmation(self: *This, allocator: std.mem.Allocator, button_index: usize) !void { 2885 switch (confirmation(self.data.view) orelse return) { 2886 .publish => if (comptime supports_drafts) { 2887 if (comptime wasm) return; 2888 const entry = self.data.selectedThread() orelse return; 2889 if (!entryDraft(entry.*)) return; 2890 const author = ((try self.session.authorize(self.data.identity, .read)) orelse return).author; 2891 try self.data.publishDraft(self.session, allocator, author, entry.id); 2892 const route = listRoute(self.data.identity, @enumFromInt(0), "", entry.id) orelse return; 2893 try self.session.navigate(route); 2894 } else unreachable, 2895 .merge => if (comptime supports_merge) { 2896 if (comptime wasm) return; 2897 const entry = self.data.selectedThread() orelse return; 2898 const revision: evt.Patch.MergeRevision = if (button_index > 1) .squash else .source; 2899 if (!Data.canMerge(entry.*, self.session) or entry.mergeability.get(revision) != .clean) return; 2900 const author = ((try self.session.authorize(self.data.identity, .write)) orelse return).author; 2901 self.data.mergePatch(self.session, allocator, author, entry.id, revision) catch |err| switch (err) { 2902 error.MergeConflict, error.MergeCheckUnavailable, error.PatchOutOfDate, error.PatchDataUnavailable => { 2903 const route = listRoute(self.data.identity, entryStatus(entry.*), "", entry.id) orelse return; 2904 try self.session.navigate(route); 2905 return; 2906 }, 2907 else => return err, 2908 }; 2909 const merged = std.meta.stringToEnum(Status, "merged") orelse return; 2910 const route = listRoute(self.data.identity, merged, "", entry.id) orelse return; 2911 try self.session.navigate(route); 2912 } else unreachable, 2913 .remove => try self.submitRemove(allocator), 2914 } 2915 } 2916 2917 fn submitRemove(self: *This, allocator: std.mem.Allocator) !void { 2918 const comment = self.data.comment_id.len != 0; 2919 const event_id = if (comment) self.data.comment_id else self.data.selected_id; 2920 const id = evt.parseEventId(event_id) catch return; 2921 try self.removeEvent(allocator, if (comment) .comment else kind, id); 2922 } 2923 2924 fn removeEvent(self: *This, allocator: std.mem.Allocator, event_kind: evt.EventKind, id: [evt.event_id_size]u8) !void { 2925 if (comptime wasm) return; 2926 const actor = (try self.session.authorize(self.data.identity, .read)) orelse return; 2927 const author = actor.author; 2928 2929 if (comptime supports_drafts) { 2930 const selected = self.data.selectedThread() orelse return; 2931 if (event_kind == kind and entryDraft(selected.*)) { 2932 try Data.removeDraft(self.session, allocator, author, &id); 2933 const route = draftsRoute(self.data.identity) orelse return; 2934 try self.session.navigate(route); 2935 return; 2936 } 2937 } 2938 2939 const io = self.session.io orelse return; 2940 const src = self.data.repo_source orelse return; 2941 if (!try src.canModify(io, allocator, actor, event_kind, &id)) return; 2942 2943 switch (src.repo_kind) { 2944 inline else => |repo_kind| { 2945 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, src.localInitOpts()); 2946 defer any_repo.deinit(io, allocator); 2947 switch (any_repo) { 2948 inline else => |*repo| try evt.remove(self.session.data.host_kind, .repo, repo_kind, repo.self_repo_opts, io, allocator, repo, &id, event_kind, author), 2949 } 2950 }, 2951 } 2952 2953 const id_hex = std.fmt.bytesToHex(id, .lower); 2954 const route = ui.RoutablePage.repoEventsRoute(self.data.identity, .removed, event_kind, &id_hex, null) orelse return; 2955 try self.session.navigate(route); 2956 } 2957 2958 // return to the list. 2959 fn focusList(self: *This, index: usize, root_focus: *Focus) void { 2960 root_focus.setFocus(self.listScroll(index).getFocus().id); 2961 } 2962 2963 // cross to the header tabs above the stack. 2964 fn focusHeader(self: *This, root_focus: *Focus) void { 2965 root_focus.setFocus(self.box.children.keys()[header_index]); 2966 } 2967 2968 pub fn clearGrid(self: *This) void { 2969 self.box.clearGrid(); 2970 } 2971 2972 pub fn getGrid(self: This) ?Grid { 2973 return self.box.getGrid(); 2974 } 2975 2976 pub fn getFocus(self: *This) *Focus { 2977 return self.box.getFocus(); 2978 } 2979 2980 // for the parent's "scroll up at the top jumps to the header" check: at the 2981 // top only while the header tabs hold focus, so up from the split 2982 // crosses into the tabs first. 2983 pub fn atTop(self: *This) bool { 2984 return self.headerActive(); 2985 } 2986 }; 2987 } 2988