2017-12-22 02:14:50 -06:00
|
|
|
import * as validator from 'validator'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { ACTIVITY_PUB } from '../../../initializers/constants'
|
2018-01-26 04:20:46 -06:00
|
|
|
import { exists, isArray, isDateValid } from '../misc'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { isActivityPubUrlValid } from './misc'
|
2017-12-22 02:14:50 -06:00
|
|
|
|
2019-12-03 14:48:31 -06:00
|
|
|
function isTypeValid (comment: any): boolean {
|
|
|
|
if (comment.type === 'Note') return true
|
|
|
|
|
|
|
|
if (comment.type === 'Tombstone' && comment.formerType === 'Note') return true
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-05-11 08:41:54 -05:00
|
|
|
function sanitizeAndCheckVideoCommentObject (comment: any) {
|
2019-12-03 14:48:31 -06:00
|
|
|
if (!comment) return false
|
|
|
|
|
|
|
|
if (!isTypeValid(comment)) return false
|
2018-05-11 08:41:54 -05:00
|
|
|
|
|
|
|
normalizeComment(comment)
|
|
|
|
|
2019-12-03 14:48:31 -06:00
|
|
|
if (comment.type === 'Tombstone') {
|
|
|
|
return isActivityPubUrlValid(comment.id) &&
|
|
|
|
isDateValid(comment.published) &&
|
|
|
|
isDateValid(comment.deleted) &&
|
|
|
|
isActivityPubUrlValid(comment.url)
|
|
|
|
}
|
|
|
|
|
2018-05-11 08:41:54 -05:00
|
|
|
return isActivityPubUrlValid(comment.id) &&
|
2017-12-22 02:14:50 -06:00
|
|
|
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
|
2017-12-22 02:14:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-05-11 08:41:54 -05:00
|
|
|
sanitizeAndCheckVideoCommentObject
|
2017-12-22 02:14:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
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') {
|
2019-05-31 08:14:40 -05:00
|
|
|
if (typeof comment.url === 'object') comment.url = comment.url.href || comment.url.url
|
|
|
|
else comment.url = comment.id
|
2018-05-11 08:41:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|