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 Fallback.RedirectController.redirector(conn, nil)
26 ActivityPubController.call(conn, :user)
29 with %User{} = user <- User.get_cached_by_nickname(nickname) do
30 redirect(conn, external: OStatus.feed_path(user))
32 nil -> {:error, :not_found}
37 def feed(conn, %{"nickname" => nickname} = params) do
38 with %User{} = user <- User.get_cached_by_nickname(nickname) do
40 Map.take(params, ["max_id"])
41 |> Map.merge(%{"whole_db" => true, "actor_id" => user.ap_id})
44 ActivityPub.fetch_public_activities(query_params)
49 |> FeedRepresenter.to_simple_form(activities, [user])
50 |> :xmerl.export_simple(:xmerl_xml)
54 |> put_resp_content_type("application/atom+xml")
55 |> send_resp(200, response)
57 nil -> {:error, :not_found}
61 defp decode_or_retry(body) do
62 with {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
63 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
67 with [decoded | _] <- Pleroma.Web.Salmon.decode(body),
68 doc <- XML.parse_document(decoded),
69 uri when not is_nil(uri) <- XML.string_from_xpath("/entry/author[1]/uri", doc),
70 {:ok, _} <- Pleroma.Web.OStatus.make_user(uri, true),
71 {:ok, magic_key} <- Pleroma.Web.Salmon.fetch_magic_key(body),
72 {:ok, doc} <- Pleroma.Web.Salmon.decode_and_validate(magic_key, body) do
78 def salmon_incoming(conn, _) do
79 {:ok, body, _conn} = read_body(conn)
80 {:ok, doc} = decode_or_retry(body)
82 Federator.enqueue(:incoming_doc, doc)
88 def object(conn, %{"uuid" => uuid}) do
89 if get_format(conn) == "activity+json" do
90 ActivityPubController.call(conn, :object)
92 with id <- o_status_url(conn, :object, uuid),
93 {_, %Activity{} = activity} <-
94 {:activity, Activity.get_create_activity_by_object_ap_id(id)},
95 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
96 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
97 case get_format(conn) do
98 "html" -> redirect(conn, to: "/notice/#{activity.id}")
99 _ -> represent_activity(conn, nil, activity, user)
114 def activity(conn, %{"uuid" => uuid}) do
115 if get_format(conn) == "activity+json" do
116 ActivityPubController.call(conn, :activity)
118 with id <- o_status_url(conn, :activity, uuid),
119 {_, %Activity{} = activity} <- {:activity, Activity.normalize(id)},
120 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
121 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
122 case format = get_format(conn) do
123 "html" -> redirect(conn, to: "/notice/#{activity.id}")
124 _ -> represent_activity(conn, format, activity, user)
139 def notice(conn, %{"id" => id}) do
140 with {_, %Activity{} = activity} <- {:activity, Repo.get(Activity, id)},
141 {_, true} <- {:public?, ActivityPub.is_public?(activity)},
142 %User{} = user <- User.get_cached_by_ap_id(activity.data["actor"]) do
143 case format = get_format(conn) do
146 |> put_resp_content_type("text/html")
147 |> send_file(200, Pleroma.Plugs.InstanceStatic.file_path("index.html"))
150 represent_activity(conn, format, activity, user)
164 defp represent_activity(
167 %Activity{data: %{"type" => "Create"}} = activity,
170 object = Object.normalize(activity.data["object"])
173 |> put_resp_header("content-type", "application/activity+json")
174 |> json(ObjectView.render("object.json", %{object: object}))
177 defp represent_activity(_conn, "activity+json", _, _) do
181 defp represent_activity(conn, _, activity, user) do
184 |> ActivityRepresenter.to_simple_form(user, true)
185 |> ActivityRepresenter.wrap_with_entry()
186 |> :xmerl.export_simple(:xmerl_xml)
190 |> put_resp_content_type("application/atom+xml")
191 |> send_resp(200, response)
194 def errors(conn, {:error, :not_found}) do
200 def errors(conn, _) do
203 |> text("Something went wrong")