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 isTypeValid (comment: any): boolean {
7 if (comment.type === 'Note') return true
9 if (comment.type === 'Tombstone' && comment.formerType === 'Note') return true
14 function sanitizeAndCheckVideoCommentObject (comment: any) {
15 if (!comment) return false
17 if (!isTypeValid(comment)) return false
19 normalizeComment(comment)
21 if (comment.type === 'Tombstone') {
22 return isActivityPubUrlValid(comment.id) &&
23 isDateValid(comment.published) &&
24 isDateValid(comment.deleted) &&
25 isActivityPubUrlValid(comment.url)
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) &&
35 comment.to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 ||
36 comment.cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1
37 ) // Only accept public comments
40 // ---------------------------------------------------------------------------
43 sanitizeAndCheckVideoCommentObject
46 // ---------------------------------------------------------------------------
48 function isCommentContentValid (content: any) {
49 return exists(content) && validator.isLength('' + content, { min: 1 })
52 function normalizeComment (comment: any) {
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