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.ObjectView
10 alias Pleroma.Web.ActivityPub.ActivityPubController
11 alias Pleroma.Web.ActivityPub.ActivityPub
13 action_fallback(:errors)
15 def feed_redirect(conn, %{"nickname" => nickname}) do
16 case get_format(conn) do
18 Fallback.RedirectController.redirector(conn, nil)
21 ActivityPubController.call(conn, :user)
24 with %User{} = user <- User.get_cached_by_nickname(nickname) do
25 redirect(conn, external: OStatus.feed_path(user))
27 nil -> {:error, :not_found}
32 def feed(conn, %{"nickname" => nickname} = params) do
33 with %User{} = user <- User.get_cached_by_nickname(nickname) do
35 Map.take(params, ["max_id"])
36 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
39 ActivityPub.fetch_public_activities(query_params)
44 |> FeedRepresenter.to_simple_form(activities, [user])
45 |> :xmerl.export_simple(:xmerl_xml)
49 |> put_resp_content_type("application/atom+xml")
50 |> send_resp(200, response)
52 nil -> {:error, :not_found}
56 defp decode_or_retry(body) do
57 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
58 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
62 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
63 doc <- XML.parse_document(decoded),
64 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
65 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
66 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
67 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
73 def salmon_incoming(conn, _) do
74 {:ok, body, _conn} = read_body(conn)
75 {:ok, doc} = decode_or_retry(body)
77 Federator.enqueue(:incoming_doc, doc)
83 def object(conn, %{"uuid" => uuid}) do
84 if get_format(conn) == "activity+json" do
85 ActivityPubController.call(conn, :object)
87 with id <- o_status_url(conn, :object, uuid),
88 {_, %Activity{} = activity} <-
89 {:activity, Activity.get_create_activity_by_object_ap_id(id)},
90 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
91 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
92 case get_format(conn) do
93 "html" -> redirect(conn, to: "/notice/#{activity.id}")
94 _ -> represent_activity(conn, nil, activity, user)
109 def activity(conn, %{"uuid" => uuid}) do
110 with id <- o_status_url(conn, :activity, uuid),
111 {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
112 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
113 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
114 case format = get_format(conn) do
115 "html" -> redirect(conn, to: "/notice/#{activity.id}")
116 _ -> represent_activity(conn, format, activity, user)
130 def notice(conn, %{"id" => id}) do
131 with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
132 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
133 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
134 case format = get_format(conn) do
137 |> put_resp_content_type("text/html")
138 |> send_file(200, "priv/static/index.html")
141 represent_activity(conn, format, activity, user)
155 defp represent_activity(conn, "activity+json", activity, user) do
157 |> put_resp_header("content-type", "application/activity+json")
158 |> json(ObjectView.render("object.json", %{object: activity}))
161 defp represent_activity(conn, _, activity, user) do
164 |> ActivityRepresenter.to_simple_form(user, true)
165 |> ActivityRepresenter.wrap_with_entry()
166 |> :xmerl.export_simple(:xmerl_xml)
170 |> put_resp_content_type("application/atom+xml")
171 |> send_resp(200, response)
174 def errors(conn, {:error, :not_found}) do
180 def errors(conn, _) do
183 |> text("Something went wrong")