]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/abuse.ts
Random listen for mocked servers
[github/Chocobozzz/PeerTube.git] / server / controllers / api / abuse.ts
index 72e62fc0b2a10d0115bd12527f5595b5affb3e79..72c418e749e871d112dc7c2b324b796e2271d8ad 100644 (file)
@@ -1,9 +1,13 @@
-import * as express from 'express'
+import express from 'express'
+import { logger } from '@server/helpers/logger'
 import { createAccountAbuse, createVideoAbuse, createVideoCommentAbuse } from '@server/lib/moderation'
+import { Notifier } from '@server/lib/notifier'
 import { AbuseModel } from '@server/models/abuse/abuse'
 import { AbuseMessageModel } from '@server/models/abuse/abuse-message'
 import { getServerActor } from '@server/models/application/application'
-import { AbuseCreate, abusePredefinedReasonsMap, AbuseState, UserRight } from '../../../shared'
+import { abusePredefinedReasonsMap } from '@shared/core-utils/abuse'
+import { HttpStatusCode } from '@shared/models'
+import { AbuseCreate, AbuseState, UserRight } from '../../../shared'
 import { getFormattedObjects } from '../../helpers/utils'
 import { sequelizeTypescript } from '../../initializers/database'
 import {
@@ -20,6 +24,7 @@ import {
   deleteAbuseMessageValidator,
   ensureUserHasRight,
   getAbuseValidator,
+  openapiOperationDoc,
   paginationValidator,
   setDefaultPagination,
   setDefaultSort
@@ -29,6 +34,7 @@ import { AccountModel } from '../../models/account/account'
 const abuseRouter = express.Router()
 
 abuseRouter.get('/',
+  openapiOperationDoc({ operationId: 'getAbuses' }),
   authenticate,
   ensureUserHasRight(UserRight.MANAGE_ABUSES),
   paginationValidator,
@@ -82,13 +88,7 @@ abuseRouter.delete('/:id/messages/:messageId',
 // ---------------------------------------------------------------------------
 
 export {
-  abuseRouter,
-
-  // FIXME: deprecated in 2.3. Remove these exports
-  listAbusesForAdmins,
-  updateAbuse,
-  deleteAbuse,
-  reportAbuse
+  abuseRouter
 }
 
 // ---------------------------------------------------------------------------
@@ -123,19 +123,28 @@ async function listAbusesForAdmins (req: express.Request, res: express.Response)
 
 async function updateAbuse (req: express.Request, res: express.Response) {
   const abuse = res.locals.abuse
+  let stateUpdated = false
 
   if (req.body.moderationComment !== undefined) abuse.moderationComment = req.body.moderationComment
-  if (req.body.state !== undefined) abuse.state = req.body.state
+
+  if (req.body.state !== undefined) {
+    abuse.state = req.body.state
+    stateUpdated = true
+  }
 
   await sequelizeTypescript.transaction(t => {
     return abuse.save({ transaction: t })
   })
 
-  // TODO: Notification
+  if (stateUpdated === true) {
+    AbuseModel.loadFull(abuse.id)
+      .then(abuseFull => Notifier.Instance.notifyOnAbuseStateChange(abuseFull))
+      .catch(err => logger.error('Cannot notify on abuse state change', { err }))
+  }
 
   // Do not send the delete to other instances, we updated OUR copy of this abuse
 
-  return res.type('json').status(204).end()
+  return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }
 
 async function deleteAbuse (req: express.Request, res: express.Response) {
@@ -147,7 +156,7 @@ async function deleteAbuse (req: express.Request, res: express.Response) {
 
   // Do not send the delete to other instances, we delete OUR copy of this abuse
 
-  return res.type('json').status(204).end()
+  return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }
 
 async function reportAbuse (req: express.Request, res: express.Response) {
@@ -219,7 +228,9 @@ async function addAbuseMessage (req: express.Request, res: express.Response) {
     abuseId: abuse.id
   })
 
-  // TODO: Notification
+  AbuseModel.loadFull(abuse.id)
+    .then(abuseFull => Notifier.Instance.notifyOnAbuseMessage(abuseFull, abuseMessage))
+    .catch(err => logger.error('Cannot notify on new abuse message', { err }))
 
   return res.json({
     abuseMessage: {
@@ -235,5 +246,5 @@ async function deleteAbuseMessage (req: express.Request, res: express.Response)
     return abuseMessage.destroy({ transaction: t })
   })
 
-  return res.sendStatus(204)
+  return res.status(HttpStatusCode.NO_CONTENT_204).end()
 }