RocketChat/Rocket.Chat

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

Summary

Maintainability
F
6 days
Test Coverage

File users.ts has 1154 lines of code (exceeds 250 allowed). Consider refactoring.
Open

import { MeteorError, Team, api } from '@rocket.chat/core-services';
import type { IExportOperation, ILoginToken, IPersonalAccessToken, IUser, UserStatus } from '@rocket.chat/core-typings';
import { Users, Subscriptions } from '@rocket.chat/models';
import {
    isUserCreateParamsPOST,
Severity: Major
Found in apps/meteor/app/api/server/v1/users.ts - About 2 days to fix

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

            async get() {
                if (!(await hasPermissionAsync(this.userId, 'view-d-room'))) {
                    return API.v1.unauthorized();
                }
    
    
    Severity: Major
    Found in apps/meteor/app/api/server/v1/users.ts - About 3 hrs to fix

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

              async post() {
                  check(
                      this.bodyParams,
                      Match.OneOf(
                          Match.ObjectIncluding({
      Severity: Major
      Found in apps/meteor/app/api/server/v1/users.ts - About 2 hrs to fix

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

                async post() {
                    const canEditOtherUserAvatar = await hasPermissionAsync(this.userId, 'edit-other-user-avatar');
        
                    if (!settings.get('Accounts_AllowUserAvatarChange') && !canEditOtherUserAvatar) {
                        throw new Meteor.Error('error-not-allowed', 'Change avatar is not allowed', {
        Severity: Minor
        Found in apps/meteor/app/api/server/v1/users.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 50 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

                async post() {
                    const canEditOtherUserAvatar = await hasPermissionAsync(this.userId, 'edit-other-user-avatar');
        
                    if (!settings.get('Accounts_AllowUserAvatarChange') && !canEditOtherUserAvatar) {
                        throw new Meteor.Error('error-not-allowed', 'Change avatar is not allowed', {
        Severity: Minor
        Found in apps/meteor/app/api/server/v1/users.ts - About 2 hrs to fix

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

                  async post() {
                      check(
                          this.bodyParams,
                          Match.OneOf(
                              Match.ObjectIncluding({
          Severity: Minor
          Found in apps/meteor/app/api/server/v1/users.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 37 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

                  async get() {
                      const { fields } = await this.parseJsonQuery();
          
                      const searchTerms: [string, 'id' | 'username' | 'importId'] | false =
                          ('userId' in this.queryParams && !!this.queryParams.userId && [this.queryParams.userId, 'id']) ||
          Severity: Minor
          Found in apps/meteor/app/api/server/v1/users.ts - About 1 hr to fix

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

                    async get() {
                        // if presence broadcast is disabled, return an empty array (all users are "offline")
                        if (settings.get('Presence_broadcast_disabled')) {
                            return API.v1.success({
                                users: [],
            Severity: Minor
            Found in apps/meteor/app/api/server/v1/users.ts - About 1 hr to fix

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

                      async post() {
                          const { secret: secretURL, ...params } = this.bodyParams;
              
                          if (this.userId) {
                              return API.v1.failure('Logged in users can not register again.');
              Severity: Minor
              Found in apps/meteor/app/api/server/v1/users.ts - About 1 hr to fix

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

                        async post() {
                            if (
                                this.bodyParams.userId &&
                                this.bodyParams.userId !== this.userId &&
                                !(await hasPermissionAsync(this.userId, 'edit-other-user-info'))
                Severity: Minor
                Found in apps/meteor/app/api/server/v1/users.ts - About 1 hr to fix

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

                          async post() {
                              const { secret: secretURL, ...params } = this.bodyParams;
                  
                              if (this.userId) {
                                  return API.v1.failure('Logged in users can not register again.');
                  Severity: Minor
                  Found in apps/meteor/app/api/server/v1/users.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 post has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                  Open

                          async post() {
                              if ('userId' in this.bodyParams || 'username' in this.bodyParams || 'user' in this.bodyParams) {
                                  // reset other user keys
                                  const user = await getUserFromParams(this.bodyParams);
                                  if (!user) {
                  Severity: Minor
                  Found in apps/meteor/app/api/server/v1/users.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 get has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                  Open

                          async get() {
                              const { fields } = await this.parseJsonQuery();
                  
                              const searchTerms: [string, 'id' | 'username' | 'importId'] | false =
                                  ('userId' in this.queryParams && !!this.queryParams.userId && [this.queryParams.userId, 'id']) ||
                  Severity: Minor
                  Found in apps/meteor/app/api/server/v1/users.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 get has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                  Open

                          async get() {
                              if (!(await hasPermissionAsync(this.userId, 'view-d-room'))) {
                                  return API.v1.unauthorized();
                              }
                  
                  
                  Severity: Minor
                  Found in apps/meteor/app/api/server/v1/users.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

                  Consider simplifying this complex logical expression.
                  Open

                              if (
                                  !isValidQuery(
                                      nonEmptyQuery,
                                      [
                                          ...inclusiveFieldsKeys,
                  Severity: Major
                  Found in apps/meteor/app/api/server/v1/users.ts - About 40 mins to fix

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

                            async post() {
                                // New change made by pull request #5152
                                if (typeof this.bodyParams.joinDefaultChannels === 'undefined') {
                                    this.bodyParams.joinDefaultChannels = true;
                                }
                    Severity: Minor
                    Found in apps/meteor/app/api/server/v1/users.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 post has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                    Open

                            async post() {
                                // // reset own keys
                                if ('userId' in this.bodyParams || 'username' in this.bodyParams || 'user' in this.bodyParams) {
                                    // reset other user keys
                                    if (!(await hasPermissionAsync(this.userId, 'edit-other-user-totp'))) {
                    Severity: Minor
                    Found in apps/meteor/app/api/server/v1/users.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 7 (exceeds 5 allowed). Consider refactoring.
                    Open

                            async get() {
                                // if presence broadcast is disabled, return an empty array (all users are "offline")
                                if (settings.get('Presence_broadcast_disabled')) {
                                    return API.v1.success({
                                        users: [],
                    Severity: Minor
                    Found in apps/meteor/app/api/server/v1/users.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

                    Avoid too many return statements within this function.
                    Open

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

                      Avoid too many return statements within this function.
                      Open

                                          return API.v1.failure(e);
                      Severity: Major
                      Found in apps/meteor/app/api/server/v1/users.ts - About 30 mins to fix

                        Avoid too many return statements within this function.
                        Open

                                        return API.v1.failure('User not found');
                        Severity: Major
                        Found in apps/meteor/app/api/server/v1/users.ts - About 30 mins to fix

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

                          API.v1.addRoute(
                              'users.getUsernameSuggestion',
                              { authRequired: true },
                              {
                                  async get() {
                          Severity: Major
                          Found in apps/meteor/app/api/server/v1/users.ts and 1 other location - About 1 hr to fix
                          apps/meteor/app/api/server/v1/users.ts on lines 79..91

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

                          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(
                              'users.getAvatarSuggestion',
                              {
                                  authRequired: true,
                              },
                          Severity: Major
                          Found in apps/meteor/app/api/server/v1/users.ts and 1 other location - About 1 hr to fix
                          apps/meteor/app/api/server/v1/users.ts on lines 775..785

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

                          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

                                      const selector: { exceptions: Required<IUser>['username'][]; conditions: Filter<IUser>; term: string } = JSON.parse(selectorRaw);
                          Severity: Minor
                          Found in apps/meteor/app/api/server/v1/users.ts and 1 other location - About 45 mins to fix
                          apps/meteor/app/api/server/lib/users.ts on lines 17..17

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

                          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 (
                                          settings.get('API_Apply_permission_view-outside-room_on_users-list') &&
                                          !(await hasPermissionAsync(this.userId, 'view-outside-room'))
                                      ) {
                                          return API.v1.unauthorized();
                          Severity: Minor
                          Found in apps/meteor/app/api/server/v1/users.ts and 1 other location - About 35 mins to fix
                          apps/meteor/app/api/server/v1/users.ts on lines 588..593

                          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

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

                                      if (
                                          settings.get('API_Apply_permission_view-outside-room_on_users-list') &&
                                          !(await hasPermissionAsync(this.userId, 'view-outside-room'))
                                      ) {
                                          return API.v1.unauthorized();
                          Severity: Minor
                          Found in apps/meteor/app/api/server/v1/users.ts and 1 other location - About 35 mins to fix
                          apps/meteor/app/api/server/v1/users.ts on lines 479..484

                          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