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