From d263e3bc2de720f4e6749cf4a8f2074427b65d07 Mon Sep 17 00:00:00 2001 From: David Yip Date: Sun, 3 Dec 2017 21:40:28 -0600 Subject: Fill out some examples for Glitch::FilterHelper. #234. Also add HTML entity decoding to Glitch::FilterHelper, which is needed to e.g. match "<" to the tag-stripped version of "

<3

" or "

<3

". --- spec/models/glitch/filter_helper_spec.rb | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 spec/models/glitch/filter_helper_spec.rb (limited to 'spec/models/glitch') diff --git a/spec/models/glitch/filter_helper_spec.rb b/spec/models/glitch/filter_helper_spec.rb new file mode 100644 index 000000000..9a808667d --- /dev/null +++ b/spec/models/glitch/filter_helper_spec.rb @@ -0,0 +1,43 @@ +require 'rails_helper' + +RSpec.describe Glitch::FilterHelper do + describe '#matches?' do + let(:alice) { Fabricate(:account, username: 'alice').tap(&:save!) } + let(:helper) { Glitch::FilterHelper.new(alice) } + + it 'ignores names of HTML tags in status text' do + status = Fabricate(:status, text: 'uh example') + Glitch::KeywordMute.create!(account: alice, keyword: 'addr') + + expect(helper.matches?(status)).to be false + end + + it 'ignores properties of HTML tags in status text' do + status = Fabricate(:status, text: 'uh example') + Glitch::KeywordMute.create!(account: alice, keyword: 'href') + + expect(helper.matches?(status)).to be false + end + + it 'matches text inside HTML tags' do + status = Fabricate(:status, text: '

HEY THIS IS SOMETHING ANNOYING

') + Glitch::KeywordMute.create!(account: alice, keyword: 'annoying') + + expect(helper.matches?(status)).to be true + end + + it 'matches < in HTML-stripped text' do + status = Fabricate(:status, text: '

I <3 oats

') + Glitch::KeywordMute.create!(account: alice, keyword: '<3') + + expect(helper.matches?(status)).to be true + end + + it 'matches < in HTML text' do + status = Fabricate(:status, text: '

I <3 oats

') + Glitch::KeywordMute.create!(account: alice, keyword: '<3') + + expect(helper.matches?(status)).to be true + end + end +end -- cgit From 53c86b29f05049d77d17a35a0ca6287174431783 Mon Sep 17 00:00:00 2001 From: David Yip Date: Sun, 3 Dec 2017 21:49:55 -0600 Subject: Glitch::FilterHelper -> Glitch::KeywordMuteHelper. #234. The class helps out with keyword mutes, not just some general concept of "filtering". --- app/lib/feed_manager.rb | 2 +- app/models/glitch/filter_helper.rb | 31 ------------------- app/models/glitch/keyword_mute_helper.rb | 31 +++++++++++++++++++ spec/models/glitch/filter_helper_spec.rb | 43 -------------------------- spec/models/glitch/keyword_mute_helper_spec.rb | 43 ++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 75 deletions(-) delete mode 100644 app/models/glitch/filter_helper.rb create mode 100644 app/models/glitch/keyword_mute_helper.rb delete mode 100644 spec/models/glitch/filter_helper_spec.rb create mode 100644 spec/models/glitch/keyword_mute_helper_spec.rb (limited to 'spec/models/glitch') diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb index 7507b37d2..c16b25635 100644 --- a/app/lib/feed_manager.rb +++ b/app/lib/feed_manager.rb @@ -178,7 +178,7 @@ class FeedManager end def keyword_filter?(status, receiver_id) - Glitch::FilterHelper.new(receiver_id).matches?(status) + Glitch::KeywordMuteHelper.new(receiver_id).matches?(status) end def filter_from_mentions?(status, receiver_id) diff --git a/app/models/glitch/filter_helper.rb b/app/models/glitch/filter_helper.rb deleted file mode 100644 index 11be877c1..000000000 --- a/app/models/glitch/filter_helper.rb +++ /dev/null @@ -1,31 +0,0 @@ -require 'htmlentities' - -class Glitch::FilterHelper - 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) - matchers_match?(status) || (status.reblog? && matchers_match?(status.reblog)) - end - - private - - def matchers_match?(status) - text_matcher.matches?(prepare_text(status.text)) || - text_matcher.matches?(prepare_text(status.spoiler_text)) || - tag_matcher.matches?(status.tags) - end - - def prepare_text(text) - entity_decoder.decode(strip_tags(text)) - end -end diff --git a/app/models/glitch/keyword_mute_helper.rb b/app/models/glitch/keyword_mute_helper.rb new file mode 100644 index 000000000..1b8c64e4e --- /dev/null +++ b/app/models/glitch/keyword_mute_helper.rb @@ -0,0 +1,31 @@ +require 'htmlentities' + +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) + matchers_match?(status) || (status.reblog? && matchers_match?(status.reblog)) + end + + private + + def matchers_match?(status) + text_matcher.matches?(prepare_text(status.text)) || + text_matcher.matches?(prepare_text(status.spoiler_text)) || + tag_matcher.matches?(status.tags) + end + + def prepare_text(text) + entity_decoder.decode(strip_tags(text)).tap { |x| puts x } + end +end diff --git a/spec/models/glitch/filter_helper_spec.rb b/spec/models/glitch/filter_helper_spec.rb deleted file mode 100644 index 9a808667d..000000000 --- a/spec/models/glitch/filter_helper_spec.rb +++ /dev/null @@ -1,43 +0,0 @@ -require 'rails_helper' - -RSpec.describe Glitch::FilterHelper do - describe '#matches?' do - let(:alice) { Fabricate(:account, username: 'alice').tap(&:save!) } - let(:helper) { Glitch::FilterHelper.new(alice) } - - it 'ignores names of HTML tags in status text' do - status = Fabricate(:status, text: 'uh example') - Glitch::KeywordMute.create!(account: alice, keyword: 'addr') - - expect(helper.matches?(status)).to be false - end - - it 'ignores properties of HTML tags in status text' do - status = Fabricate(:status, text: 'uh example') - Glitch::KeywordMute.create!(account: alice, keyword: 'href') - - expect(helper.matches?(status)).to be false - end - - it 'matches text inside HTML tags' do - status = Fabricate(:status, text: '

