]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/controllers/api/users.ts
Add ability to delete our account
[github/Chocobozzz/PeerTube.git] / server / controllers / api / users.ts
index 6e5f9913e9915a625718260c76fea16b1db4272b..3d2586c3a1f31bc8a21026f786f93a4256665d91 100644 (file)
@@ -30,6 +30,7 @@ import {
   usersVideoRatingValidator
 } from '../../middlewares'
 import {
+  deleteMeValidator,
   usersAskResetPasswordValidator,
   usersResetPasswordValidator,
   videoImportsSortValidator,
@@ -62,13 +63,17 @@ usersRouter.get('/me',
   authenticate,
   asyncMiddleware(getUserInformation)
 )
+usersRouter.delete('/me',
+  authenticate,
+  asyncMiddleware(deleteMeValidator),
+  asyncMiddleware(deleteMe)
+)
 
 usersRouter.get('/me/video-quota-used',
   authenticate,
   asyncMiddleware(getUserVideoQuotaUsed)
 )
 
-
 usersRouter.get('/me/videos/imports',
   authenticate,
   paginationValidator,
@@ -197,7 +202,7 @@ async function getUserVideos (req: express.Request, res: express.Response, next:
 async function getUserVideoImports (req: express.Request, res: express.Response, next: express.NextFunction) {
   const user = res.locals.oauth.token.User as UserModel
   const resultList = await VideoImportModel.listUserVideoImportsForApi(
-    user.Account.id,
+    user.id,
     req.query.start as number,
     req.query.count as number,
     req.query.sort
@@ -297,8 +302,18 @@ async function listUsers (req: express.Request, res: express.Response, next: exp
   return res.json(getFormattedObjects(resultList.data, resultList.total))
 }
 
+async function deleteMe (req: express.Request, res: express.Response) {
+  const user: UserModel = res.locals.oauth.token.User
+
+  await user.destroy()
+
+  auditLogger.delete(res.locals.oauth.token.User.Account.Actor.getIdentifier(), new UserAuditView(user.toFormattedJSON()))
+
+  return res.sendStatus(204)
+}
+
 async function removeUser (req: express.Request, res: express.Response, next: express.NextFunction) {
-  const user = await UserModel.loadById(req.params.id)
+  const user: UserModel = res.locals.user
 
   await user.destroy()