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}
10 alias Pleroma.Web.{OStatus, Federator}
12 alias Pleroma.Web.ActivityPub.ObjectView
13 alias Pleroma.Web.ActivityPub.ActivityPubController
14 alias Pleroma.Web.ActivityPub.ActivityPub
16 plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
17 action_fallback(:errors)
19 def feed_redirect(conn, %{"nickname" => nickname}) do
20 case get_format(conn) do
22 with %User{} = user <- User.get_cached_by_nickname_or_id(nickname) do
23 Fallback.RedirectController.redirector_with_meta(conn, %{user: user})
25 nil -> {:error, :not_found}
29 ActivityPubController.call(conn, :user)
32 with %User{} = user <- User.get_cached_by_nickname(nickname) do
33 redirect(conn, external: OStatus.feed_path(user))
35 nil -> {:error, :not_found}
40 def feed(conn, %{"nickname" => nickname} = params) do
41 with %User{} = user <- User.get_cached_by_nickname(nickname) do
43 Map.take(params, ["max_id"])
44 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
47 ActivityPub.fetch_public_activities(query_params)
52 |> FeedRepresenter.to_simple_form(activities, [user])
53 |> :xmerl.export_simple(:xmerl_xml)
57 |> put_resp_content_type("application/atom+xml")
58 |> send_resp(200, response)
60 nil -> {:error, :not_found}
64 defp decode_or_retry(body) do
65 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
66 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
70 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
71 doc <- XML.parse_document(decoded),
72 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
73 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
74 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
75 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
81 def salmon_incoming(conn, _) do
82 {:ok, body, _conn} = read_body(conn)
83 {:ok, doc} = decode_or_retry(body)
85 Federator.enqueue(:incoming_doc, doc)
91 def object(conn, %{"uuid" => uuid}) do
92 if get_format(conn) == "activity+json" do
93 ActivityPubController.call(conn, :object)
95 with id <- o_status_url(conn, :object, uuid),
96 {_, %Activity{} = activity} <- {:activity, Activity.get_create_by_object_ap_id(id)},
97 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
98 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
99 case get_format(conn) do
100 "html" -> redirect(conn, to: "/notice/#{activity.id}")
101 _ -> represent_activity(conn, nil, activity, user)
116 def activity(conn, %{"uuid" => uuid}) do
117 if get_format(conn) == "activity+json" do
118 ActivityPubController.call(conn, :activity)
120 with id <- o_status_url(conn, :activity, uuid),
121 {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
122 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
123 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
124 case format = get_format(conn) do
125 "html" -> redirect(conn, to: "/notice/#{activity.id}")
126 _ -> represent_activity(conn, format, activity, user)
141 def notice(conn, %{"id" => id}) do
142 with {_, %Activity{} = activity} <- {:activity, Activity.get_by_id(id)},
143 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
144 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
145 case format = get_format(conn) do
147 if activity.data["type"] == "Create" do
148 %Object{} = object = Object.normalize(activity.data["object"])
150 Fallback.RedirectController.redirector_with_meta(conn, %{
153 Pleroma.Web.Router.Helpers.o_status_url(
154 Pleroma.Web.Endpoint,
161 Fallback.RedirectController.redirector(conn, nil)
165 represent_activity(conn, format, activity, user)
171 |> Fallback.RedirectController.redirector(nil, 404)
175 |> Fallback.RedirectController.redirector(nil, 404)
182 defp represent_activity(
185 %Activity{data: %{"type" => "Create"}} = activity,
188 object = Object.normalize(activity.data["object"])
191 |> put_resp_header("content-type", "application/activity+json")
192 |> json(ObjectView.render("object.json", %{object: object}))
195 defp represent_activity(_conn, "activity+json", _, _) do
199 defp represent_activity(conn, _, activity, user) do
202 |> ActivityRepresenter.to_simple_form(user, true)
203 |> ActivityRepresenter.wrap_with_entry()
204 |> :xmerl.export_simple(:xmerl_xml)
208 |> put_resp_content_type("application/atom+xml")
209 |> send_resp(200, response)
212 def errors(conn, {:error, :not_found}) do
218 def errors(conn, _) do
221 |> text("Something went wrong")