Skip to content

Commit

Permalink
Add simplified crosspost body when title is copied (#1519)
Browse files Browse the repository at this point in the history
  • Loading branch information
aeharding authored Jun 8, 2024
1 parent 42494ab commit c4537b8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export default function CreateCrosspostDialog({
name: title,
url: post.post.url,
nsfw: post.post.nsfw,
body: buildCrosspostBody(post.post),
body: buildCrosspostBody(post.post, title !== post.post.name),
community_id: community.community.id,
});
} catch (error) {
Expand Down
84 changes: 83 additions & 1 deletion src/helpers/lemmy.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { keywordFoundInSentence } from "./lemmy";
import { Post } from "lemmy-js-client";
import { buildCrosspostBody, keywordFoundInSentence } from "./lemmy";

describe("keywordFoundInSentence", () => {
it("false when empty", () => {
Expand Down Expand Up @@ -57,3 +58,84 @@ describe("keywordFoundInSentence", () => {
);
});
});

describe("buildCrosspostBody", () => {
it("url only", () => {
expect(
buildCrosspostBody(
{ name: "Hi there", ap_id: "https://a.com/post/123" } as Post,
false,
),
).toBe("cross-posted from: https://a.com/post/123");
});

it("with title", () => {
expect(
buildCrosspostBody(
{ name: "Hi there", ap_id: "https://a.com/post/123" } as Post,
true,
),
).toBe(
`
cross-posted from: https://a.com/post/123
> Hi there`.trim(),
);
});

it("with title and body", () => {
expect(
buildCrosspostBody(
{
name: "Hi there",
ap_id: "https://a.com/post/123",
body: "Test",
} as Post,
true,
),
).toBe(
`
cross-posted from: https://a.com/post/123
> Hi there
>
> Test`.trim(),
);
});

it("with body only", () => {
expect(
buildCrosspostBody(
{
name: "Hi there",
ap_id: "https://a.com/post/123",
body: "Test",
} as Post,
false,
),
).toBe(
`
cross-posted from: https://a.com/post/123
> Test`.trim(),
);
});

it("trims body", () => {
expect(
buildCrosspostBody(
{
name: "Hi there",
ap_id: "https://a.com/post/123",
body: "Test\n",
} as Post,
false,
),
).toBe(
`
cross-posted from: https://a.com/post/123
> Test`.trim(),
);
});
});
14 changes: 11 additions & 3 deletions src/helpers/lemmy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,20 @@ export function getCrosspostUrl(post: Post): string | undefined {
return matches?.[1];
}

export function buildCrosspostBody(post: Post): string {
const header = `cross-posted from: ${post.ap_id}\n\n${quote(post.name)}`;
export function buildCrosspostBody(post: Post, includeTitle = true): string {
let header = `cross-posted from: ${post.ap_id}`;

if (includeTitle) {
header += `\n\n${quote(post.name)}`;
}

if (!post.body) return header;

return `${header}\n>\n${quote(post.body)}`;
header += `\n${includeTitle ? ">" : ""}\n`;

header += quote(post.body.trim());

return header;
}

export function isPost(item: PostView | CommentView): item is PostView {
Expand Down

0 comments on commit c4537b8

Please sign in to comment.