Browse Source

feat(posts): sync media refs on create/update/delete

Made-with: Cursor
main
npmrun 12 hours ago
parent
commit
bf116d1737
  1. 13
      server/service/posts/index.ts

13
server/service/posts/index.ts

@ -1,5 +1,6 @@
import { dbGlobal } from "drizzle-pkg/lib/db"; 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 { users } from "drizzle-pkg/lib/schema/auth";
import { and, desc, eq } from "drizzle-orm"; import { and, desc, eq } from "drizzle-orm";
import { visibilitySchema, type Visibility } from "#server/constants/visibility"; import { visibilitySchema, type Visibility } from "#server/constants/visibility";
@ -61,6 +62,7 @@ export async function createPost(
visibility: vis, visibility: vis,
shareToken, shareToken,
}); });
await syncPostMediaRefs(userId, id, input.bodyMarkdown, input.coverUrl ?? null);
return getPostForUser(userId, id); return getPostForUser(userId, id);
} }
@ -105,6 +107,9 @@ export async function updatePost(
}) })
.where(and(eq(posts.id, id), eq(posts.userId, userId))); .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); return getPostForUser(userId, id);
} }
@ -113,7 +118,13 @@ export async function deletePost(userId: number, id: number) {
if (!existing) { if (!existing) {
return false; 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 dbGlobal.delete(posts).where(and(eq(posts.id, id), eq(posts.userId, userId)));
await reconcileAssetTimestampsAfterRefChange(touched);
return true; return true;
} }

Loading…
Cancel
Save