remove_status_service.rb 3.54 KB
Newer Older
1 2
# frozen_string_literal: true

3
class RemoveStatusService < BaseService
4 5
  include StreamEntryRenderer

6
  def call(status)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
    @payload      = Oj.dump(event: :delete, payload: status.id)
    @status       = status
    @account      = status.account
    @tags         = status.tags.pluck(:name).to_a
    @mentions     = status.mentions.includes(:account).to_a
    @reblogs      = status.reblogs.to_a
    @stream_entry = status.stream_entry

    remove_from_self if status.account.local?
    remove_from_followers
    remove_reblogs
    remove_from_hashtags
    remove_from_public

    @status.destroy!

    return unless @account.local?

25 26 27 28
    @stream_entry = @stream_entry.reload

    remove_from_remote_followers
    remove_from_remote_affected
29 30 31 32
  end

  private

33 34
  def remove_from_self
    unpush(:home, @account, @status)
35 36
  end

37
  def remove_from_followers
38 39
    @account.followers.local.find_each do |follower|
      unpush(:home, follower, @status)
40 41 42
    end
  end

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  def remove_from_remote_affected
    # People who got mentioned in the status, or who
    # reblogged it from someone else might not follow
    # the author and wouldn't normally receive the
    # delete notification - so here, we explicitly
    # send it to them

    target_accounts = (@mentions.map(&:account).reject(&:local?) + @reblogs.map(&:account).reject(&:local?)).uniq(&:id)

    # Ostatus
    NotificationWorker.push_bulk(target_accounts.select(&:ostatus?).uniq(&:domain)) do |target_account|
      [salmon_xml, @account.id, target_account.id]
    end

    # ActivityPub
    ActivityPub::DeliveryWorker.push_bulk(target_accounts.select(&:activitypub?).uniq(&:inbox_url)) do |inbox_url|
59
      [signed_activity_json, @account.id, inbox_url]
60 61 62 63 64 65
    end
  end

  def remove_from_remote_followers
    # OStatus
    Pubsubhubbub::DistributionWorker.perform_async(@stream_entry.id)
66

67 68
    # ActivityPub
    ActivityPub::DeliveryWorker.push_bulk(@account.followers.inboxes) do |inbox_url|
69
      [signed_activity_json, @account.id, inbox_url]
70 71 72
    end
  end

73 74 75 76
  def salmon_xml
    @salmon_xml ||= stream_entry_to_xml(@stream_entry)
  end

77 78 79 80
  def signed_activity_json
    @signed_activity_json ||= Oj.dump(ActivityPub::LinkedDataSignature.new(activity_json).sign!(@account))
  end

81 82 83 84 85
  def activity_json
    @activity_json ||= ActiveModelSerializers::SerializableResource.new(
      @status,
      serializer: ActivityPub::DeleteSerializer,
      adapter: ActivityPub::Adapter
86
    ).as_json
87 88
  end

89 90 91 92
  def remove_reblogs
    # We delete reblogs of the status before the original status,
    # because once original status is gone, reblogs will disappear
    # without us being able to do all the fancy stuff
93

94
    @reblogs.each do |reblog|
Eugen Rochko's avatar
Eugen Rochko committed
95
      RemoveStatusService.new.call(reblog)
96 97 98 99
    end
  end

  def unpush(type, receiver, status)
100
    if status.reblog? && !redis.zscore(FeedManager.instance.key(type, receiver.id), status.reblog_of_id).nil?
101 102 103 104 105
      redis.zadd(FeedManager.instance.key(type, receiver.id), status.reblog_of_id, status.reblog_of_id)
    else
      redis.zremrangebyscore(FeedManager.instance.key(type, receiver.id), status.id, status.id)
    end

106
    Redis.current.publish("timeline:#{receiver.id}", @payload)
107 108
  end

109 110
  def remove_from_hashtags
    @tags.each do |hashtag|
111
      Redis.current.publish("timeline:hashtag:#{hashtag}", @payload)
112
      Redis.current.publish("timeline:hashtag:#{hashtag}:local", @payload) if @status.local?
113 114 115
    end
  end

116
  def remove_from_public
117
    Redis.current.publish('timeline:public', @payload)
118
    Redis.current.publish('timeline:public:local', @payload) if @status.local?
119 120
  end

121
  def redis
122
    Redis.current
123 124
  end
end