]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/middlewares/accounts.ts
Fix migration and test
[github/Chocobozzz/PeerTube.git] / server / helpers / middlewares / accounts.ts
index f5aa0badad321601f5e8f10579e2008a1add64fb..e9b98126273bd6250a9af6a60acfd8faeffe1938 100644 (file)
@@ -1,10 +1,11 @@
 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'
 
-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)
 }
@@ -27,8 +28,7 @@ async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, se
   if (!account) {
     if (sendNotFound === true) {
       res.status(404)
-         .send({ error: 'Account not found' })
-         .end()
+         .json({ error: 'Account not found' })
     }
 
     return false
@@ -39,11 +39,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(403)
+       .json({ error: 'User and token mismatch' })
+
+    return false
+  }
+
+  res.locals.user = user
+
+  return true
+}
+
 // ---------------------------------------------------------------------------
 
 export {
   doesAccountIdExist,
   doesLocalAccountNameExist,
   doesAccountNameWithHostExist,
-  doesAccountExist
+  doesAccountExist,
+  doesUserFeedTokenCorrespond
 }