-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcomment.ts
More file actions
141 lines (130 loc) · 3.56 KB
/
Copy pathcomment.ts
File metadata and controls
141 lines (130 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { VercelRequest, VercelResponse } from '@vercel/node'
import { DbCommentDoc, DbUserDoc } from '../types'
import { COLNAME } from './config'
import { v4 as UUID } from 'uuid'
import { attachUsers, router } from './utils'
import { Db } from 'mongodb'
import { RouteContextDefaults } from 'serverless-kit'
import { getUserModel } from './user'
const COMMENT_DEFAULTS: DbCommentDoc = {
target_type: 'post',
target_uuid: '',
uuid: '',
content: '',
author_uuid: '',
editor_uuid: '',
created_at: new Date(0),
edited_at: new Date(0),
is_deleted: false,
}
export function getCommentModel(payload: Partial<DbCommentDoc>) {
return {
...COMMENT_DEFAULTS,
...payload,
}
}
export default (req: VercelRequest, res: VercelResponse) => {
router.endpoint('/api/comment')
router.setCollection(COLNAME.COMMENT)
// Get comments for target
router
.addRoute()
.method('GET')
.path(['user', 'post', 'comment'], 'target_type')
.path(/.+/, 'target_uuid')
.parseOffsetLimitSort()
.check(checkCanCreate)
.action(async (ctx) => {
const filter = {
target_type: ctx.params.target_type,
target_uuid: ctx.params.target_uuid,
}
const total_comments = await ctx.col.find(filter).count()
const comments = await ctx.col
.find(filter)
.skip(ctx.offset)
.sort(ctx.sort)
.limit(ctx.limit + 1)
.toArray()
let has_next = false
if (comments.length > ctx.limit) {
has_next = true
comments.pop()
}
ctx.message = 'Get comments by filter'
ctx.body = {
comments: await attachUsers(ctx, comments),
filter,
total_comments,
offset: ctx.offset,
limit: ctx.limit,
sort: ctx.sort,
has_next,
}
})
// Add comment
router
.addRoute()
.method('POST')
.path(['user', 'post', 'comment'], 'target_type')
.path(/.+/, 'target_uuid')
.checkLogin()
.checkAuth(1)
.check<{
content: string
}>((ctx) => {
const content: string = ctx.req.body?.content || ''
if (!content.trim()) {
ctx.status = 400
ctx.message = 'Missing content'
return false
}
if (content.length > 1000) {
ctx.status = 413
ctx.message = 'The content should be less than 1000 words'
return false
}
ctx.content = content
})
.check(checkCanCreate)
.action(async (ctx) => {
const comment = getCommentModel({
author_uuid: ctx.user.uuid,
content: ctx.content,
created_at: new Date(),
target_type: ctx.params.target_type as 'user' | 'post' | 'comment',
target_uuid: ctx.params.target_uuid,
uuid: UUID(),
})
const dbRes = await ctx.col.insertOne(comment)
ctx.message = 'Comment created.'
ctx.body = {
comment: {
...comment,
author: getUserModel(ctx.user, true),
editor: getUserModel(null, true),
},
...dbRes,
}
})
return router.init(req, res)
}
async function checkCanCreate(
ctx: RouteContextDefaults & { db: Db; user: DbUserDoc }
) {
const target = await ctx.db
.collection(COLNAME[ctx.params.target_type.toUpperCase()])
.findOne({ uuid: ctx.params.target_uuid })
if (!target || target.is_deleted) {
ctx.status = 404
ctx.message = `Requested ${ctx.params.target_type} not found.`
return false
} else if (
target.is_private &&
!target.allowed_user.includes(ctx.user.uuid)
) {
ctx.status = 403
ctx.message = 'Permision denied'
return false
}
}