Class: Gitlab::Email::ReplyParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/email/reply_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, trim_reply: true, append_reply: false, allow_only_quotes: false) ⇒ ReplyParser

Returns a new instance of ReplyParser.



9
10
11
12
13
14
# File 'lib/gitlab/email/reply_parser.rb', line 9

def initialize(message, trim_reply: true, append_reply: false, allow_only_quotes: false)
  @message = message
  @trim_reply = trim_reply
  @append_reply = append_reply
  @allow_only_quotes = allow_only_quotes
end

Instance Attribute Details

#allow_only_quotesObject

Returns the value of attribute allow_only_quotes.



7
8
9
# File 'lib/gitlab/email/reply_parser.rb', line 7

def allow_only_quotes
  @allow_only_quotes
end

#messageObject

Returns the value of attribute message.



7
8
9
# File 'lib/gitlab/email/reply_parser.rb', line 7

def message
  @message
end

Instance Method Details

#executeObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab/email/reply_parser.rb', line 16

def execute
  body = select_body(message)

  encoding = body.encoding
  body, stripped_text = EmailReplyTrimmer.trim(body, @append_reply) if @trim_reply
  return '' unless body

  # not using /\s+$/ here because that deletes empty lines
  body = body.gsub(/[ \t]$/, '')

  # NOTE: We currently don't support empty quotes.
  # EmailReplyTrimmer allows this as a special case,
  # so we detect it manually here.
  #
  # If allow_only_quotes is true a message where all lines starts with ">" is allowed.
  # This could happen if an email has an empty quote, forwarded without any new content.
  return "" if body.lines.all? do |l|
    l.strip.empty? || (!allow_only_quotes && l.start_with?('>'))
  end

  encoded_body = force_utf8(body.force_encoding(encoding))
  return encoded_body unless @append_reply

  [encoded_body, force_utf8(stripped_text.force_encoding(encoding))]
end