user_controller.ex 2.67 KB
Newer Older
1
# Pleroma: A lightweight social networking server
2
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3
4
# SPDX-License-Identifier: AGPL-3.0-only

Maksim's avatar
Maksim committed
5
defmodule Pleroma.Web.Feed.UserController do
6
7
8
9
10
  use Pleroma.Web, :controller

  alias Pleroma.User
  alias Pleroma.Web.ActivityPub.ActivityPub
  alias Pleroma.Web.ActivityPub.ActivityPubController
Maksim's avatar
Maksim committed
11
  alias Pleroma.Web.Feed.FeedView
12
13
14
15
16
17
18

  plug(Pleroma.Plugs.SetFormatPlug when action in [:feed_redirect])

  action_fallback(:errors)

  def feed_redirect(%{assigns: %{format: "html"}} = conn, %{"nickname" => nickname}) do
    with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname_or_id(nickname)} do
Alexander Strizhakov's avatar
Alexander Strizhakov committed
19
      Pleroma.Web.Fallback.RedirectController.redirector_with_meta(conn, %{user: user})
20
21
22
23
24
    end
  end

  def feed_redirect(%{assigns: %{format: format}} = conn, _params)
      when format in ["json", "activity+json"] do
25
26
    with %{halted: false} = conn <-
           Pleroma.Plugs.EnsureAuthenticatedPlug.call(conn,
27
             unless_func: &Pleroma.Web.FederatingPlug.federating?/1
28
29
30
           ) do
      ActivityPubController.call(conn, :user)
    end
31
32
33
34
  end

  def feed_redirect(conn, %{"nickname" => nickname}) do
    with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname(nickname)} do
Maksim's avatar
Maksim committed
35
      redirect(conn, external: "#{user_feed_url(conn, :feed, user.nickname)}.atom")
36
37
38
    end
  end

39
  def feed(conn, params) do
40
    unless Pleroma.Config.restrict_unauthenticated_access?(:profiles, :local) do
41
42
43
44
45
46
47
      render_feed(conn, params)
    else
      errors(conn, {:error, :not_found})
    end
  end

  def render_feed(conn, %{"nickname" => nickname} = params) do
48
49
50
51
52
53
54
55
56
    format = get_format(conn)

    format =
      if format in ["rss", "atom"] do
        format
      else
        "atom"
      end

57
    with {_, %User{local: true} = user} <- {:fetch_user, User.get_cached_by_nickname(nickname)} do
58
      activities =
Maksim's avatar
Maksim committed
59
        %{
60
61
          type: ["Create"],
          actor_id: user.ap_id
Maksim's avatar
Maksim committed
62
        }
63
        |> Pleroma.Maps.put_if_present(:max_id, params["max_id"])
64
        |> ActivityPub.fetch_public_or_unlisted_activities()
65
66

      conn
67
      |> put_resp_content_type("application/#{format}+xml")
Maksim's avatar
Maksim committed
68
      |> put_view(FeedView)
69
      |> render("user.#{format}",
Maksim's avatar
Maksim committed
70
71
72
73
        user: user,
        activities: activities,
        feed_config: Pleroma.Config.get([:feed])
      )
74
75
76
77
78
79
80
    end
  end

  def errors(conn, {:error, :not_found}) do
    render_error(conn, :not_found, "Not found")
  end

81
  def errors(conn, {:fetch_user, %User{local: false}}), do: errors(conn, {:error, :not_found})
82
83
84
85
86
87
  def errors(conn, {:fetch_user, nil}), do: errors(conn, {:error, :not_found})

  def errors(conn, _) do
    render_error(conn, :internal_server_error, "Something went wrong")
  end
end