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)
24 entries = :xmerl_xpath.string('//entry', doc)
26 activities = Enum.map(entries, fn (entry) ->
27 {:xmlObj, :string, object_type } = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
30 'http://activitystrea.ms/schema/1.0/note' ->
31 {:ok, activity} = handle_note(entry, doc)
33 'http://activitystrea.ms/schema/1.0/comment' ->
34 {:ok, activity} = handle_note(entry, doc)
37 Logger.error("Couldn't parse incoming document")
46 def handle_note(entry, doc \\ nil) do
47 content_html = string_from_xpath("/entry/content[1]", entry)
49 uri = string_from_xpath("/entry/author/uri[1]", entry) || string_from_xpath("/feed/author/uri[1]", doc)
50 {:ok, actor} = find_or_make_user(uri)
52 context = string_from_xpath("/entry/ostatus:conversation[1]", entry) |> String.trim
53 context = if String.length(context) > 0 do
56 ActivityPub.generate_context_id
60 "https://www.w3.org/ns/activitystreams#Public"
63 mentions = :xmerl_xpath.string('/entry/link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', entry)
64 |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
68 date = string_from_xpath("/entry/published", entry)
69 id = string_from_xpath("/entry/id", entry)
75 "content" => content_html,
78 "actor" => actor.ap_id
81 inReplyTo = string_from_xpath("/entry/thr:in-reply-to[1]/@ref", entry)
83 object = if inReplyTo do
84 Map.put(object, "inReplyTo", inReplyTo)
89 ActivityPub.create(to, actor, context, object, %{}, date)
92 def find_or_make_user(uri) do
93 query = from user in User,
94 where: user.local == false and fragment("? @> ?", user.info, ^%{uri: uri})
96 user = Repo.one(query)
105 def make_user(uri) do
106 with {:ok, info} <- gather_user_info(uri) do
110 nickname: info.nickname <> "@" <> info.host,
114 # TODO: Make remote user changeset
115 # SHould enforce fqn nickname
116 Repo.insert(Ecto.Changeset.change(%User{}, data))
120 # TODO: Just takes the first one for now.
121 defp make_avatar_object(author_doc) do
122 href = string_from_xpath("/author[1]/link[@rel=\"avatar\"]/@href", author_doc)
123 type = string_from_xpath("/author[1]/link[@rel=\"avatar\"]/@type", author_doc)
140 def gather_user_info(username) do
141 with {:ok, webfinger_data} <- WebFinger.finger(username),
142 {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data.topic) do
143 {:ok, Map.merge(webfinger_data, feed_data) |> Map.put(:fqn, username)}
145 Logger.debug("Couldn't gather info for #{username}")