PeerTube/server/helpers/custom-validators/activitypub/video-comments.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

import * as validator from 'validator'
import { ACTIVITY_PUB } from '../../../initializers/constants'
2018-01-26 04:20:46 -06:00
import { exists, isArray, isDateValid } from '../misc'
import { isActivityPubUrlValid } from './misc'
2018-05-11 08:41:54 -05:00
function sanitizeAndCheckVideoCommentObject (comment: any) {
if (!comment || comment.type !== 'Note') return false
2018-05-11 08:41:54 -05:00
normalizeComment(comment)
return isActivityPubUrlValid(comment.id) &&
isCommentContentValid(comment.content) &&
isActivityPubUrlValid(comment.inReplyTo) &&
isDateValid(comment.published) &&
2018-01-26 04:20:46 -06:00
isActivityPubUrlValid(comment.url) &&
isArray(comment.to) &&
2018-04-04 02:52:45 -05:00
(
comment.to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 ||
comment.cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1
) // Only accept public comments
}
// ---------------------------------------------------------------------------
export {
2018-05-11 08:41:54 -05:00
sanitizeAndCheckVideoCommentObject
}
// ---------------------------------------------------------------------------
function isCommentContentValid (content: any) {
return exists(content) && validator.isLength('' + content, { min: 1 })
}
2018-05-11 08:41:54 -05:00
function normalizeComment (comment: any) {
if (!comment) return
2018-05-11 08:55:39 -05:00
if (typeof comment.url !== 'string') {
2018-05-11 08:41:54 -05:00
comment.url = comment.url.href || comment.url.url
}
return
}