1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.OStatus.OStatusController do
6 use Pleroma.Web, :controller
8 alias Pleroma.{Activity, Object, User}
9 alias Pleroma.Web.ActivityPub.{ActivityPub, ActivityPubController, ObjectView}
10 alias Pleroma.Web.OStatus.{ActivityRepresenter, FeedRepresenter}
11 alias Pleroma.Web.{Federator, OStatus}
14 plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
16 action_fallback(:errors)
18 def feed_redirect(conn, %{"nickname" => nickname}) do
19 case get_format(conn) do
21 with %User{} = user <- User.get_cached_by_nickname_or_id(nickname) do
22 Fallback.RedirectController.redirector_with_meta(conn, %{user: user})
24 nil -> {:error, :not_found}
28 ActivityPubController.call(conn, :user)
31 with %User{} = user <- User.get_cached_by_nickname(nickname) do
32 redirect(conn, external: OStatus.feed_path(user))
34 nil -> {:error, :not_found}
39 def feed(conn, %{"nickname" => nickname} = params) do
40 with %User{} = user <- User.get_cached_by_nickname(nickname) do
42 Map.take(params, ["max_id"])
43 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
46 ActivityPub.fetch_public_activities(query_params)
51 |> FeedRepresenter.to_simple_form(activities, [user])
52 |> :xmerl.export_simple(:xmerl_xml)
56 |> put_resp_content_type("application/atom+xml")
57 |> send_resp(200, response)
59 nil -> {:error, :not_found}
63 defp decode_or_retry(body) do
64 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
65 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
69 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
70 doc <- XML.parse_document(decoded),
71 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
72 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
73 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
74 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
80 def salmon_incoming(conn, _) do
81 {:ok, body, _conn} = read_body(conn)
82 {:ok, doc} = decode_or_retry(body)
84 Federator.enqueue(:incoming_doc, doc)
90 def object(conn, %{"uuid" => uuid}) do
91 if get_format(conn) == "activity+json" do
92 ActivityPubController.call(conn, :object)
94 with id <- o_status_url(conn, :object, uuid),
95 {_, %Activity{} = activity} <- {:activity, Activity.get_create_by_object_ap_id(id)},
96 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
97 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
98 case get_format(conn) do
99 "html" -> redirect(conn, to: "/notice/#{activity.id}")
100 _ -> represent_activity(conn, nil, activity, user)
115 def activity(conn, %{"uuid" => uuid}) do
116 if get_format(conn) == "activity+json" do
117 ActivityPubController.call(conn, :activity)
119 with id <- o_status_url(conn, :activity, uuid),
120 {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
121 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
122 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
123 case format = get_format(conn) do
124 "html" -> redirect(conn, to: "/notice/#{activity.id}")
125 _ -> represent_activity(conn, format, activity, user)
140 def notice(conn, %{"id" => id}) do
141 with {_, %Activity{} = activity} <- {:activity, Activity.get_by_id(id)},
142 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
143 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
144 case format = get_format(conn) do
146 if activity.data["type"] == "Create" do
147 %Object{} = object = Object.normalize(activity.data["object"])
149 Fallback.RedirectController.redirector_with_meta(conn, %{
152 Pleroma.Web.Router.Helpers.o_status_url(
153 Pleroma.Web.Endpoint,
160 Fallback.RedirectController.redirector(conn, nil)
164 represent_activity(conn, format, activity, user)
170 |> Fallback.RedirectController.redirector(nil, 404)
174 |> Fallback.RedirectController.redirector(nil, 404)
181 defp represent_activity(
184 %Activity{data: %{"type" => "Create"}} = activity,
187 object = Object.normalize(activity.data["object"])
190 |> put_resp_header("content-type", "application/activity+json")
191 |> json(ObjectView.render("object.json", %{object: object}))
194 defp represent_activity(_conn, "activity+json", _, _) do
198 defp represent_activity(conn, _, activity, user) do
201 |> ActivityRepresenter.to_simple_form(user, true)
202 |> ActivityRepresenter.wrap_with_entry()
203 |> :xmerl.export_simple(:xmerl_xml)
207 |> put_resp_content_type("application/atom+xml")
208 |> send_resp(200, response)
211 def errors(conn, {:error, :not_found}) do
217 def errors(conn, _) do
220 |> text("Something went wrong")