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, 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/share' ->
51 with {:ok, activity, retweeted_activity} <- handle_share(entry, doc),
52 do: [activity, retweeted_activity]
54 'http://activitystrea.ms/schema/1.0/favorite' ->
55 with {:ok, activity, favorited_activity} <- handle_favorite(entry, doc),
56 do: [activity, favorited_activity]
60 'http://activitystrea.ms/schema/1.0/note' ->
61 with {:ok, activity} <- NoteHandler.handle_note(entry, doc), do: activity
63 'http://activitystrea.ms/schema/1.0/comment' ->
64 with {:ok, activity} <- NoteHandler.handle_note(entry, doc), do: activity
67 Logger.error("Couldn't parse incoming document")
73 Logger.error("Error occured while handling activity")
74 Logger.error(xml_string)
75 Logger.error(inspect(e))
87 def make_share(entry, doc, retweeted_activity) do
88 with {:ok, actor} <- find_make_or_update_user(doc),
89 %Object{} = object <- Object.get_by_ap_id(retweeted_activity.data["object"]["id"]),
90 id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
91 {:ok, activity, _object} = ActivityPub.announce(actor, object, id, false) do
96 def handle_share(entry, doc) do
97 with {:ok, retweeted_activity} <- get_or_build_object(entry),
98 {:ok, activity} <- make_share(entry, doc, retweeted_activity) do
99 {:ok, activity, retweeted_activity}
105 def make_favorite(entry, doc, favorited_activity) do
106 with {:ok, actor} <- find_make_or_update_user(doc),
107 %Object{} = object <- Object.get_by_ap_id(favorited_activity.data["object"]["id"]),
108 id when not is_nil(id) <- string_from_xpath("/entry/id", entry),
109 {:ok, activity, _object} = ActivityPub.like(actor, object, id, false) do
114 def get_or_build_object(entry) do
115 with {:ok, activity} <- get_or_try_fetching(entry) do
119 with [object] <- :xmerl_xpath.string('/entry/activity:object', entry) do
120 NoteHandler.handle_note(object, object)
125 def get_or_try_fetching(entry) do
126 Logger.debug("Trying to get entry from db")
128 with id when not is_nil(id) <- string_from_xpath("//activity:object[1]/id", entry),
129 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
133 Logger.debug("Couldn't get, will try to fetch")
135 with href when not is_nil(href) <-
136 string_from_xpath("//activity:object[1]/link[@type=\"text/html\"]/@href", entry),
137 {:ok, [favorited_activity]} <- fetch_activity_from_url(href) do
138 {:ok, favorited_activity}
140 e -> Logger.debug("Couldn't find href: #{inspect(e)}")
145 def handle_favorite(entry, doc) do
146 with {:ok, favorited_activity} <- get_or_try_fetching(entry),
147 {:ok, activity} <- make_favorite(entry, doc, favorited_activity) do
148 {:ok, activity, favorited_activity}
154 def get_attachments(entry) do
155 :xmerl_xpath.string('/entry/link[@rel="enclosure"]', entry)
156 |> Enum.map(fn enclosure ->
157 with href when not is_nil(href) <- string_from_xpath("/link/@href", enclosure),
158 type when not is_nil(type) <- string_from_xpath("/link/@type", enclosure) do
160 "type" => "Attachment",
175 Gets the content from a an entry.
177 def get_content(entry) do
178 string_from_xpath("//content", entry)
182 Get the cw that mastodon uses.
185 with cw when not is_nil(cw) <- string_from_xpath("/*/summary", entry) do
192 def get_tags(entry) do
193 :xmerl_xpath.string('//category', entry)
194 |> Enum.map(fn category -> string_from_xpath("/category/@term", category) end)
196 |> Enum.map(&String.downcase/1)
199 def maybe_update(doc, user) do
200 if "true" == string_from_xpath("//author[1]/ap_enabled", doc) do
201 Transmogrifier.upgrade_user_from_ap_id(user.ap_id)
203 maybe_update_ostatus(doc, user)
207 def maybe_update_ostatus(doc, user) do
215 with false <- user.local,
216 avatar <- make_avatar_object(doc),
217 bio <- string_from_xpath("//author[1]/summary", doc),
218 name <- string_from_xpath("//author[1]/poco:displayName", doc),
220 Map.put(user.info, "banner", make_avatar_object(doc, "header") || user.info["banner"]),
222 avatar: avatar || old_data.avatar,
223 name: name || old_data.name,
224 bio: bio || old_data.bio,
225 info: info || old_data.info
227 false <- new_data == old_data do
228 change = Ecto.Changeset.change(user, new_data)
236 def find_make_or_update_user(doc) do
237 uri = string_from_xpath("//author/uri[1]", doc)
239 with {:ok, user} <- find_or_make_user(uri) do
240 maybe_update(doc, user)
244 def find_or_make_user(uri) do
245 query = from(user in User, where: user.ap_id == ^uri)
247 user = Repo.one(query)
256 def make_user(uri, update \\ false) do
257 with {:ok, info} <- gather_user_info(uri) do
260 nickname: info["nickname"] <> "@" <> info["host"],
263 avatar: info["avatar"],
267 with false <- update,
268 %User{} = user <- User.get_by_ap_id(data.ap_id) do
271 _e -> User.insert_or_update_user(data)
276 # TODO: Just takes the first one for now.
277 def make_avatar_object(author_doc, rel \\ "avatar") do
278 href = string_from_xpath("//author[1]/link[@rel=\"#{rel}\"]/@href", author_doc)
279 type = string_from_xpath("//author[1]/link[@rel=\"#{rel}\"]/@type", author_doc)
297 def gather_user_info(username) do
298 with {:ok, webfinger_data} <- WebFinger.finger(username),
299 {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data["topic"]) do
300 {:ok, Map.merge(webfinger_data, feed_data) |> Map.put("fqn", username)}
303 Logger.debug(fn -> "Couldn't gather info for #{username}" end)
308 # Regex-based 'parsing' so we don't have to pull in a full html parser
309 # It's a hack anyway. Maybe revisit this in the future
310 @mastodon_regex ~r/<link href='(.*)' rel='alternate' type='application\/atom\+xml'>/
311 @gs_regex ~r/<link title=.* href="(.*)" type="application\/atom\+xml" rel="alternate">/
312 @gs_classic_regex ~r/<link rel="alternate" href="(.*)" type="application\/atom\+xml" title=.*>/
313 def get_atom_url(body) do
315 Regex.match?(@mastodon_regex, body) ->
316 [[_, match]] = Regex.scan(@mastodon_regex, body)
319 Regex.match?(@gs_regex, body) ->
320 [[_, match]] = Regex.scan(@gs_regex, body)
323 Regex.match?(@gs_classic_regex, body) ->
324 [[_, match]] = Regex.scan(@gs_classic_regex, body)
328 Logger.debug(fn -> "Couldn't find Atom link in #{inspect(body)}" end)
329 {:error, "Couldn't find the Atom link"}
333 def fetch_activity_from_atom_url(url) do
334 with true <- String.starts_with?(url, "http"),
335 {:ok, %{body: body, status_code: code}} when code in 200..299 <-
338 [Accept: "application/atom+xml"],
339 follow_redirect: true,
343 Logger.debug("Got document from #{url}, handling...")
344 handle_incoming(body)
347 Logger.debug("Couldn't get #{url}: #{inspect(e)}")
352 def fetch_activity_from_html_url(url) do
353 Logger.debug("Trying to fetch #{url}")
355 with true <- String.starts_with?(url, "http"),
356 {:ok, %{body: body}} <-
357 @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000),
358 {:ok, atom_url} <- get_atom_url(body) do
359 fetch_activity_from_atom_url(atom_url)
362 Logger.debug("Couldn't get #{url}: #{inspect(e)}")
367 def fetch_activity_from_url(url) do
369 with {:ok, activities} when length(activities) > 0 <- fetch_activity_from_atom_url(url) do
373 with {:ok, activities} <- fetch_activity_from_html_url(url) do
379 Logger.debug("Couldn't get #{url}: #{inspect(e)}")
380 {:error, "Couldn't get #{url}: #{inspect(e)}"}