discourse/lib/quote_comparer.rb
Sam Saffron 30990006a9 DEV: enable frozen string literal on all files
This reduces chances of errors where consumers of strings mutate inputs
and reduces memory usage of the app.

Test suite passes now, but there may be some stuff left, so we will run
a few sites on a branch prior to merging
2019-05-13 09:31:32 +08:00

27 lines
738 B
Ruby

# frozen_string_literal: true
class QuoteComparer
def self.whitespace
" \t\r\n".freeze
end
def initialize(topic_id, post_number, text)
@topic_id = topic_id
@post_number = post_number
@text = text
@parent_post = Post.where(topic_id: @topic_id, post_number: @post_number).first
end
# This algorithm is far from perfect, but it follows the Discourse
# philosophy of "catch the obvious cases, leave moderation for the
# complicated ones"
def modified?
return true if @text.blank? || @parent_post.blank?
parent_text = Nokogiri::HTML::fragment(@parent_post.cooked).text.delete(QuoteComparer.whitespace)
text = @text.delete(QuoteComparer.whitespace)
!parent_text.include?(text)
end
end