diff --git a/server/service/posts/index.ts b/server/service/posts/index.ts index 6b8481a..ba183ec 100644 --- a/server/service/posts/index.ts +++ b/server/service/posts/index.ts @@ -1,5 +1,6 @@ import { dbGlobal } from "drizzle-pkg/lib/db"; -import { posts } from "drizzle-pkg/lib/schema/content"; +import { postMediaRefs, posts } from "drizzle-pkg/lib/schema/content"; +import { reconcileAssetTimestampsAfterRefChange, syncPostMediaRefs } from "#server/service/media"; import { users } from "drizzle-pkg/lib/schema/auth"; import { and, desc, eq } from "drizzle-orm"; import { visibilitySchema, type Visibility } from "#server/constants/visibility"; @@ -61,6 +62,7 @@ export async function createPost( visibility: vis, shareToken, }); + await syncPostMediaRefs(userId, id, input.bodyMarkdown, input.coverUrl ?? null); return getPostForUser(userId, id); } @@ -105,6 +107,9 @@ export async function updatePost( }) .where(and(eq(posts.id, id), eq(posts.userId, userId))); + const body = patch.bodyMarkdown !== undefined ? patch.bodyMarkdown : existing.bodyMarkdown; + const cover = patch.coverUrl !== undefined ? patch.coverUrl : existing.coverUrl; + await syncPostMediaRefs(userId, id, body, cover ?? null); return getPostForUser(userId, id); } @@ -113,7 +118,13 @@ export async function deletePost(userId: number, id: number) { if (!existing) { return false; } + const refRows = await dbGlobal + .select({ assetId: postMediaRefs.assetId }) + .from(postMediaRefs) + .where(eq(postMediaRefs.postId, id)); + const touched = refRows.map((r) => r.assetId); await dbGlobal.delete(posts).where(and(eq(posts.id, id), eq(posts.userId, userId))); + await reconcileAssetTimestampsAfterRefChange(touched); return true; }