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'
6 function sanitizeAndCheckVideoCommentObject (comment: any) {
7 if (!comment) return false
9 if (!isCommentTypeValid(comment)) return false
11 normalizeComment(comment)
13 if (comment.type === 'Tombstone') {
14 return isActivityPubUrlValid(comment.id) &&
15 isDateValid(comment.published) &&
16 isDateValid(comment.deleted) &&
17 isActivityPubUrlValid(comment.url)
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) &&
27 comment.to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 ||
28 comment.cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1
29 ) // Only accept public comments
32 // ---------------------------------------------------------------------------
35 sanitizeAndCheckVideoCommentObject
38 // ---------------------------------------------------------------------------
40 function isCommentContentValid (content: any) {
41 return exists(content) && validator.isLength('' + content, { min: 1 })
44 function normalizeComment (comment: any) {
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
55 function isCommentTypeValid (comment: any): boolean {
56 if (comment.type === 'Note') return true
58 if (comment.type === 'Tombstone' && comment.formerType === 'Note') return true