1
0
Fork 0
peertube/server/helpers/custom-validators/activitypub/video-comments.ts

60 lines
1.7 KiB
TypeScript
Raw Normal View History

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