Skip to content

Commit

Permalink
PUT /api/v1/media/:id
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Jun 15, 2024
1 parent 2299751 commit 4a0fc1c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 23 deletions.
22 changes: 22 additions & 0 deletions src/api/v1/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,26 @@ app.get("/:id", async (c) => {
return c.json(serializeMedium(medium));
});

app.put("/:id", tokenRequired, scopeRequired(["write:media"]), async (c) => {
const mediumId = c.req.param("id");
let description: string | undefined;
try {
const json = await c.req.json();
description = json.description;
} catch (e) {
const form = await c.req.formData();
description = form.get("description")?.toString();
}
if (description == null) {
return c.json({ error: "description is required" }, 422);
}
const result = await db
.update(media)
.set({ description })
.where(eq(media.id, mediumId))
.returning();
if (result.length < 1) return c.json({ error: "Not found" }, 404);
return c.json(serializeMedium(result[0]));
});

export default app;
4 changes: 2 additions & 2 deletions src/api/v1/statuses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ app.post(
const content =
data.status == null ? null : await formatText(db, data.status, fedCtx);
const summary =
data.spoiler_text == null
data.spoiler_text == null || data.spoiler_text.trim() === ""
? null
: await formatText(db, data.spoiler_text, fedCtx);
const mentionedIds = [
Expand Down Expand Up @@ -198,7 +198,7 @@ app.put(
const content =
data.status == null ? null : await formatText(db, data.status, fedCtx);
const summary =
data.spoiler_text == null
data.spoiler_text == null || data.spoiler_text.trim() === ""
? null
: await formatText(db, data.spoiler_text, fedCtx);
const hashtags = [
Expand Down
72 changes: 55 additions & 17 deletions src/components/Post.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { FC } from "hono/jsx";
import type { Account, Post as DbPost } from "../schema";
import type { Account, Medium as DbMedium, Post as DbPost } from "../schema";

export interface PostProps {
post: DbPost & { account: Account };
post: DbPost & { account: Account; media: DbMedium[] };
}

export const Post: FC<PostProps> = ({ post }) => {
Expand All @@ -28,28 +28,16 @@ export const Post: FC<PostProps> = ({ post }) => {
</p>
</hgroup>
</header>
{post.summaryHtml == null ? (
post.contentHtml && (
<div
// biome-ignore lint/security/noDangerouslySetInnerHtml: xss
dangerouslySetInnerHTML={{ __html: post.contentHtml }}
lang={post.language ?? undefined}
/>
)
{post.summaryHtml == null || post.summaryHtml.trim() === "" ? (
<PostContent post={post} />
) : (
<details>
<summary
// biome-ignore lint/security/noDangerouslySetInnerHtml: xss
dangerouslySetInnerHTML={{ __html: post.summaryHtml }}
lang={post.language ?? undefined}
/>
{post.contentHtml && (
<div
// biome-ignore lint/security/noDangerouslySetInnerHtml: xss
dangerouslySetInnerHTML={{ __html: post.contentHtml }}
lang={post.language ?? undefined}
/>
)}
<PostContent post={post} />
</details>
)}
<footer>
Expand All @@ -67,4 +55,54 @@ export const Post: FC<PostProps> = ({ post }) => {
);
};

interface PostContentProps {
readonly post: DbPost & { media: DbMedium[] };
}

const PostContent: FC<PostContentProps> = ({ post }: PostContentProps) => {
return (
<>
{post.contentHtml && (
<div
// biome-ignore lint/security/noDangerouslySetInnerHtml: xss
dangerouslySetInnerHTML={{ __html: post.contentHtml }}
lang={post.language ?? undefined}
/>
)}
{post.media.length > 0 && (
<div>
{post.media.map((medium) =>
medium.description && medium.description.trim() !== "" ? (
<figure>
<Medium medium={medium} />
<figcaption>{medium.description}</figcaption>
</figure>
) : (
<Medium medium={medium} />
),
)}
</div>
)}
</>
);
};

interface MediumProps {
medium: DbMedium;
}

const Medium: FC<MediumProps> = ({ medium }) => {
return (
<a href={medium.url}>
<img
key={medium.id}
src={medium.thumbnailUrl}
alt={medium.description ?? ""}
width={medium.thumbnailWidth}
height={medium.thumbnailHeight}
/>
</a>
);
};

export default Post;
9 changes: 5 additions & 4 deletions src/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { db } from "./db";
import {
type Account,
type AccountOwner,
type Medium,
type Post,
accountOwners,
posts,
Expand All @@ -30,14 +31,14 @@ app.get("/", async (c) => {
or(eq(posts.visibility, "public"), eq(posts.visibility, "unlisted")),
),
orderBy: desc(posts.id),
with: { account: true },
with: { account: true, media: true },
});
return c.html(<ProfilePage accountOwner={owner} posts={postList} />);
});

export interface ProfilePageProps {
accountOwner: AccountOwner & { account: Account };
posts: (Post & { account: Account })[];
posts: (Post & { account: Account; media: Medium[] })[];
}

export const ProfilePage: FC<ProfilePageProps> = ({ accountOwner, posts }) => {
Expand Down Expand Up @@ -73,14 +74,14 @@ app.get("/:id", async (c) => {
eq(posts.id, postId),
or(eq(posts.visibility, "public"), eq(posts.visibility, "unlisted")),
),
with: { account: true },
with: { account: true, media: true },
});
if (post == null) return c.notFound();
return c.html(<PostPage post={post} />);
});

export interface PostPageProps {
post: Post & { account: Account };
post: Post & { account: Account; media: Medium[] };
}

export const PostPage: FC<PostPageProps> = ({ post }) => {
Expand Down

0 comments on commit 4a0fc1c

Please sign in to comment.