about summary refs log tree commit diff
path: root/app/services/process_mentions_service.rb
blob: 9d219f3f63fccf21f2b04f3e2778d32c5b18985d (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
47
# 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] :reveal_implicit_mentions Append implicit mentions to text
  # @option [Boolean] :deliver Deliver mention notifications
  def call(status, mentions: [], reveal_implicit_mentions: true, deliver: true)
    return unless status.local?

    @status = status
    @status.text, mentions = ResolveMentionsService.new.call(@status, mentions: mentions, reveal_implicit_mentions: reveal_implicit_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)
    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