2019-09-26 05:53:42 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 07:49:20 +01:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-09-26 05:53:42 +02: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 11:50:52 +02:00
|
|
|
only: [add_link_headers: 2, add_link_headers: 3]
|
2019-09-26 05:53:42 +02:00
|
|
|
|
2020-08-14 19:55:45 +02:00
|
|
|
alias Pleroma.Config
|
2019-09-26 05:53:42 +02:00
|
|
|
alias Pleroma.Pagination
|
2019-10-10 21:35:32 +02:00
|
|
|
alias Pleroma.User
|
2019-09-26 05:53:42 +02:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
2020-06-24 09:53:10 +02:00
|
|
|
alias Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
|
2020-06-24 08:57:27 +02:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
2020-06-24 08:35:00 +02:00
|
|
|
alias Pleroma.Web.Plugs.RateLimiter
|
2019-09-26 05:53:42 +02:00
|
|
|
|
2020-05-11 13:24:59 +02:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
2020-04-24 15:52:38 +02:00
|
|
|
plug(:skip_plug, EnsurePublicOrAuthenticatedPlug when action in [:public, :hashtag])
|
|
|
|
|
2020-04-01 18:49:09 +02:00
|
|
|
# TODO: Replace with a macro when there is a Phoenix release with the following commit in it:
|
2020-03-01 00:03:46 +01:00
|
|
|
# https://github.com/phoenixframework/phoenix/commit/2e8c63c01fec4dde5467dbbbf9705ff9e780735e
|
|
|
|
|
2020-02-28 15:44:59 +01: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)
|
|
|
|
|
2019-10-02 19:42:40 +02:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:home, :direct])
|
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list)
|
|
|
|
|
2020-04-22 17:50:25 +02:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["read:statuses"], fallback: :proceed_unauthenticated}
|
|
|
|
when action in [:public, :hashtag]
|
|
|
|
)
|
|
|
|
|
2019-09-26 05:53:42 +02:00
|
|
|
plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
|
|
|
|
|
2020-05-11 13:24:59 +02:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TimelineOperation
|
|
|
|
|
2019-09-26 05:53:42 +02:00
|
|
|
# GET /api/v1/timelines/home
|
|
|
|
def home(%{assigns: %{user: user}} = conn, params) do
|
|
|
|
params =
|
|
|
|
params
|
2020-06-04 19:33:16 +02: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 12:10:09 +02:00
|
|
|
|> Map.put(:announce_filtering_user, user)
|
2020-06-04 19:33:16 +02:00
|
|
|
|> Map.put(:user, user)
|
2021-01-26 09:58:54 +01:00
|
|
|
|> Map.put(:local_only, params[:local])
|
|
|
|
|> Map.delete(:local)
|
2019-09-26 05:53:42 +02:00
|
|
|
|
|
|
|
activities =
|
2020-05-07 21:52:45 +02:00
|
|
|
[user.ap_id | User.following(user)]
|
2019-09-26 05:53:42 +02:00
|
|
|
|> ActivityPub.fetch_activities(params)
|
|
|
|
|> Enum.reverse()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(activities)
|
2020-04-01 18:49:09 +02:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-11-16 19:23:25 +01:00
|
|
|
as: :activity,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
2020-04-01 18:49:09 +02:00
|
|
|
)
|
2019-09-26 05:53:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/timelines/direct
|
|
|
|
def direct(%{assigns: %{user: user}} = conn, params) do
|
|
|
|
params =
|
|
|
|
params
|
2020-06-04 19:33:16 +02:00
|
|
|
|> Map.put(:type, "Create")
|
|
|
|
|> Map.put(:blocking_user, user)
|
|
|
|
|> Map.put(:user, user)
|
2019-09-26 05:53:42 +02:00
|
|
|
|> Map.put(:visibility, "direct")
|
|
|
|
|
|
|
|
activities =
|
|
|
|
[user.ap_id]
|
|
|
|
|> ActivityPub.fetch_activities_query(params)
|
|
|
|
|> Pagination.fetch_paginated(params)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(activities)
|
2020-04-01 18:49:09 +02:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-05-09 17:05:44 +02:00
|
|
|
as: :activity
|
2020-04-01 18:49:09 +02:00
|
|
|
)
|
2019-09-26 05:53:42 +02:00
|
|
|
end
|
|
|
|
|
2020-07-08 11:54:23 +02:00
|
|
|
defp restrict_unauthenticated?(true = _local_only) do
|
2020-08-14 19:55:45 +02:00
|
|
|
Config.restrict_unauthenticated_access?(:timelines, :local)
|
2020-07-08 11:36:44 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
defp restrict_unauthenticated?(_) do
|
2020-08-14 19:55:45 +02:00
|
|
|
Config.restrict_unauthenticated_access?(:timelines, :federated)
|
2020-07-07 19:37:11 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/timelines/public
|
|
|
|
def public(%{assigns: %{user: user}} = conn, params) do
|
|
|
|
local_only = params[:local]
|
2020-03-20 11:04:37 +01:00
|
|
|
|
2020-07-07 19:37:11 +02:00
|
|
|
if is_nil(user) and restrict_unauthenticated?(local_only) do
|
|
|
|
fail_on_bad_auth(conn)
|
2020-04-22 17:50:25 +02:00
|
|
|
else
|
2020-03-20 11:04:37 +01:00
|
|
|
activities =
|
|
|
|
params
|
2020-06-04 19:33:16 +02: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-02 04:12:59 +02:00
|
|
|
|> Map.put(:instance, params[:instance])
|
2020-03-20 11:04:37 +01:00
|
|
|
|> ActivityPub.fetch_public_activities()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(activities, %{"local" => local_only})
|
2020-04-01 18:49:09 +02:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-11-16 19:23:25 +01:00
|
|
|
as: :activity,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
2020-04-01 18:49:09 +02:00
|
|
|
)
|
2020-03-20 11:04:37 +01:00
|
|
|
end
|
2019-09-26 05:53:42 +02:00
|
|
|
end
|
|
|
|
|
2020-07-07 19:37:11 +02:00
|
|
|
defp fail_on_bad_auth(conn) do
|
|
|
|
render_error(conn, :unauthorized, "authorization required for timeline view")
|
|
|
|
end
|
|
|
|
|
2020-04-22 17:50:25 +02:00
|
|
|
defp hashtag_fetching(params, user, local_only) do
|
2019-09-26 05:53:42 +02:00
|
|
|
tags =
|
2020-06-04 19:33:16 +02:00
|
|
|
[params[:tag], params[:any]]
|
2019-09-26 05:53:42 +02:00
|
|
|
|> List.flatten()
|
2020-06-04 19:33:16 +02:00
|
|
|
|> Enum.reject(&is_nil/1)
|
|
|
|
|> Enum.map(&String.downcase/1)
|
2021-01-28 22:17:33 +01:00
|
|
|
|> Enum.uniq()
|
2019-09-26 05:53:42 +02:00
|
|
|
|
|
|
|
tag_all =
|
|
|
|
params
|
2020-06-04 19:33:16 +02:00
|
|
|
|> Map.get(:all, [])
|
|
|
|
|> Enum.map(&String.downcase/1)
|
2019-09-26 05:53:42 +02:00
|
|
|
|
|
|
|
tag_reject =
|
|
|
|
params
|
2020-06-04 19:33:16 +02:00
|
|
|
|> Map.get(:none, [])
|
|
|
|
|> Enum.map(&String.downcase/1)
|
2019-09-26 05:53:42 +02:00
|
|
|
|
2020-01-14 17:24:26 +01:00
|
|
|
_activities =
|
2019-09-26 05:53:42 +02:00
|
|
|
params
|
2020-06-04 19:33:16 +02:00
|
|
|
|> 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)
|
|
|
|
|> Map.put(:tag_all, tag_all)
|
|
|
|
|> Map.put(:tag_reject, tag_reject)
|
2019-09-26 05:53:42 +02:00
|
|
|
|> ActivityPub.fetch_public_activities()
|
2020-01-14 17:24:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/timelines/tag/:tag
|
|
|
|
def hashtag(%{assigns: %{user: user}} = conn, params) do
|
2020-06-04 19:33:16 +02:00
|
|
|
local_only = params[:local]
|
2019-09-26 05:53:42 +02:00
|
|
|
|
2020-07-07 19:37:11 +02:00
|
|
|
if is_nil(user) and restrict_unauthenticated?(local_only) do
|
|
|
|
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 19:23:25 +01:00
|
|
|
as: :activity,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
2020-07-07 19:37:11 +02:00
|
|
|
)
|
|
|
|
end
|
2019-09-26 05:53:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/timelines/list/:list_id
|
2020-05-11 13:24:59 +02:00
|
|
|
def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
|
2019-09-26 05:53:42 +02:00
|
|
|
with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
|
|
|
|
params =
|
|
|
|
params
|
2020-08-26 15:37:30 +02:00
|
|
|
|> Map.put(:type, "Create")
|
|
|
|
|> Map.put(:blocking_user, user)
|
|
|
|
|> Map.put(:user, user)
|
|
|
|
|> Map.put(:muting_user, user)
|
2021-01-26 14:55:44 +01:00
|
|
|
|> Map.put(:local_only, params[:local])
|
2019-09-26 05:53:42 +02: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 21:35:32 +02:00
|
|
|
|
|
|
|
user_following = User.following(user)
|
|
|
|
|
2019-09-26 05:53:42 +02:00
|
|
|
activities =
|
|
|
|
following
|
2019-10-10 21:35:32 +02:00
|
|
|
|> Enum.filter(fn x -> x in user_following end)
|
2019-09-26 05:53:42 +02:00
|
|
|
|> ActivityPub.fetch_activities_bounded(following, params)
|
|
|
|
|> Enum.reverse()
|
|
|
|
|
2020-04-01 18:49:09 +02:00
|
|
|
render(conn, "index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-11-16 19:23:25 +01:00
|
|
|
as: :activity,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
2020-04-01 18:49:09 +02:00
|
|
|
)
|
2019-09-26 05:53:42 +02:00
|
|
|
else
|
|
|
|
_e -> render_error(conn, :forbidden, "Error.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|