]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/middlewares/accounts.ts
replace numbers with typed http status codes (#3409)
[github/Chocobozzz/PeerTube.git] / server / helpers / middlewares / accounts.ts
index f5aa0badad321601f5e8f10579e2008a1add64fb..23470cac6ce0009e71abc4136364fc99719cb159 100644 (file)
@@ -1,10 +1,12 @@
 import { Response } from 'express'
 import { AccountModel } from '../../models/account/account'
 import * as Bluebird from 'bluebird'
-import { MAccountDefault } from '../../typings/models'
+import { MAccountDefault } from '../../types/models'
+import { UserModel } from '@server/models/account/user'
+import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
 
-function doesAccountIdExist (id: number, res: Response, sendNotFound = true) {
-  const promise = AccountModel.load(id)
+function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
+  const promise = AccountModel.load(parseInt(id + '', 10))
 
   return doesAccountExist(promise, res, sendNotFound)
 }
@@ -26,9 +28,8 @@ async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, se
 
   if (!account) {
     if (sendNotFound === true) {
-      res.status(404)
-         .send({ error: 'Account not found' })
-         .end()
+      res.status(HttpStatusCode.NOT_FOUND_404)
+         .json({ error: 'Account not found' })
     }
 
     return false
@@ -39,11 +40,27 @@ async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, se
   return true
 }
 
+async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) {
+  const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10))
+
+  if (token !== user.feedToken) {
+    res.status(HttpStatusCode.FORBIDDEN_403)
+       .json({ error: 'User and token mismatch' })
+
+    return false
+  }
+
+  res.locals.user = user
+
+  return true
+}
+
 // ---------------------------------------------------------------------------
 
 export {
   doesAccountIdExist,
   doesLocalAccountNameExist,
   doesAccountNameWithHostExist,
-  doesAccountExist
+  doesAccountExist,
+  doesUserFeedTokenCorrespond
 }