]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/video-blacklist.ts
Stronger model typings
[github/Chocobozzz/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, MVideoBlacklist, 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<MVideoBlacklist>({
35 where: {
36 videoId: video.id
37 },
38 defaults: videoBlacklistToCreate,
39 transaction
40 })
41 video.VideoBlacklist = videoBlacklist
42
43 if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(video)
44
45 logger.info('Video %s auto-blacklisted.', video.uuid)
46
47 return true
48 }
49
50 async function autoBlacklistNeeded (parameters: {
51 video: MVideoWithBlacklistLight,
52 isRemote: boolean,
53 isNew: boolean,
54 user?: MUser
55 }) {
56 const { user, video, isRemote, isNew } = parameters
57
58 // Already blacklisted
59 if (video.VideoBlacklist) return false
60 if (!CONFIG.AUTO_BLACKLIST.VIDEOS.OF_USERS.ENABLED || !user) return false
61 if (isRemote || isNew === false) return false
62
63 if (user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) || user.hasAdminFlag(UserAdminFlag.BY_PASS_VIDEO_AUTO_BLACKLIST)) return false
64
65 return true
66 }
67
68 // ---------------------------------------------------------------------------
69
70 export {
71 autoBlacklistVideoIfNeeded
72 }