process_hashtags_service.rb 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # frozen_string_literal: true
  2. class ProcessHashtagsService < BaseService
  3. def call(status, raw_tags = [])
  4. @status = status
  5. @account = status.account
  6. @raw_tags = status.local? ? Extractor.extract_hashtags(status.text) : raw_tags
  7. @previous_tags = status.tags.to_a
  8. @current_tags = []
  9. assign_tags!
  10. update_featured_tags!
  11. end
  12. private
  13. def assign_tags!
  14. @status.tags = @current_tags = Tag.find_or_create_by_names(@raw_tags)
  15. end
  16. def update_featured_tags!
  17. return unless @status.distributable?
  18. added_tags = @current_tags - @previous_tags
  19. unless added_tags.empty?
  20. @account.featured_tags.where(tag_id: added_tags.map(&:id)).each do |featured_tag|
  21. featured_tag.increment(@status.created_at)
  22. end
  23. end
  24. removed_tags = @previous_tags - @current_tags
  25. unless removed_tags.empty?
  26. @account.featured_tags.where(tag_id: removed_tags.map(&:id)).each do |featured_tag|
  27. featured_tag.decrement(@status.id)
  28. end
  29. end
  30. end
  31. end