2016-11-16 00:56:29 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-05 23:20:05 +09:00
|
|
|
class ProcessHashtagsService < BaseService
|
2022-02-10 08:15:30 +09:00
|
|
|
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
|
2016-11-05 23:20:05 +09:00
|
|
|
end
|
2019-02-04 12:25:59 +09:00
|
|
|
|
2022-02-10 08:15:30 +09:00
|
|
|
removed_tags = @previous_tags - @current_tags
|
2019-02-04 12:25:59 +09:00
|
|
|
|
2022-02-10 08:15:30 +09:00
|
|
|
unless removed_tags.empty?
|
|
|
|
@account.featured_tags.where(tag_id: removed_tags.map(&:id)).each do |featured_tag|
|
|
|
|
featured_tag.decrement(@status.id)
|
|
|
|
end
|
2019-02-04 12:25:59 +09:00
|
|
|
end
|
2016-11-05 23:20:05 +09:00
|
|
|
end
|
|
|
|
end
|