Skip to content

Commit 7528dc6

Browse files
committed
Merge branch 'development' into feature/turborepo
2 parents fa82ab7 + d49516e commit 7528dc6

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

apps/server/src/common/plugins/global/authPlugin.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,12 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
3535
await userService.restoreToken({ request, reply })
3636
}
3737

38-
// const user = await userService.findById(accessTokenData.user_id)
39-
40-
// if (!user) {
41-
// cookie.clearCookie(reply, 'access_token')
42-
// cookie.clearCookie(reply, 'refresh_token')
43-
// throw new Error('User not found')
44-
// }
38+
const user = await userService.checkExistsUser(accessTokenData.user_id)
39+
if (!user) {
40+
cookie.clearCookie(reply, 'access_token')
41+
cookie.clearCookie(reply, 'refresh_token')
42+
throw new Error('User not found')
43+
}
4544

4645
request.user = { id: accessTokenData.user_id }
4746
return

apps/server/src/graphql/resolvers/postResolvers.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@ const postResolvers: Resolvers = {
1818
Post: {
1919
user: async (parent: PostIncludeUser) => {
2020
if (!parent.user) {
21-
const userService = container.resolve(UserService)
22-
return await userService.getCurrentUser(parent.fk_user_id)
21+
if (parent?.fk_user_id) {
22+
const userService = container.resolve(UserService)
23+
return await userService.getCurrentUser(parent.fk_user_id)
24+
}
25+
26+
if (parent.id) {
27+
const postService = container.resolve(PostService)
28+
const userService = container.resolve(UserService)
29+
const post = await postService.findById(parent.id)
30+
return await userService.getCurrentUser(post?.fk_user_id)
31+
}
32+
33+
return null
2334
}
2435
return parent?.user
2536
},

apps/server/src/lib/elasticSearch/ElasticSearchService.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { injectable, singleton } from 'tsyringe'
44
import { BuildQueryService } from './BuildQueryService.js'
55
import { PostIncludeTags } from '@services/PostService/PostServiceInterface.js'
66
import { Post } from '@packages/database/velog-rds'
7+
import { UserService } from '@services/UserService/index.js'
78

89
interface Service {
910
get client(): Client
@@ -13,7 +14,10 @@ interface Service {
1314
@injectable()
1415
@singleton()
1516
export class ElasticSearchService implements Service {
16-
constructor(private readonly buildQueryService: BuildQueryService) {}
17+
constructor(
18+
private readonly userService: UserService,
19+
private readonly buildQueryService: BuildQueryService,
20+
) {}
1721
public get client(): Client {
1822
return new Client({ node: ENV.esHost })
1923
}
@@ -147,14 +151,26 @@ export class ElasticSearchService implements Service {
147151
},
148152
})
149153

150-
const posts = result.body.hits.hits.map((hit: any) => hit._source)
151-
posts.forEach((p: any) => {
152-
p.released_at = new Date(p.released_at)
154+
const sources = result.body.hits.hits
155+
.map((hit: any) => hit._source)
156+
.map((p: any) => ({ ...p, released_at: new Date(p.released_at) }))
157+
158+
const promises = sources.map(async (post: any) => {
159+
try {
160+
const result = await this.userService.checkExistsUser(post?.user?.id)
161+
return { id: post.id, result }
162+
} catch (error) {
163+
console.error('Error checking user:', error)
164+
return { id: post.id, result: false }
165+
}
153166
})
154167

168+
const promiseResult = await Promise.all(promises)
169+
const existsUserPosts = promiseResult.filter(({ result }) => result).map(({ id }) => id)
170+
155171
const data = {
156172
count: result.body.hits.total.value,
157-
posts: result.body.hits.hits.map((hit: any) => hit._source),
173+
posts: sources.filter((post: any) => existsUserPosts.includes(post.id)),
158174
}
159175

160176
return data

apps/server/src/lib/redis/RedisService.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class RedisService extends Redis implements Service {
3333
`ssr:/@${username}/series/${seriesUrlSlug}`,
3434
changeEmail: (code: string) => `changeEmailCode:${code}`,
3535
trendingWriters: () => `trending:writers`,
36+
existsUser: (userId: string) => `exists:user:${userId}`,
3637
}
3738
}
3839

@@ -61,6 +62,7 @@ type GenerateRedisKey = {
6162
postSeries: (username: string, seriesUrlSlug: string) => string
6263
changeEmail: (code: string) => string
6364
trendingWriters: () => string
65+
existsUser: (userId: string) => string
6466
}
6567

6668
type QueueName = 'createFeed' | 'checkPostSpam'

apps/server/src/services/UserService/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ interface Service {
4141
initiateChangeEmail(email: string, signedUserId?: string): Promise<void>
4242
confirmChangeEmail(code: string, signedUserId?: string): Promise<void>
4343
checkTrust(userId: string): Promise<boolean>
44+
checkExistsUser(userId: string): Promise<boolean>
4445
}
4546

4647
@injectable()
@@ -316,6 +317,19 @@ export class UserService implements Service {
316317
const diffDays = differenceInDays(today, joinDay)
317318
return diffDays > 20
318319
}
320+
public async checkExistsUser(userId?: string): Promise<boolean> {
321+
if (!userId) return false
322+
323+
const key = this.redis.generateKey.existsUser(userId)
324+
const value = await this.redis.get(key)
325+
if (value === 'true') return true
326+
if (value === 'false') return false
327+
328+
const user = await this.findById(userId)
329+
const save = user ? 'true' : 'false'
330+
await this.redis.set(key, save, 'EX', Time.ONE_MINUTE_IN_S * 10)
331+
return !!user
332+
}
319333
}
320334

321335
type FindByIdOrUsernameArgs = {

0 commit comments

Comments
 (0)