about summary refs log tree commit diff
path: root/app/models/remote_profile.rb
diff options
context:
space:
mode:
authorYamagishi Kazutoshi <ykzts@desire.sh>2017-06-11 17:41:59 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-06-11 10:41:59 +0200
commitf3e8bc9f8f6007b7bddc230be9c2ec865b5cb75e (patch)
tree37709dae6053cfd27dee0810d0c2d92aaa5e46dd /app/models/remote_profile.rb
parentdcf0530218c60ff079ca38d7d3707ac80bde7f97 (diff)
Refactor UpdateRemoteProfileService (#3690)
Diffstat (limited to 'app/models/remote_profile.rb')
-rw-r--r--app/models/remote_profile.rb53
1 files changed, 53 insertions, 0 deletions
diff --git a/app/models/remote_profile.rb b/app/models/remote_profile.rb
new file mode 100644
index 000000000..93c759930
--- /dev/null
+++ b/app/models/remote_profile.rb
@@ -0,0 +1,53 @@
+# 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: TagManager::XMLNS)
+  end
+
+  def author
+    @author ||= root.at_xpath('./atom:author|./dfrn:owner', atom: TagManager::XMLNS, dfrn: 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: TagManager::POCO_XMLNS)&.content
+  end
+
+  def note
+    @note ||= author.at_xpath('./atom:summary|./poco:note', atom: TagManager::XMLNS, poco: TagManager::POCO_XMLNS)&.content
+  end
+
+  def scope
+    @scope ||= author.at_xpath('./mastodon:scope', mastodon: 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 locked?
+    scope == 'private'
+  end
+
+  private
+
+  def link_href_from_xml(xml, type)
+    xml.at_xpath(%(./atom:link[@rel="#{type}"]/@href), atom: TagManager::XMLNS)&.content
+  end
+end