1 defmodule Pleroma.Web.OStatus do
2 @httpoison Application.get_env(:pleroma, :httpoison)
8 alias Pleroma.{Repo, User, Web, Object, Activity}
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.ActivityPub.Utils
11 alias Pleroma.Web.{WebFinger, Websub}
12 alias Pleroma.Web.OStatus.FollowHandler
14 def feed_path(user) do
15 "#{user.ap_id}/feed.atom"
18 def pubsub_path(user) do
19 "#{Web.base_url}/push/hub/#{user.nickname}"
22 def salmon_path(user) do
23 "#{user.ap_id}/salmon"
26 def handle_incoming(xml_string) do
27 doc = parse_document(xml_string)
28 entries = :xmerl_xpath.string('//entry', doc)
30 activities = Enum.map(entries, fn (entry) ->
31 {:xmlObj, :string, object_type} = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
32 {:xmlObj, :string, verb} = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry)
35 'http://activitystrea.ms/schema/1.0/follow' ->
36 with {:ok, activity} <- FollowHandler.handle(entry, doc), do: activity
37 'http://activitystrea.ms/schema/1.0/share' ->
38 with {:ok, activity, retweeted_activity} <- handle_share(entry, doc), do: [activity, retweeted_activity]
39 'http://activitystrea.ms/schema/1.0/favorite' ->
40 with {:ok, activity, favorited_activity} <- handle_favorite(entry, doc), do: [activity, favorited_activity]
43 'http://activitystrea.ms/schema/1.0/note' ->
44 with {:ok, activity} <- handle_note(entry, doc), do: activity
45 'http://activitystrea.ms/schema/1.0/comment' ->
46 with {:ok, activity} <- handle_note(entry, doc), do: activity
48 Logger.error("Couldn't parse incoming document")
56 def make_share(entry, doc, retweeted_activity) do
57 with {:ok, actor} <- find_make_or_update_user(doc),
58 %Object{} = object <- Object.get_by_ap_id(retweeted_activity.data["object"]["id"]),
59 id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
60 {:ok, activity, _object} = ActivityPub.announce(actor, object, id, false) do
65 def handle_share(entry, doc) do
66 with [object] <- :xmerl_xpath.string('/entry/activity:object', entry),
67 {:ok, retweeted_activity} <- handle_note(object, object),
68 {:ok, activity} <- make_share(entry, doc, retweeted_activity) do
69 {:ok, activity, retweeted_activity}
75 def make_favorite(entry, doc, favorited_activity) do
76 with {:ok, actor} <- find_make_or_update_user(doc),
77 %Object{} = object <- Object.get_by_ap_id(favorited_activity.data["object"]["id"]),
78 id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
79 {:ok, activity, _object} = ActivityPub.like(actor, object, id, false) do
84 def get_or_try_fetching(entry) do
85 Logger.debug("Trying to get entry from db")
86 with id when not is_nil(id) <- string_from_xpath("//activity:object[1]/id", entry),
87 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
90 Logger.debug("Couldn't get, will try to fetch")
91 with href when not is_nil(href) <- string_from_xpath("//activity:object[1]/link[@type=\"text/html\"]/@href", entry),
92 {:ok, [favorited_activity]} <- fetch_activity_from_html_url(href) do
93 {:ok, favorited_activity}
94 else e -> Logger.debug("Couldn't find href: #{inspect(e)}")
99 def handle_favorite(entry, doc) do
100 with {:ok, favorited_activity} <- get_or_try_fetching(entry),
101 {:ok, activity} <- make_favorite(entry, doc, favorited_activity) do
102 {:ok, activity, favorited_activity}
108 def get_attachments(entry) do
109 :xmerl_xpath.string('/entry/link[@rel="enclosure"]', entry)
110 |> Enum.map(fn (enclosure) ->
111 with href when not is_nil(href) <- string_from_xpath("/link/@href", enclosure),
112 type when not is_nil(type) <- string_from_xpath("/link/@type", enclosure) do
114 "type" => "Attachment",
123 |> Enum.filter(&(&1))
126 def get_content(entry) do
127 base_content = string_from_xpath("//content", entry)
129 with scope when not is_nil(scope) <- string_from_xpath("//mastodon:scope", entry),
130 cw when not is_nil(cw) <- string_from_xpath("/*/summary", entry) do
131 "<span class='mastodon-cw'>#{cw}</span><br>#{base_content}"
132 else _e -> base_content
136 def get_tags(entry) do
137 :xmerl_xpath.string('//category', entry)
138 |> Enum.map(fn (category) -> string_from_xpath("/category/@term", category) end)
141 def handle_note(entry, doc \\ nil) do
142 content_html = get_content(entry)
144 [author] = :xmerl_xpath.string('//author[1]', doc)
145 {:ok, actor} = find_make_or_update_user(author)
146 inReplyTo = string_from_xpath("//thr:in-reply-to[1]/@ref", entry)
148 if inReplyTo && !Object.get_cached_by_ap_id(inReplyTo) do
149 inReplyToHref = string_from_xpath("//thr:in-reply-to[1]/@href", entry)
151 fetch_activity_from_html_url(inReplyToHref)
153 Logger.debug("Couldn't find a href link to #{inReplyTo}")
157 context = (string_from_xpath("//ostatus:conversation[1]", entry) || "") |> String.trim
159 attachments = get_attachments(entry)
161 context = with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
164 if String.length(context) > 0 do
167 Utils.generate_context_id
171 tags = get_tags(entry)
174 "https://www.w3.org/ns/activitystreams#Public",
175 User.ap_followers(actor)
178 mentions = :xmerl_xpath.string('//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', entry)
179 |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
183 date = string_from_xpath("//published", entry)
184 id = string_from_xpath("//id", entry)
190 "content" => content_html,
192 "context" => context,
193 "actor" => actor.ap_id,
194 "attachment" => attachments,
198 object = if inReplyTo do
199 replied_to_object = Object.get_cached_by_ap_id(inReplyTo)
200 if replied_to_object do
202 |> Map.put("inReplyTo", inReplyTo)
203 |> Map.put("inReplyToStatusId", replied_to_object.id)
206 |> Map.put("inReplyTo", inReplyTo)
212 # TODO: Bail out sooner and use transaction.
213 if Object.get_by_ap_id(id) do
214 {:ok, Activity.get_create_activity_by_object_ap_id(id)}
216 ActivityPub.create(to, actor, context, object, %{}, date, false)
220 def find_make_or_update_user(doc) do
221 uri = string_from_xpath("//author/uri[1]", doc)
222 with {:ok, user} <- find_or_make_user(uri) do
223 avatar = make_avatar_object(doc)
224 if !user.local && user.avatar != avatar do
225 change = Ecto.Changeset.change(user, %{avatar: avatar})
233 def find_or_make_user(uri) do
234 query = from user in User,
235 where: user.ap_id == ^uri
237 user = Repo.one(query)
246 def make_user(uri) do
247 with {:ok, info} <- gather_user_info(uri) do
250 nickname: info["nickname"] <> "@" <> info["host"],
253 avatar: info["avatar"]
255 with %User{} = user <- User.get_by_ap_id(data.ap_id) do
258 cs = User.remote_user_creation(data)
264 # TODO: Just takes the first one for now.
265 def make_avatar_object(author_doc) do
266 href = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@href", author_doc)
267 type = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@type", author_doc)
284 def gather_user_info(username) do
285 with {:ok, webfinger_data} <- WebFinger.finger(username),
286 {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data["topic"]) do
287 {:ok, Map.merge(webfinger_data, feed_data) |> Map.put("fqn", username)}
289 Logger.debug(fn -> "Couldn't gather info for #{username}" end)
294 # Regex-based 'parsing' so we don't have to pull in a full html parser
295 # It's a hack anyway. Maybe revisit this in the future
296 @mastodon_regex ~r/<link href='(.*)' rel='alternate' type='application\/atom\+xml'>/
297 @gs_regex ~r/<link title=.* href="(.*)" type="application\/atom\+xml" rel="alternate">/
298 @gs_classic_regex ~r/<link rel="alternate" href="(.*)" type="application\/atom\+xml" title=.*>/
299 def get_atom_url(body) do
301 Regex.match?(@mastodon_regex, body) ->
302 [[_, match]] = Regex.scan(@mastodon_regex, body)
304 Regex.match?(@gs_regex, body) ->
305 [[_, match]] = Regex.scan(@gs_regex, body)
307 Regex.match?(@gs_classic_regex, body) ->
308 [[_, match]] = Regex.scan(@gs_classic_regex, body)
311 Logger.debug(fn -> "Couldn't find atom link in #{inspect(body)}" end)
312 {:error, "Couldn't find the atom link"}
316 def fetch_activity_from_html_url(url) do
317 Logger.debug("Trying to fetch #{url}")
318 with {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true),
319 {:ok, atom_url} <- get_atom_url(body),
320 {:ok, %{status_code: code, body: body}} when code in 200..299 <- @httpoison.get(atom_url, [], follow_redirect: true) do
321 Logger.debug("Got document from #{url}, handling...")
322 handle_incoming(body)
323 else e -> Logger.debug("Couldn't get #{url}: #{inspect(e)}")