Skip to content

Commit

Permalink
Merge pull request #43 from velog-io/development
Browse files Browse the repository at this point in the history
Token 처리시에 user 조회 기능 추가 (cache와 함께)
  • Loading branch information
winverse authored Apr 2, 2024
2 parents 316e35e + d49516e commit 014ca8c
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 14 deletions.
13 changes: 6 additions & 7 deletions packages/velog-server/src/common/plugins/global/authPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@ const authPlugin: FastifyPluginAsync = async (fastify) => {
await userService.restoreToken({ request, reply })
}

// const user = await userService.findById(accessTokenData.user_id)

// if (!user) {
// cookie.clearCookie(reply, 'access_token')
// cookie.clearCookie(reply, 'refresh_token')
// throw new Error('User not found')
// }
const user = await userService.checkExistsUser(accessTokenData.user_id)
if (!user) {
cookie.clearCookie(reply, 'access_token')
cookie.clearCookie(reply, 'refresh_token')
throw new Error('User not found')
}

request.user = { id: accessTokenData.user_id }
return
Expand Down
15 changes: 13 additions & 2 deletions packages/velog-server/src/graphql/resolvers/postResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,19 @@ const postResolvers: Resolvers = {
Post: {
user: async (parent: PostIncludeUser) => {
if (!parent.user) {
const userService = container.resolve(UserService)
return await userService.getCurrentUser(parent.fk_user_id)
if (parent?.fk_user_id) {
const userService = container.resolve(UserService)
return await userService.getCurrentUser(parent.fk_user_id)
}

if (parent.id) {
const postService = container.resolve(PostService)
const userService = container.resolve(UserService)
const post = await postService.findById(parent.id)
return await userService.getCurrentUser(post?.fk_user_id)
}

return null
}
return parent?.user
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { injectable, singleton } from 'tsyringe'
import { BuildQueryService } from './BuildQueryService.js'
import { PostIncludeTags } from '@services/PostService/PostServiceInterface.js'
import { Post } from '@prisma/client'
import { UserService } from '@services/UserService/index.js'

interface Service {
get client(): Client
Expand All @@ -13,7 +14,10 @@ interface Service {
@injectable()
@singleton()
export class ElasticSearchService implements Service {
constructor(private readonly buildQueryService: BuildQueryService) {}
constructor(
private readonly userService: UserService,
private readonly buildQueryService: BuildQueryService,
) {}
public get client(): Client {
return new Client({ node: ENV.esHost })
}
Expand Down Expand Up @@ -147,14 +151,26 @@ export class ElasticSearchService implements Service {
},
})

const posts = result.body.hits.hits.map((hit: any) => hit._source)
posts.forEach((p: any) => {
p.released_at = new Date(p.released_at)
const sources = result.body.hits.hits
.map((hit: any) => hit._source)
.map((p: any) => ({ ...p, released_at: new Date(p.released_at) }))

const promises = sources.map(async (post: any) => {
try {
const result = await this.userService.checkExistsUser(post?.user?.id)
return { id: post.id, result }
} catch (error) {
console.error('Error checking user:', error)
return { id: post.id, result: false }
}
})

const promiseResult = await Promise.all(promises)
const existsUserPosts = promiseResult.filter(({ result }) => result).map(({ id }) => id)

const data = {
count: result.body.hits.total.value,
posts: result.body.hits.hits.map((hit: any) => hit._source),
posts: sources.filter((post: any) => existsUserPosts.includes(post.id)),
}

return data
Expand Down
2 changes: 2 additions & 0 deletions packages/velog-server/src/lib/redis/RedisService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class RedisService extends Redis implements Service {
`ssr:/@${username}/series/${seriesUrlSlug}`,
changeEmail: (code: string) => `changeEmailCode:${code}`,
trendingWriters: () => `trending:writers`,
existsUser: (userId: string) => `exists:user:${userId}`,
}
}

Expand Down Expand Up @@ -61,6 +62,7 @@ type GenerateRedisKey = {
postSeries: (username: string, seriesUrlSlug: string) => string
changeEmail: (code: string) => string
trendingWriters: () => string
existsUser: (userId: string) => string
}

type QueueName = 'createFeed' | 'checkPostSpam'
Expand Down
14 changes: 14 additions & 0 deletions packages/velog-server/src/services/UserService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ interface Service {
initiateChangeEmail(email: string, signedUserId?: string): Promise<void>
confirmChangeEmail(code: string, signedUserId?: string): Promise<void>
checkTrust(userId: string): Promise<boolean>
checkExistsUser(userId: string): Promise<boolean>
}

@injectable()
Expand Down Expand Up @@ -316,6 +317,19 @@ export class UserService implements Service {
const diffDays = differenceInDays(today, joinDay)
return diffDays > 20
}
public async checkExistsUser(userId?: string): Promise<boolean> {
if (!userId) return false

const key = this.redis.generateKey.existsUser(userId)
const value = await this.redis.get(key)
if (value === 'true') return true
if (value === 'false') return false

const user = await this.findById(userId)
const save = user ? 'true' : 'false'
await this.redis.set(key, save, 'EX', Time.ONE_MINUTE_IN_S * 10)
return !!user
}
}

type FindByIdOrUsernameArgs = {
Expand Down

0 comments on commit 014ca8c

Please sign in to comment.