2016-11-15 07:56:29 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-05 07:20:05 -07:00
|
|
|
class ProcessHashtagsService < BaseService
|
|
|
|
def call(status, tags = [])
|
2019-05-06 18:51:20 -07:00
|
|
|
if status.local?
|
|
|
|
tags = Extractor.extract_hashtags(status.text) | (tags.nil? ? [] : tags)
|
|
|
|
end
|
2019-02-03 19:25:59 -08:00
|
|
|
records = []
|
2016-11-05 07:20:05 -07:00
|
|
|
|
2018-05-27 12:45:30 -07:00
|
|
|
tags.map { |str| str.mb_chars.downcase }.uniq(&:to_s).each do |name|
|
2019-05-20 23:55:03 -07:00
|
|
|
name = name.gsub(/\.\.+/, '.')
|
2019-05-06 18:51:20 -07:00
|
|
|
next if name.blank?
|
2019-05-20 23:55:03 -07:00
|
|
|
component_indices = name.size.times.select {|i| name[i] == '.'}
|
2019-05-05 21:04:23 -07:00
|
|
|
component_indices << name.size - 1
|
2019-05-05 22:57:25 -07:00
|
|
|
component_indices.take(6).each_with_index do |i, nest|
|
|
|
|
frag = (nest != 5) ? name[0..i] : name
|
2019-05-05 21:04:23 -07:00
|
|
|
tag = Tag.where(name: frag).first_or_create(name: frag)
|
2019-02-03 19:25:59 -08:00
|
|
|
|
2019-05-05 23:29:33 -07:00
|
|
|
next if status.tags.include?(tag)
|
2019-05-05 21:04:23 -07:00
|
|
|
status.tags << tag
|
|
|
|
next if tag.local || tag.private
|
|
|
|
|
|
|
|
records << tag
|
|
|
|
TrendingTags.record_use!(tag, status.account, status.created_at) if status.distributable?
|
|
|
|
end
|
2016-11-05 07:20:05 -07:00
|
|
|
end
|
2019-02-03 19:25:59 -08:00
|
|
|
|
2019-05-05 21:04:23 -07:00
|
|
|
return unless status.distributable?
|
2019-02-03 19:25:59 -08:00
|
|
|
|
|
|
|
status.account.featured_tags.where(tag_id: records.map(&:id)).each do |featured_tag|
|
|
|
|
featured_tag.increment(status.created_at)
|
|
|
|
end
|
2016-11-05 07:20:05 -07:00
|
|
|
end
|
|
|
|
end
|