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} <-
97 {:activity, Activity.get_create_activity_by_object_ap_id(id)},
98 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
99 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
100 case get_format(conn) do
101 "html" -> redirect(conn, to: "/notice/#{activity.id}")
102 _ -> represent_activity(conn, nil, activity, user)
117 def activity(conn, %{"uuid" => uuid}) do
118 if get_format(conn) == "activity+json" do
119 ActivityPubController.call(conn, :activity)
121 with id <- o_status_url(conn, :activity, uuid),
122 {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
123 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
124 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
125 case format = get_format(conn) do
126 "html" -> redirect(conn, to: "/notice/#{activity.id}")
127 _ -> represent_activity(conn, format, activity, user)
142 def notice(conn, %{"id" => id}) do
143 with {_, %Activity{} = activity} <- {:activity, Activity.get_by_id(id)},
144 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
145 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
146 case format = get_format(conn) do
148 # Only Create actvities have a map at object
149 if is_map(activity.data["object"]) do
150 Fallback.RedirectController.redirector_with_meta(conn, %{
155 Fallback.RedirectController.redirector(conn, nil)
159 represent_activity(conn, format, activity, user)
173 defp represent_activity(
176 %Activity{data: %{"type" => "Create"}} = activity,
179 object = Object.normalize(activity.data["object"])
182 |> put_resp_header("content-type", "application/activity+json")
183 |> json(ObjectView.render("object.json", %{object: object}))
186 defp represent_activity(_conn, "activity+json", _, _) do
190 defp represent_activity(conn, _, activity, user) do
193 |> ActivityRepresenter.to_simple_form(user, true)
194 |> ActivityRepresenter.wrap_with_entry()
195 |> :xmerl.export_simple(:xmerl_xml)
199 |> put_resp_content_type("application/atom+xml")
200 |> send_resp(200, response)
203 def errors(conn, {:error, :not_found}) do
209 def errors(conn, _) do
212 |> text("Something went wrong")