discourse/discourse

View on GitHub
app/models/post_action.rb

Summary

Maintainability
F
3 days
Test Coverage

File post_action.rb has 518 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require_dependency 'rate_limiter'
require_dependency 'system_message'

class PostAction < ActiveRecord::Base
  class AlreadyActed < StandardError; end
Severity: Major
Found in app/models/post_action.rb - About 1 day to fix

    Class PostAction has 42 methods (exceeds 20 allowed). Consider refactoring.
    Open

    class PostAction < ActiveRecord::Base
      class AlreadyActed < StandardError; end
      class FailedToCreatePost < StandardError; end
    
      include RateLimiter::OnCreateRecord
    Severity: Minor
    Found in app/models/post_action.rb - About 5 hrs to fix

      Method act has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring.
      Open

        def self.act(user, post, post_action_type_id, opts = {})
          limit_action!(user, post, post_action_type_id)
      
          begin
            related_post_id = create_message_for_post_action(user, post, post_action_type_id, opts)
      Severity: Minor
      Found in app/models/post_action.rb - 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

      Method act has 47 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

        def self.act(user, post, post_action_type_id, opts = {})
          limit_action!(user, post, post_action_type_id)
      
          begin
            related_post_id = create_message_for_post_action(user, post, post_action_type_id, opts)
      Severity: Minor
      Found in app/models/post_action.rb - About 1 hr to fix

        Method post_action_rate_limiter has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
        Open

          def post_action_rate_limiter
            return unless is_flag? || is_bookmark? || is_like?
        
            return @rate_limiter if @rate_limiter.present?
        
        
        Severity: Minor
        Found in app/models/post_action.rb - 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

        Method auto_hide_if_needed has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
        Open

          def self.auto_hide_if_needed(acting_user, post, post_action_type)
            return if post.hidden?
            return if (!acting_user.staff?) && post.user&.staff?
        
            if post_action_type == :spam &&
        Severity: Minor
        Found in app/models/post_action.rb - 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

        Method update_counters has 28 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

          def update_counters
            # Update denormalized counts
            column = "#{post_action_type_key}_count"
            count = PostAction.where(post_id: post_id)
              .where(post_action_type_id: post_action_type_id)
        Severity: Minor
        Found in app/models/post_action.rb - About 1 hr to fix

          Method hide_post! has 26 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

            def self.hide_post!(post, post_action_type, reason = nil)
              return if post.hidden
          
              unless reason
                reason = guess_hide_reason(post)
          Severity: Minor
          Found in app/models/post_action.rb - About 1 hr to fix

            Method create_message_for_post_action has a Cognitive Complexity of 9 (exceeds 5 allowed). Consider refactoring.
            Open

              def self.create_message_for_post_action(user, post, post_action_type_id, opts)
                post_action_type = PostActionType.types[post_action_type_id]
            
                return unless opts[:message] && [:notify_moderators, :notify_user, :spam].include?(post_action_type)
            
            
            Severity: Minor
            Found in app/models/post_action.rb - 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

            Method hide_post! has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
            Open

              def self.hide_post!(post, post_action_type, reason = nil)
                return if post.hidden
            
                unless reason
                  reason = guess_hide_reason(post)
            Severity: Minor
            Found in app/models/post_action.rb - 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

            Method remove_act has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
            Open

              def self.remove_act(user, post, post_action_type_id)
            
                limit_action!(user, post, post_action_type_id)
            
                finder = PostAction.where(post_id: post.id, user_id: user.id, post_action_type_id: post_action_type_id)
            Severity: Minor
            Found in app/models/post_action.rb - 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

            Method update_counters has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
            Open

              def update_counters
                # Update denormalized counts
                column = "#{post_action_type_key}_count"
                count = PostAction.where(post_id: post_id)
                  .where(post_action_type_id: post_action_type_id)
            Severity: Minor
            Found in app/models/post_action.rb - 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

            There are no issues that match your filters.

            Category
            Status