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 action_fallback(:errors)
14 def feed_redirect(conn, %{"nickname" => nickname}) do
15 case get_format(conn) do
17 Fallback.RedirectController.redirector(conn, nil)
20 ActivityPubController.call(conn, :user)
23 with %User{} = user <- User.get_cached_by_nickname(nickname) do
24 redirect(conn, external: OStatus.feed_path(user))
26 nil -> {:error, :not_found}
31 def feed(conn, %{"nickname" => nickname} = params) do
32 with %User{} = user <- User.get_cached_by_nickname(nickname) do
34 Map.take(params, ["max_id"])
35 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
38 ActivityPub.fetch_public_activities(query_params)
43 |> FeedRepresenter.to_simple_form(activities, [user])
44 |> :xmerl.export_simple(:xmerl_xml)
48 |> put_resp_content_type("application/atom+xml")
49 |> send_resp(200, response)
51 nil -> {:error, :not_found}
55 defp decode_or_retry(body) do
56 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
57 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
61 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
62 doc <- XML.parse_document(decoded),
63 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
64 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
65 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
66 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
72 def salmon_incoming(conn, _) do
73 {:ok, body, _conn} = read_body(conn)
74 {:ok, doc} = decode_or_retry(body)
76 Federator.enqueue(:incoming_doc, doc)
82 def object(conn, %{"uuid" => uuid}) do
83 if get_format(conn) == "activity+json" do
84 ActivityPubController.call(conn, :object)
86 with id <- o_status_url(conn, :object, uuid),
87 {_, %Activity{} = activity} <-
88 {:activity, Activity.get_create_activity_by_object_ap_id(id)},
89 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
90 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
91 case get_format(conn) do
92 "html" -> redirect(conn, to: "/notice/#{activity.id}")
93 _ -> represent_activity(conn, activity, user)
108 def activity(conn, %{"uuid" => uuid}) do
109 with id <- o_status_url(conn, :activity, uuid),
110 {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(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
114 "html" -> redirect(conn, to: "/notice/#{activity.id}")
115 _ -> represent_activity(conn, activity, user)
129 def notice(conn, %{"id" => id}) do
130 with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
131 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
132 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
133 case get_format(conn) do
136 |> put_resp_content_type("text/html")
137 |> send_file(200, "priv/static/index.html")
140 represent_activity(conn, activity, user)
154 defp represent_activity(conn, activity, user) do
157 |> ActivityRepresenter.to_simple_form(user, true)
158 |> ActivityRepresenter.wrap_with_entry()
159 |> :xmerl.export_simple(:xmerl_xml)
163 |> put_resp_content_type("application/atom+xml")
164 |> send_resp(200, response)
167 def errors(conn, {:error, :not_found}) do
173 def errors(conn, _) do
176 |> text("Something went wrong")