discourse/discourse

View on GitHub
lib/email/receiver.rb

Summary

Maintainability
F
1 wk
Test Coverage

File receiver.rb has 983 lines of code (exceeds 250 allowed). Consider refactoring.
Open

require "digest"
require_dependency "new_post_manager"
require_dependency "html_to_markdown"
require_dependency "plain_text_to_markdown"
require_dependency "upload_creator"
Severity: Major
Found in lib/email/receiver.rb - About 2 days to fix

    Class Receiver has 81 methods (exceeds 20 allowed). Consider refactoring.
    Open

      class Receiver
        # If you add a new error, you need to
        #   * add it to Email::Processor#handle_failure()
        #   * add text to server.en.yml (parent key: "emails.incoming.errors")
        class ProcessingError              < StandardError; end
    Severity: Major
    Found in lib/email/receiver.rb - About 1 day to fix

      Method process_internal has a Cognitive Complexity of 38 (exceeds 5 allowed). Consider refactoring.
      Open

          def process_internal
            handle_bounce if is_bounce?
            raise NoSenderDetectedError if @from_email.blank?
            raise FromReplyByAddressError if is_from_reply_by_email_address?
            raise ScreenedEmailError if ScreenedEmail.should_block?(@from_email)
      Severity: Minor
      Found in lib/email/receiver.rb - About 5 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 select_body has a Cognitive Complexity of 33 (exceeds 5 allowed). Consider refactoring.
      Open

          def select_body
            text = nil
            html = nil
            text_content_type = nil
      
      
      Severity: Minor
      Found in lib/email/receiver.rb - About 4 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 add_other_addresses has a Cognitive Complexity of 27 (exceeds 5 allowed). Consider refactoring.
      Open

          def add_other_addresses(post, sender)
            %i(to cc bcc).each do |d|
              if @mail[d] && @mail[d].address_list && @mail[d].address_list.addresses
                @mail[d].address_list.addresses.each do |address_field|
                  begin
      Severity: Minor
      Found in lib/email/receiver.rb - About 3 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 parse_from_field has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring.
      Open

          def parse_from_field(mail = nil)
            mail ||= @mail
      
            if email_log.present?
              email = email_log.to_address || email_log.user&.email
      Severity: Minor
      Found in lib/email/receiver.rb - About 3 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 add_attachments has a Cognitive Complexity of 21 (exceeds 5 allowed). Consider refactoring.
      Open

          def add_attachments(raw, user, options = {})
            raw = raw.dup
      
            rejected_attachments = []
            attachments.each do |attachment|
      Severity: Minor
      Found in lib/email/receiver.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 find_or_create_user has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
      Open

          def find_or_create_user(email, display_name, raise_on_failed_create: false)
            user = nil
      
            User.transaction do
              user = User.find_by_email(email)
      Severity: Minor
      Found in lib/email/receiver.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 process_destination has a Cognitive Complexity of 18 (exceeds 5 allowed). Consider refactoring.
      Open

          def process_destination(destination, user, body, elided, hidden_reason_id)
            return if SiteSetting.forwarded_emails_behaviour != "hide" &&
                      has_been_forwarded? &&
                      process_forwarded_email(destination, user)
      
      
      Severity: Minor
      Found in lib/email/receiver.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 process_internal has 60 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          def process_internal
            handle_bounce if is_bounce?
            raise NoSenderDetectedError if @from_email.blank?
            raise FromReplyByAddressError if is_from_reply_by_email_address?
            raise ScreenedEmailError if ScreenedEmail.should_block?(@from_email)
      Severity: Major
      Found in lib/email/receiver.rb - About 2 hrs to fix

        Method select_body has 59 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            def select_body
              text = nil
              html = nil
              text_content_type = nil
        
        
        Severity: Major
        Found in lib/email/receiver.rb - About 2 hrs to fix

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

              def self.check_address(address, include_verp = false)
                # only check for a group/category when 'email_in' is enabled
                if SiteSetting.email_in
                  group = Group.find_by_email(address)
                  return { type: :group, obj: group } if group
          Severity: Minor
          Found in lib/email/receiver.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 create_post has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring.
          Open

              def create_post(options = {})
                options[:via_email] = true
                options[:raw_email] = @raw_email
          
                options[:created_at] ||= @mail.date
          Severity: Minor
          Found in lib/email/receiver.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 process! has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
          Open

              def process!
                return if is_blacklisted?
                id_hash = Digest::SHA1.hexdigest(@message_id)
                DistributedMutex.synchronize("process_email_#{id_hash}") do
                  begin
          Severity: Minor
          Found in lib/email/receiver.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 process_destination has 38 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              def process_destination(destination, user, body, elided, hidden_reason_id)
                return if SiteSetting.forwarded_emails_behaviour != "hide" &&
                          has_been_forwarded? &&
                          process_forwarded_email(destination, user)
          
          
          Severity: Minor
          Found in lib/email/receiver.rb - About 1 hr to fix

            Method create_post has 37 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                def create_post(options = {})
                  options[:via_email] = true
                  options[:raw_email] = @raw_email
            
                  options[:created_at] ||= @mail.date
            Severity: Minor
            Found in lib/email/receiver.rb - About 1 hr to fix

              Method add_attachments has 35 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                  def add_attachments(raw, user, options = {})
                    raw = raw.dup
              
                    rejected_attachments = []
                    attachments.each do |attachment|
              Severity: Minor
              Found in lib/email/receiver.rb - About 1 hr to fix

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

                    def create_reply(options = {})
                      raise TopicNotFoundError if options[:topic].nil? || options[:topic].trashed?
                      raise BouncedEmailError if options[:bounce] && options[:topic].archetype != Archetype.private_message
                
                      options[:post] = nil if options[:post]&.trashed?
                Severity: Minor
                Found in lib/email/receiver.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 forwarded_email_create_replies has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring.
                Open

                    def forwarded_email_create_replies(destination, user)
                      embedded = Mail.new(embedded_email_raw)
                      email, display_name = parse_from_field(embedded)
                
                      return false if email.blank? || !email["@"]
                Severity: Minor
                Found in lib/email/receiver.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 create_group_post has 30 lines of code (exceeds 25 allowed). Consider refactoring.
                Open

                    def create_group_post(group, user, body, elided, hidden_reason_id)
                      message_ids = Email::Receiver.extract_reply_message_ids(@mail, max_message_id_count: 5)
                      post_ids = []
                
                      incoming_emails = IncomingEmail
                Severity: Minor
                Found in lib/email/receiver.rb - About 1 hr to fix

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

                      def parse_from_field(mail = nil)
                        mail ||= @mail
                  
                        if email_log.present?
                          email = email_log.to_address || email_log.user&.email
                  Severity: Minor
                  Found in lib/email/receiver.rb - About 1 hr to fix

                    Method handle_bounce has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring.
                    Open

                        def handle_bounce
                          @incoming_email.update_columns(is_bounce: true)
                    
                          if email_log.present?
                            email_log.update_columns(bounced: true)
                    Severity: Minor
                    Found in lib/email/receiver.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 handle_bounce has 27 lines of code (exceeds 25 allowed). Consider refactoring.
                    Open

                        def handle_bounce
                          @incoming_email.update_columns(is_bounce: true)
                    
                          if email_log.present?
                            email_log.update_columns(bounced: true)
                    Severity: Minor
                    Found in lib/email/receiver.rb - About 1 hr to fix

                      Method forwarded_email_create_topic has 27 lines of code (exceeds 25 allowed). Consider refactoring.
                      Open

                          def forwarded_email_create_topic(destination: , user: , raw: , title: , date: nil, embedded_user: nil)
                            case destination[:type]
                            when :group
                              group = destination[:obj]
                              topic_user = embedded_user&.call || user
                      Severity: Minor
                      Found in lib/email/receiver.rb - About 1 hr to fix

                        Method forwarded_email_create_topic has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                        Open

                            def forwarded_email_create_topic(destination: , user: , raw: , title: , date: nil, embedded_user: nil)
                              case destination[:type]
                              when :group
                                group = destination[:obj]
                                topic_user = embedded_user&.call || user
                        Severity: Minor
                        Found in lib/email/receiver.rb - 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

                        Method fix_charset has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring.
                        Open

                            def fix_charset(mail_part)
                              return nil if mail_part.blank? || mail_part.body.blank?
                        
                              string = mail_part.body.decoded rescue nil
                        
                        
                        Severity: Minor
                        Found in lib/email/receiver.rb - 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

                        Method create_group_post has 5 arguments (exceeds 4 allowed). Consider refactoring.
                        Open

                            def create_group_post(group, user, body, elided, hidden_reason_id)
                        Severity: Minor
                        Found in lib/email/receiver.rb - About 35 mins to fix

                          Method process_destination has 5 arguments (exceeds 4 allowed). Consider refactoring.
                          Open

                              def process_destination(destination, user, body, elided, hidden_reason_id)
                          Severity: Minor
                          Found in lib/email/receiver.rb - About 35 mins to fix

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

                                def forwarded_reply_key?(post_reply_key, user)
                                  incoming_emails = IncomingEmail
                                    .joins(:post)
                                    .where('posts.topic_id = ?', post_reply_key.post.topic_id)
                                    .addressed_to(post_reply_key.reply_key)
                            Severity: Minor
                            Found in lib/email/receiver.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

                            Avoid too many return statements within this method.
                            Open

                                  return extract_from_address_and_name(mail.from) if mail.from.is_a? String
                            Severity: Major
                            Found in lib/email/receiver.rb - About 30 mins to fix

                              Avoid too many return statements within this method.
                              Open

                                        return [from_address, from_display_name] if from_address
                              Severity: Major
                              Found in lib/email/receiver.rb - About 30 mins to fix

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

                                    def create_group_post(group, user, body, elided, hidden_reason_id)
                                      message_ids = Email::Receiver.extract_reply_message_ids(@mail, max_message_id_count: 5)
                                      post_ids = []
                                
                                      incoming_emails = IncomingEmail
                                Severity: Minor
                                Found in lib/email/receiver.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