about summary refs log tree commit diff
path: root/app/services/process_hashtags_service.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/process_hashtags_service.rb')
-rw-r--r--app/services/process_hashtags_service.rb42
1 files changed, 31 insertions, 11 deletions
diff --git a/app/services/process_hashtags_service.rb b/app/services/process_hashtags_service.rb
index 47277c56c..43d7bcca6 100644
--- a/app/services/process_hashtags_service.rb
+++ b/app/services/process_hashtags_service.rb
@@ -1,20 +1,40 @@
 # frozen_string_literal: true
 
 class ProcessHashtagsService < BaseService
-  def call(status, tags = [])
-    tags    = Extractor.extract_hashtags(status.text) if status.local?
-    records = []
-
-    Tag.find_or_create_by_names(tags) do |tag|
-      status.tags << tag
-      records << tag
-      tag.update(last_status_at: status.created_at) if tag.last_status_at.nil? || (tag.last_status_at < status.created_at && tag.last_status_at < 12.hours.ago)
+  def call(status, raw_tags = [])
+    @status        = status
+    @account       = status.account
+    @raw_tags      = status.local? ? Extractor.extract_hashtags(status.text) : raw_tags
+    @previous_tags = status.tags.to_a
+    @current_tags  = []
+
+    assign_tags!
+    update_featured_tags!
+  end
+
+  private
+
+  def assign_tags!
+    @status.tags = @current_tags = Tag.find_or_create_by_names(@raw_tags)
+  end
+
+  def update_featured_tags!
+    return unless @status.distributable?
+
+    added_tags = @current_tags - @previous_tags
+
+    unless added_tags.empty?
+      @account.featured_tags.where(tag_id: added_tags.map(&:id)).each do |featured_tag|
+        featured_tag.increment(@status.created_at)
+      end
     end
 
-    return unless status.distributable?
+    removed_tags = @previous_tags - @current_tags
 
-    status.account.featured_tags.where(tag_id: records.map(&:id)).each do |featured_tag|
-      featured_tag.increment(status.created_at)
+    unless removed_tags.empty?
+      @account.featured_tags.where(tag_id: removed_tags.map(&:id)).each do |featured_tag|
+        featured_tag.decrement(@status.id)
+      end
     end
   end
 end