From f1f67c46c5b369476090429a46dbc646d772ae25 Mon Sep 17 00:00:00 2001 From: David Yip Date: Sat, 10 Feb 2018 10:32:14 -0600 Subject: [PATCH] Use Html2Text to generate plaintext for keyword mutes. #236. This allows us to match URLs inside link hrefs. --- app/models/glitch/keyword_mute_helper.rb | 8 ++------ spec/models/glitch/keyword_mute_helper_spec.rb | 7 +++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/models/glitch/keyword_mute_helper.rb b/app/models/glitch/keyword_mute_helper.rb index 1b8c64e4ed..6d067947f1 100644 --- a/app/models/glitch/keyword_mute_helper.rb +++ b/app/models/glitch/keyword_mute_helper.rb @@ -1,16 +1,12 @@ -require 'htmlentities' +require 'html2text' class Glitch::KeywordMuteHelper - include ActionView::Helpers::SanitizeHelper - attr_reader :text_matcher attr_reader :tag_matcher - attr_reader :entity_decoder def initialize(receiver_id) @text_matcher = Glitch::KeywordMute.text_matcher_for(receiver_id) @tag_matcher = Glitch::KeywordMute.tag_matcher_for(receiver_id) - @entity_decoder = HTMLEntities.new end def matches?(status) @@ -26,6 +22,6 @@ class Glitch::KeywordMuteHelper end def prepare_text(text) - entity_decoder.decode(strip_tags(text)).tap { |x| puts x } + Html2Text.convert(text) end end diff --git a/spec/models/glitch/keyword_mute_helper_spec.rb b/spec/models/glitch/keyword_mute_helper_spec.rb index 9d09e58da8..b3f991d5b8 100644 --- a/spec/models/glitch/keyword_mute_helper_spec.rb +++ b/spec/models/glitch/keyword_mute_helper_spec.rb @@ -39,5 +39,12 @@ RSpec.describe Glitch::KeywordMuteHelper do expect(helper.matches?(status)).to be true end + + it 'matches link hrefs in HTML text' do + status = Fabricate(:status, text: '

yep

') + Glitch::KeywordMute.create!(account: alice, keyword: 'milk') + + expect(helper.matches?(status)).to be true + end end end