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.

207 lines
6.4KB

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