Chocobozzz/PeerTube

View on GitHub
server/core/middlewares/validators/videos/videos.ts

Summary

Maintainability
D
3 days
Test Coverage

File videos.ts has 458 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import { arrayify } from '@peertube/peertube-core-utils'
import { HttpStatusCode, ServerErrorCode, UserRight, VideoState } from '@peertube/peertube-models'
import { Redis } from '@server/lib/redis.js'
import { buildUploadXFile, safeUploadXCleanup } from '@server/lib/uploadx.js'
import { getServerActor } from '@server/models/application/application.js'
Severity: Minor
Found in server/core/middlewares/validators/videos/videos.ts - About 7 hrs to fix

    Function getCommonVideoEditAttributes has 77 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

    export function getCommonVideoEditAttributes () {
      return [
        body('thumbnailfile')
          .custom((value, { req }) => isVideoImageValid(req.files, 'thumbnailfile')).withMessage(
            'This thumbnail file is not supported or too large. Please, make sure it is of the following type: ' +
    Severity: Major
    Found in server/core/middlewares/validators/videos/videos.ts - About 3 hrs to fix

      Function videosUpdateValidator has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
      Open

        async (req: express.Request, res: express.Response, next: express.NextFunction) => {
          if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
          if (areErrorsInScheduleUpdate(req, res)) return cleanUpReqFiles(req)
          if (!await doesVideoExist(req.params.id, res)) return cleanUpReqFiles(req)
      
      
      Severity: Minor
      Found in server/core/middlewares/validators/videos/videos.ts - About 55 mins to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function commonVideosFiltersValidator has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
      Open

        (req: express.Request, res: express.Response, next: express.NextFunction) => {
          if (areValidationErrors(req, res)) return
      
          const user = res.locals.oauth?.token.User
      
      
      Severity: Minor
      Found in server/core/middlewares/validators/videos/videos.ts - About 45 mins to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function videosCustomGetValidator has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
      Open

      export const videosCustomGetValidator = (fetchType: FetchType) => {
        return [
          isValidVideoIdParam('id'),
      
          isValidVideoPasswordHeader(),
      Severity: Minor
      Found in server/core/middlewares/validators/videos/videos.ts - About 45 mins to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Avoid too many return statements within this function.
      Open

        return res.fail({
          status: HttpStatusCode.FORBIDDEN_403,
          message: 'Cannot get this video regarding follow constraints',
          type: ServerErrorCode.DOES_NOT_RESPECT_FOLLOW_CONSTRAINTS,
          data: {
      Severity: Major
      Found in server/core/middlewares/validators/videos/videos.ts - About 30 mins to fix

        Avoid too many return statements within this function.
        Open

            if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.UPDATE_ANY_VIDEO, res)) return cleanUpReqFiles(req)
        Severity: Major
        Found in server/core/middlewares/validators/videos/videos.ts - About 30 mins to fix

          Avoid too many return statements within this function.
          Open

              return next()
          Severity: Major
          Found in server/core/middlewares/validators/videos/videos.ts - About 30 mins to fix

            Avoid too many return statements within this function.
            Open

                  return res.fail({ message: 'Cannot update privacy of a live that has already started' })
            Severity: Major
            Found in server/core/middlewares/validators/videos/videos.ts - About 30 mins to fix

              Avoid too many return statements within this function.
              Open

                  return next()
              Severity: Major
              Found in server/core/middlewares/validators/videos/videos.ts - About 30 mins to fix

                Avoid too many return statements within this function.
                Open

                    return next()
                Severity: Major
                Found in server/core/middlewares/validators/videos/videos.ts - About 30 mins to fix

                  Avoid too many return statements within this function.
                  Open

                      if (req.body.channelId && !await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
                  Severity: Major
                  Found in server/core/middlewares/validators/videos/videos.ts - About 30 mins to fix

                    Avoid too many return statements within this function.
                    Open

                          return next()
                    Severity: Major
                    Found in server/core/middlewares/validators/videos/videos.ts - About 30 mins to fix

                      Function checkVideoFollowConstraints has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                      Open

                      export async function checkVideoFollowConstraints (req: express.Request, res: express.Response, next: express.NextFunction) {
                        const video = getVideoWithAttributes(res)
                      
                        // Anybody can watch local videos
                        if (video.isOwned() === true) return next()
                      Severity: Minor
                      Found in server/core/middlewares/validators/videos/videos.ts - About 25 mins to fix

                      Cognitive Complexity

                      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                      A method's cognitive complexity is based on a few simple rules:

                      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                      • Code is considered more complex for each "break in the linear flow of the code"
                      • Code is considered more complex when "flow breaking structures are nested"

                      Further reading

                      Similar blocks of code found in 3 locations. Consider refactoring.
                      Open

                        body('name')
                          .trim()
                          .custom(isVideoNameValid).withMessage(
                            `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
                          ),
                      Severity: Major
                      Found in server/core/middlewares/validators/videos/videos.ts and 2 other locations - About 1 hr to fix
                      server/core/middlewares/validators/videos/video-imports.ts on lines 35..39
                      server/core/middlewares/validators/videos/videos.ts on lines 145..149

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 65.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Similar blocks of code found in 3 locations. Consider refactoring.
                      Open

                          body('previewfile')
                            .custom((value, { req }) => isVideoImageValid(req.files, 'previewfile')).withMessage(
                              'This preview file is not supported or too large. Please, make sure it is of the following type: ' +
                              CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
                            ),
                      Severity: Major
                      Found in server/core/middlewares/validators/videos/videos.ts and 2 other locations - About 1 hr to fix
                      server/core/middlewares/validators/videos/video-playlists.ts on lines 374..379
                      server/core/middlewares/validators/videos/videos.ts on lines 351..355

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 65.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Similar blocks of code found in 3 locations. Consider refactoring.
                      Open

                          body('thumbnailfile')
                            .custom((value, { req }) => isVideoImageValid(req.files, 'thumbnailfile')).withMessage(
                              'This thumbnail file is not supported or too large. Please, make sure it is of the following type: ' +
                              CONSTRAINTS_FIELDS.VIDEOS.IMAGE.EXTNAME.join(', ')
                            ),
                      Severity: Major
                      Found in server/core/middlewares/validators/videos/videos.ts and 2 other locations - About 1 hr to fix
                      server/core/middlewares/validators/videos/video-playlists.ts on lines 374..379
                      server/core/middlewares/validators/videos/videos.ts on lines 356..360

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 65.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Similar blocks of code found in 3 locations. Consider refactoring.
                      Open

                        body('name')
                          .trim()
                          .custom(isVideoNameValid).withMessage(
                            `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
                          ),
                      Severity: Major
                      Found in server/core/middlewares/validators/videos/videos.ts and 2 other locations - About 1 hr to fix
                      server/core/middlewares/validators/videos/video-imports.ts on lines 35..39
                      server/core/middlewares/validators/videos/videos.ts on lines 64..68

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 65.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      Similar blocks of code found in 2 locations. Consider refactoring.
                      Open

                          body('tags')
                            .optional()
                            .customSanitizer(toValueOrNull)
                            .custom(areVideoTagsValid)
                            .withMessage(
                      Severity: Major
                      Found in server/core/middlewares/validators/videos/videos.ts and 1 other location - About 1 hr to fix
                      server/core/middlewares/validators/watched-words.ts on lines 65..70

                      Duplicated Code

                      Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                      Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                      When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                      Tuning

                      This issue has a mass of 64.

                      We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                      The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                      If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                      See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                      Refactorings

                      Further Reading

                      There are no issues that match your filters.

                      Category
                      Status