RocketChat/Rocket.Chat

View on GitHub
apps/meteor/app/api/server/v1/rooms.ts

Summary

Maintainability
F
1 wk
Test Coverage

File rooms.ts has 603 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import { Media } from '@rocket.chat/core-services';
import type { IRoom, IUpload } from '@rocket.chat/core-typings';
import { Messages, Rooms, Users, Uploads } from '@rocket.chat/models';
import type { Notifications } from '@rocket.chat/rest-typings';
import { isGETRoomsNameExists, isRoomsImagesProps, isRoomsMuteUnmuteUserProps } from '@rocket.chat/rest-typings';
Severity: Major
Found in apps/meteor/app/api/server/v1/rooms.ts - About 1 day to fix

    Function post has 60 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

            async post() {
                const { rid, type } = this.bodyParams;
    
                if (!rid || !type || !['email', 'file'].includes(type)) {
                    throw new Meteor.Error('error-invalid-params');
    Severity: Major
    Found in apps/meteor/app/api/server/v1/rooms.ts - About 2 hrs to fix

      Function post has a Cognitive Complexity of 16 (exceeds 5 allowed). Consider refactoring.
      Open

              async post() {
                  const { rid, type } = this.bodyParams;
      
                  if (!rid || !type || !['email', 'file'].includes(type)) {
                      throw new Meteor.Error('error-invalid-params');
      Severity: Minor
      Found in apps/meteor/app/api/server/v1/rooms.ts - About 2 hrs 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 post has 34 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

              async post() {
                  if (!(await canAccessRoomIdAsync(this.urlParams.rid, this.userId))) {
                      return API.v1.unauthorized();
                  }
      
      
      Severity: Minor
      Found in apps/meteor/app/api/server/v1/rooms.ts - About 1 hr to fix

        Function post has 31 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

                async post() {
                    const { _id } = await findRoomByIdOrName({ params: this.bodyParams });
        
                    const {
                        latest,
        Severity: Minor
        Found in apps/meteor/app/api/server/v1/rooms.ts - About 1 hr to fix

          Function findRoomByIdOrName has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
          Open

          async function findRoomByIdOrName({
              params,
              checkedArchived = true,
          }: {
              params:
          Severity: Minor
          Found in apps/meteor/app/api/server/v1/rooms.ts - About 1 hr 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

          Consider simplifying this complex logical expression.
          Open

              if (
                  (!('roomId' in params) && !('roomName' in params)) ||
                  ('roomId' in params && !(params as { roomId?: string }).roomId && 'roomName' in params && !(params as { roomName?: string }).roomName)
              ) {
                  throw new Meteor.Error('error-roomid-param-not-provided', 'The parameter "roomId" or "roomName" is required');
          Severity: Major
          Found in apps/meteor/app/api/server/v1/rooms.ts - About 40 mins to fix

            Consider simplifying this complex logical expression.
            Open

                        if (type === 'email') {
                            const { toUsers, toEmails, subject, messages } = this.bodyParams;
            
                            if ((!toUsers || toUsers.length === 0) && (!toEmails || toEmails.length === 0)) {
                                throw new Meteor.Error('error-invalid-recipient');
            Severity: Major
            Found in apps/meteor/app/api/server/v1/rooms.ts - About 40 mins to fix

              Avoid too many return statements within this function.
              Open

                          return API.v1.success({ discussion });
              Severity: Major
              Found in apps/meteor/app/api/server/v1/rooms.ts - About 30 mins to fix

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

                        async post() {
                            // eslint-disable-next-line @typescript-eslint/naming-convention
                            const { prid, pmid, reply, t_name, users, encrypted, topic } = this.bodyParams;
                            if (!prid) {
                                return API.v1.failure('Body parameter "prid" is required.');
                Severity: Minor
                Found in apps/meteor/app/api/server/v1/rooms.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 2 locations. Consider refactoring.
                Open

                    {
                        async get() {
                            const room = await Rooms.findOneById<Pick<IRoom, '_id' | 't' | 'teamId' | 'prid'>>(this.queryParams.roomId, {
                                projection: { t: 1, teamId: 1, prid: 1 },
                            });
                Severity: Major
                Found in apps/meteor/app/api/server/v1/rooms.ts and 1 other location - About 2 days to fix
                apps/meteor/app/api/server/v1/channels.ts on lines 817..853

                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 389.

                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

                API.v1.addRoute(
                    'rooms.muteUser',
                    { authRequired: true, validateParams: isRoomsMuteUnmuteUserProps },
                    {
                        async post() {
                Severity: Major
                Found in apps/meteor/app/api/server/v1/rooms.ts and 1 other location - About 4 hrs to fix
                apps/meteor/app/api/server/v1/rooms.ts on lines 703..719

                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 133.

                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

                API.v1.addRoute(
                    'rooms.unmuteUser',
                    { authRequired: true, validateParams: isRoomsMuteUnmuteUserProps },
                    {
                        async post() {
                Severity: Major
                Found in apps/meteor/app/api/server/v1/rooms.ts and 1 other location - About 4 hrs to fix
                apps/meteor/app/api/server/v1/rooms.ts on lines 685..701

                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 133.

                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

                API.v1.addRoute(
                    'rooms.autocomplete.channelAndPrivate',
                    { authRequired: true },
                    {
                        async get() {
                Severity: Major
                Found in apps/meteor/app/api/server/v1/rooms.ts and 1 other location - About 4 hrs to fix
                apps/meteor/app/api/server/v1/rooms.ts on lines 459..477

                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 117.

                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

                API.v1.addRoute(
                    'rooms.autocomplete.adminRooms',
                    { authRequired: true },
                    {
                        async get() {
                Severity: Major
                Found in apps/meteor/app/api/server/v1/rooms.ts and 1 other location - About 4 hrs to fix
                apps/meteor/app/api/server/v1/rooms.ts on lines 498..516

                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 117.

                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

                    if (
                        (!('roomId' in params) && !('roomName' in params)) ||
                        ('roomId' in params && !(params as { roomId?: string }).roomId && 'roomName' in params && !(params as { roomName?: string }).roomName)
                    ) {
                        throw new Meteor.Error('error-roomid-param-not-provided', 'The parameter "roomId" or "roomName" is required');
                Severity: Major
                Found in apps/meteor/app/api/server/v1/rooms.ts and 1 other location - About 3 hrs to fix
                apps/meteor/app/api/server/v1/groups.ts on lines 30..35

                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 95.

                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

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

                    if ('roomId' in params) {
                        room = await Rooms.findOneById(params.roomId || '', { projection });
                    } else if ('roomName' in params) {
                        room = await Rooms.findOneByName(params.roomName || '', { projection });
                    }
                Severity: Major
                Found in apps/meteor/app/api/server/v1/rooms.ts and 1 other location - About 2 hrs to fix
                apps/meteor/app/api/server/v1/channels.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 87.

                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 7 locations. Consider refactoring.
                Open

                            const { cursor, totalCount } = await Rooms.findPaginated(ourQuery, {
                                sort: sort || { fname: 1 },
                                skip: offset,
                                limit: count,
                                projection: fields,
                Severity: Major
                Found in apps/meteor/app/api/server/v1/rooms.ts and 6 other locations - About 1 hr to fix
                apps/meteor/app/api/server/v1/channels.ts on lines 788..793
                apps/meteor/app/api/server/v1/channels.ts on lines 898..903
                apps/meteor/app/api/server/v1/channels.ts on lines 999..1004
                apps/meteor/app/api/server/v1/groups.ts on lines 394..399
                apps/meteor/app/api/server/v1/groups.ts on lines 451..456
                apps/meteor/app/api/server/v1/groups.ts on lines 683..688

                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 60.

                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

                            if (!room || !(await canAccessRoomAsync(room, { _id: this.userId }))) {
                                return API.v1.failure('not-allowed', 'Not Allowed');
                            }
                Severity: Minor
                Found in apps/meteor/app/api/server/v1/rooms.ts and 1 other location - About 40 mins to fix
                apps/meteor/app/api/server/v1/rooms.ts on lines 367..369

                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 48.

                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

                            if (!room || !(await canAccessRoomAsync(room, { _id: this.userId }))) {
                                return API.v1.failure('not-allowed', 'Not Allowed');
                            }
                Severity: Minor
                Found in apps/meteor/app/api/server/v1/rooms.ts and 1 other location - About 40 mins to fix
                apps/meteor/app/api/server/v1/rooms.ts on lines 296..298

                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 48.

                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

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

                                if (isNaN(Date.parse(updatedSince))) {
                                    throw new Meteor.Error('error-updatedSince-param-invalid', 'The "updatedSince" query parameter must be a valid date.');
                                } else {
                                    updatedSinceDate = new Date(updatedSince);
                                }
                Severity: Minor
                Found in apps/meteor/app/api/server/v1/rooms.ts and 2 other locations - About 35 mins to fix
                apps/meteor/app/api/server/v1/chat.ts on lines 568..572
                apps/meteor/app/api/server/v1/chat.ts on lines 664..668

                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 46.

                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