Skip to content

Commit 3d75eb0

Browse files
committed
fix: temp posts performance issue
1 parent cd26cbe commit 3d75eb0

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

src/graphql/post.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import imageService from '../services/imageService';
2727
import externalInterationService from '../services/externalIntegrationService';
2828
import postService from '../services/postService';
2929
import postHistoryService, { CreatePostHistoryArgs } from '../services/postHistoryService';
30+
import userService from '../services/userService';
3031

3132
type ReadingListQueryParams = {
3233
type: 'LIKED' | 'READ';
@@ -429,6 +430,25 @@ export const resolvers: IResolvers<any, ApolloContext> = {
429430
throw new ApolloError('Max limit is 100', 'BAD_REQUEST');
430431
}
431432

433+
if (temp_only) {
434+
if (!username) {
435+
throw new ApolloError('username is missing', 'BAD_REQUEST');
436+
}
437+
if (!context.user_id) {
438+
throw new ApolloError('Not logged in', 'NO_PERMISSION');
439+
}
440+
const user = await userService.findUserByUsername(username);
441+
if (!user) {
442+
throw new ApolloError('Invalid username', 'NOT_FOUND');
443+
}
444+
if (user.id !== context.user_id) {
445+
throw new ApolloError('You have no permission to load temp posts', 'NO_PERMISSION');
446+
}
447+
return postService.findTempPosts(context.user_id, limit, cursor);
448+
}
449+
450+
console.log('posts query', { cursor, limit, username, temp_only, tag });
451+
432452
const userRepo = getRepository(User);
433453
const user = username
434454
? await userRepo.findOne({

src/services/postService.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@ if (!API_V3_HOST) {
1414
}
1515

1616
const postService = {
17+
async findTempPosts(userId: string, limit: number, cursor?: string) {
18+
const cursorPost = cursor
19+
? await db.post.findUnique({
20+
where: {
21+
id: cursor,
22+
},
23+
})
24+
: null;
25+
26+
const posts = await db.post.findMany({
27+
where: {
28+
fk_user_id: userId,
29+
is_temp: true,
30+
released_at: cursorPost?.released_at ? { lt: cursorPost.released_at } : undefined,
31+
},
32+
include: {
33+
postTags: {
34+
include: {
35+
tag: true,
36+
},
37+
},
38+
user: true,
39+
},
40+
orderBy: {
41+
created_at: 'desc',
42+
},
43+
take: limit,
44+
});
45+
46+
return posts.map(this.serialize);
47+
},
1748
async findPostsByUserId({ userId, size, cursor, isUserSelf = false }: FindPostParams) {
1849
const cursorPost = cursor
1950
? await db.post.findUnique({
@@ -233,6 +264,7 @@ const postService = {
233264
url: `https://velog.io/@${post.user.username}/${encodeURI(post.url_slug ?? '')}`,
234265
title: post.title!,
235266
thumbnail: post.thumbnail,
267+
created_at: post.created_at,
236268
released_at: post.released_at!,
237269
updated_at: post.updated_at!,
238270
short_description: shortDescription,

src/services/userService.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ const userService = {
4343
thumbnail: user.userProfile!.thumbnail,
4444
};
4545
},
46+
async findUserByUsername(username: string) {
47+
const user = await db.user.findUnique({
48+
where: {
49+
username,
50+
},
51+
});
52+
return user;
53+
},
4654
async findUserById(id: string): Promise<User | null> {
4755
const user = await db.user.findUnique({
4856
where: {

0 commit comments

Comments
 (0)