Skip to content

Commit

Permalink
탬플릿 gql
Browse files Browse the repository at this point in the history
  • Loading branch information
robin-maki committed Jun 24, 2024
1 parent 9afb004 commit 722a01e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
1 change: 1 addition & 0 deletions apps/website/src/lib/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export const PostState = {
DRAFT: 'DRAFT',
EPHEMERAL: 'EPHEMERAL',
PUBLISHED: 'PUBLISHED',
TEMPLATE: 'TEMPLATE',
} as const;

export const PostSynchronizationKind = {
Expand Down
56 changes: 45 additions & 11 deletions apps/website/src/lib/server/graphql/schemas/post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { init as cuid } from '@paralleldrive/cuid2';
import * as Sentry from '@sentry/sveltekit';
import { getSchema } from '@tiptap/core';
import { Node } from '@tiptap/pm/model';
import { hash, verify } from 'argon2';
import dayjs from 'dayjs';
import { and, asc, count, desc, eq, exists, gt, gte, isNotNull, isNull, lt, ne, notExists, or, sql } from 'drizzle-orm';
Expand Down Expand Up @@ -28,6 +30,7 @@ import {
BookmarkGroups,
database,
inArray,
notInArray,
PostComments,
PostContentSnapshots,
PostContentStates,
Expand Down Expand Up @@ -71,6 +74,7 @@ import {
useFirstRow,
useFirstRowOrThrow,
} from '$lib/server/utils';
import { extensions } from '$lib/tiptap';
import { base36To10, createEmptyTiptapDocumentNode } from '$lib/utils';
import { PublishPostInputSchema } from '$lib/validations/post';
import { builder } from '../builder';
Expand Down Expand Up @@ -1003,6 +1007,8 @@ const CreatePostInput = builder.inputType('CreatePostInput', {
fields: (t) => ({
spaceId: t.id({ required: false }),
collectionId: t.id({ required: false }),
isTemplate: t.boolean({ defaultValue: false }),
usingTemplate: t.id({ required: false }),
}),
});

Expand Down Expand Up @@ -1279,23 +1285,47 @@ builder.mutationFields((t) => ({
}

return await database.transaction(async (tx) => {
const publishOptions = input.usingTemplate
? await database
.select({
visibility: Posts.visibility,
discloseStats: Posts.discloseStats,
receiveFeedback: Posts.receiveFeedback,
receivePatronage: Posts.receivePatronage,
receiveTagContribution: Posts.receiveTagContribution,
protectContent: Posts.protectContent,
})
.from(Posts)
.where(and(eq(Posts.id, input.usingTemplate)))
.then(useFirstRowOrThrow(new NotFoundError()))
: ({
visibility: 'PUBLIC',
discloseStats: true,
receiveFeedback: true,
receivePatronage: true,
receiveTagContribution: true,
protectContent: true,
} as const);

const [post] = await tx
.insert(Posts)
.values({
permalink,
userId: context.session.userId,
spaceId: input.spaceId,
state: 'EPHEMERAL',
visibility: 'PUBLIC',
discloseStats: true,
receiveFeedback: true,
receivePatronage: true,
receiveTagContribution: true,
protectContent: true,
state: input.isTemplate ? 'TEMPLATE' : 'EPHEMERAL',
...publishOptions,
})
.returning({ id: Posts.id });

const node = createEmptyTiptapDocumentNode();
const node = input.usingTemplate
? await tx
.select({ content: PostContentStates.content })
.from(PostContentStates)
.where(eq(PostContentStates.postId, input.usingTemplate))
.then((rows) => Node.fromJSON(getSchema(extensions), rows[0].content))
: createEmptyTiptapDocumentNode();

const doc = prosemirrorToYDoc(node, 'content');
const update = Y.encodeStateAsUpdateV2(doc);
const vector = Y.encodeStateVector(doc);
Expand Down Expand Up @@ -1350,7 +1380,7 @@ builder.mutationFields((t) => ({
.where(
and(
eq(Posts.id, input.postId),
ne(Posts.state, 'DELETED'),
notInArray(Posts.state, ['DELETED', 'TEMPLATE']),
or(eq(Spaces.state, 'ACTIVE'), isNull(Spaces.id)),
),
);
Expand Down Expand Up @@ -1519,7 +1549,9 @@ builder.mutationFields((t) => ({
.select({ userId: Posts.userId, space: { id: Spaces.id } })
.from(Posts)
.innerJoin(Spaces, eq(Spaces.id, Posts.spaceId))
.where(and(eq(Posts.id, input.postId), eq(Posts.state, 'PUBLISHED'), eq(Spaces.state, 'ACTIVE')));
.where(
and(eq(Posts.id, input.postId), inArray(Posts.state, ['PUBLISHED', 'TEMPLATE']), eq(Spaces.state, 'ACTIVE')),
);

if (posts.length === 0) {
throw new NotFoundError();
Expand Down Expand Up @@ -1565,7 +1597,9 @@ builder.mutationFields((t) => ({
.select({ userId: Posts.userId, space: { id: Spaces.id } })
.from(Posts)
.innerJoin(Spaces, eq(Spaces.id, Posts.spaceId))
.where(and(eq(Posts.id, input.postId), eq(Posts.state, 'PUBLISHED'), eq(Spaces.state, 'ACTIVE')));
.where(
and(eq(Posts.id, input.postId), inArray(Posts.state, ['PUBLISHED', 'TEMPLATE']), eq(Spaces.state, 'ACTIVE')),
);

if (posts.length === 0) {
throw new NotFoundError();
Expand Down
22 changes: 22 additions & 0 deletions apps/website/src/lib/server/graphql/schemas/template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { and, eq } from 'drizzle-orm';
import { database, Posts } from '$lib/server/database';
import { builder } from '../builder';
import { Post } from './post';

builder.inputType('CreateTemplateBasedPostInput', {
fields: (t) => ({
templateId: t.id(),
}),
});

builder.queryFields((t) => ({
templatePosts: t.withAuth({ user: true }).field({
type: [Post],
resolve: async (_, __, context) => {
return await database
.select()
.from(Posts)
.where(and(eq(Posts.state, 'TEMPLATE'), eq(Posts.userId, context.session.userId)));
},
}),
}));

0 comments on commit 722a01e

Please sign in to comment.