HEY THIS IS SOMETHING ANNOYING

') - Glitch::KeywordMute.create!(account: alice, keyword: 'annoying') - - expect(helper.matches?(status)).to be true - end - - it 'matches < in HTML-stripped text' do - status = Fabricate(:status, text: '

I <3 oats

') - Glitch::KeywordMute.create!(account: alice, keyword: '<3') - - expect(helper.matches?(status)).to be true - end - - it 'matches < in HTML text' do - status = Fabricate(:status, text: '

I <3 oats

') - Glitch::KeywordMute.create!(account: alice, keyword: '<3') - - expect(helper.matches?(status)).to be true - end - end -end diff --git a/spec/models/glitch/keyword_mute_helper_spec.rb b/spec/models/glitch/keyword_mute_helper_spec.rb new file mode 100644 index 000000000..9d09e58da --- /dev/null +++ b/spec/models/glitch/keyword_mute_helper_spec.rb @@ -0,0 +1,43 @@ +require 'rails_helper' + +RSpec.describe Glitch::KeywordMuteHelper do + describe '#matches?' do + let(:alice) { Fabricate(:account, username: 'alice').tap(&:save!) } + let(:helper) { Glitch::KeywordMuteHelper.new(alice) } + + it 'ignores names of HTML tags in status text' do + status = Fabricate(:status, text: 'uh example') + Glitch::KeywordMute.create!(account: alice, keyword: 'addr') + + expect(helper.matches?(status)).to be false + end + + it 'ignores properties of HTML tags in status text' do + status = Fabricate(:status, text: 'uh example') + Glitch::KeywordMute.create!(account: alice, keyword: 'href') + + expect(helper.matches?(status)).to be false + end + + it 'matches text inside HTML tags' do + status = Fabricate(:status, text: '

HEY THIS IS SOMETHING ANNOYING

') + Glitch::KeywordMute.create!(account: alice, keyword: 'annoying') + + expect(helper.matches?(status)).to be true + end + + it 'matches < in HTML-stripped text' do + status = Fabricate(:status, text: '

I <3 oats

') + Glitch::KeywordMute.create!(account: alice, keyword: '<3') + + expect(helper.matches?(status)).to be true + end + + it 'matches < in HTML text' do + status = Fabricate(:status, text: '

I <3 oats

') + Glitch::KeywordMute.create!(account: alice, keyword: '<3') + + expect(helper.matches?(status)).to be true + end + end +end -- cgit From f1f67c46c5b369476090429a46dbc646d772ae25 Mon Sep 17 00:00:00 2001 From: David Yip Date: Sat, 10 Feb 2018 10:32:14 -0600 Subject: 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(-) (limited to 'spec/models/glitch') diff --git a/app/models/glitch/keyword_mute_helper.rb b/app/models/glitch/keyword_mute_helper.rb index 1b8c64e4e..6d067947f 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 9d09e58da..b3f991d5b 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 -- cgit