]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/feeds.ts
refactor scoped token service
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / feeds.ts
index f34c2b174527085750e48ac5bc11c4e3e549bdae..35080ffcab2c2d759276e50a6296f882fdd5d8d0 100644 (file)
@@ -9,7 +9,8 @@ import {
   doesAccountIdExist,
   doesAccountNameWithHostExist,
   doesVideoChannelIdExist,
-  doesVideoChannelNameWithHostExist
+  doesVideoChannelNameWithHostExist,
+  doesUserFeedTokenCorrespond
 } from '../../helpers/middlewares'
 
 const feedsFormatValidator = [
@@ -62,6 +63,24 @@ const videoFeedsValidator = [
   }
 ]
 
+const videoSubscriptonFeedsValidator = [
+  query('accountId').custom(isIdValid),
+  query('token'),
+
+  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
+    logger.debug('Checking feeds parameters', { parameters: req.query })
+
+    if (areValidationErrors(req, res)) return
+
+    // a token alone is erroneous
+    if (req.query.token && !req.query.accountId) return
+    if (req.query.accountId && !await doesAccountIdExist(req.query.accountId, res)) return
+    if (req.query.token && !await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
+
+    return next()
+  }
+]
+
 const videoCommentsFeedsValidator = [
   query('videoId').optional().custom(isIdOrUUIDValid),
 
@@ -70,6 +89,12 @@ const videoCommentsFeedsValidator = [
 
     if (areValidationErrors(req, res)) return
 
+    if (req.query.videoId && (req.query.videoChannelId || req.query.videoChannelName)) {
+      return res.status(400).send({
+        message: 'videoId cannot be mixed with a channel filter'
+      }).end()
+    }
+
     if (req.query.videoId && !await doesVideoExist(req.query.videoId, res)) return
 
     return next()
@@ -82,5 +107,6 @@ export {
   feedsFormatValidator,
   setFeedFormatContentType,
   videoFeedsValidator,
+  videoSubscriptonFeedsValidator,
   videoCommentsFeedsValidator
 }