]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Check video channel name is unique on our instance
authorChocobozzz <me@florianbigard.com>
Mon, 1 Oct 2018 13:18:07 +0000 (15:18 +0200)
committerChocobozzz <me@florianbigard.com>
Mon, 1 Oct 2018 13:20:14 +0000 (15:20 +0200)
client/src/app/+my-account/my-account-video-channels/my-account-video-channel-create.component.ts
server/controllers/api/users/index.ts
server/controllers/api/video-channel.ts
server/lib/activitypub/actor.ts
server/middlewares/activitypub.ts
server/middlewares/validators/video-channels.ts
server/tests/api/check-params/video-channels.ts

index 79ac07c93f4470a7b880036a402b8686c2fc9510..81608d837b2e30aaf28ec7fb283d7a7d49808b9e 100644 (file)
@@ -63,7 +63,14 @@ export class MyAccountVideoChannelCreateComponent extends MyAccountVideoChannelE
         this.router.navigate([ '/my-account', 'video-channels' ])
       },
 
-      err => this.error = err.message
+      err => {
+        if (err.status === 409) {
+          this.error = this.i18n('This name already exists on this instance.')
+          return
+        }
+
+        this.error = err.message
+      }
     )
   }
 
index 8b8ebcd234ead49b36991125fc9fda579a388017..0b008152076560f238a65a234d567af1c1c251c3 100644 (file)
@@ -34,7 +34,6 @@ import {
   usersVerifyEmailValidator
 } from '../../../middlewares/validators'
 import { UserModel } from '../../../models/account/user'
-import { OAuthTokenModel } from '../../../models/oauth/oauth-token'
 import { auditLoggerFactory, getAuditIdFromRes, UserAuditView } from '../../../helpers/audit-logger'
 import { meRouter } from './me'
 import { deleteUserToken } from '../../../lib/oauth-model'
index 4ca5ba9bcef136ebfdc408ab590d47828a974b6b..1fa842d9c984b2a112c6184831666e113c5e9bb1 100644 (file)
@@ -46,7 +46,7 @@ videoChannelRouter.get('/',
 
 videoChannelRouter.post('/',
   authenticate,
-  videoChannelsAddValidator,
+  asyncMiddleware(videoChannelsAddValidator),
   asyncRetryTransactionMiddleware(addVideoChannel)
 )
 
index d37a695a725dc7c3abf44d889382a9dea8b18d76..45dd4443dad8191d60d47437b9cd46e6489aab3f 100644 (file)
@@ -56,7 +56,7 @@ async function getOrCreateActorAndServerAndModel (
   // We don't have this actor in our database, fetch it on remote
   if (!actor) {
     const { result } = await fetchRemoteActor(actorUrl)
-    if (result === undefined) throw new Error('Cannot fetch remote actor.')
+    if (result === undefined) throw new Error('Cannot fetch remote actor ' + actorUrl)
 
     // Create the attributed to actor
     // In PeerTube a video channel is owned by an account
index 12d5c22c54acb15965538cd99bb9573d27e632d5..d7f59be8c5a2553ebd8fb76c1f4835d20facaf50 100644 (file)
@@ -18,7 +18,7 @@ async function checkSignature (req: Request, res: Response, next: NextFunction)
   try {
     actor = await getOrCreateActorAndServerAndModel(creator)
   } catch (err) {
-    logger.error('Cannot create remote actor and check signature.', { err })
+    logger.warn('Cannot create remote actor %s and check signature.', creator, { err })
     return res.sendStatus(403)
   }
 
index 79587b0284aa4fef262c4036c68050d2ebf28856..56a347b39ba4df6eb670345532dbaeec2fcf76db 100644 (file)
@@ -14,6 +14,7 @@ import { UserModel } from '../../models/account/user'
 import { VideoChannelModel } from '../../models/video/video-channel'
 import { areValidationErrors } from './utils'
 import { isActorPreferredUsernameValid } from '../../helpers/custom-validators/activitypub/actor'
+import { ActorModel } from '../../models/activitypub/actor'
 
 const listVideoAccountChannelsValidator = [
   param('accountName').exists().withMessage('Should have a valid account name'),
@@ -34,11 +35,19 @@ const videoChannelsAddValidator = [
   body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
   body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
 
-  (req: express.Request, res: express.Response, next: express.NextFunction) => {
+  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
     logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
 
     if (areValidationErrors(req, res)) return
 
+    const actor = await ActorModel.loadLocalByName(req.body.name)
+    if (actor) {
+      res.status(409)
+         .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
+         .end()
+      return false
+    }
+
     return next()
   }
 ]
index bcf4b7473e319857d1d0be42f752e1a47063c1b6..3a7942945a107c4571f0642773c6189a285e431b 100644 (file)
@@ -136,6 +136,16 @@ describe('Test video channels API validator', function () {
         statusCodeExpected: 200
       })
     })
+
+    it('Should fail when adding a channel with the same username', async function () {
+      await makePostBodyRequest({
+        url: server.url,
+        path: videoChannelPath,
+        token: server.accessToken,
+        fields: baseCorrectParams,
+        statusCodeExpected: 409
+      })
+    })
   })
 
   describe('When updating a video channel', function () {