1 defmodule Pleroma.Web.OStatus.OStatusController do
2 use Pleroma.Web, :controller
4 alias Pleroma.{User, Activity, Object}
5 alias Pleroma.Web.OStatus.{FeedRepresenter, ActivityRepresenter}
7 alias Pleroma.Web.{OStatus, Federator}
9 alias Pleroma.Web.ActivityPub.ObjectView
10 alias Pleroma.Web.ActivityPub.ActivityPubController
11 alias Pleroma.Web.ActivityPub.ActivityPub
13 plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
14 action_fallback(:errors)
16 def feed_redirect(conn, %{"nickname" => nickname}) do
17 case get_format(conn) do
19 Fallback.RedirectController.redirector(conn, nil)
22 ActivityPubController.call(conn, :user)
25 with %User{} = user <- User.get_cached_by_nickname(nickname) do
26 redirect(conn, external: OStatus.feed_path(user))
28 nil -> {:error, :not_found}
33 def feed(conn, %{"nickname" => nickname} = params) do
34 with %User{} = user <- User.get_cached_by_nickname(nickname) do
36 Map.take(params, ["max_id"])
37 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
40 ActivityPub.fetch_public_activities(query_params)
45 |> FeedRepresenter.to_simple_form(activities, [user])
46 |> :xmerl.export_simple(:xmerl_xml)
50 |> put_resp_content_type("application/atom+xml")
51 |> send_resp(200, response)
53 nil -> {:error, :not_found}
57 defp decode_or_retry(body) do
58 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
59 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
63 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
64 doc <- XML.parse_document(decoded),
65 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
66 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
67 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
68 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
74 def salmon_incoming(conn, _) do
75 {:ok, body, _conn} = read_body(conn)
76 {:ok, doc} = decode_or_retry(body)
78 Federator.enqueue(:incoming_doc, doc)
84 def object(conn, %{"uuid" => uuid}) do
85 if get_format(conn) == "activity+json" do
86 ActivityPubController.call(conn, :object)
88 with id <- o_status_url(conn, :object, uuid),
89 {_, %Activity{} = activity} <-
90 {:activity, Activity.get_create_activity_by_object_ap_id(id)},
91 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
92 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
93 case get_format(conn) do
94 "html" -> redirect(conn, to: "/notice/#{activity.id}")
95 _ -> represent_activity(conn, nil, activity, user)
110 def activity(conn, %{"uuid" => uuid}) do
111 with id <- o_status_url(conn, :activity, uuid),
112 {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
113 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
114 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
115 case format = get_format(conn) do
116 "html" -> redirect(conn, to: "/notice/#{activity.id}")
117 _ -> represent_activity(conn, format, activity, user)
131 def notice(conn, %{"id" => id}) do
132 with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
133 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
134 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
135 case format = get_format(conn) do
138 |> put_resp_content_type("text/html")
139 |> send_file(200, "priv/static/index.html")
142 represent_activity(conn, format, activity, user)
156 defp represent_activity(
159 %Activity{data: %{"type" => "Create"}} = activity,
162 object = Object.normalize(activity.data["object"])
165 |> put_resp_header("content-type", "application/activity+json")
166 |> json(ObjectView.render("object.json", %{object: object}))
169 defp represent_activity(conn, "activity+json", _, _) do
173 defp represent_activity(conn, _, activity, user) do
176 |> ActivityRepresenter.to_simple_form(user, true)
177 |> ActivityRepresenter.wrap_with_entry()
178 |> :xmerl.export_simple(:xmerl_xml)
182 |> put_resp_content_type("application/atom+xml")
183 |> send_resp(200, response)
186 def errors(conn, {:error, :not_found}) do
192 def errors(conn, _) do
195 |> text("Something went wrong")