]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/moderation.ts
Force live stream termination
[github/Chocobozzz/PeerTube.git] / server / lib / moderation.ts
index 4fc9cd747609a22d0d612caafc8e6c1fb944b566..dc5d8c83c3592bc94235805255a4900cb4805581 100644 (file)
@@ -1,6 +1,8 @@
+import express, { VideoUploadFile } from 'express'
 import { PathLike } from 'fs-extra'
 import { Transaction } from 'sequelize/types'
 import { AbuseAuditView, auditLoggerFactory } from '@server/helpers/audit-logger'
+import { afterCommitIfTransaction } from '@server/helpers/database-utils'
 import { logger } from '@server/helpers/logger'
 import { AbuseModel } from '@server/models/abuse/abuse'
 import { VideoAbuseModel } from '@server/models/abuse/video-abuse'
@@ -11,19 +13,16 @@ import {
   MAbuseFull,
   MAccountDefault,
   MAccountLight,
+  MComment,
   MCommentAbuseAccountVideo,
   MCommentOwnerVideo,
   MUser,
   MVideoAbuseVideoFull,
   MVideoAccountLightBlacklistAllFiles
 } from '@server/types/models'
-import { ActivityCreate } from '../../shared/models/activitypub'
-import { VideoTorrentObject } from '../../shared/models/activitypub/objects'
-import { VideoCommentObject } from '../../shared/models/activitypub/objects/video-comment-object'
-import { VideoCreate, VideoImportCreate } from '../../shared/models/videos'
-import { VideoCommentCreate } from '../../shared/models/videos/video-comment.model'
-import { UserModel } from '../models/account/user'
-import { ActorModel } from '../models/activitypub/actor'
+import { LiveVideoCreate, VideoCreate, VideoImportCreate } from '../../shared/models/videos'
+import { VideoCommentCreate } from '../../shared/models/videos/comment'
+import { UserModel } from '../models/user/user'
 import { VideoModel } from '../models/video/video'
 import { VideoCommentModel } from '../models/video/video-comment'
 import { sendAbuse } from './activitypub/send/send-flag'
@@ -34,16 +33,32 @@ export type AcceptResult = {
   errorMessage?: string
 }
 
-// Can be filtered by plugins
+// ---------------------------------------------------------------------------
+
+// Stub function that can be filtered by plugins
 function isLocalVideoAccepted (object: {
   videoBody: VideoCreate
-  videoFile: Express.Multer.File & { duration?: number }
+  videoFile: VideoUploadFile
+  user: UserModel
+}): AcceptResult {
+  return { accepted: true }
+}
+
+// ---------------------------------------------------------------------------
+
+// Stub function that can be filtered by plugins
+function isLocalLiveVideoAccepted (object: {
+  liveVideoBody: LiveVideoCreate
   user: UserModel
 }): AcceptResult {
   return { accepted: true }
 }
 
