video-comments.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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'
  5. function sanitizeAndCheckVideoCommentObject (comment: any) {
  6. if (!comment || comment.type !== 'Note') return false
  7. normalizeComment(comment)
  8. return isActivityPubUrlValid(comment.id) &&
  9. isCommentContentValid(comment.content) &&
  10. isActivityPubUrlValid(comment.inReplyTo) &&
  11. isDateValid(comment.published) &&
  12. isActivityPubUrlValid(comment.url) &&
  13. isArray(comment.to) &&
  14. (
  15. comment.to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 ||
  16. comment.cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1
  17. ) // Only accept public comments
  18. }
  19. // ---------------------------------------------------------------------------
  20. export {
  21. sanitizeAndCheckVideoCommentObject
  22. }
  23. // ---------------------------------------------------------------------------
  24. function isCommentContentValid (content: any) {
  25. return exists(content) && validator.isLength('' + content, { min: 1 })
  26. }
  27. function normalizeComment (comment: any) {
  28. if (!comment) return
  29. if (typeof comment.url !== 'string') {
  30. if (typeof comment.url === 'object') comment.url = comment.url.href || comment.url.url
  31. else comment.url = comment.id
  32. }
  33. return
  34. }