defmodule Pleroma.Web.OStatus do
import Ecto.Query
+ import Pleroma.Web.XML
require Logger
alias Pleroma.{Repo, User, Web}
end
def handle_incoming(xml_string) do
- {doc, _rest} = :xmerl_scan.string(to_charlist(xml_string))
+ doc = parse_document(xml_string)
{:xmlObj, :string, object_type } = :xmerl_xpath.string('string(/entry/activity:object-type[1])', doc)
end
end
- defp string_from_xpath(xpath, doc) do
- {:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc)
-
- res = res
- |> to_string
- |> String.trim
-
- if res == "", do: nil, else: res
- end
-
def make_user(author_doc) do
author = string_from_xpath("/author[1]/uri", author_doc)
name = string_from_xpath("/author[1]/name", author_doc)
defmodule Pleroma.Web.Salmon do
use Bitwise
+ alias Pleroma.Web.XML
def decode(salmon) do
- {doc, _rest} = :xmerl_scan.string(to_charlist(salmon))
+ doc = XML.parse_document(salmon)
{:xmlObj, :string, data} = :xmerl_xpath.string('string(//me:data[1])', doc)
{:xmlObj, :string, sig} = :xmerl_xpath.string('string(//me:sig[1])', doc)
def fetch_magic_key(salmon) do
[data, _, _, _, _] = decode(salmon)
- {doc, _rest} = :xmerl_scan.string(to_charlist(data))
+ doc = XML.parse_document(data)
{:xmlObj, :string, uri} = :xmerl_xpath.string('string(//author[1]/uri)', doc)
uri = to_string(uri)
base = URI.parse(uri).host
# TODO: Find out if this endpoint is mandated by the standard.
+ # At least diaspora does it differently
{:ok, response} = HTTPoison.get(base <> "/.well-known/webfinger", ["Accept": "application/xrd+xml"], [params: [resource: uri]])
- {doc, _rest} = :xmerl_scan.string(to_charlist(response.body))
+ doc = XML.parse_document(response.body)
{:xmlObj, :string, magickey} = :xmerl_xpath.string('string(//Link[@rel="magic-public-key"]/@href)', doc)
"data:application/magic-public-key," <> magickey = to_string(magickey)
alias Pleroma.Web.Websub.{WebsubServerSubscription, WebsubClientSubscription}
alias Pleroma.Web.OStatus.FeedRepresenter
alias Pleroma.Web.OStatus
+ alias Pleroma.Web.XML
import Ecto.Query
--- /dev/null
+defmodule Pleroma.Web.XML do
+ def string_from_xpath(xpath, doc) do
+ {:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc)
+
+ res = res
+ |> to_string
+ |> String.trim
+
+ if res == "", do: nil, else: res
+ end
+
+ def parse_document(text) do
+ {doc, _rest} = text
+ |> :binary.bin_to_list
+ |> :xmerl_scan.string
+
+ doc
+ end
+end
defmodule Pleroma.Web.OStatusTest do
use Pleroma.DataCase
alias Pleroma.Web.OStatus
+ alias Pleroma.Web.XML
test "handle incoming notes" do
incoming = File.read!("test/fixtures/incoming_note_activity.xml")
describe "new remote user creation" do
test "make new user or find them based on an 'author' xml doc" do
incoming = File.read!("test/fixtures/user_name_only.xml")
- {doc, _rest} = :xmerl_scan.string(to_charlist(incoming))
+ doc = XML.parse_document(incoming)
{:ok, user} = OStatus.find_or_make_user(doc)
test "tries to use the information in poco fields" do
incoming = File.read!("test/fixtures/user_full.xml")
- {doc, _rest} = :xmerl_scan.string(to_charlist(incoming))
+ doc = XML.parse_document(incoming)
{:ok, user} = OStatus.find_or_make_user(doc)