Skip to content

Commit 0e51bf5

Browse files
committed
fix: not including users in Elasticsearch data
1 parent 54018d1 commit 0e51bf5

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

packages/velog-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
},

packages/velog-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 '@prisma/client'
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

packages/velog-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'

packages/velog-server/src/services/UserService/index.ts

Lines changed: 12 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,17 @@ 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+
const key = this.redis.generateKey.existsUser(userId)
323+
const value = await this.redis.get(key)
324+
if (value === 'true') return true
325+
if (value === 'false') return false
326+
const user = await this.findById(userId)
327+
const save = user ? 'true' : 'false'
328+
await this.redis.set(key, save, 'EX', Time.ONE_MINUTE_IN_S * 10)
329+
return !!user
330+
}
319331
}
320332

321333
type FindByIdOrUsernameArgs = {

0 commit comments

Comments
 (0)