Merge branch 'emoji-notifications' into 'develop'
[akkoma] / lib / pleroma / web / feed / feed_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Feed.FeedController do
6 use Pleroma.Web, :controller
7
8 alias Fallback.RedirectController
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.ActivityPub.ActivityPubController
12
13 plug(Pleroma.Plugs.SetFormatPlug when action in [:feed_redirect])
14
15 action_fallback(:errors)
16
17 def feed_redirect(%{assigns: %{format: "html"}} = conn, %{"nickname" => nickname}) do
18 with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname_or_id(nickname)} do
19 RedirectController.redirector_with_meta(conn, %{user: user})
20 end
21 end
22
23 def feed_redirect(%{assigns: %{format: format}} = conn, _params)
24 when format in ["json", "activity+json"] do
25 ActivityPubController.call(conn, :user)
26 end
27
28 def feed_redirect(conn, %{"nickname" => nickname}) do
29 with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname(nickname)} do
30 redirect(conn, external: "#{feed_url(conn, :feed, user.nickname)}.atom")
31 end
32 end
33
34 def feed(conn, %{"nickname" => nickname} = params) do
35 with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname(nickname)} do
36 activities =
37 %{
38 "type" => ["Create"],
39 "whole_db" => true,
40 "actor_id" => user.ap_id
41 }
42 |> Map.merge(Map.take(params, ["max_id"]))
43 |> ActivityPub.fetch_public_activities()
44
45 conn
46 |> put_resp_content_type("application/atom+xml")
47 |> render("feed.xml",
48 user: user,
49 activities: activities,
50 feed_config: Pleroma.Config.get([:feed])
51 )
52 end
53 end
54
55 def errors(conn, {:error, :not_found}) do
56 render_error(conn, :not_found, "Not found")
57 end
58
59 def errors(conn, {:fetch_user, nil}), do: errors(conn, {:error, :not_found})
60
61 def errors(conn, _) do
62 render_error(conn, :internal_server_error, "Something went wrong")
63 end
64 end