1 defmodule Pleroma.Web.OStatus do
5 alias Pleroma.{Repo, User, Web}
6 alias Pleroma.Web.ActivityPub.ActivityPub
9 "#{user.ap_id}/feed.atom"
12 def pubsub_path(user) do
13 "#{Web.base_url}/push/hub/#{user.nickname}"
16 def salmon_path(user) do
17 "#{user.ap_id}/salmon"
20 def handle_incoming(xml_string) do
21 {doc, _rest} = :xmerl_scan.string(to_charlist(xml_string))
23 {:xmlObj, :string, object_type } = :xmerl_xpath.string('string(/entry/activity:object-type[1])', doc)
26 'http://activitystrea.ms/schema/1.0/note' ->
29 Logger.error("Couldn't parse incoming document")
35 def handle_note(doc) do
36 content_html = string_from_xpath("/entry/content[1]", doc)
38 [author] = :xmerl_xpath.string('/entry/author[1]', doc)
39 {:ok, actor} = find_or_make_user(author)
41 context = string_from_xpath("/entry/ostatus:conversation[1]", doc) |> String.trim
42 context = if String.length(context) > 0 do
45 ActivityPub.generate_context_id
49 "https://www.w3.org/ns/activitystreams#Public"
52 mentions = :xmerl_xpath.string('/entry/link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', doc)
53 |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
57 date = string_from_xpath("/entry/published", doc)
62 "content" => content_html,
65 "actor" => actor.ap_id
68 ActivityPub.create(to, actor, context, object, %{}, date)
71 def find_or_make_user(author_doc) do
72 {:xmlObj, :string, uri } = :xmerl_xpath.string('string(/author[1]/uri)', author_doc)
74 query = from user in User,
75 where: user.local == false and fragment("? @> ?", user.info, ^%{ostatus_uri: to_string(uri)})
77 user = Repo.one(query)
86 defp string_from_xpath(xpath, doc) do
87 {:xmlObj, :string, res} = :xmerl_xpath.string('string(#{xpath})', doc)
93 if res == "", do: nil, else: res
96 def make_user(author_doc) do
97 author = string_from_xpath("/author[1]/uri", author_doc)
98 name = string_from_xpath("/author[1]/name", author_doc)
99 preferredUsername = string_from_xpath("/author[1]/poco:preferredUsername", author_doc)
100 displayName = string_from_xpath("/author[1]/poco:displayName", author_doc)
101 avatar = make_avatar_object(author_doc)
105 name: preferredUsername || name,
106 nickname: displayName || name,
109 "ostatus_uri" => author,
110 "host" => URI.parse(author).host,
111 "system" => "ostatus"
116 Repo.insert(Ecto.Changeset.change(%User{}, data))
119 # TODO: Just takes the first one for now.
120 defp make_avatar_object(author_doc) do
121 href = string_from_xpath("/author[1]/link[@rel=\"avatar\"]/@href", author_doc)
122 type = string_from_xpath("/author[1]/link[@rel=\"avatar\"]/@type", author_doc)