]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/custom-validators/activitypub/video-comments.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / activitypub / video-comments.ts
... / ...
CommitLineData
1import validator from 'validator'
2import { ACTIVITY_PUB } from '../../../initializers/constants'
3import { exists, isArray, isDateValid } from '../misc'
4import { isActivityPubUrlValid } from './misc'
5
6function sanitizeAndCheckVideoCommentObject (comment: any) {
7 if (!comment) return false
8
9 if (!isCommentTypeValid(comment)) return false
10
11 normalizeComment(comment)
12
13 if (comment.type === 'Tombstone') {
14 return isActivityPubUrlValid(comment.id) &&
15 isDateValid(comment.published) &&
16 isDateValid(comment.deleted) &&
17 isActivityPubUrlValid(comment.url)
18 }
19
20 return isActivityPubUrlValid(comment.id) &&
21 isCommentContentValid(comment.content) &&
22 isActivityPubUrlValid(comment.inReplyTo) &&
23 isDateValid(comment.published) &&
24 isActivityPubUrlValid(comment.url) &&
25 isArray(comment.to) &&
26 (
27 comment.to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 ||
28 comment.cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1
29 ) // Only accept public comments
30}
31
32// ---------------------------------------------------------------------------
33
34export {
35 sanitizeAndCheckVideoCommentObject
36}
37
38// ---------------------------------------------------------------------------
39
40function isCommentContentValid (content: any) {
41 return exists(content) && validator.isLength('' + content, { min: 1 })
42}
43
44function normalizeComment (comment: any) {
45 if (!comment) return
46
47 if (typeof comment.url !== 'string') {
48 if (typeof comment.url === 'object') comment.url = comment.url.href || comment.url.url
49 else comment.url = comment.id
50 }
51}
52
53function isCommentTypeValid (comment: any): boolean {
54 if (comment.type === 'Note') return true
55
56 if (comment.type === 'Tombstone' && comment.formerType === 'Note') return true
57
58 return false
59}