2017-12-04 04:40:28 +01:00
|
|
|
require 'htmlentities'
|
|
|
|
|
2017-12-04 04:49:55 +01:00
|
|
|
class Glitch::KeywordMuteHelper
|
2017-12-04 00:28:30 +01:00
|
|
|
include ActionView::Helpers::SanitizeHelper
|
|
|
|
|
|
|
|
attr_reader :text_matcher
|
|
|
|
attr_reader :tag_matcher
|
2017-12-04 04:40:28 +01:00
|
|
|
attr_reader :entity_decoder
|
2017-12-04 00:28:30 +01:00
|
|
|
|
|
|
|
def initialize(receiver_id)
|
2017-12-04 04:40:28 +01:00
|
|
|
@text_matcher = Glitch::KeywordMute.text_matcher_for(receiver_id)
|
|
|
|
@tag_matcher = Glitch::KeywordMute.tag_matcher_for(receiver_id)
|
|
|
|
@entity_decoder = HTMLEntities.new
|
2017-12-04 00:28:30 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def matches?(status)
|
|
|
|
matchers_match?(status) || (status.reblog? && matchers_match?(status.reblog))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def matchers_match?(status)
|
2017-12-04 04:40:28 +01:00
|
|
|
text_matcher.matches?(prepare_text(status.text)) ||
|
|
|
|
text_matcher.matches?(prepare_text(status.spoiler_text)) ||
|
2017-12-04 00:28:30 +01:00
|
|
|
tag_matcher.matches?(status.tags)
|
|
|
|
end
|
2017-12-04 04:40:28 +01:00
|
|
|
|
|
|
|
def prepare_text(text)
|
2017-12-04 04:49:55 +01:00
|
|
|
entity_decoder.decode(strip_tags(text)).tap { |x| puts x }
|
2017-12-04 04:40:28 +01:00
|
|
|
end
|
2017-12-04 00:28:30 +01:00
|
|
|
end
|