Merge branch 'feature/correctly-send-activities' into develop
[oweals/peertube.git] / server / helpers / custom-validators / activitypub / video-comments.ts
1 import * as validator from 'validator'
2 import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers'
3 import { exists, isArray, isDateValid } from '../misc'
4 import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
5
6 function sanitizeAndCheckVideoCommentObject (comment: any) {
7   if (!comment || comment.type !== 'Note') return false
8
9   normalizeComment(comment)
10
11   return isActivityPubUrlValid(comment.id) &&
12     isCommentContentValid(comment.content) &&
13     isActivityPubUrlValid(comment.inReplyTo) &&
14     isDateValid(comment.published) &&
15     isActivityPubUrlValid(comment.url) &&
16     isArray(comment.to) &&
17     (
18       comment.to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 ||
19       comment.cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1
20     ) // Only accept public comments
21 }
22
23 // ---------------------------------------------------------------------------
24
25 export {
26   sanitizeAndCheckVideoCommentObject
27 }
28
29 // ---------------------------------------------------------------------------
30
31 function isCommentContentValid (content: any) {
32   return exists(content) && validator.isLength('' + content, { min: 1 })
33 }
34
35 function normalizeComment (comment: any) {
36   if (!comment) return
37
38   if (typeof comment.url !== 'string') {
39     comment.url = comment.url.href || comment.url.url
40   }
41
42   return
43 }