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}
12 def feed_path(user) do
13 "#{user.ap_id}/feed.atom"
16 def pubsub_path(user) do
17 "#{Web.base_url}/push/hub/#{user.nickname}"
20 def salmon_path(user) do
21 "#{user.ap_id}/salmon"
24 def handle_incoming(xml_string) do
25 doc = parse_document(xml_string)
26 entries = :xmerl_xpath.string('//entry', doc)
28 activities = Enum.map(entries, fn (entry) ->
29 {:xmlObj, :string, object_type} = :xmerl_xpath.string('string(/entry/activity:object-type[1])', entry)
30 {:xmlObj, :string, verb} = :xmerl_xpath.string('string(/entry/activity:verb[1])', entry)
33 'http://activitystrea.ms/schema/1.0/share' ->
34 with {:ok, activity, retweeted_activity} <- handle_share(entry, doc), do: [activity, retweeted_activity]
35 'http://activitystrea.ms/schema/1.0/favorite' ->
36 with {:ok, activity, favorited_activity} <- handle_favorite(entry, doc), do: [activity, favorited_activity]
39 'http://activitystrea.ms/schema/1.0/note' ->
40 with {:ok, activity} <- handle_note(entry, doc), do: activity
41 'http://activitystrea.ms/schema/1.0/comment' ->
42 with {:ok, activity} <- handle_note(entry, doc), do: activity
44 Logger.error("Couldn't parse incoming document")
52 def make_share(_entry, doc, retweeted_activity) do
53 with {:ok, actor} <- find_make_or_update_user(doc),
54 %Object{} = object <- Object.get_cached_by_ap_id(retweeted_activity.data["object"]["id"]),
55 {:ok, activity, _object} = ActivityPub.announce(actor, object, false) do
60 def handle_share(entry, doc) do
61 with [object] <- :xmerl_xpath.string('/entry/activity:object', entry),
62 {:ok, retweeted_activity} <- handle_note(object, object),
63 {:ok, activity} <- make_share(entry, doc, retweeted_activity) do
64 {:ok, activity, retweeted_activity}
70 def make_favorite(_entry, doc, favorited_activity) do
71 with {:ok, actor} <- find_make_or_update_user(doc),
72 %Object{} = object <- Object.get_cached_by_ap_id(favorited_activity.data["object"]["id"]),
73 {:ok, activity, _object} = ActivityPub.like(actor, object, false) do
78 def get_or_try_fetching(entry) do
79 with id when not is_nil(id) <- string_from_xpath("//activity:object[1]/id", entry),
80 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
83 with href when not is_nil(href) <- string_from_xpath("//activity:object[1]/link[@type=\"text/html\"]/@href", entry),
84 {:ok, [favorited_activity]} <- fetch_activity_from_html_url(href) do
85 {:ok, favorited_activity}
90 def handle_favorite(entry, doc) do
91 with {:ok, favorited_activity} <- get_or_try_fetching(entry),
92 {:ok, activity} <- make_favorite(entry, doc, favorited_activity) do
93 {:ok, activity, favorited_activity}
99 def get_attachments(entry) do
100 :xmerl_xpath.string('/entry/link[@rel="enclosure"]', entry)
101 |> Enum.map(fn (enclosure) ->
102 with href when not is_nil(href) <- string_from_xpath("/link/@href", enclosure),
103 type when not is_nil(type) <- string_from_xpath("/link/@type", enclosure) do
105 "type" => "Attachment",
114 |> Enum.filter(&(&1))
117 def handle_note(entry, doc \\ nil) do
118 content_html = string_from_xpath("//content[1]", entry)
120 [author] = :xmerl_xpath.string('//author[1]', doc)
121 {:ok, actor} = find_make_or_update_user(author)
122 inReplyTo = string_from_xpath("//thr:in-reply-to[1]/@ref", entry)
124 if !Object.get_cached_by_ap_id(inReplyTo) do
125 inReplyToHref = string_from_xpath("//thr:in-reply-to[1]/@href", entry)
127 fetch_activity_from_html_url(inReplyToHref)
131 context = (string_from_xpath("//ostatus:conversation[1]", entry) || "") |> String.trim
133 attachments = get_attachments(entry)
135 context = with %{data: %{"context" => context}} <- Object.get_cached_by_ap_id(inReplyTo) do
138 if String.length(context) > 0 do
141 ActivityPub.generate_context_id
146 "https://www.w3.org/ns/activitystreams#Public",
147 User.ap_followers(actor)
150 mentions = :xmerl_xpath.string('//link[@rel="mentioned" and @ostatus:object-type="http://activitystrea.ms/schema/1.0/person"]', entry)
151 |> Enum.map(fn(person) -> string_from_xpath("@href", person) end)
155 date = string_from_xpath("//published", entry)
156 id = string_from_xpath("//id", entry)
162 "content" => content_html,
164 "context" => context,
165 "actor" => actor.ap_id,
166 "attachment" => attachments
169 object = if inReplyTo do
170 Map.put(object, "inReplyTo", inReplyTo)
175 # TODO: Bail out sooner and use transaction.
176 if Object.get_by_ap_id(id) do
177 {:error, "duplicate activity"}
179 ActivityPub.create(to, actor, context, object, %{}, date, false)
183 def find_make_or_update_user(doc) do
184 uri = string_from_xpath("//author/uri[1]", doc)
185 with {:ok, user} <- find_or_make_user(uri) do
186 avatar = make_avatar_object(doc)
187 if user.avatar != avatar do
188 change = Ecto.Changeset.change(user, %{avatar: avatar})
196 def find_or_make_user(uri) do
197 query = from user in User,
198 where: user.local == false and fragment("? @> ?", user.info, ^%{uri: uri})
200 user = Repo.one(query)
209 def make_user(uri) do
210 with {:ok, info} <- gather_user_info(uri) do
214 nickname: info["nickname"] <> "@" <> info["host"],
217 avatar: info["avatar"]
219 # TODO: Make remote user changeset
220 # SHould enforce fqn nickname
221 Repo.insert(Ecto.Changeset.change(%User{}, data))
225 # TODO: Just takes the first one for now.
226 def make_avatar_object(author_doc) do
227 href = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@href", author_doc)
228 type = string_from_xpath("//author[1]/link[@rel=\"avatar\"]/@type", author_doc)
245 def gather_user_info(username) do
246 with {:ok, webfinger_data} <- WebFinger.finger(username),
247 {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data["topic"]) do
248 {:ok, Map.merge(webfinger_data, feed_data) |> Map.put("fqn", username)}
250 Logger.debug(fn -> "Couldn't gather info for #{username}" end)
255 # Regex-based 'parsing' so we don't have to pull in a full html parser
256 # It's a hack anyway. Maybe revisit this in the future
257 @mastodon_regex ~r/<link href='(.*)' rel='alternate' type='application\/atom\+xml'>/
258 @gs_regex ~r/<link title=.* href="(.*)" type="application\/atom\+xml" rel="alternate">/
259 @gs_classic_regex ~r/<link rel="alternate" href="(.*)" type="application\/atom\+xml" title=.*>/
260 def get_atom_url(body) do
262 Regex.match?(@mastodon_regex, body) ->
263 [[_, match]] = Regex.scan(@mastodon_regex, body)
265 Regex.match?(@gs_regex, body) ->
266 [[_, match]] = Regex.scan(@gs_regex, body)
268 Regex.match?(@gs_classic_regex, body) ->
269 [[_, match]] = Regex.scan(@gs_classic_regex, body)
272 Logger.debug(fn -> "Couldn't find atom link in #{inspect(body)}" end)
273 {:error, "Couldn't find the atom link"}
277 def fetch_activity_from_html_url(url) do
278 with {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true),
279 {:ok, atom_url} <- get_atom_url(body),
280 {:ok, %{status_code: code, body: body}} when code in 200..299 <- @httpoison.get(atom_url, [], follow_redirect: true) do
281 handle_incoming(body)