From 26573ad7e67e64d6db222877cf2853920c2c7dae Mon Sep 17 00:00:00 2001 From: David Yip Date: Sun, 3 Jun 2018 19:41:54 -0500 Subject: Thread scopes through #matches?. #454. Also add an apply_to_mentions attribute on Glitch::KeywordMute, which is used to calculate scope. Next up: additions to the test suite to demonstrate how scoping works. --- app/models/glitch/keyword_mute.rb | 53 ++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 18 deletions(-) (limited to 'app/models/glitch/keyword_mute.rb') diff --git a/app/models/glitch/keyword_mute.rb b/app/models/glitch/keyword_mute.rb index e7cbbe617..11b7958f6 100644 --- a/app/models/glitch/keyword_mute.rb +++ b/app/models/glitch/keyword_mute.rb @@ -3,12 +3,13 @@ # # Table name: glitch_keyword_mutes # -# id :integer not null, primary key -# account_id :integer not null -# keyword :string not null -# whole_word :boolean default(TRUE), not null -# created_at :datetime not null -# updated_at :datetime not null +# id :bigint(8) not null, primary key +# account_id :bigint(8) not null +# keyword :string not null +# whole_word :boolean default(TRUE), not null +# created_at :datetime not null +# updated_at :datetime not null +# apply_to_mentions :boolean default(TRUE), not null # class Glitch::KeywordMute < ApplicationRecord @@ -18,6 +19,12 @@ class Glitch::KeywordMute < ApplicationRecord after_commit :invalidate_cached_matchers + module Scopes + Unscoped = 0b00 + HomeFeed = 0b01 + Mentions = 0b10 + end + def self.text_matcher_for(account_id) TextMatcher.new(account_id) end @@ -26,6 +33,13 @@ class Glitch::KeywordMute < ApplicationRecord TagMatcher.new(account_id) end + def scope + s = Scopes::Unscoped + s |= Scopes::HomeFeed + s |= Scopes::Mentions if apply_to_mentions? + s + end + private def invalidate_cached_matchers @@ -36,10 +50,12 @@ class Glitch::KeywordMute < ApplicationRecord class CachedKeywordMute attr_reader :keyword attr_reader :whole_word + attr_reader :scope - def initialize(keyword, whole_word) + def initialize(keyword, whole_word, scope) @keyword = keyword @whole_word = whole_word + @scope = scope end def boundary_regex_for_keyword @@ -49,26 +65,27 @@ class Glitch::KeywordMute < ApplicationRecord /(?mix:#{sb}#{Regexp.escape(keyword)}#{eb})/ end - def matches?(str) - str =~ (whole_word ? boundary_regex_for_keyword : /#{keyword}/i) + def matches?(str, required_scope) + ((required_scope & scope) == required_scope) && \ + str =~ (whole_word ? boundary_regex_for_keyword : /#{keyword}/i) end end class Matcher attr_reader :account_id - attr_reader :words + attr_reader :keywords def initialize(account_id) @account_id = account_id - @words = Rails.cache.fetch(self.class.cache_key(account_id)) { fetch_keywords } + @keywords = Rails.cache.fetch(self.class.cache_key(account_id)) { fetch_keywords } end protected def fetch_keywords - Glitch::KeywordMute.where(account_id: account_id).pluck(:whole_word, :keyword).map do |whole_word, keyword| - CachedKeywordMute.new(transform_keyword(keyword), whole_word) - end + Glitch::KeywordMute.select(:whole_word, :keyword, :apply_to_mentions) + .where(account_id: account_id) + .map { |kw| CachedKeywordMute.new(transform_keyword(kw.keyword), kw.whole_word, kw.scope) } end def transform_keyword(keyword) @@ -81,8 +98,8 @@ class Glitch::KeywordMute < ApplicationRecord format('keyword_mutes:regex:text:%s', account_id) end - def matches?(str) - words.any? { |w| w.matches?(str) } + def matches?(str, scope) + keywords.any? { |kw| kw.matches?(str, scope) } end end @@ -91,9 +108,9 @@ class Glitch::KeywordMute < ApplicationRecord format('keyword_mutes:regex:tag:%s', account_id) end - def matches?(tags) + def matches?(tags, scope) tags.pluck(:name).any? do |n| - words.any? { |w| w.matches?(n) } + keywords.any? { |kw| kw.matches?(n, scope) } end end -- cgit