<commit_before>import * as validator from 'validator'
import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers'
import { exists, isArray, isDateValid } from '../misc'
import { isActivityPubUrlValid, isBaseActivityValid } from './misc'

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

function sanitizeAndCheckVideoCommentObject (comment: any) {
  if (comment.type !== 'Note') return false

  normalizeComment(comment)

  return isActivityPubUrlValid(comment.id) &&
    isCommentContentValid(comment.content) &&
    isActivityPubUrlValid(comment.inReplyTo) &&
    isDateValid(comment.published) &&
    isActivityPubUrlValid(comment.url) &&
    isArray(comment.to) &&
    (
      comment.to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 ||
      comment.cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1
    ) // Only accept public comments
}

function isVideoCommentDeleteActivityValid (activity: any) {
  return isBaseActivityValid(activity, 'Delete')
}

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

export {
  isVideoCommentCreateActivityValid,
  isVideoCommentDeleteActivityValid,
  sanitizeAndCheckVideoCommentObject
}

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

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

function normalizeComment (comment: any) {
  if (!comment) return

  if (typeof comment.url !== 'string') {
    comment.url = comment.url.href || comment.url.url
  }

  return
}
<commit_msg>Check the comment is defined when validating body<commit_after>import * as validator from 'validator'
import { ACTIVITY_PUB, CONSTRAINTS_FIELDS } from '../../../initializers'
import { exists, isArray, isDateValid } from '../misc'
import { isActivityPubUrlValid, isBaseActivityValid } from './misc'

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

function sanitizeAndCheckVideoCommentObject (comment: any) {
  if (!comment || comment.type !== 'Note') return false

  normalizeComment(comment)

  return isActivityPubUrlValid(comment.id) &&
    isCommentContentValid(comment.content) &&
    isActivityPubUrlValid(comment.inReplyTo) &&
    isDateValid(comment.published) &&
    isActivityPubUrlValid(comment.url) &&
    isArray(comment.to) &&
    (
      comment.to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 ||
      comment.cc.indexOf(ACTIVITY_PUB.PUBLIC) !== -1
    ) // Only accept public comments
}

function isVideoCommentDeleteActivityValid (activity: any) {
  return isBaseActivityValid(activity, 'Delete')
}

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

export {
  isVideoCommentCreateActivityValid,
  isVideoCommentDeleteActivityValid,
  sanitizeAndCheckVideoCommentObject
}

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

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

function normalizeComment (comment: any) {
  if (!comment) return

  if (typeof comment.url !== 'string') {
    comment.url = comment.url.href || comment.url.url
  }

  return
}

{'fraction_non_alphanumeric': 0.1974616292798111, 'fraction_numerical': 0.0017709563164108619, 'mean_word_length': 3.0345238095238094, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1461964', 'n_tokens_mistral': 943, 'n_tokens_neox': 928, 'n_words': 188}