logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

remote_profile.rb (1402B)


  1. # frozen_string_literal: true
  2. class RemoteProfile
  3. include ActiveModel::Model
  4. attr_reader :document
  5. def initialize(body)
  6. @document = Nokogiri::XML.parse(body, nil, 'utf-8')
  7. end
  8. def root
  9. @root ||= document.at_xpath('/atom:feed|/atom:entry', atom: OStatus::TagManager::XMLNS)
  10. end
  11. def author
  12. @author ||= root.at_xpath('./atom:author|./dfrn:owner', atom: OStatus::TagManager::XMLNS, dfrn: OStatus::TagManager::DFRN_XMLNS)
  13. end
  14. def hub_link
  15. @hub_link ||= link_href_from_xml(root, 'hub')
  16. end
  17. def display_name
  18. @display_name ||= author.at_xpath('./poco:displayName', poco: OStatus::TagManager::POCO_XMLNS)&.content
  19. end
  20. def note
  21. @note ||= author.at_xpath('./atom:summary|./poco:note', atom: OStatus::TagManager::XMLNS, poco: OStatus::TagManager::POCO_XMLNS)&.content
  22. end
  23. def scope
  24. @scope ||= author.at_xpath('./mastodon:scope', mastodon: OStatus::TagManager::MTDN_XMLNS)&.content
  25. end
  26. def avatar
  27. @avatar ||= link_href_from_xml(author, 'avatar')
  28. end
  29. def header
  30. @header ||= link_href_from_xml(author, 'header')
  31. end
  32. def emojis
  33. @emojis ||= author.xpath('./xmlns:link[@rel="emoji"]', xmlns: OStatus::TagManager::XMLNS)
  34. end
  35. def locked?
  36. scope == 'private'
  37. end
  38. private
  39. def link_href_from_xml(xml, type)
  40. xml.at_xpath(%(./atom:link[@rel="#{type}"]/@href), atom: OStatus::TagManager::XMLNS)&.content
  41. end
  42. end