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 action_fallback(:errors)
20 def feed_redirect(conn, %{"nickname" => nickname}) do
21 case get_format(conn) do
23 with %User{} = user <- User.get_cached_by_nickname_or_id(nickname) do
24 Fallback.RedirectController.redirector_with_meta(conn, %{user: user})
26 nil -> {:error, :not_found}
30 ActivityPubController.call(conn, :user)
33 with %User{} = user <- User.get_cached_by_nickname(nickname) do
34 redirect(conn, external: OStatus.feed_path(user))
36 nil -> {:error, :not_found}
41 def feed(conn, %{"nickname" => nickname} = params) do
42 with %User{} = user <- User.get_cached_by_nickname(nickname) do
44 Map.take(params, ["max_id"])
45 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
48 ActivityPub.fetch_public_activities(query_params)
53 |> FeedRepresenter.to_simple_form(activities, [user])
54 |> :xmerl.export_simple(:xmerl_xml)
58 |> put_resp_content_type("application/atom+xml")
59 |> send_resp(200, response)
61 nil -> {:error, :not_found}
65 defp decode_or_retry(body) do
66 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
67 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
71 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
72 doc <- XML.parse_document(decoded),
73 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
74 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
75 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
76 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
82 def salmon_incoming(conn, _) do
83 {:ok, body, _conn} = read_body(conn)
84 {:ok, doc} = decode_or_retry(body)
86 Federator.enqueue(:incoming_doc, doc)
92 def object(conn, %{"uuid" => uuid}) do
93 if get_format(conn) == "activity+json" do
94 ActivityPubController.call(conn, :object)
96 with id <- o_status_url(conn, :object, uuid),
97 {_, %Activity{} = activity} <-
98 {:activity, Activity.get_create_activity_by_object_ap_id(id)},
99 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
100 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
101 case get_format(conn) do
102 "html" -> redirect(conn, to: "/notice/#{activity.id}")
103 _ -> represent_activity(conn, nil, activity, user)
118 def activity(conn, %{"uuid" => uuid}) do
119 if get_format(conn) == "activity+json" do
120 ActivityPubController.call(conn, :activity)
122 with id <- o_status_url(conn, :activity, uuid),
123 {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
124 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
125 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
126 case format = get_format(conn) do
127 "html" -> redirect(conn, to: "/notice/#{activity.id}")
128 _ -> represent_activity(conn, format, activity, user)
143 def notice(conn, %{"id" => id}) do
144 with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
145 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
146 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
147 case format = get_format(conn) do
149 Fallback.RedirectController.redirector_with_meta(conn, %{activity: activity, user: user})
152 represent_activity(conn, format, activity, user)
166 defp represent_activity(
169 %Activity{data: %{"type" => "Create"}} = activity,
172 object = Object.normalize(activity.data["object"])
175 |> put_resp_header("content-type", "application/activity+json")
176 |> json(ObjectView.render("object.json", %{object: object}))
179 defp represent_activity(_conn, "activity+json", _, _) do
183 defp represent_activity(conn, _, activity, user) do
186 |> ActivityRepresenter.to_simple_form(user, true)
187 |> ActivityRepresenter.wrap_with_entry()
188 |> :xmerl.export_simple(:xmerl_xml)
192 |> put_resp_content_type("application/atom+xml")
193 |> send_resp(200, response)
196 def errors(conn, {:error, :not_found}) do
202 def errors(conn, _) do
205 |> text("Something went wrong")