about summary refs log tree commit diff
path: root/app/services/update_remote_profile_service.rb
blob: aca1185de2c9de1885308780014504ef9e985b5d (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

class UpdateRemoteProfileService < BaseService
  attr_reader :account, :remote_profile

  def call(body, account, resubscribe = false)
    @account        = account
    @remote_profile = RemoteProfile.new(body)

    return if remote_profile.root.nil?

    update_account unless remote_profile.author.nil?

    old_hub_url     = account.hub_url
    account.hub_url = remote_profile.hub_link if remote_profile.hub_link.present? && remote_profile.hub_link != old_hub_url

    account.save_with_optional_media!

    Pubsubhubbub::SubscribeWorker.perform_async(account.id) if resubscribe && account.hub_url != old_hub_url
  end

  private

  def update_account
    account.display_name = remote_profile.display_name || ''
    account.note         = remote_profile.note         || ''
    account.locked       = remote_profile.locked?

    if !account.suspended? && !DomainBlock.find_by(domain: account.domain)&.reject_media?
      if remote_profile.avatar.present?
        account.avatar_remote_url = remote_profile.avatar
      else
        account.avatar_remote_url = ''
        account.avatar.destroy
      end

      if remote_profile.header.present?
        account.header_remote_url = remote_profile.header
      else
        account.header_remote_url = ''
        account.header.destroy
      end

      save_emojis(account) if remote_profile.emojis.present?
    end
  end

  def save_emojis(parent)
    do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media?

    return if do_not_download

    remote_account.emojis.each do |link|
      next unless link['href'] && link['name']

      shortcode = link['name'].delete(':')
      emoji     = CustomEmoji.find_by(shortcode: shortcode, domain: parent.account.domain)

      next unless emoji.nil?

      emoji = CustomEmoji.new(shortcode: shortcode, domain: parent.account.domain)
      emoji.image_remote_url = link['href']
      emoji.save
    end
  end
end