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 with %User{} = user <- User.get_cached_by_nickname(nickname) do
20 Fallback.RedirectController.redirector_with_meta(conn, %{user: user})
24 ActivityPubController.call(conn, :user)
27 with %User{} = user <- User.get_cached_by_nickname(nickname) do
28 redirect(conn, external: OStatus.feed_path(user))
30 nil -> {:error, :not_found}
35 def feed(conn, %{"nickname" => nickname} = params) do
36 with %User{} = user <- User.get_cached_by_nickname(nickname) do
38 Map.take(params, ["max_id"])
39 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
42 ActivityPub.fetch_public_activities(query_params)
47 |> FeedRepresenter.to_simple_form(activities, [user])
48 |> :xmerl.export_simple(:xmerl_xml)
52 |> put_resp_content_type("application/atom+xml")
53 |> send_resp(200, response)
55 nil -> {:error, :not_found}
59 defp decode_or_retry(body) do
60 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
61 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
65 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
66 doc <- XML.parse_document(decoded),
67 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
68 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
69 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
70 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
76 def salmon_incoming(conn, _) do
77 {:ok, body, _conn} = read_body(conn)
78 {:ok, doc} = decode_or_retry(body)
80 Federator.enqueue(:incoming_doc, doc)
86 def object(conn, %{"uuid" => uuid}) do
87 if get_format(conn) == "activity+json" do
88 ActivityPubController.call(conn, :object)
90 with id <- o_status_url(conn, :object, uuid),
91 {_, %Activity{} = activity} <-
92 {:activity, Activity.get_create_activity_by_object_ap_id(id)},
93 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
94 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
95 case get_format(conn) do
96 "html" -> redirect(conn, to: "/notice/#{activity.id}")
97 _ -> represent_activity(conn, nil, activity, user)
112 def activity(conn, %{"uuid" => uuid}) do
113 with id <- o_status_url(conn, :activity, uuid),
114 {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
115 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
116 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
117 case format = get_format(conn) do
118 "html" -> redirect(conn, to: "/notice/#{activity.id}")
119 _ -> represent_activity(conn, format, activity, user)
133 def notice(conn, %{"id" => id}) do
134 with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
135 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
136 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
137 case format = get_format(conn) do
139 Fallback.RedirectController.redirector_with_meta(conn, %{activity: activity, user: user})
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")