1 defmodule Pleroma.Web.OStatus do
6 alias Pleroma.{Repo, User, Web}
7 alias Pleroma.Web.ActivityPub.ActivityPub
8 alias Pleroma.Web.{WebFinger, Websub}
10 def feed_path(user) do
11 "#{user.ap_id}/feed.atom"
14 def pubsub_path(user) do
15 "#{Web.base_url}/push/hub/#{user.nickname}"
18 def salmon_path(user) do
19 "#{user.ap_id}/salmon"
22 def handle_incoming(xml_string) do
23 doc = parse_document(xml_string)
25 {:xmlObj, :string, object_type } = :xmerl_xpath.string('string(/entry/activity:object-type[1])', doc)
28 'http://activitystrea.ms/schema/1.0/note' ->
31 Logger.error("Couldn't parse incoming document")
37 def handle_note(doc) do
38 content_html = string_from_xpath("/entry/content[1]", doc)
40 uri = string_from_xpath("/entry/author/uri[1]", doc)
41 {:ok, actor} = find_or_make_user(uri)
43 context = string_from_xpath("/entry/ostatus:conversation[1]", doc) |> String.trim
44 context = if String.length(context) > 0 do
47 ActivityPub.generate_context_id
51 "https://www.w3.org/ns/activitystreams#Public"
54 mentions = :xmerl_xpath.string('/entry/link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', doc)
55 |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
59 date = string_from_xpath("/entry/published", doc)
64 "content" => content_html,
67 "actor" => actor.ap_id
70 inReplyTo = string_from_xpath("/entry/thr:in-reply-to[1]/@href", doc)
72 object = if inReplyTo do
73 Map.put(object, "inReplyTo", inReplyTo)
78 ActivityPub.create(to, actor, context, object, %{}, date)
81 def find_or_make_user(uri) do
82 query = from user in User,
83 where: user.local == false and fragment("? @> ?", user.info, ^%{uri: uri})
85 user = Repo.one(query)
95 with {:ok, info} <- gather_user_info(uri) do
99 nickname: info.nickname <> "@" <> info.host,
103 # TODO: Make remote user changeset
104 # SHould enforce fqn nickname
105 Repo.insert(Ecto.Changeset.change(%User{}, data))
109 # TODO: Just takes the first one for now.
110 defp make_avatar_object(author_doc) do
111 href = string_from_xpath("/author[1]/link[@rel=\"avatar\"]/@href", author_doc)
112 type = string_from_xpath("/author[1]/link[@rel=\"avatar\"]/@type", author_doc)
129 def gather_user_info(username) do
130 with {:ok, webfinger_data} <- WebFinger.finger(username),
131 {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data.topic) do
132 {:ok, Map.merge(webfinger_data, feed_data) |> Map.put(:fqn, username)}
134 Logger.debug("Couldn't gather info for #{username}")