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