Fix tagpolicy to also work with Update
[akkoma] / lib / pleroma / web / feed / user_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Feed.UserController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Config
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.ActivityPub.ActivityPubController
12 alias Pleroma.Web.Feed.FeedView
13
14 plug(Pleroma.Web.Plugs.SetFormatPlug when action in [:feed_redirect])
15
16 action_fallback(:errors)
17
18 def feed_redirect(%{assigns: %{format: "html"}} = conn, %{"nickname" => nickname}) do
19 with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname_or_id(nickname)} do
20 Pleroma.Web.Fallback.RedirectController.redirector_with_meta(conn, %{user: user})
21 else
22 _ -> Pleroma.Web.Fallback.RedirectController.redirector(conn, nil)
23 end
24 end
25
26 def feed_redirect(%{assigns: %{format: format}} = conn, _params)
27 when format in ["json", "activity+json"] do
28 ActivityPubController.call(conn, :user)
29 end
30
31 def feed_redirect(conn, %{"nickname" => nickname}) do
32 with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname(nickname)} do
33 redirect(conn, external: "#{Routes.user_feed_url(conn, :feed, user.nickname)}.atom")
34 end
35 end
36
37 def feed(conn, %{"nickname" => nickname} = params) do
38 format = get_format(conn)
39
40 format =
41 if format in ["atom", "rss"] do
42 format
43 else
44 "atom"
45 end
46
47 with {_, %User{local: true} = user} <- {:fetch_user, User.get_cached_by_nickname(nickname)},
48 {_, :visible} <- {:visibility, User.visible_for(user, _reading_user = nil)} do
49 activities =
50 %{
51 type: ["Create"],
52 actor_id: user.ap_id
53 }
54 |> Pleroma.Maps.put_if_present(:max_id, params["max_id"])
55 |> ActivityPub.fetch_public_or_unlisted_activities()
56
57 conn
58 |> put_resp_content_type("application/#{format}+xml")
59 |> put_view(FeedView)
60 |> render("user.#{format}",
61 user: user,
62 activities: activities,
63 feed_config: Config.get([:feed]),
64 view_module: FeedView
65 )
66 end
67 end
68
69 def errors(conn, {:error, :not_found}) do
70 render_error(conn, :not_found, "Not found")
71 end
72
73 def errors(conn, {:fetch_user, %User{local: false}}), do: errors(conn, {:error, :not_found})
74 def errors(conn, {:fetch_user, nil}), do: errors(conn, {:error, :not_found})
75
76 def errors(conn, {:visibility, _}), do: errors(conn, {:error, :not_found})
77
78 def errors(conn, _) do
79 render_error(conn, :internal_server_error, "Something went wrong")
80 end
81 end