+// ---------------------------------------------------------------------------
+
+// Stub function that can be filtered by plugins
 function isLocalVideoThreadAccepted (_object: {
+  req: express.Request
   commentBody: VideoCommentCreate
   video: VideoModel
   user: UserModel
@@ -51,7 +66,9 @@ function isLocalVideoThreadAccepted (_object: {
   return { accepted: true }
 }
 
+// Stub function that can be filtered by plugins
 function isLocalVideoCommentReplyAccepted (_object: {
+  req: express.Request
   commentBody: VideoCommentCreate
   parentComment: VideoCommentModel
   video: VideoModel
@@ -60,22 +77,18 @@ function isLocalVideoCommentReplyAccepted (_object: {
   return { accepted: true }
 }
 
-function isRemoteVideoAccepted (_object: {
-  activity: ActivityCreate
-  videoAP: VideoTorrentObject
-  byActor: ActorModel
-}): AcceptResult {
-  return { accepted: true }
-}
+// ---------------------------------------------------------------------------
 
+// Stub function that can be filtered by plugins
 function isRemoteVideoCommentAccepted (_object: {
-  activity: ActivityCreate
-  commentAP: VideoCommentObject
-  byActor: ActorModel
+  comment: MComment
 }): AcceptResult {
   return { accepted: true }
 }
 
+// ---------------------------------------------------------------------------
+
+// Stub function that can be filtered by plugins
 function isPreImportVideoAccepted (object: {
   videoImportBody: VideoImportCreate
   user: MUser
@@ -83,6 +96,7 @@ function isPreImportVideoAccepted (object: {
   return { accepted: true }
 }
 
+// Stub function that can be filtered by plugins
 function isPostImportVideoAccepted (object: {
   videoFilePath: PathLike
   videoFile: VideoFileModel
@@ -91,6 +105,8 @@ function isPostImportVideoAccepted (object: {
   return { accepted: true }
 }
 
+// ---------------------------------------------------------------------------
+
 async function createVideoAbuse (options: {
   baseAbuse: FilteredModelAttributes<AbuseModel>
   videoInstance: MVideoAccountLightBlacklistAllFiles
@@ -98,15 +114,16 @@ async function createVideoAbuse (options: {
   endAt: number
   transaction: Transaction
   reporterAccount: MAccountDefault
+  skipNotification: boolean
 }) {
-  const { baseAbuse, videoInstance, startAt, endAt, transaction, reporterAccount } = options
+  const { baseAbuse, videoInstance, startAt, endAt, transaction, reporterAccount, skipNotification } = options
 
   const associateFun = async (abuseInstance: MAbuseFull) => {
     const videoAbuseInstance: MVideoAbuseVideoFull = await VideoAbuseModel.create({
       abuseId: abuseInstance.id,
       videoId: videoInstance.id,
-      startAt: startAt,
-      endAt: endAt
+      startAt,
+      endAt
     }, { transaction })
 
     videoAbuseInstance.Video = videoInstance
@@ -120,6 +137,7 @@ async function createVideoAbuse (options: {
     reporterAccount,
     flaggedAccount: videoInstance.VideoChannel.Account,
     transaction,
+    skipNotification,
     associateFun
   })
 }
@@ -129,8 +147,9 @@ function createVideoCommentAbuse (options: {
   commentInstance: MCommentOwnerVideo
   transaction: Transaction
   reporterAccount: MAccountDefault
+  skipNotification: boolean
 }) {
-  const { baseAbuse, commentInstance, transaction, reporterAccount } = options
+  const { baseAbuse, commentInstance, transaction, reporterAccount, skipNotification } = options
 
   const associateFun = async (abuseInstance: MAbuseFull) => {
     const commentAbuseInstance: MCommentAbuseAccountVideo = await VideoCommentAbuseModel.create({
@@ -149,6 +168,7 @@ function createVideoCommentAbuse (options: {
     reporterAccount,
     flaggedAccount: commentInstance.Account,
     transaction,
+    skipNotification,
     associateFun
   })
 }
@@ -158,11 +178,12 @@ function createAccountAbuse (options: {
   accountInstance: MAccountDefault
   transaction: Transaction
   reporterAccount: MAccountDefault
+  skipNotification: boolean
 }) {
-  const { baseAbuse, accountInstance, transaction, reporterAccount } = options
+  const { baseAbuse, accountInstance, transaction, reporterAccount, skipNotification } = options
 
-  const associateFun = async () => {
-    return { isOwned: accountInstance.isOwned() }
+  const associateFun = () => {
+    return Promise.resolve({ isOwned: accountInstance.isOwned() })
   }
 
   return createAbuse({
@@ -170,14 +191,18 @@ function createAccountAbuse (options: {
     reporterAccount,
     flaggedAccount: accountInstance,
     transaction,
+    skipNotification,
     associateFun
   })
 }
 
+// ---------------------------------------------------------------------------
+
 export {
+  isLocalLiveVideoAccepted,
+
   isLocalVideoAccepted,
   isLocalVideoThreadAccepted,
-  isRemoteVideoAccepted,
   isRemoteVideoCommentAccepted,
   isLocalVideoCommentReplyAccepted,
   isPreImportVideoAccepted,
@@ -195,10 +220,11 @@ async function createAbuse (options: {
   base: FilteredModelAttributes<AbuseModel>
   reporterAccount: MAccountDefault
   flaggedAccount: MAccountLight
-  associateFun: (abuseInstance: MAbuseFull) => Promise<{ isOwned: boolean} >
+  associateFun: (abuseInstance: MAbuseFull) => Promise<{ isOwned: boolean }>
+  skipNotification: boolean
   transaction: Transaction
 }) {
-  const { base, reporterAccount, flaggedAccount, associateFun, transaction } = options
+  const { base, reporterAccount, flaggedAccount, associateFun, transaction, skipNotification } = options
   const auditLogger = auditLoggerFactory('abuse')
 
   const abuseAttributes = Object.assign({}, base, { flaggedAccountId: flaggedAccount.id })
@@ -210,17 +236,21 @@ async function createAbuse (options: {
   const { isOwned } = await associateFun(abuseInstance)
 
   if (isOwned === false) {
-    await sendAbuse(reporterAccount.Actor, abuseInstance, abuseInstance.FlaggedAccount, transaction)
+    sendAbuse(reporterAccount.Actor, abuseInstance, abuseInstance.FlaggedAccount, transaction)
   }
 
-  const abuseJSON = abuseInstance.toFormattedJSON()
+  const abuseJSON = abuseInstance.toFormattedAdminJSON()
   auditLogger.create(reporterAccount.Actor.getIdentifier(), new AbuseAuditView(abuseJSON))
 
-  Notifier.Instance.notifyOnNewAbuse({
-    abuse: abuseJSON,
-    abuseInstance,
-    reporter: reporterAccount.Actor.getIdentifier()
-  })
+  if (!skipNotification) {
+    afterCommitIfTransaction(transaction, () => {
+      Notifier.Instance.notifyOnNewAbuse({
+        abuse: abuseJSON,
+        abuseInstance,
+        reporter: reporterAccount.Actor.getIdentifier()
+      })
+    })
+  }
 
   logger.info('Abuse report %d created.', abuseInstance.id)