Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.5KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.MastodonAPI.TimelineController do
  5. use Pleroma.Web, :controller
  6. import Pleroma.Web.ControllerHelper,
  7. only: [add_link_headers: 2, add_link_headers: 3]
  8. alias Pleroma.Config
  9. alias Pleroma.Pagination
  10. alias Pleroma.User
  11. alias Pleroma.Web.ActivityPub.ActivityPub
  12. alias Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
  13. alias Pleroma.Web.Plugs.OAuthScopesPlug
  14. alias Pleroma.Web.Plugs.RateLimiter
  15. plug(Pleroma.Web.ApiSpec.CastAndValidate)
  16. plug(:skip_plug, EnsurePublicOrAuthenticatedPlug when action in [:public, :hashtag])
  17. # TODO: Replace with a macro when there is a Phoenix release with the following commit in it:
  18. # https://github.com/phoenixframework/phoenix/commit/2e8c63c01fec4dde5467dbbbf9705ff9e780735e
  19. plug(RateLimiter, [name: :timeline, bucket_name: :direct_timeline] when action == :direct)
  20. plug(RateLimiter, [name: :timeline, bucket_name: :public_timeline] when action == :public)
  21. plug(RateLimiter, [name: :timeline, bucket_name: :home_timeline] when action == :home)
  22. plug(RateLimiter, [name: :timeline, bucket_name: :hashtag_timeline] when action == :hashtag)
  23. plug(RateLimiter, [name: :timeline, bucket_name: :list_timeline] when action == :list)
  24. plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:home, :direct])
  25. plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list)
  26. plug(
  27. OAuthScopesPlug,
  28. %{scopes: ["read:statuses"], fallback: :proceed_unauthenticated}
  29. when action in [:public, :hashtag]
  30. )
  31. plug(:put_view, Pleroma.Web.MastodonAPI.StatusView)
  32. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TimelineOperation
  33. # GET /api/v1/timelines/home
  34. def home(%{assigns: %{user: user}} = conn, params) do
  35. params =
  36. params
  37. |> Map.put(:type, ["Create", "Announce"])
  38. |> Map.put(:blocking_user, user)
  39. |> Map.put(:muting_user, user)
  40. |> Map.put(:reply_filtering_user, user)
  41. |> Map.put(:announce_filtering_user, user)
  42. |> Map.put(:user, user)
  43. |> Map.put(:local_only, params[:local])
  44. |> Map.delete(:local)
  45. activities =
  46. [user.ap_id | User.following(user)]
  47. |> ActivityPub.fetch_activities(params)
  48. |> Enum.reverse()
  49. conn
  50. |> add_link_headers(activities)
  51. |> render("index.json",
  52. activities: activities,
  53. for: user,
  54. as: :activity,
  55. with_muted: Map.get(params, :with_muted, false)
  56. )
  57. end
  58. # GET /api/v1/timelines/direct
  59. def direct(%{assigns: %{user: user}} = conn, params) do
  60. params =
  61. params
  62. |> Map.put(:type, "Create")
  63. |> Map.put(:blocking_user, user)
  64. |> Map.put(:user, user)
  65. |> Map.put(:visibility, "direct")
  66. activities =
  67. [user.ap_id]
  68. |> ActivityPub.fetch_activities_query(params)
  69. |> Pagination.fetch_paginated(params)
  70. conn
  71. |> add_link_headers(activities)
  72. |> render("index.json",
  73. activities: activities,
  74. for: user,
  75. as: :activity
  76. )
  77. end
  78. defp restrict_unauthenticated?(true = _local_only) do
  79. Config.restrict_unauthenticated_access?(:timelines, :local)
  80. end
  81. defp restrict_unauthenticated?(_) do
  82. Config.restrict_unauthenticated_access?(:timelines, :federated)
  83. end
  84. # GET /api/v1/timelines/public
  85. def public(%{assigns: %{user: user}} = conn, params) do
  86. local_only = params[:local]
  87. if is_nil(user) and restrict_unauthenticated?(local_only) do
  88. fail_on_bad_auth(conn)
  89. else
  90. activities =
  91. params
  92. |> Map.put(:type, ["Create"])
  93. |> Map.put(:local_only, local_only)
  94. |> Map.put(:blocking_user, user)
  95. |> Map.put(:muting_user, user)
  96. |> Map.put(:reply_filtering_user, user)
  97. |> Map.put(:instance, params[:instance])
  98. |> ActivityPub.fetch_public_activities()
  99. conn
  100. |> add_link_headers(activities, %{"local" => local_only})
  101. |> render("index.json",
  102. activities: activities,
  103. for: user,
  104. as: :activity,
  105. with_muted: Map.get(params, :with_muted, false)
  106. )
  107. end
  108. end
  109. defp fail_on_bad_auth(conn) do
  110. render_error(conn, :unauthorized, "authorization required for timeline view")
  111. end
  112. defp hashtag_fetching(params, user, local_only) do
  113. # Note: not sanitizing tag options at this stage (may be mix-cased, have duplicates etc.)
  114. tags_any =
  115. [params[:tag], params[:any]]
  116. |> List.flatten()
  117. |> Enum.filter(& &1)
  118. tag_all = Map.get(params, :all, [])
  119. tag_reject = Map.get(params, :none, [])
  120. params
  121. |> Map.put(:type, "Create")
  122. |> Map.put(:local_only, local_only)
  123. |> Map.put(:blocking_user, user)
  124. |> Map.put(:muting_user, user)
  125. |> Map.put(:user, user)
  126. |> Map.put(:tag, tags_any)
  127. |> Map.put(:tag_all, tag_all)
  128. |> Map.put(:tag_reject, tag_reject)
  129. |> ActivityPub.fetch_public_activities()
  130. end
  131. # GET /api/v1/timelines/tag/:tag
  132. def hashtag(%{assigns: %{user: user}} = conn, params) do
  133. local_only = params[:local]
  134. if is_nil(user) and restrict_unauthenticated?(local_only) do
  135. fail_on_bad_auth(conn)
  136. else
  137. activities = hashtag_fetching(params, user, local_only)
  138. conn
  139. |> add_link_headers(activities, %{"local" => local_only})
  140. |> render("index.json",
  141. activities: activities,
  142. for: user,
  143. as: :activity,
  144. with_muted: Map.get(params, :with_muted, false)
  145. )
  146. end
  147. end
  148. # GET /api/v1/timelines/list/:list_id
  149. def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
  150. with %Pleroma.List{title: _title, following: following} <- Pleroma.List.get(id, user) do
  151. params =
  152. params
  153. |> Map.put(:type, "Create")
  154. |> Map.put(:blocking_user, user)
  155. |> Map.put(:user, user)
  156. |> Map.put(:muting_user, user)
  157. |> Map.put(:local_only, params[:local])
  158. # we must filter the following list for the user to avoid leaking statuses the user
  159. # does not actually have permission to see (for more info, peruse security issue #270).
  160. user_following = User.following(user)
  161. activities =
  162. following
  163. |> Enum.filter(fn x -> x in user_following end)
  164. |> ActivityPub.fetch_activities_bounded(following, params)
  165. |> Enum.reverse()
  166. render(conn, "index.json",
  167. activities: activities,
  168. for: user,
  169. as: :activity,
  170. with_muted: Map.get(params, :with_muted, false)
  171. )
  172. else
  173. _e -> render_error(conn, :forbidden, "Error.")
  174. end
  175. end
  176. end