2019-10-07 06:20:41 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-01 22:08:45 -07:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-10-07 06:20:41 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-12-05 23:32:29 -07:00
|
|
|
defmodule Pleroma.Web.Feed.UserController do
|
2019-10-07 06:20:41 -06:00
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPubController
|
2019-12-05 23:32:29 -07:00
|
|
|
alias Pleroma.Web.Feed.FeedView
|
2019-10-07 06:20:41 -06:00
|
|
|
|
2020-06-24 00:26:17 -06:00
|
|
|
plug(Pleroma.Web.Plugs.SetFormatPlug when action in [:feed_redirect])
|
2019-10-07 06:20:41 -06:00
|
|
|
|
|
|
|
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
|
2020-06-23 12:10:32 -06:00
|
|
|
Pleroma.Web.Fallback.RedirectController.redirector_with_meta(conn, %{user: user})
|
2019-10-07 06:20:41 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def feed_redirect(%{assigns: %{format: format}} = conn, _params)
|
|
|
|
when format in ["json", "activity+json"] do
|
2020-03-09 11:51:44 -06:00
|
|
|
with %{halted: false} = conn <-
|
2020-06-24 01:55:56 -06:00
|
|
|
Pleroma.Web.Plugs.EnsureAuthenticatedPlug.call(conn,
|
2020-04-30 09:19:51 -06:00
|
|
|
unless_func: &Pleroma.Web.FederatingPlug.federating?/1
|
2020-03-09 11:51:44 -06:00
|
|
|
) do
|
|
|
|
ActivityPubController.call(conn, :user)
|
|
|
|
end
|
2019-10-07 06:20:41 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def feed_redirect(conn, %{"nickname" => nickname}) do
|
|
|
|
with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname(nickname)} do
|
2019-12-05 23:32:29 -07:00
|
|
|
redirect(conn, external: "#{user_feed_url(conn, :feed, user.nickname)}.atom")
|
2019-10-07 06:20:41 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-31 14:58:21 -06:00
|
|
|
def feed(conn, params) do
|
2020-09-01 09:43:44 -06:00
|
|
|
unless Pleroma.Config.restrict_unauthenticated_access?(:profiles, :local) do
|
2020-08-31 14:58:21 -06:00
|
|
|
render_feed(conn, params)
|
|
|
|
else
|
|
|
|
errors(conn, {:error, :not_found})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def render_feed(conn, %{"nickname" => nickname} = params) do
|
2020-03-13 08:41:26 -06:00
|
|
|
format = get_format(conn)
|
|
|
|
|
|
|
|
format =
|
|
|
|
if format in ["rss", "atom"] do
|
|
|
|
format
|
|
|
|
else
|
|
|
|
"atom"
|
|
|
|
end
|
|
|
|
|
2020-07-29 06:02:02 -06:00
|
|
|
with {_, %User{local: true} = user} <- {:fetch_user, User.get_cached_by_nickname(nickname)} do
|
2019-10-07 06:20:41 -06:00
|
|
|
activities =
|
2019-11-07 23:23:24 -07:00
|
|
|
%{
|
2020-06-04 11:33:16 -06:00
|
|
|
type: ["Create"],
|
|
|
|
actor_id: user.ap_id
|
2019-11-07 23:23:24 -07:00
|
|
|
}
|
2020-06-05 13:15:10 -06:00
|
|
|
|> Pleroma.Maps.put_if_present(:max_id, params["max_id"])
|
2020-05-25 14:27:47 -06:00
|
|
|
|> ActivityPub.fetch_public_or_unlisted_activities()
|
2019-10-07 06:20:41 -06:00
|
|
|
|
|
|
|
conn
|
2020-03-13 08:41:26 -06:00
|
|
|
|> put_resp_content_type("application/#{format}+xml")
|
2019-12-05 23:32:29 -07:00
|
|
|
|> put_view(FeedView)
|
2020-03-13 08:41:26 -06:00
|
|
|
|> render("user.#{format}",
|
2019-11-07 23:23:24 -07:00
|
|
|
user: user,
|
|
|
|
activities: activities,
|
|
|
|
feed_config: Pleroma.Config.get([:feed])
|
|
|
|
)
|
2019-10-07 06:20:41 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def errors(conn, {:error, :not_found}) do
|
|
|
|
render_error(conn, :not_found, "Not found")
|
|
|
|
end
|
|
|
|
|
2020-07-29 06:02:02 -06:00
|
|
|
def errors(conn, {:fetch_user, %User{local: false}}), do: errors(conn, {:error, :not_found})
|
2019-10-07 06:20:41 -06:00
|
|
|
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
|