Accessibility fixes for #2149
[oweals/peertube.git] / server / lib / video-blacklist.ts
1 import { Transaction } from 'sequelize'
2 import { CONFIG } from '../initializers/config'
3 import { UserRight, VideoBlacklistType } from '../../shared/models'
4 import { VideoBlacklistModel } from '../models/video/video-blacklist'
5 import { logger } from '../helpers/logger'
6 import { UserAdminFlag } from '../../shared/models/users/user-flag.model'
7 import { Hooks } from './plugins/hooks'
8 import { Notifier } from './notifier'
9 import { MUser, MVideoBlacklistVideo, MVideoWithBlacklistLight } from '@server/typings/models'
10
11 async function autoBlacklistVideoIfNeeded (parameters: {
12   video: MVideoWithBlacklistLight,
13   user?: MUser,
14   isRemote: boolean,
15   isNew: boolean,
16   notify?: boolean,
17   transaction?: Transaction
18 }) {
19   const { video, user, isRemote, isNew, notify = true, transaction } = parameters
20   const doAutoBlacklist = await Hooks.wrapPromiseFun(
21     autoBlacklistNeeded,
22     { video, user, isRemote, isNew },
23     'filter:video.auto-blacklist.result'
24   )
25
26   if (!doAutoBlacklist) return false
27
28   const videoBlacklistToCreate = {
29     videoId: video.id,
30     unfederated: true,
31     reason: 'Auto-blacklisted. Moderator review required.',
32     type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED
33   }
34   const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate<MVideoBlacklistVideo>({
35     where: {
36       videoId: video.id
37     },
38     defaults: videoBlacklistToCreate,
39     transaction
40   })
41   video.VideoBlacklist = videoBlacklist
42
43   videoBlacklist.Video = video
44
45   if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist)
46
47   logger.info('Video %s auto-blacklisted.', video.uuid)
48
49   return true
50 }
51
52 async function autoBlacklistNeeded (parameters: {
53   video: MVideoWithBlacklistLight,
54   isRemote: boolean,
55   isNew: boolean,
56   user?: MUser
57 }) {
58   const { user, video, isRemote, isNew } = parameters
59
60   // Already blacklisted
61   if (video.VideoBlacklist) return false
62   if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
63   if (isRemote || isNew === false) return false
64
65   if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)) return false
66
67   return true
68 }
69
70 // ---------------------------------------------------------------------------
71
72 export {
73   autoBlacklistVideoIfNeeded
74 }