diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/lib/feed_manager.rb | 8 | ||||
-rw-r--r-- | app/models/bookmark.rb | 6 | ||||
-rw-r--r-- | app/models/glitch/keyword_mute.rb | 53 | ||||
-rw-r--r-- | app/models/glitch/keyword_mute_helper.rb | 12 |
4 files changed, 48 insertions, 31 deletions
diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb index 3a2dcac68..74794f00c 100644 --- a/app/lib/feed_manager.rb +++ b/app/lib/feed_manager.rb @@ -153,7 +153,7 @@ class FeedManager def filter_from_home?(status, receiver_id) return false if receiver_id == status.account_id return true if status.reply? && (status.in_reply_to_id.nil? || status.in_reply_to_account_id.nil?) - return true if keyword_filter?(status, receiver_id) + return true if keyword_filter?(status, receiver_id, Glitch::KeywordMute::Scopes::HomeFeed) check_for_mutes = [status.account_id] check_for_mutes.concat(status.mentions.pluck(:account_id)) @@ -182,8 +182,8 @@ class FeedManager false end - def keyword_filter?(status, receiver_id) - Glitch::KeywordMuteHelper.new(receiver_id).matches?(status) + def keyword_filter?(status, receiver_id, scope) + Glitch::KeywordMuteHelper.new(receiver_id).matches?(status, scope) end def filter_from_mentions?(status, receiver_id) @@ -197,7 +197,7 @@ class FeedManager should_filter = blocks_or_mutes?(receiver_id, check_for_blocks, :mentions) # Filter if it's from someone I blocked, in reply to someone I blocked, or mentioning someone I blocked (or muted) should_filter ||= (status.account.silenced? && !Follow.where(account_id: receiver_id, target_account_id: status.account_id).exists?) # of if the account is silenced and I'm not following them - should_filter ||= keyword_filter?(status, receiver_id) # or if the mention contains a muted keyword + should_filter ||= keyword_filter?(status, receiver_id, Glitch::KeywordMute::Scopes::Mentions) # or if the mention contains a muted keyword should_filter end diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb index 01dc48ee7..916261a17 100644 --- a/app/models/bookmark.rb +++ b/app/models/bookmark.rb @@ -3,11 +3,11 @@ # # Table name: bookmarks # -# id :integer not null, primary key +# id :bigint(8) not null, primary key +# account_id :bigint(8) not null +# status_id :bigint(8) not null # created_at :datetime not null # updated_at :datetime not null -# account_id :integer not null -# status_id :integer not null # class Bookmark < ApplicationRecord 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 diff --git a/app/models/glitch/keyword_mute_helper.rb b/app/models/glitch/keyword_mute_helper.rb index 6d067947f..955c3b1f3 100644 --- a/app/models/glitch/keyword_mute_helper.rb +++ b/app/models/glitch/keyword_mute_helper.rb @@ -9,16 +9,16 @@ class Glitch::KeywordMuteHelper @tag_matcher = Glitch::KeywordMute.tag_matcher_for(receiver_id) end - def matches?(status) - matchers_match?(status) || (status.reblog? && matchers_match?(status.reblog)) + def matches?(status, scope) + matchers_match?(status, scope) || (status.reblog? && matchers_match?(status.reblog, scope)) 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) + def matchers_match?(status, scope) + text_matcher.matches?(prepare_text(status.text), scope) || + text_matcher.matches?(prepare_text(status.spoiler_text), scope) || + tag_matcher.matches?(status.tags, scope) end def prepare_text(text) |