2017-10-14 18:36:53 -07:00
|
|
|
# frozen_string_literal: true
|
2017-10-09 15:28:28 -07:00
|
|
|
# == Schema Information
|
|
|
|
#
|
2017-10-21 12:47:17 -07:00
|
|
|
# Table name: glitch_keyword_mutes
|
2017-10-09 15:28:28 -07:00
|
|
|
#
|
2018-06-03 17:41:54 -07:00
|
|
|
# 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
|
2017-10-09 15:28:28 -07:00
|
|
|
#
|
|
|
|
|
2017-10-21 12:47:17 -07:00
|
|
|
class Glitch::KeywordMute < ApplicationRecord
|
2017-10-14 18:36:53 -07:00
|
|
|
belongs_to :account, required: true
|
|
|
|
|
|
|
|
validates_presence_of :keyword
|
|
|
|
|
2017-11-15 15:26:29 -08:00
|
|
|
after_commit :invalidate_cached_matchers
|
2017-10-15 00:52:53 -07:00
|
|
|
|
2018-06-03 17:41:54 -07:00
|
|
|
module Scopes
|
|
|
|
Unscoped = 0b00
|
|
|
|
HomeFeed = 0b01
|
|
|
|
Mentions = 0b10
|
|
|
|
end
|
|
|
|
|
2017-11-15 15:26:29 -08:00
|
|
|
def self.text_matcher_for(account_id)
|
|
|
|
TextMatcher.new(account_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.tag_matcher_for(account_id)
|
|
|
|
TagMatcher.new(account_id)
|
2017-10-15 00:52:53 -07:00
|
|
|
end
|
|
|
|
|
2018-06-03 17:41:54 -07:00
|
|
|
def scope
|
|
|
|
s = Scopes::Unscoped
|
|
|
|
s |= Scopes::HomeFeed
|
|
|
|
s |= Scopes::Mentions if apply_to_mentions?
|
|
|
|
s
|
|
|
|
end
|
|
|
|
|
2017-10-15 00:52:53 -07:00
|
|
|
private
|
|
|
|
|
2017-11-15 15:26:29 -08:00
|
|
|
def invalidate_cached_matchers
|
|
|
|
Rails.cache.delete(TextMatcher.cache_key(account_id))
|
|
|
|
Rails.cache.delete(TagMatcher.cache_key(account_id))
|
2017-10-14 18:36:53 -07:00
|
|
|
end
|
|
|
|
|
2018-06-03 16:12:55 -07:00
|
|
|
class CachedKeywordMute
|
|
|
|
attr_reader :keyword
|
|
|
|
attr_reader :whole_word
|
2018-06-03 17:41:54 -07:00
|
|
|
attr_reader :scope
|
2018-06-03 16:12:55 -07:00
|
|
|
|
2018-06-03 17:41:54 -07:00
|
|
|
def initialize(keyword, whole_word, scope)
|
2018-06-03 16:12:55 -07:00
|
|
|
@keyword = keyword
|
|
|
|
@whole_word = whole_word
|
2018-06-03 17:41:54 -07:00
|
|
|
@scope = scope
|
2018-06-03 16:12:55 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def boundary_regex_for_keyword
|
|
|
|
sb = keyword =~ /\A[[:word:]]/ ? '\b' : ''
|
|
|
|
eb = keyword =~ /[[:word:]]\Z/ ? '\b' : ''
|
|
|
|
|
|
|
|
/(?mix:#{sb}#{Regexp.escape(keyword)}#{eb})/
|
|
|
|
end
|
|
|
|
|
2018-06-03 17:41:54 -07:00
|
|
|
def matches?(str, required_scope)
|
|
|
|
((required_scope & scope) == required_scope) && \
|
2018-06-12 14:39:30 -07:00
|
|
|
str =~ (whole_word ? boundary_regex_for_keyword : /#{Regexp.escape(keyword)}/i)
|
2018-06-03 16:12:55 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Matcher
|
2017-10-21 22:24:32 -07:00
|
|
|
attr_reader :account_id
|
2018-06-03 17:41:54 -07:00
|
|
|
attr_reader :keywords
|
2017-10-14 18:36:53 -07:00
|
|
|
|
2017-10-15 00:52:53 -07:00
|
|
|
def initialize(account_id)
|
2017-10-21 22:24:32 -07:00
|
|
|
@account_id = account_id
|
2018-06-03 17:41:54 -07:00
|
|
|
@keywords = Rails.cache.fetch(self.class.cache_key(account_id)) { fetch_keywords }
|
2017-10-21 22:24:32 -07:00
|
|
|
end
|
|
|
|
|
2017-11-15 15:26:29 -08:00
|
|
|
protected
|
|
|
|
|
2018-06-03 16:12:55 -07:00
|
|
|
def fetch_keywords
|
2018-06-03 17:41:54 -07:00
|
|
|
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) }
|
2017-11-15 15:26:29 -08:00
|
|
|
end
|
|
|
|
|
2018-06-03 16:12:55 -07:00
|
|
|
def transform_keyword(keyword)
|
|
|
|
keyword
|
2017-11-15 15:26:29 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-03 16:12:55 -07:00
|
|
|
class TextMatcher < Matcher
|
2017-11-15 15:26:29 -08:00
|
|
|
def self.cache_key(account_id)
|
2017-11-15 16:08:03 -08:00
|
|
|
format('keyword_mutes:regex:text:%s', account_id)
|
2017-11-15 15:26:29 -08:00
|
|
|
end
|
|
|
|
|
2018-06-03 17:41:54 -07:00
|
|
|
def matches?(str, scope)
|
|
|
|
keywords.any? { |kw| kw.matches?(str, scope) }
|
2017-10-14 18:36:53 -07:00
|
|
|
end
|
2017-11-15 15:26:29 -08:00
|
|
|
end
|
2017-10-14 18:36:53 -07:00
|
|
|
|
2018-06-03 16:12:55 -07:00
|
|
|
class TagMatcher < Matcher
|
2017-11-15 15:26:29 -08:00
|
|
|
def self.cache_key(account_id)
|
2017-11-15 16:08:03 -08:00
|
|
|
format('keyword_mutes:regex:tag:%s', account_id)
|
2017-11-15 15:26:29 -08:00
|
|
|
end
|
2017-10-21 22:24:32 -07:00
|
|
|
|
2018-06-03 17:41:54 -07:00
|
|
|
def matches?(tags, scope)
|
2018-06-03 16:12:55 -07:00
|
|
|
tags.pluck(:name).any? do |n|
|
2018-06-03 17:41:54 -07:00
|
|
|
keywords.any? { |kw| kw.matches?(n, scope) }
|
2018-06-03 16:12:55 -07:00
|
|
|
end
|
2017-11-15 15:26:29 -08:00
|
|
|
end
|
|
|
|
|
2018-06-03 16:12:55 -07:00
|
|
|
protected
|
2017-11-15 15:26:29 -08:00
|
|
|
|
2018-06-03 16:12:55 -07:00
|
|
|
def transform_keyword(kw)
|
|
|
|
Tag::HASHTAG_RE =~ kw ? $1 : kw
|
2017-10-21 13:44:47 -07:00
|
|
|
end
|
2017-10-14 00:28:20 -07:00
|
|
|
end
|
2017-10-09 15:28:28 -07:00
|
|
|
end
|