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)
28 {:xmlObj, :string, verb} = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry)
31 'http://activitystrea.ms/schema/1.0/share' ->
32 with {:ok, activity, retweeted_activity} <- handle_share(entry, doc), do: [activity, retweeted_activity]
35 'http://activitystrea.ms/schema/1.0/note' ->
36 with {:ok, activity} <- handle_note(entry, doc), do: activity
37 'http://activitystrea.ms/schema/1.0/comment' ->
38 with {:ok, activity} <- handle_note(entry, doc), do: activity
40 Logger.error("Couldn't parse incoming document")
48 def make_share(_entry, doc, retweeted_activity) do
49 with {:ok, actor} <- find_make_or_update_user(doc),
50 %Object{} = object <- Object.get_cached_by_ap_id(retweeted_activity.data["object"]["id"]),
51 {:ok, activity, _object} = ActivityPub.announce(actor, object, false) do
56 def handle_share(entry, doc) do
57 with [object] <- :xmerl_xpath.string('/entry/activity:object', entry),
58 {:ok, retweeted_activity} <- handle_note(object, object),
59 {:ok, activity} <- make_share(entry, doc, retweeted_activity) do
60 {:ok, activity, retweeted_activity}
66 def get_attachments(entry) do
67 :xmerl_xpath.string('/entry/link[@rel="enclosure"]', entry)
68 |> Enum.map(fn (enclosure) ->
69 with href when not is_nil(href) <- string_from_xpath("/link/@href", enclosure),
70 type when not is_nil(type) <- string_from_xpath("/link/@type", enclosure) do
72 "type" => "Attachment",
84 def handle_note(entry, doc \\ nil) do
85 content_html = string_from_xpath("//content[1]", entry)
87 [author] = :xmerl_xpath.string('//author[1]', doc)
88 {:ok, actor} = find_make_or_update_user(author)
89 inReplyTo = string_from_xpath("//thr:in-reply-to[1]/@ref", entry)
91 context = (string_from_xpath("//ostatus:conversation[1]", entry) || "") |> String.trim
93 attachments = get_attachments(entry)
95 context = with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
98 if String.length(context) > 0 do
101 ActivityPub.generate_context_id
106 "https://www.w3.org/ns/activitystreams#Public"
109 mentions = :xmerl_xpath.string('//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', entry)
110 |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
114 date = string_from_xpath("//published", entry)
115 id = string_from_xpath("//id", entry)
121 "content" => content_html,
123 "context" => context,
124 "actor" => actor.ap_id,
125 "attachment" => attachments
128 object = if inReplyTo do
129 Map.put(object, "inReplyTo", inReplyTo)
134 # TODO: Bail out sooner and use transaction.
135 if Object.get_by_ap_id(id) do
136 {:error, "duplicate activity"}
138 ActivityPub.create(to, actor, context, object, %{}, date, false)
142 def find_make_or_update_user(doc) do
143 uri = string_from_xpath("//author/uri[1]", doc)
144 with {:ok, user} <- find_or_make_user(uri) do
145 avatar = make_avatar_object(doc)
146 if user.avatar != avatar do
147 change = Ecto.Changeset.change(user, %{avatar: avatar})
155 def find_or_make_user(uri) do
156 query = from user in User,
157 where: user.local == false and fragment("? @> ?", user.info, ^%{uri: uri})
159 user = Repo.one(query)
168 def make_user(uri) do
169 with {:ok, info} <- gather_user_info(uri) do
173 nickname: info["nickname"] <> "@" <> info["host"],
176 avatar: info["avatar"]
178 # TODO: Make remote user changeset
179 # SHould enforce fqn nickname
180 Repo.insert(Ecto.Changeset.change(%User{}, data))
184 # TODO: Just takes the first one for now.
185 def make_avatar_object(author_doc) do
186 href = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@href", author_doc)
187 type = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@type", author_doc)
204 def gather_user_info(username) do
205 with {:ok, webfinger_data} <- WebFinger.finger(username),
206 {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data["topic"]) do
207 {:ok, Map.merge(webfinger_data, feed_data) |> Map.put("fqn", username)}
209 Logger.debug(fn -> "Couldn't gather info for #{username}" end)