aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/activitypub/video-comments.ts
blob: 489ff27de325d22f47e064cc222622272ef8bb59 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import * as validator from 'validator'
import { exists, isDateValid } from '../misc'
import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
import * as sanitizeHtml from 'sanitize-html'

function isVideoCommentCreateActivityValid (activity: any) {
  return isBaseActivityValid(activity, 'Create') &&
    isVideoCommentObjectValid(activity.object)
}

function isVideoCommentObjectValid (comment: any) {
  return comment.type === 'Note' &&
    isActivityPubUrlValid(comment.id) &&
    sanitizeCommentHTML(comment) &&
    isCommentContentValid(comment.content) &&
    isActivityPubUrlValid(comment.inReplyTo) &&
    isDateValid(comment.published) &&
    isActivityPubUrlValid(comment.url)
}

// ---------------------------------------------------------------------------

export {
  isVideoCommentCreateActivityValid
}

// ---------------------------------------------------------------------------

function sanitizeCommentHTML (comment: any) {
  return sanitizeHtml(comment.content, {
    allowedTags: [ 'b', 'i', 'em', 'span', 'a' ],
    allowedAttributes: {
      'a': [ 'href' ]
    }
  })
}

function isCommentContentValid (content: any) {
  return exists(content) && validator.isLength('' + content, { min: 1 })
}