about summary refs log tree commit diff
path: root/app/services/process_mentions_service.rb
blob: b5134bf9c52d3006da4d5d19e7ea767eccd39730 (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 ProcessMentionsService < BaseService
  include Payloadable

  # Scan status for mentions and fetch remote mentioned users, create
  # local mention pointers, send Salmon notifications to mentioned
  # remote users
  # @param [Status] status
  # @option [Enumerable] :mentions Mentions to include
  # @option [Boolean] :deliver Deliver mention notifications
  def call(status, mentions: [], deliver: true)
    return unless status.local? && !(status.frozen? || status.destroyed?)

    @status = status
    @status.text, mentions = ResolveMentionsService.new.call(@status, mentions: mentions)
    @status.save!

    return unless deliver

    check_for_spam(status)

    @activitypub_json = {}
    mentions.each { |mention| create_notification(mention) }
  end

  private

  def create_notification(mention)
    mentioned_account = mention.account

    if mentioned_account.local?
      LocalNotificationWorker.perform_async(mentioned_account.id, mention.id, mention.class.name) unless !@status.notify? || mention.silent?
    elsif mentioned_account.activitypub? && !@status.local_only?
      ActivityPub::DeliveryWorker.perform_async(activitypub_json(mentioned_account.domain), mention.status.account_id, mentioned_account.inbox_url)
    end
  end

  def activitypub_json(domain)
    @activitypub_json[domain] ||= Oj.dump(serialize_payload(ActivityPub::ActivityPresenter.from_status(@status, embed: false), ActivityPub::ActivitySerializer, signer: @status.account, target_domain: domain))
  end

  def check_for_spam(status)
    SpamCheck.perform(status)
  end
end