about summary refs log tree commit diff
path: root/app/models/trends.rb
blob: 0be900b048b4761773a4b03a302c028a7b55425a (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
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true

module Trends
  def self.table_name_prefix
    'trends_'
  end

  def self.links
    @links ||= Trends::Links.new
  end

  def self.tags
    @tags ||= Trends::Tags.new
  end

  def self.statuses
    @statuses ||= Trends::Statuses.new
  end

  def self.register!(status)
    [links, tags, statuses].each { |trend_type| trend_type.register(status) }
  end

  def self.refresh!
    [links, tags, statuses].each(&:refresh)
  end

  def self.request_review!
    return unless enabled?

    links_requiring_review    = links.request_review
    tags_requiring_review     = tags.request_review
    statuses_requiring_review = statuses.request_review

    User.staff.includes(:account).find_each do |user|
      links    = user.allows_trending_tags_review_emails? ? links_requiring_review : []
      tags     = user.allows_trending_links_review_emails? ? tags_requiring_review : []
      statuses = user.allows_trending_statuses_review_emails? ? statuses_requiring_review : []
      next if links.empty? && tags.empty? && statuses.empty?

      AdminMailer.new_trends(user.account, links, tags, statuses).deliver_later!
    end
  end

  def self.enabled?
    Setting.trends
  end

  def self.available_locales
    @available_locales ||= I18n.available_locales.map { |locale| locale.to_s.split(/[_-]/).first }.uniq
  end
end