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