Skip to content

Commit d1235e0

Browse files
committed
fix approval page bug
1 parent f3d3497 commit d1235e0

File tree

5 files changed

+45
-15
lines changed

5 files changed

+45
-15
lines changed

pages/api/open/approve.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { NextApiRequest, NextApiResponse } from 'next'
22
import { resolvedConfig } from '../../../utils.server'
33
import jwt from 'jsonwebtoken'
44
import { CommentService } from '../../../service/comment.service'
5-
import { SecretKey, TokenService } from '../../../service/token.service'
5+
import { SecretKey, TokenBody, TokenService } from '../../../service/token.service'
66

77
export default async function handler(
88
req: NextApiRequest,
@@ -49,22 +49,24 @@ export default async function handler(
4949
return
5050
}
5151

52-
let commentId
52+
let tokenBody: TokenBody.ApproveComment
5353

5454
try {
55-
commentId = tokenService.validate(token, SecretKey.ApproveComment).commentId
55+
tokenBody = tokenService.validate(token, SecretKey.ApproveComment) as TokenBody.ApproveComment
5656
} catch (e) {
5757
res.status(403)
5858
res.send('Invalid token')
5959
return
6060
}
6161

6262
// firstly, approve comment
63-
await commentService.approve(commentId)
63+
await commentService.approve(tokenBody.commentId)
6464

6565
// then append reply
6666
if (replyContent) {
67-
await commentService.addCommentAsModerator(commentId, replyContent)
67+
await commentService.addCommentAsModerator(tokenBody.commentId, replyContent, {
68+
owner: tokenBody.owner
69+
})
6870
}
6971

7072
res.json({

service/comment.service.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Comment, Page, Prisma } from '@prisma/client'
2-
import { RequestScopeService } from '.'
1+
import { Comment, Page, Prisma, User } from '@prisma/client'
2+
import { RequestScopeService, UserSession } from '.'
33
import { prisma, resolvedConfig } from '../utils.server'
44
import { PageService } from './page.service'
55
import dayjs from 'dayjs'
@@ -191,8 +191,13 @@ export class CommentService extends RequestScopeService {
191191
return created
192192
}
193193

194-
async addCommentAsModerator(parentId: string, content: string) {
195-
const session = await this.getSession()
194+
async addCommentAsModerator(parentId: string, content: string, options?: {
195+
owner?: User
196+
}) {
197+
const session = options?.owner ? {
198+
user: options.owner,
199+
uid: options.owner.id
200+
} : await this.getSession()
196201
const parent = await prisma.comment.findUnique({
197202
where: {
198203
id: parentId,

service/notification.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Comment } from '@prisma/client'
22
import { RequestScopeService } from '.'
3-
import { prisma, resolvedConfig } from '../utils.server'
3+
import { getSession, prisma, resolvedConfig } from '../utils.server'
44
import { UserService } from './user.service'
55
import { markdown } from './comment.service'
66
import { TokenService } from './token.service'
@@ -68,7 +68,8 @@ export class NotificationService extends RequestScopeService {
6868
let unsubscribeToken = this.tokenService.genUnsubscribeNewCommentToken(
6969
project.owner.id,
7070
)
71-
const approveToken = this.tokenService.genApproveToken(comment.id)
71+
72+
const approveToken = await this.tokenService.genApproveToken(comment.id)
7273

7374
const msg = {
7475
to: notificationEmail, // Change to your recipient

service/token.service.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import { User } from '@prisma/client'
12
import jwt from 'jsonwebtoken'
2-
import { resolvedConfig } from '../utils.server'
3+
import { UserSession } from '.'
4+
import { prisma, resolvedConfig } from '../utils.server'
35

46
export enum UnSubscribeType {
57
NEW_COMMENT = 'NEW_COMMENT',
@@ -17,7 +19,8 @@ export module TokenBody {
1719
}
1820

1921
export type ApproveComment = {
20-
commentId: string
22+
commentId: string,
23+
owner: User
2124
}
2225

2326
export type UnsubscribeNewComment = {
@@ -38,11 +41,30 @@ export class TokenService {
3841
}) as string
3942
}
4043

41-
genApproveToken(commentId: string) {
44+
async genApproveToken(commentId: string) {
45+
46+
const comment = await prisma.comment.findUnique({
47+
where: {
48+
id: commentId
49+
},
50+
select: {
51+
page: {
52+
select: {
53+
project: {
54+
select: {
55+
owner: true
56+
}
57+
}
58+
}
59+
}
60+
}
61+
})
62+
4263
return this.sign(
4364
SecretKey.ApproveComment,
4465
{
4566
commentId,
67+
owner: comment.page.project.owner,
4668
} as TokenBody.ApproveComment,
4769
'3 days',
4870
)

service/webhook.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class WebhookService extends RequestScopeService {
5454
},
5555
})
5656

57-
const approveToken = this.tokenService.genApproveToken(comment.id)
57+
const approveToken = await this.tokenService.genApproveToken(comment.id)
5858
const approveLink = `${resolvedConfig.host}/open/approve?token=${approveToken}`
5959

6060
statService.capture('webhook_trigger', {

0 commit comments

Comments
 (0)