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