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.{WebFinger, Websub}
11 alias Pleroma.Web.OStatus.{FollowHandler, UnfollowHandler, NoteHandler, DeleteHandler}
12 alias Pleroma.Web.ActivityPub.Transmogrifier
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 remote_follow_path do
27 "#{Web.base_url()}/ostatus_subscribe?acct={uri}"
30 def handle_incoming(xml_string) do
31 with doc when doc != :error <- parse_document(xml_string) do
32 entries = :xmerl_xpath.string('//entry', doc)
35 Enum.map(entries, fn entry ->
36 {:xmlObj, :string, object_type} =
37 :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
39 {:xmlObj, :string, verb} = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry)
40 Logger.debug("Handling #{verb}")
44 'http://activitystrea.ms/schema/1.0/delete' ->
45 with {:ok, activity} <- DeleteHandler.handle_delete(entry, doc), do: activity
47 'http://activitystrea.ms/schema/1.0/follow' ->
48 with {:ok, activity} <- FollowHandler.handle(entry, doc), do: activity
50 'http://activitystrea.ms/schema/1.0/unfollow' ->
51 with {:ok, activity} <- UnfollowHandler.handle(entry, doc), do: activity
53 'http://activitystrea.ms/schema/1.0/share' ->
54 with {:ok, activity, retweeted_activity} <- handle_share(entry, doc),
55 do: [activity, retweeted_activity]
57 'http://activitystrea.ms/schema/1.0/favorite' ->
58 with {:ok, activity, favorited_activity} <- handle_favorite(entry, doc),
59 do: [activity, favorited_activity]
63 'http://activitystrea.ms/schema/1.0/note' ->
64 with {:ok, activity} <- NoteHandler.handle_note(entry, doc), do: activity
66 'http://activitystrea.ms/schema/1.0/comment' ->
67 with {:ok, activity} <- NoteHandler.handle_note(entry, doc), do: activity
70 Logger.error("Couldn't parse incoming document")
76 Logger.error("Error occured while handling activity")
77 Logger.error(xml_string)
78 Logger.error(inspect(e))
90 def make_share(entry, doc, retweeted_activity) do
91 with {:ok, actor} <- find_make_or_update_user(doc),
92 %Object{} = object <- Object.get_by_ap_id(retweeted_activity.data["object"]["id"]),
93 id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
94 {:ok, activity, _object} = ActivityPub.announce(actor, object, id, false) do
99 def handle_share(entry, doc) do
100 with {:ok, retweeted_activity} <- get_or_build_object(entry),
101 {:ok, activity} <- make_share(entry, doc, retweeted_activity) do
102 {:ok, activity, retweeted_activity}
108 def make_favorite(entry, doc, favorited_activity) do
109 with {:ok, actor} <- find_make_or_update_user(doc),
110 %Object{} = object <- Object.get_by_ap_id(favorited_activity.data["object"]["id"]),
111 id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
112 {:ok, activity, _object} = ActivityPub.like(actor, object, id, false) do
117 def get_or_build_object(entry) do
118 with {:ok, activity} <- get_or_try_fetching(entry) do
122 with [object] <- :xmerl_xpath.string('/entry/activity:object', entry) do
123 NoteHandler.handle_note(object, object)
128 def get_or_try_fetching(entry) do
129 Logger.debug("Trying to get entry from db")
131 with id when not is_nil(id) <- string_from_xpath("//activity:object[1]/id", entry),
132 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
136 Logger.debug("Couldn't get, will try to fetch")
138 with href when not is_nil(href) <-
139 string_from_xpath("//activity:object[1]/link[@type=\"text/html\"]/@href", entry),
140 {:ok, [favorited_activity]} <- fetch_activity_from_url(href) do
141 {:ok, favorited_activity}
143 e -> Logger.debug("Couldn't find href: #{inspect(e)}")
148 def handle_favorite(entry, doc) do
149 with {:ok, favorited_activity} <- get_or_try_fetching(entry),
150 {:ok, activity} <- make_favorite(entry, doc, favorited_activity) do
151 {:ok, activity, favorited_activity}
157 def get_attachments(entry) do
158 :xmerl_xpath.string('/entry/link[@rel="enclosure"]', entry)
159 |> Enum.map(fn enclosure ->
160 with href when not is_nil(href) <- string_from_xpath("/link/@href", enclosure),
161 type when not is_nil(type) <- string_from_xpath("/link/@type", enclosure) do
163 "type" => "Attachment",
178 Gets the content from a an entry.
180 def get_content(entry) do
181 string_from_xpath("//content", entry)
185 Get the cw that mastodon uses.
188 with cw when not is_nil(cw) <- string_from_xpath("/*/summary", entry) do
195 def get_tags(entry) do
196 :xmerl_xpath.string('//category', entry)
197 |> Enum.map(fn category -> string_from_xpath("/category/@term", category) end)
199 |> Enum.map(&String.downcase/1)
202 def maybe_update(doc, user) do
203 if "true" == string_from_xpath("//author[1]/ap_enabled", doc) do
204 Transmogrifier.upgrade_user_from_ap_id(user.ap_id)
206 maybe_update_ostatus(doc, user)
210 def maybe_update_ostatus(doc, user) do
218 with false <- user.local,
219 avatar <- make_avatar_object(doc),
220 bio <- string_from_xpath("//author[1]/summary", doc),
221 name <- string_from_xpath("//author[1]/poco:displayName", doc),
223 Map.put(user.info, "banner", make_avatar_object(doc, "header") || user.info["banner"]),
225 avatar: avatar || old_data.avatar,
226 name: name || old_data.name,
227 bio: bio || old_data.bio,
228 info: info || old_data.info
230 false <- new_data == old_data do
231 change = Ecto.Changeset.change(user, new_data)
239 def find_make_or_update_user(doc) do
240 uri = string_from_xpath("//author/uri[1]", doc)
242 with {:ok, user} <- find_or_make_user(uri) do
243 maybe_update(doc, user)
247 def find_or_make_user(uri) do
248 query = from(user in User, where: user.ap_id == ^uri)
250 user = Repo.one(query)
259 def make_user(uri, update \\ false) do
260 with {:ok, info} <- gather_user_info(uri) do
263 nickname: info["nickname"] <> "@" <> info["host"],
266 avatar: info["avatar"],
270 with false <- update,
271 %User{} = user <- User.get_by_ap_id(data.ap_id) do
274 _e -> User.insert_or_update_user(data)
279 # TODO: Just takes the first one for now.
280 def make_avatar_object(author_doc, rel \\ "avatar") do
281 href = string_from_xpath("//author[1]/link[@rel=\"#{rel}\"]/@href", author_doc)
282 type = string_from_xpath("//author[1]/link[@rel=\"#{rel}\"]/@type", author_doc)
300 def gather_user_info(username) do
301 with {:ok, webfinger_data} <- WebFinger.finger(username),
302 {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data["topic"]) do
303 {:ok, Map.merge(webfinger_data, feed_data) |> Map.put("fqn", username)}
306 Logger.debug(fn -> "Couldn't gather info for #{username}" end)
311 # Regex-based 'parsing' so we don't have to pull in a full html parser
312 # It's a hack anyway. Maybe revisit this in the future
313 @mastodon_regex ~r/<link href='(.*)' rel='alternate' type='application\/atom\+xml'>/
314 @gs_regex ~r/<link title=.* href="(.*)" type="application\/atom\+xml" rel="alternate">/
315 @gs_classic_regex ~r/<link rel="alternate" href="(.*)" type="application\/atom\+xml" title=.*>/
316 def get_atom_url(body) do
318 Regex.match?(@mastodon_regex, body) ->
319 [[_, match]] = Regex.scan(@mastodon_regex, body)
322 Regex.match?(@gs_regex, body) ->
323 [[_, match]] = Regex.scan(@gs_regex, body)
326 Regex.match?(@gs_classic_regex, body) ->
327 [[_, match]] = Regex.scan(@gs_classic_regex, body)
331 Logger.debug(fn -> "Couldn't find Atom link in #{inspect(body)}" end)
332 {:error, "Couldn't find the Atom link"}
336 def fetch_activity_from_atom_url(url) do
337 with true <- String.starts_with?(url, "http"),
338 {:ok, %{body: body, status_code: code}} when code in 200..299 <-
341 [Accept: "application/atom+xml"],
342 follow_redirect: true,
346 Logger.debug("Got document from #{url}, handling...")
347 handle_incoming(body)
350 Logger.debug("Couldn't get #{url}: #{inspect(e)}")
355 def fetch_activity_from_html_url(url) do
356 Logger.debug("Trying to fetch #{url}")
358 with true <- String.starts_with?(url, "http"),
359 {:ok, %{body: body}} <-
360 @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000),
361 {:ok, atom_url} <- get_atom_url(body) do
362 fetch_activity_from_atom_url(atom_url)
365 Logger.debug("Couldn't get #{url}: #{inspect(e)}")
370 def fetch_activity_from_url(url) do
372 with {:ok, activities} when length(activities) > 0 <- fetch_activity_from_atom_url(url) do
376 with {:ok, activities} <- fetch_activity_from_html_url(url) do
382 Logger.debug("Couldn't get #{url}: #{inspect(e)}")
383 {:error, "Couldn't get #{url}: #{inspect(e)}"}