about summary refs log tree commit diff
path: root/app/services/publish_status_service.rb
blob: 351bcd7f36600010e6614928d2a2c9e1f1c01222 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true
class PublishStatusService < BaseService
  include Redisable

  def call(status)
    @status       = status
    republishing  = status.published?
    status.notify = false if republishing

    update_status!
    reset_status_caches
    distribute
    bump_potential_friendship! unless republishing
  end

  private

  def update_status!
    @status.update!(published: true, publish_at: nil, created_at: Time.now.utc - 1.second, expires_at: @status.expires_at.blank? ? nil : Time.now.utc + (@status.expires_at - @status.created_at))
    ProcessMentionsService.new.call(@status)
  end

  def reset_status_caches
    Rails.cache.delete_matched("statuses/#{@status.id}-*")
    Rails.cache.delete("statuses/#{@status.id}")
    Rails.cache.delete("statuses/*:#{@status.id}")
    Rails.cache.delete(@status)
    Rails.cache.delete_matched("format:#{@status.id}:*")
    redis.zremrangebyscore("spam_check:#{@status.account.id}", @status.id, @status.id)
  end

  def distribute
    LinkCrawlWorker.perform_in(rand(1..30).seconds, @status.id) unless @status.spoiler_text?
    DistributionWorker.perform_async(@status.id)
    ActivityPub::DistributionWorker.perform_async(@status.id) if @status.local? && !@status.local_only?
  end

  def bump_potential_friendship!
    return if !@status.reply? || @status.account.id == @status.in_reply_to_account_id

    ActivityTracker.increment('activity:interactions')
    return if @status.account.following?(@status.in_reply_to_account_id)

    PotentialFriendshipTracker.record(@status.account.id, @status.in_reply_to_account_id, :reply)
  end
end