about summary refs log tree commit diff
path: root/app/models/custom_filter.rb
blob: bd098c742bed238418053620d6a120bba5b93169 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# frozen_string_literal: true
# == Schema Information
#
# Table name: custom_filters
#
#  id         :bigint(8)        not null, primary key
#  account_id :bigint(8)
#  expires_at :datetime
#  phrase     :text             default(""), not null
#  created_at :datetime         not null
#  updated_at :datetime         not null
#  is_enabled :boolean          default(TRUE), not null
#

class CustomFilter < ApplicationRecord
  include Expireable
  include Redisable

  scope :enabled, -> { where(is_enabled: true) }

  belongs_to :account

  validates :phrase, presence: true

  after_save :remove_cache
  after_save :update_feeds

  after_destroy :remove_cache
  after_destroy :update_feeds

  private

  def update_feeds
    FilterFeedsWorker.perform_async(account_id)
  end

  def remove_cache
    redis.del("filtered_statuses:#{account_id}")
  end
end