2019-09-25 21:53:42 -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-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-04-01 10:49:09 -06:00
|
|
|
only: [add_link_headers: 2, add_link_headers: 3, truthy_param?: 1, skip_relationships?: 1]
|
2019-09-25 21:53:42 -06:00
|
|
|
|
|
|
|
alias Pleroma.Pagination
|
2019-10-02 11:42:40 -06:00
|
|
|
alias Pleroma.Plugs.OAuthScopesPlug
|
2020-02-28 07:44:59 -07:00
|
|
|
alias Pleroma.Plugs.RateLimiter
|
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-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)
|
|
|
|
|
2019-10-02 11:42:40 -06:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:home, :direct])
|
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list)
|
|
|
|
|
2020-03-20 04:04:37 -06:00
|
|
|
plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug when action != :public)
|
2019-10-02 11:42:40 -06:00
|
|
|
|
2019-09-25 21:53:42 -06:00
|
|
|
plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
|
|
|
|
|
|
|
|
# GET /api/v1/timelines/home
|
|
|
|
def home(%{assigns: %{user: user}} = conn, params) do
|
|
|
|
params =
|
|
|
|
params
|
|
|
|
|> Map.put("type", ["Create", "Announce"])
|
|
|
|
|> Map.put("blocking_user", user)
|
|
|
|
|> Map.put("muting_user", user)
|
2020-04-24 10:25:26 -06:00
|
|
|
|> Map.put("reply_filtering_user", user)
|
2019-09-25 21:53:42 -06:00
|
|
|
|> Map.put("user", user)
|
|
|
|
|
2019-10-10 13:35:32 -06:00
|
|
|
recipients = [user.ap_id | User.following(user)]
|
2019-09-25 21:53:42 -06:00
|
|
|
|
|
|
|
activities =
|
|
|
|
recipients
|
|
|
|
|> ActivityPub.fetch_activities(params)
|
|
|
|
|> Enum.reverse()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(activities)
|
2020-04-01 10:49:09 -06:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
|
|
|
as: :activity,
|
|
|
|
skip_relationships: skip_relationships?(params)
|
|
|
|
)
|
2019-09-25 21:53:42 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/timelines/direct
|
|
|
|
def direct(%{assigns: %{user: user}} = conn, params) do
|
|
|
|
params =
|
|
|
|
params
|
|
|
|
|> Map.put("type", "Create")
|
|
|
|
|> Map.put("blocking_user", user)
|
|
|
|
|> Map.put("user", user)
|
|
|
|
|> 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 10:49:09 -06:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
|
|
|
as: :activity,
|
|
|
|
skip_relationships: skip_relationships?(params)
|
|
|
|
)
|
2019-09-25 21:53:42 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/timelines/public
|
|
|
|
def public(%{assigns: %{user: user}} = conn, params) do
|
|
|
|
local_only = truthy_param?(params["local"])
|
|
|
|
|
2020-03-20 04:04:37 -06:00
|
|
|
cfg_key =
|
|
|
|
if local_only do
|
|
|
|
:local
|
|
|
|
else
|
|
|
|
:federated
|
|
|
|
end
|
|
|
|
|
|
|
|
restrict? = Pleroma.Config.get([:restrict_unauthenticated, :timelines, cfg_key])
|
|
|
|
|
|
|
|
if not (restrict? and is_nil(user)) do
|
|
|
|
activities =
|
|
|
|
params
|
|
|
|
|> Map.put("type", ["Create", "Announce"])
|
|
|
|
|> Map.put("local_only", local_only)
|
|
|
|
|> Map.put("blocking_user", user)
|
|
|
|
|> Map.put("muting_user", user)
|
2020-04-24 10:25:26 -06:00
|
|
|
|> Map.put("reply_filtering_user", user)
|
2020-03-20 04:04:37 -06:00
|
|
|
|> ActivityPub.fetch_public_activities()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(activities, %{"local" => local_only})
|
2020-04-01 10:49:09 -06:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
|
|
|
as: :activity,
|
|
|
|
skip_relationships: skip_relationships?(params)
|
|
|
|
)
|
2020-03-20 04:04:37 -06:00
|
|
|
else
|
|
|
|
render_error(conn, :unauthorized, "authorization required for timeline view")
|
|
|
|
end
|
2019-09-25 21:53:42 -06:00
|
|
|
end
|
|
|
|
|
2020-01-14 09:24:26 -07:00
|
|
|
def hashtag_fetching(params, user, local_only) do
|
2019-09-25 21:53:42 -06:00
|
|
|
tags =
|
|
|
|
[params["tag"], params["any"]]
|
|
|
|
|> List.flatten()
|
|
|
|
|> Enum.uniq()
|
|
|
|
|> Enum.filter(& &1)
|
|
|
|
|> Enum.map(&String.downcase(&1))
|
|
|
|
|
|
|
|
tag_all =
|
|
|
|
params
|
|
|
|
|> Map.get("all", [])
|
|
|
|
|> Enum.map(&String.downcase(&1))
|
|
|
|
|
|
|
|
tag_reject =
|
|
|
|
params
|
|
|
|
|> Map.get("none", [])
|
|
|
|
|> Enum.map(&String.downcase(&1))
|
|
|
|
|
2020-01-14 09:24:26 -07:00
|
|
|
_activities =
|
2019-09-25 21:53:42 -06:00
|
|
|
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)
|
|
|
|
|> 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
|
|
|
|
local_only = truthy_param?(params["local"])
|
|
|
|
|
|
|
|
activities = hashtag_fetching(params, user, local_only)
|
2019-09-25 21:53:42 -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,
|
|
|
|
as: :activity,
|
|
|
|
skip_relationships: skip_relationships?(params)
|
|
|
|
)
|
2019-09-25 21:53:42 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /api/v1/timelines/list/:list_id
|
|
|
|
def list(%{assigns: %{user: user}} = conn, %{"list_id" => id} = params) do
|
|
|
|
with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
|
|
|
|
params =
|
|
|
|
params
|
|
|
|
|> Map.put("type", "Create")
|
|
|
|
|> Map.put("blocking_user", user)
|
|
|
|
|> Map.put("user", user)
|
|
|
|
|> Map.put("muting_user", user)
|
|
|
|
|
|
|
|
# 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()
|
|
|
|
|
2020-04-01 10:49:09 -06:00
|
|
|
render(conn, "index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
|
|
|
as: :activity,
|
|
|
|
skip_relationships: skip_relationships?(params)
|
|
|
|
)
|
2019-09-25 21:53:42 -06:00
|
|
|
else
|
|
|
|
_e -> render_error(conn, :forbidden, "Error.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|