The code powering m.abunchtell.com https://m.abunchtell.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

118 lines
3.2 KiB

  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: tags
  5. #
  6. # id :bigint(8) not null, primary key
  7. # name :string default(""), not null
  8. # created_at :datetime not null
  9. # updated_at :datetime not null
  10. #
  11. class Tag < ApplicationRecord
  12. has_and_belongs_to_many :statuses
  13. has_and_belongs_to_many :accounts
  14. has_and_belongs_to_many :sample_accounts, -> { searchable.discoverable.popular.limit(3) }, class_name: 'Account'
  15. has_many :featured_tags, dependent: :destroy, inverse_of: :tag
  16. has_one :account_tag_stat, dependent: :destroy
  17. HASHTAG_NAME_RE = '([[:word:]_][[:word:]_·]*[[:alpha:]_·][[:word:]_·]*[[:word:]_])|([[:word:]_]*[[:alpha:]][[:word:]_]*)'
  18. HASHTAG_RE = /(?:^|[^\/\)\w])#(#{HASHTAG_NAME_RE})/i
  19. validates :name, presence: true, format: { with: /\A(#{HASHTAG_NAME_RE})\z/i }
  20. scope :discoverable, -> { joins(:account_tag_stat).where(AccountTagStat.arel_table[:accounts_count].gt(0)).where(account_tag_stats: { hidden: false }).order(Arel.sql('account_tag_stats.accounts_count desc')) }
  21. scope :hidden, -> { where(account_tag_stats: { hidden: true }) }
  22. scope :most_used, ->(account) { joins(:statuses).where(statuses: { account: account }).group(:id).order(Arel.sql('count(*) desc')) }
  23. delegate :accounts_count,
  24. :accounts_count=,
  25. :increment_count!,
  26. :decrement_count!,
  27. :hidden?,
  28. to: :account_tag_stat
  29. after_save :save_account_tag_stat
  30. def account_tag_stat
  31. super || build_account_tag_stat
  32. end
  33. def cached_sample_accounts
  34. Rails.cache.fetch("#{cache_key}/sample_accounts", expires_in: 12.hours) { sample_accounts }
  35. end
  36. def to_param
  37. name
  38. end
  39. def history
  40. days = []
  41. 7.times do |i|
  42. day = i.days.ago.beginning_of_day.to_i
  43. days << {
  44. day: day.to_s,
  45. uses: Redis.current.get("activity:tags:#{id}:#{day}") || '0',
  46. accounts: Redis.current.pfcount("activity:tags:#{id}:#{day}:accounts").to_s,
  47. }
  48. end
  49. days
  50. end
  51. class << self
  52. def find_or_create_by_names(name_or_names)
  53. Array(name_or_names).map(&method(:normalize)).uniq { |str| str.mb_chars.downcase.to_s }.map do |normalized_name|
  54. tag = matching_name(normalized_name).first || create(name: normalized_name)
  55. yield tag if block_given?
  56. tag
  57. end
  58. end
  59. def search_for(term, limit = 5, offset = 0)
  60. pattern = sanitize_sql_like(normalize(term.strip)) + '%'
  61. Tag.where(arel_table[:name].lower.matches(pattern.mb_chars.downcase.to_s))
  62. .order(:name)
  63. .limit(limit)
  64. .offset(offset)
  65. end
  66. def find_normalized(name)
  67. matching_name(name).first
  68. end
  69. def find_normalized!(name)
  70. find_normalized(name) || raise(ActiveRecord::RecordNotFound)
  71. end
  72. def matching_name(name_or_names)
  73. names = Array(name_or_names).map { |name| normalize(name).mb_chars.downcase.to_s }
  74. if names.size == 1
  75. where(arel_table[:name].lower.eq(names.first))
  76. else
  77. where(arel_table[:name].lower.in(names))
  78. end
  79. end
  80. private
  81. def normalize(str)
  82. str.gsub(/\A#/, '')
  83. end
  84. end
  85. private
  86. def save_account_tag_stat
  87. return unless account_tag_stat&.changed?
  88. account_tag_stat.save
  89. end
  90. end