1 defmodule Pleroma.Web.OStatus.OStatusController do
2 use Pleroma.Web, :controller
4 alias Pleroma.{User, Activity}
5 alias Pleroma.Web.OStatus.{FeedRepresenter, ActivityRepresenter}
7 alias Pleroma.Web.{OStatus, Federator}
9 alias Pleroma.Web.ActivityPub.ActivityPubController
10 alias Pleroma.Web.ActivityPub.ActivityPub
12 def feed_redirect(conn, %{"nickname" => nickname} = params) do
13 user = User.get_cached_by_nickname(nickname)
15 case get_format(conn) do
16 "html" -> Fallback.RedirectController.redirector(conn, nil)
17 "activity+json" -> ActivityPubController.user(conn, params)
18 _ -> redirect(conn, external: OStatus.feed_path(user))
22 def feed(conn, %{"nickname" => nickname} = params) do
23 user = User.get_cached_by_nickname(nickname)
26 Map.take(params, ["max_id"])
27 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
30 ActivityPub.fetch_public_activities(query_params)
35 |> FeedRepresenter.to_simple_form(activities, [user])
36 |> :xmerl.export_simple(:xmerl_xml)
40 |> put_resp_content_type("application/atom+xml")
41 |> send_resp(200, response)
44 defp decode_or_retry(body) do
45 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
46 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
50 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
51 doc <- XML.parse_document(decoded),
52 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
53 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
54 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
55 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
61 def salmon_incoming(conn, _) do
62 {:ok, body, _conn} = read_body(conn)
63 {:ok, doc} = decode_or_retry(body)
65 Federator.enqueue(:incoming_doc, doc)
71 def object(conn, %{"uuid" => uuid} = params) do
72 if get_format(conn) == "activity+json" do
73 ActivityPubController.object(conn, params)
75 with id <- o_status_url(conn, :object, uuid),
76 %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
77 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
78 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
79 case get_format(conn) do
80 "html" -> redirect(conn, to: "/notice/#{activity.id}")
81 _ -> represent_activity(conn, activity, user)
92 def activity(conn, %{"uuid" => uuid}) do
93 with id <- o_status_url(conn, :activity, uuid),
94 %Activity{} = activity <- Activity.get_by_ap_id(id),
95 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
96 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
97 case get_format(conn) do
98 "html" -> redirect(conn, to: "/notice/#{activity.id}")
99 _ -> represent_activity(conn, activity, user)
109 def notice(conn, %{"id" => id}) do
110 with %Activity{} = activity <- Repo.get(Activity, id),
111 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
112 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
113 case get_format(conn) do
116 |> put_resp_content_type("text/html")
117 |> send_file(200, "priv/static/index.html")
120 represent_activity(conn, activity, user)
130 defp represent_activity(conn, activity, user) do
133 |> ActivityRepresenter.to_simple_form(user, true)
134 |> ActivityRepresenter.wrap_with_entry()
135 |> :xmerl.export_simple(:xmerl_xml)
139 |> put_resp_content_type("application/atom+xml")
140 |> send_resp(200, response)