aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/video-blacklist.ts
blob: 9749ce2f6cbc282e2bc69100be302fd954c78699 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { Transaction } from 'sequelize'
import { CONFIG } from '../initializers/config'
import { UserRight, VideoBlacklistType } from '../../shared/models'
import { VideoBlacklistModel } from '../models/video/video-blacklist'
import { UserModel } from '../models/account/user'
import { VideoModel } from '../models/video/video'
import { logger } from '../helpers/logger'
import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
import { Hooks } from './plugins/hooks'

async function autoBlacklistVideoIfNeeded (parameters: {
  video: VideoModel,
  user?: UserModel,
  isRemote: boolean,
  isNew: boolean,
  transaction?: Transaction
}) {
  const { video, user, isRemote, isNew, transaction } = parameters
  const doAutoBlacklist = await Hooks.wrapPromiseFun(
    autoBlacklistNeeded,
    { video, user, isRemote, isNew },
    'filter:video.auto-blacklist.result'
  )

  if (!doAutoBlacklist) return false

  const videoBlacklistToCreate = {
    videoId: video.id,
    unfederated: true,
    reason: 'Auto-blacklisted. Moderator review required.',
    type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
  }
  const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate({
    where: {
      videoId: video.id
    },
    defaults: videoBlacklistToCreate,
    transaction
  })

  video.VideoBlacklist = videoBlacklist

  logger.info('Video %s auto-blacklisted.', video.uuid)

  return true
}

async function autoBlacklistNeeded (parameters: {
  video: VideoModel,
  isRemote: boolean,
  isNew: boolean,
  user?: UserModel
}) {
  const { user, video, isRemote, isNew } = parameters

  // Already blacklisted
  if (video.VideoBlacklist) return false
  if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
  if (isRemote || isNew) return false

  if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)) return false

  return true
}

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

export {
  autoBlacklistVideoIfNeeded
}