about summary refs log tree commit diff
path: root/app/models/remote_profile.rb
blob: 742d2b56f4c61e6864d3e34e34d1b1ac3cb23575 (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
# frozen_string_literal: true

class RemoteProfile
  include ActiveModel::Model

  attr_reader :document

  def initialize(body)
    @document = Nokogiri::XML.parse(body, nil, 'utf-8')
  end

  def root
    @root ||= document.at_xpath('/atom:feed|/atom:entry', atom: OStatus::TagManager::XMLNS)
  end

  def author
    @author ||= root.at_xpath('./atom:author|./dfrn:owner', atom: OStatus::TagManager::XMLNS, dfrn: OStatus::TagManager::DFRN_XMLNS)
  end

  def hub_link
    @hub_link ||= link_href_from_xml(root, 'hub')
  end

  def display_name
    @display_name ||= author.at_xpath('./poco:displayName', poco: OStatus::TagManager::POCO_XMLNS)&.content
  end

  def note
    @note ||= author.at_xpath('./atom:summary|./poco:note', atom: OStatus::TagManager::XMLNS, poco: OStatus::TagManager::POCO_XMLNS)&.content
  end

  def scope
    @scope ||= author.at_xpath('./mastodon:scope', mastodon: OStatus::TagManager::MTDN_XMLNS)&.content
  end

  def avatar
    @avatar ||= link_href_from_xml(author, 'avatar')
  end

  def header
    @header ||= link_href_from_xml(author, 'header')
  end

  def emojis
    @emojis ||= author.xpath('./xmlns:link[@rel="emoji"]', xmlns: OStatus::TagManager::XMLNS)
  end

  def locked?
    scope == 'private'
  end

  private

  def link_href_from_xml(xml, type)
    xml.at_xpath(%(./atom:link[@rel="#{type}"]/@href), atom: OStatus::TagManager::XMLNS)&.content
  end
end