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