RocketChat/Rocket.Chat

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

Summary

Maintainability
F
3 days
Test Coverage

File im.ts has 469 lines of code (exceeds 250 allowed). Consider refactoring.
Open

/**
 * Docs: https://github.com/RocketChat/developer-docs/blob/master/reference/api/rest-api/endpoints/team-collaboration-endpoints/im-endpoints
 */
import type { IMessage, IRoom, ISubscription } from '@rocket.chat/core-typings';
import { Subscriptions, Uploads, Messages, Rooms, Users } from '@rocket.chat/models';
Severity: Minor
Found in apps/meteor/app/api/server/v1/im.ts - About 7 hrs to fix

    Function get has 48 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

            async get() {
                const access = await hasPermissionAsync(this.userId, 'view-room-administration');
                const { roomId, userId: ruserId } = this.queryParams;
                if (!roomId) {
                    throw new Meteor.Error('error-room-param-not-provided', 'Query param "roomId" is required');
    Severity: Minor
    Found in apps/meteor/app/api/server/v1/im.ts - About 1 hr to fix

      Function get has 37 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

              async get() {
                  const { room } = await findDirectMessageRoom(this.queryParams, this.userId);
      
                  const canAccess = await canAccessRoomIdAsync(room._id, this.userId);
                  if (!canAccess) {
      Severity: Minor
      Found in apps/meteor/app/api/server/v1/im.ts - About 1 hr to fix

        Function get has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
        Open

                async get() {
                    const access = await hasPermissionAsync(this.userId, 'view-room-administration');
                    const { roomId, userId: ruserId } = this.queryParams;
                    if (!roomId) {
                        throw new Meteor.Error('error-room-param-not-provided', 'Query param "roomId" is required');
        Severity: Minor
        Found in apps/meteor/app/api/server/v1/im.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

        Function get has 35 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

                async get() {
                    if (settings.get('API_Enable_Direct_Message_History_EndPoint') !== true) {
                        throw new Meteor.Error('error-endpoint-disabled', 'This endpoint is disabled', {
                            route: '/api/v1/im.messages.others',
                        });
        Severity: Minor
        Found in apps/meteor/app/api/server/v1/im.ts - About 1 hr to fix

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

                  async post() {
                      const { roomId } = this.bodyParams;
                      if (!roomId) {
                          throw new Meteor.Error('error-room-param-not-provided', 'Body param "roomId" is required');
                      }
          Severity: Minor
          Found in apps/meteor/app/api/server/v1/im.ts - About 35 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 get has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
          Open

                  async get() {
                      if (settings.get('API_Enable_Direct_Message_History_EndPoint') !== true) {
                          throw new Meteor.Error('error-endpoint-disabled', 'This endpoint is disabled', {
                              route: '/api/v1/im.messages.others',
                          });
          Severity: Minor
          Found in apps/meteor/app/api/server/v1/im.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

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

          const findDirectMessageRoom = async (
              keys: findDirectMessageRoomProps,
              uid: string,
          ): Promise<{ room: IRoom; subscription: ISubscription | null }> => {
              if (!('roomId' in keys) && !('username' in keys)) {
          Severity: Minor
          Found in apps/meteor/app/api/server/v1/im.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

                      const objectParams = {
                          rid: room._id,
                          latest: latest ? new Date(latest) : new Date(),
                          oldest: oldest && new Date(oldest),
                          inclusive: inclusive === 'true',
          Severity: Major
          Found in apps/meteor/app/api/server/v1/im.ts and 1 other location - About 2 hrs to fix
          apps/meteor/app/api/server/v1/channels.ts on lines 162..171

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

          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

                      return API.v1.success({
                          ims: await Promise.all(ims.map((room: IRoom) => composeRoomWithLastMessage(room, this.userId))),
                          offset,
                          count: ims.length,
                          total,
          Severity: Major
          Found in apps/meteor/app/api/server/v1/im.ts and 1 other location - About 2 hrs to fix
          apps/meteor/app/api/server/v1/im.ts on lines 508..513

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

          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

                      return API.v1.success({
                          ims: await Promise.all(rooms.map((room: IRoom) => composeRoomWithLastMessage(room, this.userId))),
                          offset,
                          count: rooms.length,
                          total,
          Severity: Major
          Found in apps/meteor/app/api/server/v1/im.ts and 1 other location - About 2 hrs to fix
          apps/meteor/app/api/server/v1/im.ts on lines 474..479

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

          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

                      const { cursor, totalCount } = Uploads.findPaginatedWithoutThumbs(ourQuery, {
                          sort: sort || { name: 1 },
                          skip: offset,
                          limit: count,
                          projection: fields,
          Severity: Major
          Found in apps/meteor/app/api/server/v1/im.ts and 1 other location - About 1 hr to fix
          apps/meteor/app/statistics/server/functions/getStatistics.ts on lines 29..34

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

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

                      const room = await Rooms.findOneById<Pick<IRoom, '_id' | 't'>>(roomId, { projection: { _id: 1, t: 1 } });
          Severity: Major
          Found in apps/meteor/app/api/server/v1/im.ts and 13 other locations - About 1 hr to fix
          apps/meteor/app/2fa/server/functions/resetTOTP.ts on lines 12..14
          apps/meteor/app/livechat/server/lib/Departments.ts on lines 14..16
          apps/meteor/app/livechat/server/methods/searchAgent.ts on lines 33..35
          apps/meteor/ee/app/livechat-enterprise/server/hooks/resumeOnHold.ts on lines 15..17
          apps/meteor/ee/app/livechat-enterprise/server/lib/VisitorInactivityMonitor.ts on lines 78..81
          apps/meteor/ee/app/livechat-enterprise/server/lib/VisitorInactivityMonitor.ts on lines 155..160
          apps/meteor/ee/app/livechat-enterprise/server/methods/resumeOnHold.ts on lines 22..24
          apps/meteor/server/modules/watchers/watchers.module.ts on lines 217..219
          apps/meteor/server/modules/watchers/watchers.module.ts on lines 227..229
          apps/meteor/server/services/team/service.ts on lines 328..328
          apps/meteor/server/services/team/service.ts on lines 401..401
          apps/meteor/server/services/team/service.ts on lines 526..528
          apps/meteor/server/services/team/service.ts on lines 769..771

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

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

                      return API.v1.success({
                          messages: await normalizeMessagesForUser(messages, this.userId),
                          count: messages.length,
                          offset,
                          total,
          Severity: Major
          Found in apps/meteor/app/api/server/v1/im.ts and 4 other locations - About 50 mins to fix
          apps/meteor/app/api/server/v1/channels.ts on lines 308..313
          apps/meteor/app/api/server/v1/chat.ts on lines 495..500
          apps/meteor/app/api/server/v1/chat.ts on lines 545..550
          apps/meteor/app/api/server/v1/groups.ts on lines 773..778

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

          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

                      return API.v1.success({
                          joined,
                          members,
                          unreads,
                          unreadsFrom,
          Severity: Major
          Found in apps/meteor/app/api/server/v1/im.ts and 2 other locations - About 50 mins to fix
          apps/meteor/app/api/server/v1/channels.ts on lines 631..639
          apps/meteor/app/api/server/v1/groups.ts on lines 295..303

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

          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

                      check(
                          this.queryParams,
                          Match.ObjectIncluding({
                              status: Match.Maybe([String]),
                              filter: Match.Maybe(String),
          Severity: Major
          Found in apps/meteor/app/api/server/v1/im.ts and 2 other locations - About 40 mins to fix
          apps/meteor/app/api/server/v1/channels.ts on lines 1074..1080
          apps/meteor/app/api/server/v1/groups.ts on lines 719..725

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

          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

                      return API.v1.success({
                          files: await addUserToFileObj(files),
                          count: files.length,
                          offset,
                          total,
          Severity: Minor
          Found in apps/meteor/app/api/server/v1/im.ts and 2 other locations - About 35 mins to fix
          apps/meteor/app/api/server/v1/channels.ts on lines 797..802
          apps/meteor/app/api/server/v1/groups.ts on lines 403..408

          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