2019-09-25 21:53:42 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-09-25 21:53:42 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.TimelineController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
import Pleroma.Web.ControllerHelper,
|
2020-05-13 03:50:52 -06:00
|
|
|
only: [add_link_headers: 2, add_link_headers: 3]
|
2019-09-25 21:53:42 -06:00
|
|
|
|
2020-08-14 11:55:45 -06:00
|
|
|
alias Pleroma.Config
|
2019-09-25 21:53:42 -06:00
|
|
|
alias Pleroma.Pagination
|
2019-10-10 13:35:32 -06:00
|
|
|
alias Pleroma.User
|
2019-09-25 21:53:42 -06:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
2020-06-24 00:57:27 -06:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
2020-06-24 00:35:00 -06:00
|
|
|
alias Pleroma.Web.Plugs.RateLimiter
|
2019-09-25 21:53:42 -06:00
|
|
|
|
2020-05-11 05:24:59 -06:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
2023-03-17 09:33:28 -06:00
|
|
|
plug(:skip_public_check when action in [:public, :hashtag, :bubble])
|
2020-04-24 07:52:38 -06:00
|
|
|
|
2020-04-01 10:49:09 -06:00
|
|
|
# TODO: Replace with a macro when there is a Phoenix release with the following commit in it:
|
2020-02-29 16:03:46 -07:00
|
|
|
# https://github.com/phoenixframework/phoenix/commit/2e8c63c01fec4dde5467dbbbf9705ff9e780735e
|
|
|
|
|
2020-02-28 07:44:59 -07:00
|
|
|
plug(RateLimiter, [name: :timeline, bucket_name: :direct_timeline] when action == :direct)
|
|
|
|
plug(RateLimiter, [name: :timeline, bucket_name: :public_timeline] when action == :public)
|
|
|
|
plug(RateLimiter, [name: :timeline, bucket_name: :home_timeline] when action == :home)
|
|
|
|
plug(RateLimiter, [name: :timeline, bucket_name: :hashtag_timeline] when action == :hashtag)
|
|
|
|
plug(RateLimiter, [name: :timeline, bucket_name: :list_timeline] when action == :list)
|
2022-07-22 08:55:38 -06:00
|
|
|
plug(RateLimiter, [name: :timeline, bucket_name: :bubble_timeline] when action == :bubble)
|
2020-02-28 07:44:59 -07:00
|
|
|
|
2023-03-17 09:33:28 -06:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:home, :direct])
|
2019-10-02 11:42:40 -06:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list)
|
|
|
|
|
2020-04-22 09:50:25 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["read:statuses"], fallback: :proceed_unauthenticated}
|
2023-03-17 09:33:28 -06:00
|
|
|
when action in [:public, :hashtag, :bubble]
|
2020-04-22 09:50:25 -06:00
|
|
|
)
|
|
|
|
|
2023-03-29 05:01:16 -06:00
|
|
|
require Logger
|
|
|
|
|
2020-05-11 05:24:59 -06:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TimelineOperation
|
|
|
|
|
2019-09-25 21:53:42 -06:00
|
|
|
# GET /api/v1/timelines/home
|
|
|
|
def home(%{assigns: %{user: user}} = conn, params) do
|
2023-03-29 05:01:16 -06:00
|
|
|
%{nickname: nickname} = user
|
|
|
|
|
|
|
|
Logger.debug("TimelineController.home: #{nickname}")
|
|
|
|
|
2022-12-05 05:58:48 -07:00
|
|
|
followed_hashtags =
|
|
|
|
user
|
|
|
|
|> User.followed_hashtags()
|
|
|
|
|> Enum.map(& &1.id)
|
|
|
|
|
2019-09-25 21:53:42 -06:00
|
|
|
params =
|
|
|
|
params
|
2020-06-04 11:33:16 -06:00
|
|
|
|> Map.put(:type, ["Create", "Announce"])
|
|
|
|
|> Map.put(:blocking_user, user)
|
|
|
|
|> Map.put(:muting_user, user)
|
|
|
|
|> Map.put(:reply_filtering_user, user)
|
2020-06-10 04:10:09 -06:00
|
|
|
|> Map.put(:announce_filtering_user, user)
|
2020-06-04 11:33:16 -06:00
|
|
|
|> Map.put(:user, user)
|
2021-01-26 01:58:54 -07:00
|
|
|
|> Map.put(:local_only, params[:local])
|
2022-12-05 05:58:48 -07:00
|
|
|
|> Map.put(:followed_hashtags, followed_hashtags)
|
2021-01-26 01:58:54 -07:00
|
|
|
|> Map.delete(:local)
|
2019-09-25 21:53:42 -06:00
|
|
|
|
2023-03-29 05:01:16 -06:00
|
|
|
Logger.debug("TimelineController.home: #{nickname} - fetching activities")
|
|
|
|
|
2019-09-25 21:53:42 -06:00
|
|
|
activities =
|
2020-05-07 13:52:45 -06:00
|
|
|
[user.ap_id | User.following(user)]
|
2019-09-25 21:53:42 -06:00
|
|
|
|> ActivityPub.fetch_activities(params)
|
|
|
|
|> Enum.reverse()
|
|
|
|
|
2023-03-29 05:01:16 -06:00
|
|
|
Logger.debug("TimelineController.home: #{nickname} - rendering")
|
|
|
|
|
2019-09-25 21:53:42 -06:00
|
|
|
conn
|
|
|
|
|> add_link_headers(activities)
|
2020-04-01 10:49:09 -06:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-11-16 11:23:25 -07:00
|
|
|
as: :activity,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
2020-04-01 10:49:09 -06:00
|
|
|
)
|
2019-09-25 21:53:42 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/timelines/direct
|
|
|
|
def direct(%{assigns: %{user: user}} = conn, params) do
|
2023-03-29 05:01:16 -06:00
|
|
|
Logger.debug("TimelineController.direct: #{user.nickname}")
|
|
|
|
|
2019-09-25 21:53:42 -06:00
|
|
|
params =
|
|
|
|
params
|
2020-06-04 11:33:16 -06:00
|
|
|
|> Map.put(:type, "Create")
|
|
|
|
|> Map.put(:blocking_user, user)
|
|
|
|
|> Map.put(:user, user)
|
2019-09-25 21:53:42 -06:00
|
|
|
|> Map.put(:visibility, "direct")
|
|
|
|
|
2023-03-29 05:01:16 -06:00
|
|
|
Logger.debug("TimelineController.direct: #{user.nickname} - fetching activities")
|
|
|
|
|
2019-09-25 21:53:42 -06:00
|
|
|
activities =
|
|
|
|
[user.ap_id]
|
|
|
|
|> ActivityPub.fetch_activities_query(params)
|
|
|
|
|> Pagination.fetch_paginated(params)
|
|
|
|
|
2023-03-29 05:01:16 -06:00
|
|
|
Logger.debug("TimelineController.direct: #{user.nickname} - rendering")
|
|
|
|
|
2019-09-25 21:53:42 -06:00
|
|
|
conn
|
|
|
|
|> add_link_headers(activities)
|
2020-04-01 10:49:09 -06:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-05-09 09:05:44 -06:00
|
|
|
as: :activity
|
2020-04-01 10:49:09 -06:00
|
|
|
)
|
2019-09-25 21:53:42 -06:00
|
|
|
end
|
|
|
|
|
2023-03-17 09:33:28 -06:00
|
|
|
defp restrict_unauthenticated?(type) do
|
|
|
|
Config.restrict_unauthenticated_access?(:timelines, type)
|
2020-07-07 11:37:11 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/timelines/public
|
|
|
|
def public(%{assigns: %{user: user}} = conn, params) do
|
2023-03-29 05:01:16 -06:00
|
|
|
Logger.debug("TimelineController.public")
|
2020-07-07 11:37:11 -06:00
|
|
|
local_only = params[:local]
|
2023-03-17 09:33:28 -06:00
|
|
|
timeline_type = if local_only, do: :local, else: :federated
|
2020-03-20 04:04:37 -06:00
|
|
|
|
2023-03-17 09:33:28 -06:00
|
|
|
with {:enabled, true} <-
|
|
|
|
{:enabled, local_only || Config.get([:instance, :federated_timeline_available], true)},
|
|
|
|
{:authenticated, true} <-
|
|
|
|
{:authenticated, !(is_nil(user) and restrict_unauthenticated?(timeline_type))} do
|
2023-03-29 05:01:16 -06:00
|
|
|
Logger.debug("TimelineController.public: fetching activities")
|
|
|
|
|
2020-03-20 04:04:37 -06:00
|
|
|
activities =
|
|
|
|
params
|
2020-06-04 11:33:16 -06:00
|
|
|
|> Map.put(:type, ["Create"])
|
|
|
|
|> Map.put(:local_only, local_only)
|
|
|
|
|> Map.put(:blocking_user, user)
|
|
|
|
|> Map.put(:muting_user, user)
|
|
|
|
|> Map.put(:reply_filtering_user, user)
|
2020-07-01 20:12:59 -06:00
|
|
|
|> Map.put(:instance, params[:instance])
|
2022-08-02 08:46:46 -06:00
|
|
|
# Restricts unfederated content to authenticated users
|
|
|
|
|> Map.put(:includes_local_public, not is_nil(user))
|
2020-03-20 04:04:37 -06:00
|
|
|
|> ActivityPub.fetch_public_activities()
|
|
|
|
|
2023-03-29 05:01:16 -06:00
|
|
|
Logger.debug("TimelineController.public: rendering")
|
|
|
|
|
2020-03-20 04:04:37 -06:00
|
|
|
conn
|
|
|
|
|> add_link_headers(activities, %{"local" => local_only})
|
2020-04-01 10:49:09 -06:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-11-16 11:23:25 -07:00
|
|
|
as: :activity,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
2020-04-01 10:49:09 -06:00
|
|
|
)
|
2023-03-17 09:33:28 -06:00
|
|
|
else
|
|
|
|
{:enabled, false} ->
|
|
|
|
conn
|
|
|
|
|> put_status(404)
|
|
|
|
|> json(%{error: "Federated timeline is disabled"})
|
|
|
|
|
|
|
|
{:authenticated, false} ->
|
|
|
|
fail_on_bad_auth(conn)
|
2020-03-20 04:04:37 -06:00
|
|
|
end
|
2019-09-25 21:53:42 -06:00
|
|
|
end
|
|
|
|
|
2022-07-22 08:55:38 -06:00
|
|
|
# GET /api/v1/timelines/bubble
|
|
|
|
def bubble(%{assigns: %{user: user}} = conn, params) do
|
2023-03-29 05:01:16 -06:00
|
|
|
Logger.debug("TimelineController.bubble")
|
|
|
|
|
2023-03-17 09:33:28 -06:00
|
|
|
if is_nil(user) and restrict_unauthenticated?(:bubble) do
|
2022-07-22 08:55:38 -06:00
|
|
|
fail_on_bad_auth(conn)
|
|
|
|
else
|
2023-03-17 09:33:28 -06:00
|
|
|
bubble_instances =
|
|
|
|
Enum.uniq(
|
|
|
|
Config.get([:instance, :local_bubble], []) ++
|
|
|
|
[Pleroma.Web.Endpoint.host()]
|
|
|
|
)
|
|
|
|
|
2023-03-29 05:01:16 -06:00
|
|
|
Logger.debug("TimelineController.bubble: fetching activities")
|
|
|
|
|
2022-07-22 08:55:38 -06:00
|
|
|
activities =
|
|
|
|
params
|
|
|
|
|> Map.put(:type, ["Create"])
|
|
|
|
|> Map.put(:blocking_user, user)
|
|
|
|
|> Map.put(:muting_user, user)
|
|
|
|
|> Map.put(:reply_filtering_user, user)
|
|
|
|
|> Map.put(:instance, bubble_instances)
|
|
|
|
|> ActivityPub.fetch_public_activities()
|
|
|
|
|
2023-03-29 05:01:16 -06:00
|
|
|
Logger.debug("TimelineController.bubble: rendering")
|
|
|
|
|
2022-07-22 08:55:38 -06:00
|
|
|
conn
|
|
|
|
|> add_link_headers(activities)
|
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
|
|
|
as: :activity,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-07 11:37:11 -06:00
|
|
|
defp fail_on_bad_auth(conn) do
|
|
|
|
render_error(conn, :unauthorized, "authorization required for timeline view")
|
|
|
|
end
|
|
|
|
|
2020-04-22 09:50:25 -06:00
|
|
|
defp hashtag_fetching(params, user, local_only) do
|
2021-03-07 01:33:21 -07:00
|
|
|
# Note: not sanitizing tag options at this stage (may be mix-cased, have duplicates etc.)
|
|
|
|
tags_any =
|
2020-06-04 11:33:16 -06:00
|
|
|
[params[:tag], params[:any]]
|
2019-09-25 21:53:42 -06:00
|
|
|
|> List.flatten()
|
2021-03-07 01:33:21 -07:00
|
|
|
|> Enum.filter(& &1)
|
|
|
|
|
|
|
|
tag_all = Map.get(params, :all, [])
|
|
|
|
tag_reject = Map.get(params, :none, [])
|
|
|
|
|
|
|
|
params
|
|
|
|
|> Map.put(:type, "Create")
|
|
|
|
|> Map.put(:local_only, local_only)
|
|
|
|
|> Map.put(:blocking_user, user)
|
|
|
|
|> Map.put(:muting_user, user)
|
|
|
|
|> Map.put(:user, user)
|
|
|
|
|> Map.put(:tag, tags_any)
|
|
|
|
|> Map.put(:tag_all, tag_all)
|
|
|
|
|> Map.put(:tag_reject, tag_reject)
|
|
|
|
|> ActivityPub.fetch_public_activities()
|
2020-01-14 09:24:26 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/timelines/tag/:tag
|
|
|
|
def hashtag(%{assigns: %{user: user}} = conn, params) do
|
2020-06-04 11:33:16 -06:00
|
|
|
local_only = params[:local]
|
2019-09-25 21:53:42 -06:00
|
|
|
|
2023-03-17 09:33:28 -06:00
|
|
|
if is_nil(user) and restrict_unauthenticated?(if local_only, do: :local, else: :federated) do
|
2020-07-07 11:37:11 -06:00
|
|
|
fail_on_bad_auth(conn)
|
|
|
|
else
|
|
|
|
activities = hashtag_fetching(params, user, local_only)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(activities, %{"local" => local_only})
|
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-11-16 11:23:25 -07:00
|
|
|
as: :activity,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
2020-07-07 11:37:11 -06:00
|
|
|
)
|
|
|
|
end
|
2019-09-25 21:53:42 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/timelines/list/:list_id
|
2020-05-11 05:24:59 -06:00
|
|
|
def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
|
2019-09-25 21:53:42 -06:00
|
|
|
with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
|
|
|
|
params =
|
|
|
|
params
|
2020-08-26 07:37:30 -06:00
|
|
|
|> Map.put(:type, "Create")
|
|
|
|
|> Map.put(:blocking_user, user)
|
|
|
|
|> Map.put(:user, user)
|
|
|
|
|> Map.put(:muting_user, user)
|
2021-01-26 06:55:44 -07:00
|
|
|
|> Map.put(:local_only, params[:local])
|
2019-09-25 21:53:42 -06:00
|
|
|
|
|
|
|
# we must filter the following list for the user to avoid leaking statuses the user
|
|
|
|
# does not actually have permission to see (for more info, peruse security issue #270).
|
2019-10-10 13:35:32 -06:00
|
|
|
|
|
|
|
user_following = User.following(user)
|
|
|
|
|
2019-09-25 21:53:42 -06:00
|
|
|
activities =
|
|
|
|
following
|
2019-10-10 13:35:32 -06:00
|
|
|
|> Enum.filter(fn x -> x in user_following end)
|
2019-09-25 21:53:42 -06:00
|
|
|
|> ActivityPub.fetch_activities_bounded(following, params)
|
|
|
|
|> Enum.reverse()
|
|
|
|
|
2021-07-02 07:03:41 -06:00
|
|
|
conn
|
|
|
|
|> add_link_headers(activities)
|
|
|
|
|> render("index.json",
|
2020-04-01 10:49:09 -06:00
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-11-16 11:23:25 -07:00
|
|
|
as: :activity,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
2020-04-01 10:49:09 -06:00
|
|
|
)
|
2019-09-25 21:53:42 -06:00
|
|
|
else
|
|
|
|
_e -> render_error(conn, :forbidden, "Error.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|