about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2019-02-04 04:25:59 +0100
committerGitHub <noreply@github.com>2019-02-04 04:25:59 +0100
commit364f2ff9aa2b4bf601d68a12bce758aeb5530467 (patch)
tree6e47b26ef9148d3b88dd9748460d8cf51beac748 /app/models
parentd14c276e58f0f223b0e4889d342a948c961081b2 (diff)
Add featured hashtags to profiles (#9755)
* Add hashtag filter to profiles

GET /@:username/tagged/:hashtag
GET /api/v1/accounts/:id/statuses?tagged=:hashtag

* Display featured hashtags on public profile

* Use separate model for featured tags

* Update featured hashtag counters on-write

* Limit featured tags to 10
Diffstat (limited to 'app/models')
-rw-r--r--app/models/concerns/account_associations.rb1
-rw-r--r--app/models/featured_tag.rb46
-rw-r--r--app/models/tag.rb2
3 files changed, 49 insertions, 0 deletions
diff --git a/app/models/concerns/account_associations.rb b/app/models/concerns/account_associations.rb
index 7dafeee34..397ec4a22 100644
--- a/app/models/concerns/account_associations.rb
+++ b/app/models/concerns/account_associations.rb
@@ -55,5 +55,6 @@ module AccountAssociations
 
     # Hashtags
     has_and_belongs_to_many :tags
+    has_many :featured_tags, -> { includes(:tag) }, dependent: :destroy, inverse_of: :account
   end
 end
diff --git a/app/models/featured_tag.rb b/app/models/featured_tag.rb
new file mode 100644
index 000000000..b5a10ad2d
--- /dev/null
+++ b/app/models/featured_tag.rb
@@ -0,0 +1,46 @@
+# frozen_string_literal: true
+# == Schema Information
+#
+# Table name: featured_tags
+#
+#  id             :bigint(8)        not null, primary key
+#  account_id     :bigint(8)
+#  tag_id         :bigint(8)
+#  statuses_count :bigint(8)        default(0), not null
+#  last_status_at :datetime
+#  created_at     :datetime         not null
+#  updated_at     :datetime         not null
+#
+
+class FeaturedTag < ApplicationRecord
+  belongs_to :account, inverse_of: :featured_tags, required: true
+  belongs_to :tag, inverse_of: :featured_tags, required: true
+
+  delegate :name, to: :tag, allow_nil: true
+
+  validates :name, presence: true
+  validate :validate_featured_tags_limit, on: :create
+
+  def name=(str)
+    self.tag = Tag.find_or_initialize_by(name: str.delete('#').mb_chars.downcase.to_s)
+  end
+
+  def increment(timestamp)
+    update(statuses_count: statuses_count + 1, last_status_at: timestamp)
+  end
+
+  def decrement(deleted_status_id)
+    update(statuses_count: [0, statuses_count - 1].max, last_status_at: account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag).where.not(id: deleted_status_id).select(:created_at).first&.created_at)
+  end
+
+  def reset_data
+    self.statuses_count = account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag).count
+    self.last_status_at = account.statuses.where(visibility: %i(public unlisted)).tagged_with(tag).select(:created_at).first&.created_at
+  end
+
+  private
+
+  def validate_featured_tags_limit
+    errors.add(:base, I18n.t('featured_tags.errors.limit')) if account.featured_tags.count >= 10
+  end
+end
diff --git a/app/models/tag.rb b/app/models/tag.rb
index 99830ae92..4373e967b 100644
--- a/app/models/tag.rb
+++ b/app/models/tag.rb
@@ -14,6 +14,7 @@ class Tag < ApplicationRecord
   has_and_belongs_to_many :accounts
   has_and_belongs_to_many :sample_accounts, -> { searchable.discoverable.popular.limit(3) }, class_name: 'Account'
 
+  has_many :featured_tags, dependent: :destroy, inverse_of: :tag
   has_one :account_tag_stat, dependent: :destroy
 
   HASHTAG_NAME_RE = '[[:word:]_]*[[:alpha:]_·][[:word:]_]*'
@@ -23,6 +24,7 @@ class Tag < ApplicationRecord
 
   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')) }
   scope :hidden, -> { where(account_tag_stats: { hidden: true }) }
+  scope :most_used, ->(account) { joins(:statuses).where(statuses: { account: account }).group(:id).order(Arel.sql('count(*) desc')) }
 
   delegate :accounts_count,
            :accounts_count=,