From 4537a003adf0796aef697527c7ce1e90d900f881 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Wed, 6 Nov 2024 14:46:58 +0100 Subject: [PATCH] Refactor issue thread compute to the backend --- .../radicle-tauri/src/commands/cob/issue.rs | 10 +++ crates/radicle-tauri/src/lib.rs | 1 + .../radicle-types/bindings/cob/issue/Issue.ts | 3 +- .../bindings/cob/thread/Thread.ts | 8 ++ crates/radicle-types/src/cobs/issue.rs | 16 ++-- crates/radicle-types/src/cobs/thread.rs | 9 ++ crates/radicle-types/src/traits/issue.rs | 39 +++++++++ crates/test-http-api/src/api.rs | 10 +++ src/components/Icon.svelte | 1 + src/components/IssueTeaser.svelte | 4 +- src/views/repo/Issue.svelte | 50 +++++------ src/views/repo/router.ts | 10 ++- tests/e2e/repo/issue.spec.ts | 83 +++++++++++++++++++ tests/e2e/repo/issues.spec.ts | 2 - tests/e2e/theme.spec.ts | 12 ++- 15 files changed, 213 insertions(+), 45 deletions(-) create mode 100644 crates/radicle-types/bindings/cob/thread/Thread.ts diff --git a/crates/radicle-tauri/src/commands/cob/issue.rs b/crates/radicle-tauri/src/commands/cob/issue.rs index 6b5e17c23..212a3a33b 100644 --- a/crates/radicle-tauri/src/commands/cob/issue.rs +++ b/crates/radicle-tauri/src/commands/cob/issue.rs @@ -47,3 +47,13 @@ pub(crate) fn issue_by_id( ) -> Result, Error> { ctx.issue_by_id(rid, id).map_err(Error::from) } + +#[tauri::command] +pub(crate) fn comment_threads_by_issue_id( + ctx: tauri::State, + rid: identity::RepoId, + id: git::Oid, +) -> Result>, Error> { + ctx.comment_threads_by_issue_id(rid, id) + .map_err(Error::from) +} diff --git a/crates/radicle-tauri/src/lib.rs b/crates/radicle-tauri/src/lib.rs index 896f43576..ef61f8655 100644 --- a/crates/radicle-tauri/src/lib.rs +++ b/crates/radicle-tauri/src/lib.rs @@ -102,6 +102,7 @@ pub fn run() { cob::save_embed, cob::issue::list_issues, cob::issue::issue_by_id, + cob::issue::comment_threads_by_issue_id, cob::issue::create_issue, cob::issue::edit_issue, cob::patch::list_patches, diff --git a/crates/radicle-types/bindings/cob/issue/Issue.ts b/crates/radicle-types/bindings/cob/issue/Issue.ts index e2885e4bf..ccd304a22 100644 --- a/crates/radicle-types/bindings/cob/issue/Issue.ts +++ b/crates/radicle-types/bindings/cob/issue/Issue.ts @@ -10,7 +10,8 @@ export type Issue = { title: string; state: State; assignees: Array; - discussion: Array>; + body: Comment; + commentCount: number; labels: Array; timestamp: number; }; diff --git a/crates/radicle-types/bindings/cob/thread/Thread.ts b/crates/radicle-types/bindings/cob/thread/Thread.ts new file mode 100644 index 000000000..2a2fd73c4 --- /dev/null +++ b/crates/radicle-types/bindings/cob/thread/Thread.ts @@ -0,0 +1,8 @@ +// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. +import type { Comment } from "./Comment"; +import type { Never } from "../Never"; + +export type Thread = { + root: Comment; + replies: Array>; +}; diff --git a/crates/radicle-types/src/cobs/issue.rs b/crates/radicle-types/src/cobs/issue.rs index 47b2ea01d..f51060b6a 100644 --- a/crates/radicle-types/src/cobs/issue.rs +++ b/crates/radicle-types/src/cobs/issue.rs @@ -21,7 +21,9 @@ pub struct Issue { title: String, state: cobs::issue::State, assignees: Vec, - discussion: Vec>, + body: cobs::thread::Comment, + #[ts(type = "number")] + comment_count: usize, #[ts(as = "Vec")] labels: Vec, #[ts(type = "number")] @@ -30,6 +32,8 @@ pub struct Issue { impl Issue { pub fn new(id: &issue::IssueId, issue: &issue::Issue, aliases: &impl AliasStore) -> Self { + let (root_oid, root_comment) = issue.root(); + Self { id: id.to_string(), author: cobs::Author::new(*issue.author().id(), aliases), @@ -39,10 +43,12 @@ impl Issue { .assignees() .map(|did| cobs::Author::new(*did, aliases)) .collect::>(), - discussion: issue - .comments() - .map(|(id, c)| cobs::thread::Comment::::new(*id, c.clone(), aliases)) - .collect::>(), + body: cobs::thread::Comment::::new( + *root_oid, + root_comment.clone(), + aliases, + ), + comment_count: issue.replies().count(), labels: issue.labels().cloned().collect::>(), timestamp: issue.timestamp(), } diff --git a/crates/radicle-types/src/cobs/thread.rs b/crates/radicle-types/src/cobs/thread.rs index 2bdc6632e..a86285100 100644 --- a/crates/radicle-types/src/cobs/thread.rs +++ b/crates/radicle-types/src/cobs/thread.rs @@ -22,6 +22,15 @@ pub struct CreateReviewComment { pub embeds: Vec, } +#[derive(Serialize, TS)] +#[serde(rename_all = "camelCase")] +#[ts(export)] +#[ts(export_to = "cob/thread/")] +pub struct Thread { + pub root: Comment, + pub replies: Vec>, +} + #[derive(Serialize, TS)] #[serde(rename_all = "camelCase")] #[ts(export)] diff --git a/crates/radicle-types/src/traits/issue.rs b/crates/radicle-types/src/traits/issue.rs index 0bec2271b..07d9c7134 100644 --- a/crates/radicle-types/src/traits/issue.rs +++ b/crates/radicle-types/src/traits/issue.rs @@ -50,6 +50,45 @@ pub trait Issues: Profile { Ok::<_, Error>(issue) } + + fn comment_threads_by_issue_id( + &self, + rid: identity::RepoId, + id: git::Oid, + ) -> Result>, Error> { + let profile = self.profile(); + let repo = profile.storage.repository(rid)?; + let issues = profile.issues(&repo)?; + let issue = issues.get(&id.into())?; + + let aliases = &profile.aliases(); + let comments = issue.map(|issue| { + issue + .replies() + // Filter out replies that aren't top level replies + .filter(|c| { + let Some(oid) = c.1.reply_to() else { + return false; + }; + + oid == id + }) + .map(|(oid, c)| { + let root = cobs::thread::Comment::::new(*oid, c.clone(), aliases); + let replies = issue + .replies_to(oid) + .map(|(oid, c)| { + cobs::thread::Comment::::new(*oid, c.clone(), aliases) + }) + .collect::>(); + + cobs::thread::Thread { root, replies } + }) + .collect::>() + }); + + Ok::<_, Error>(comments) + } } pub trait IssuesMut: Profile { diff --git a/crates/test-http-api/src/api.rs b/crates/test-http-api/src/api.rs index 3af4d6782..ecabc88cc 100644 --- a/crates/test-http-api/src/api.rs +++ b/crates/test-http-api/src/api.rs @@ -64,6 +64,7 @@ pub fn router(ctx: Context) -> Router { .route("/create_issue_comment", post(create_issue_comment_handler)) .route("/edit_issue", post(edit_issue_handler)) .route("/issue_by_id", post(issue_handler)) + .route("/comment_threads_by_issue_id", post(issue_threads_handler)) .route("/list_patches", post(patches_handler)) .route("/patch_by_id", post(patch_handler)) .route("/revisions_by_patch", post(revision_handler)) @@ -275,6 +276,15 @@ async fn issue_handler( Ok::<_, Error>(Json(issue)) } +async fn issue_threads_handler( + State(ctx): State, + Json(IssueBody { rid, id }): Json, +) -> impl IntoResponse { + let issue_threads = ctx.comment_threads_by_issue_id(rid, id)?; + + Ok::<_, Error>(Json(issue_threads)) +} + #[derive(Serialize, Deserialize)] #[serde(rename_all = "camelCase")] struct PatchesBody { diff --git a/src/components/Icon.svelte b/src/components/Icon.svelte index fe58e740a..787d5c770 100644 --- a/src/components/Icon.svelte +++ b/src/components/Icon.svelte @@ -66,6 +66,7 @@ style:cursor={styleCursor} role="img" {onclick} + aria-label={`icon-${name}`} width={size} height={size} fill="currentColor" diff --git a/src/components/IssueTeaser.svelte b/src/components/IssueTeaser.svelte index 5832a561c..a65b4a266 100644 --- a/src/components/IssueTeaser.svelte +++ b/src/components/IssueTeaser.svelte @@ -85,10 +85,10 @@
{label}
{/each} - {#if issue.discussion.length > 1} + {#if issue.commentCount > 0}
- {issue.discussion.length - 1} + {issue.commentCount}
{/if} diff --git a/src/views/repo/Issue.svelte b/src/views/repo/Issue.svelte index 803f0cc11..fce2cfa14 100644 --- a/src/views/repo/Issue.svelte +++ b/src/views/repo/Issue.svelte @@ -5,6 +5,7 @@ import type { Issue } from "@bindings/cob/issue/Issue"; import type { Operation } from "@bindings/cob/issue/Operation"; import type { RepoInfo } from "@bindings/repo/RepoInfo"; + import type { Thread } from "@bindings/cob/thread/Thread"; import partial from "lodash/partial"; import { tick } from "svelte"; @@ -31,7 +32,7 @@ import Link from "@app/components/Link.svelte"; import NodeId from "@app/components/NodeId.svelte"; import TextInput from "@app/components/TextInput.svelte"; - import Thread from "@app/components/Thread.svelte"; + import ThreadComponent from "@app/components/Thread.svelte"; import Layout from "./Layout.svelte"; @@ -41,6 +42,7 @@ issues: Issue[]; activity: Operation[]; config: Config; + threads: Thread[]; } /* eslint-disable prefer-const */ @@ -50,6 +52,7 @@ issues: initialIssues, activity, config, + threads, }: Props = $props(); /* eslint-enable prefer-const */ @@ -73,23 +76,6 @@ }); const project = $derived(repo.payloads["xyz.radicle.project"]!); - const issueBody = $derived(issue.discussion[0]); - const threads = $derived( - issue.discussion - .filter( - comment => - (comment.id !== issueBody.id && !comment.replyTo) || - comment.replyTo === issueBody.id, - ) - .map(thread => { - return { - root: thread, - replies: issue.discussion - .filter(comment => comment.replyTo === thread.id) - .sort((a, b) => a.edits[0].timestamp - b.edits[0].timestamp), - }; - }, []), - ); async function toggleReply() { topLevelReplyOpen = !topLevelReplyOpen; @@ -105,7 +91,7 @@ } async function reload() { - [issue, activity] = await Promise.all([ + [issue, activity, threads] = await Promise.all([ invoke("issue_by_id", { rid: repo.rid, id: issue.id, @@ -115,6 +101,10 @@ typeName: "xyz.radicle.issue", id: issue.id, }), + invoke("comment_threads_by_issue_id", { + rid: repo.rid, + id: issue.id, + }), ]); } @@ -410,7 +400,7 @@ {:else}
- {#if roles.isDelegateOrAuthor( config.publicKey, repo.delegates.map(delegate => delegate.did), issueBody.author.did, )} + {#if roles.isDelegateOrAuthor( config.publicKey, repo.delegates.map(delegate => delegate.did), issue.body.author.did, )}
1 - ? issueBody.edits.at(-1) + lastEdit={issue.body.edits.length > 1 + ? issue.body.edits.at(-1) : undefined} - author={issueBody.author} - reactions={issueBody.reactions} - timestamp={issueBody.edits.slice(-1)[0].timestamp} - body={issueBody.edits.slice(-1)[0].body} + author={issue.body.author} + reactions={issue.body.reactions} + timestamp={issue.body.edits.slice(-1)[0].timestamp} + body={issue.body.edits.slice(-1)[0].body} editComment={roles.isDelegateOrAuthor( config.publicKey, repo.delegates.map(delegate => delegate.did), - issueBody.author.did, - ) && partial(editComment, issueBody.id)} + issue.body.author.did, + ) && partial(editComment, issue.body.id)} reactOnComment={partial( reactOnComment, config.publicKey, - issueBody.id, + issue.body.id, )}> {#snippet actions()} @@ -461,7 +451,7 @@ {:else if op.type === "comment"} {@const thread = threads.find(t => t.root.id === op.entryId)} {#if thread} - { - const [config, repo, issue, activity, issues] = await Promise.all([ + const [config, repo, issue, activity, issues, threads] = await Promise.all([ invoke("config"), invoke("repo_by_id", { rid: route.rid, @@ -194,11 +196,15 @@ export async function loadIssue( rid: route.rid, status: "all", }), + invoke("comment_threads_by_issue_id", { + rid: route.rid, + id: route.issue, + }), ]); return { resource: "repo.issue", - params: { repo, config, issue, activity, issues }, + params: { repo, config, issue, activity, issues, threads }, }; } diff --git a/tests/e2e/repo/issue.spec.ts b/tests/e2e/repo/issue.spec.ts index 23861a3aa..57d1a9f95 100644 --- a/tests/e2e/repo/issue.spec.ts +++ b/tests/e2e/repo/issue.spec.ts @@ -6,3 +6,86 @@ test("navigate single issue", async ({ page }) => { await expect(page).toHaveURL(/\/issues\/[0-9a-f]{40}/); }); + +test("correct order of threads", async ({ page }) => { + await page.goto("/repos"); + await page.getByRole("button", { name: "cobs" }).click(); + await page + .getByRole("button", { name: "This title has **markdown**" }) + .click(); + const body = page.locator(".issue-body"); + await expect(body.getByText("This is a description")).toBeVisible(); + + const topLevelComments = await page.locator(".comments").all(); + expect(topLevelComments).toHaveLength(2); + + const [first, second] = topLevelComments; + await expect(first.getByText("This is a multiline comment")).toBeVisible(); + await expect( + first.getByText("This is a reply, to a first comment"), + ).toBeVisible(); + await expect( + second.getByText("A root level comment after a reply, for margins sake."), + ).toBeVisible(); +}); + +test("creation of top level comments", async ({ page }) => { + await page.goto("/repos"); + await page.getByRole("button", { name: "cobs" }).click(); + await page.getByRole("button", { name: "New" }).click(); + await page + .getByPlaceholder("Title") + .fill("Make sure that comment creation is working"); + await page + .getByPlaceholder("Description") + .fill( + "It's important for us that the comment creation flow works as expected.", + ); + await page.getByRole("button", { name: "Save" }).click(); + await expect( + page.getByText("Make sure that comment creation is working").last(), + ).toBeVisible(); + await expect( + page.getByRole("link", { name: "icon-issue Make sure that" }), + ).toBeVisible(); + await expect( + page + .getByText( + "It's important for us that the comment creation flow works as expected.", + ) + .last(), + ).toBeVisible(); + + await page.getByRole("button", { name: "Leave a comment" }).click(); + await page + .getByPlaceholder("Leave a comment") + .fill("A top level comment by playwright"); + await page.getByRole("button", { name: "icon-checkmark Comment" }).click(); + await expect( + page.getByText("A top level comment by playwright"), + ).toBeVisible(); + + await page.getByLabel("icon-reply").first().click(); + await page + .getByPlaceholder("Leave a comment") + .fill( + "A top level comment by playwright created by replying to the issue body", + ); + await page.getByRole("button", { name: "icon-checkmark Comment" }).click(); + await expect( + page.getByText( + "A top level comment by playwright created by replying to the issue body", + ), + ).toBeVisible(); + + await page.getByLabel("icon-reply").nth(1).click(); + await page + .getByPlaceholder("Reply to comment") + .fill("A reply comment by playwright replying to the first comment"); + await page.getByRole("button", { name: "icon-checkmark Reply" }).click(); + await expect( + page.getByText( + "A reply comment by playwright replying to the first comment", + ), + ).toBeVisible(); +}); diff --git a/tests/e2e/repo/issues.spec.ts b/tests/e2e/repo/issues.spec.ts index a6084fcfd..4c9b3c937 100644 --- a/tests/e2e/repo/issues.spec.ts +++ b/tests/e2e/repo/issues.spec.ts @@ -2,8 +2,6 @@ import { test, cobRid, expect } from "@tests/support/fixtures.js"; test("navigate issues listing", async ({ page }) => { await page.goto(`/repos/${cobRid}/issues?show=all`); - await expect(page.locator(".issue-teaser")).toHaveCount(3); - await page.getByRole("link", { name: "Closed" }).click(); await expect(page.locator(".issue-teaser")).toHaveCount(2); await expect(page).toHaveURL(`/repos/${cobRid}/issues?status=closed`); diff --git a/tests/e2e/theme.spec.ts b/tests/e2e/theme.spec.ts index 8d0cde7d5..ef6eca009 100644 --- a/tests/e2e/theme.spec.ts +++ b/tests/e2e/theme.spec.ts @@ -11,7 +11,9 @@ test("theme persistence", async ({ page }) => { await expect(page.getByRole("button", { name: "markdown" })).toBeVisible(); await page.getByRole("button", { name: "Settings" }).click(); - await page.getByRole("button", { name: "Light", exact: true }).click(); + await page + .getByRole("button", { name: "icon-sun Light", exact: true }) + .click(); await expect(page.locator("html")).toHaveAttribute("data-theme", "light"); await page.reload(); @@ -24,9 +26,13 @@ test("change theme", async ({ page }) => { await expect(page.getByRole("button", { name: "markdown" })).toBeVisible(); await page.getByRole("button", { name: "Settings" }).click(); - await page.getByRole("button", { name: "Light", exact: true }).click(); + await page + .getByRole("button", { name: "icon-sun Light", exact: true }) + .click(); await expect(page.locator("html")).toHaveAttribute("data-theme", "light"); - await page.getByRole("button", { name: "Dark", exact: true }).click(); + await page + .getByRole("button", { name: "icon-moon Dark", exact: true }) + .click(); await expect(page.locator("html")).toHaveAttribute("data-theme", "dark"); });