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.{User, Activity, Object}
9 alias Pleroma.Web.OStatus.{FeedRepresenter, ActivityRepresenter}
11 alias Pleroma.Web.{OStatus, Federator}
13 alias Pleroma.Web.ActivityPub.ObjectView
14 alias Pleroma.Web.ActivityPub.ActivityPubController
15 alias Pleroma.Web.ActivityPub.ActivityPub
17 plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
18 plug(:set_requester_reachable when action in [:salmon_incoming])
20 action_fallback(:errors)
22 def feed_redirect(conn, %{"nickname" => nickname}) do
23 case get_format(conn) do
25 Fallback.RedirectController.redirector(conn, nil)
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, Repo.get(Activity, 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
147 |> put_resp_content_type("text/html")
148 |> send_file(200, Pleroma.Plugs.InstanceStatic.file_path("index.html"))
151 represent_activity(conn, format, activity, user)
165 defp represent_activity(
168 %Activity{data: %{"type" => "Create"}} = activity,
171 object = Object.normalize(activity.data["object"])
174 |> put_resp_header("content-type", "application/activity+json")
175 |> json(ObjectView.render("object.json", %{object: object}))
178 defp represent_activity(_conn, "activity+json", _, _) do
182 defp represent_activity(conn, _, activity, user) do
185 |> ActivityRepresenter.to_simple_form(user, true)
186 |> ActivityRepresenter.wrap_with_entry()
187 |> :xmerl.export_simple(:xmerl_xml)
191 |> put_resp_content_type("application/atom+xml")
192 |> send_resp(200, response)
195 def errors(conn, {:error, :not_found}) do
201 def errors(conn, _) do
204 |> text("Something went wrong")
207 defp set_requester_reachable(conn, _) do
208 Pleroma.Web.ControllerHelper.set_requester_reachable(conn)