1 defmodule Pleroma.Web.OStatus do
6 alias Pleroma.{Repo, User, Web, Object}
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 with {:ok, activity} <- handle_note(entry, doc), do: activity
32 'http://activitystrea.ms/schema/1.0/comment' ->
33 with {:ok, activity} <- handle_note(entry, doc), do: activity
35 Logger.error("Couldn't parse incoming document")
42 def handle_note(entry, doc \\ nil) do
43 content_html = string_from_xpath("/entry/content[1]", entry)
45 uri = string_from_xpath("/entry/author/uri[1]", entry) || string_from_xpath("/feed/author/uri[1]", doc)
46 {:ok, actor} = find_or_make_user(uri)
48 context = (string_from_xpath("/entry/ostatus:conversation[1]", entry) || "") |> String.trim
49 context = if String.length(context) > 0 do
52 ActivityPub.generate_context_id
56 "https://www.w3.org/ns/activitystreams#Public"
59 mentions = :xmerl_xpath.string('/entry/link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', entry)
60 |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
64 date = string_from_xpath("/entry/published", entry)
65 id = string_from_xpath("/entry/id", entry)
71 "content" => content_html,
74 "actor" => actor.ap_id
77 inReplyTo = string_from_xpath("/entry/thr:in-reply-to[1]/@ref", entry)
79 object = if inReplyTo do
80 Map.put(object, "inReplyTo", inReplyTo)
85 # TODO: Bail out sooner and use transaction.
86 if Object.get_by_ap_id(id) do
87 {:error, "duplicate activity"}
89 ActivityPub.create(to, actor, context, object, %{}, date)
93 def find_or_make_user(uri) do
94 query = from user in User,
95 where: user.local == false and fragment("? @> ?", user.info, ^%{uri: uri})
97 user = Repo.one(query)
106 def make_user(uri) do
107 with {:ok, info} <- gather_user_info(uri) do
111 nickname: info.nickname <> "@" <> info.host,
116 # TODO: Make remote user changeset
117 # SHould enforce fqn nickname
118 Repo.insert(Ecto.Changeset.change(%User{}, data))
122 # TODO: Just takes the first one for now.
123 def make_avatar_object(author_doc) do
124 href = string_from_xpath("/feed/author[1]/link[@rel=\"avatar\"]/@href", author_doc)
125 type = string_from_xpath("/feed/author[1]/link[@rel=\"avatar\"]/@type", author_doc)
142 def gather_user_info(username) do
143 with {:ok, webfinger_data} <- WebFinger.finger(username),
144 {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data.topic) do
145 {:ok, Map.merge(webfinger_data, feed_data) |> Map.put(:fqn, username)}
147 Logger.debug("Couldn't gather info for #{username}")