1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.StatusController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper, only: [try_render: 3, add_link_headers: 2]
12 alias Pleroma.Activity
13 alias Pleroma.Bookmark
15 alias Pleroma.Plugs.OAuthScopesPlug
16 alias Pleroma.Plugs.RateLimiter
18 alias Pleroma.ScheduledActivity
20 alias Pleroma.Web.ActivityPub.ActivityPub
21 alias Pleroma.Web.ActivityPub.Visibility
22 alias Pleroma.Web.CommonAPI
23 alias Pleroma.Web.MastodonAPI.AccountView
24 alias Pleroma.Web.MastodonAPI.ScheduledActivityView
26 @unauthenticated_access %{fallback: :proceed_unauthenticated, scopes: []}
30 %{@unauthenticated_access | scopes: ["read:statuses"]}
41 %{scopes: ["write:statuses"]}
50 plug(OAuthScopesPlug, %{scopes: ["read:favourites"]} when action == :favourites)
54 %{scopes: ["write:favourites"]} when action in [:favourite, :unfavourite]
59 %{scopes: ["write:mutes"]} when action in [:mute_conversation, :unmute_conversation]
64 %{@unauthenticated_access | scopes: ["read:accounts"]}
65 when action in [:favourited_by, :reblogged_by]
68 plug(OAuthScopesPlug, %{scopes: ["write:accounts"]} when action in [:pin, :unpin])
70 # Note: scope not present in Mastodon: read:bookmarks
71 plug(OAuthScopesPlug, %{scopes: ["read:bookmarks"]} when action == :bookmarks)
73 # Note: scope not present in Mastodon: write:bookmarks
76 %{scopes: ["write:bookmarks"]} when action in [:bookmark, :unbookmark]
79 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug)
81 @rate_limited_status_actions ~w(reblog unreblog favourite unfavourite create delete)a
85 [name: :status_id_action, bucket_name: "status_id_action:reblog_unreblog", params: ["id"]]
86 when action in ~w(reblog unreblog)a
91 [name: :status_id_action, bucket_name: "status_id_action:fav_unfav", params: ["id"]]
92 when action in ~w(favourite unfavourite)a
95 plug(RateLimiter, [name: :statuses_actions] when action in @rate_limited_status_actions)
97 action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
100 GET `/api/v1/statuses?ids[]=1&ids[]=2`
102 `ids` query param is required
104 def index(%{assigns: %{user: user}} = conn, %{"ids" => ids}) do
110 |> Activity.all_by_ids_with_object()
111 |> Enum.filter(&Visibility.visible_for_user?(&1, user))
113 render(conn, "index.json", activities: activities, for: user, as: :activity)
117 POST /api/v1/statuses
119 Creates a scheduled status when `scheduled_at` param is present and it's far enough
122 %{assigns: %{user: user}} = conn,
123 %{"status" => _, "scheduled_at" => scheduled_at} = params
125 params = Map.put(params, "in_reply_to_status_id", params["in_reply_to_id"])
127 with {:far_enough, true} <- {:far_enough, ScheduledActivity.far_enough?(scheduled_at)},
128 attrs <- %{"params" => params, "scheduled_at" => scheduled_at},
129 {:ok, scheduled_activity} <- ScheduledActivity.create(user, attrs) do
131 |> put_view(ScheduledActivityView)
132 |> render("show.json", scheduled_activity: scheduled_activity)
135 create(conn, Map.drop(params, ["scheduled_at"]))
143 POST /api/v1/statuses
145 Creates a regular status
147 def create(%{assigns: %{user: user}} = conn, %{"status" => _} = params) do
148 params = Map.put(params, "in_reply_to_status_id", params["in_reply_to_id"])
150 with {:ok, activity} <- CommonAPI.post(user, params) do
151 try_render(conn, "show.json",
155 with_direct_conversation_id: true
160 |> put_status(:unprocessable_entity)
161 |> json(%{error: message})
165 def create(%{assigns: %{user: _user}} = conn, %{"media_ids" => _} = params) do
166 create(conn, Map.put(params, "status", ""))
169 @doc "GET /api/v1/statuses/:id"
170 def show(%{assigns: %{user: user}} = conn, %{"id" => id}) do
171 with %Activity{} = activity <- Activity.get_by_id_with_object(id),
172 true <- Visibility.visible_for_user?(activity, user) do
173 try_render(conn, "show.json",
176 with_direct_conversation_id: true
179 _ -> {:error, :not_found}
183 @doc "DELETE /api/v1/statuses/:id"
184 def delete(%{assigns: %{user: user}} = conn, %{"id" => id}) do
185 with {:ok, %Activity{}} <- CommonAPI.delete(id, user) do
188 {:error, :not_found} = e -> e
189 _e -> render_error(conn, :forbidden, "Can't delete this post")
193 @doc "POST /api/v1/statuses/:id/reblog"
194 def reblog(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id} = params) do
195 with {:ok, announce, _activity} <- CommonAPI.repeat(ap_id_or_id, user, params),
196 %Activity{} = announce <- Activity.normalize(announce.data) do
197 try_render(conn, "show.json", %{activity: announce, for: user, as: :activity})
201 @doc "POST /api/v1/statuses/:id/unreblog"
202 def unreblog(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
203 with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
204 %Activity{} = activity <- Activity.get_create_by_object_ap_id_with_object(id) do
205 try_render(conn, "show.json", %{activity: activity, for: user, as: :activity})
209 @doc "POST /api/v1/statuses/:id/favourite"
210 def favourite(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
211 with {:ok, _fav, %{data: %{"id" => id}}} <- CommonAPI.favorite(ap_id_or_id, user),
212 %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
213 try_render(conn, "show.json", activity: activity, for: user, as: :activity)
217 @doc "POST /api/v1/statuses/:id/unfavourite"
218 def unfavourite(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
219 with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unfavorite(ap_id_or_id, user),
220 %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do
221 try_render(conn, "show.json", activity: activity, for: user, as: :activity)
225 @doc "POST /api/v1/statuses/:id/pin"
226 def pin(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
227 with {:ok, activity} <- CommonAPI.pin(ap_id_or_id, user) do
228 try_render(conn, "show.json", activity: activity, for: user, as: :activity)
232 @doc "POST /api/v1/statuses/:id/unpin"
233 def unpin(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
234 with {:ok, activity} <- CommonAPI.unpin(ap_id_or_id, user) do
235 try_render(conn, "show.json", activity: activity, for: user, as: :activity)
239 @doc "POST /api/v1/statuses/:id/bookmark"
240 def bookmark(%{assigns: %{user: user}} = conn, %{"id" => id}) do
241 with %Activity{} = activity <- Activity.get_by_id_with_object(id),
242 %User{} = user <- User.get_cached_by_nickname(user.nickname),
243 true <- Visibility.visible_for_user?(activity, user),
244 {:ok, _bookmark} <- Bookmark.create(user.id, activity.id) do
245 try_render(conn, "show.json", activity: activity, for: user, as: :activity)
249 @doc "POST /api/v1/statuses/:id/unbookmark"
250 def unbookmark(%{assigns: %{user: user}} = conn, %{"id" => id}) do
251 with %Activity{} = activity <- Activity.get_by_id_with_object(id),
252 %User{} = user <- User.get_cached_by_nickname(user.nickname),
253 true <- Visibility.visible_for_user?(activity, user),
254 {:ok, _bookmark} <- Bookmark.destroy(user.id, activity.id) do
255 try_render(conn, "show.json", activity: activity, for: user, as: :activity)
259 @doc "POST /api/v1/statuses/:id/mute"
260 def mute_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
261 with %Activity{} = activity <- Activity.get_by_id(id),
262 {:ok, activity} <- CommonAPI.add_mute(user, activity) do
263 try_render(conn, "show.json", activity: activity, for: user, as: :activity)
267 @doc "POST /api/v1/statuses/:id/unmute"
268 def unmute_conversation(%{assigns: %{user: user}} = conn, %{"id" => id}) do
269 with %Activity{} = activity <- Activity.get_by_id(id),
270 {:ok, activity} <- CommonAPI.remove_mute(user, activity) do
271 try_render(conn, "show.json", activity: activity, for: user, as: :activity)
275 @doc "GET /api/v1/statuses/:id/card"
276 @deprecated "https://github.com/tootsuite/mastodon/pull/11213"
277 def card(%{assigns: %{user: user}} = conn, %{"id" => status_id}) do
278 with %Activity{} = activity <- Activity.get_by_id(status_id),
279 true <- Visibility.visible_for_user?(activity, user) do
280 data = Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
281 render(conn, "card.json", data)
283 _ -> render_error(conn, :not_found, "Record not found")
287 @doc "GET /api/v1/statuses/:id/favourited_by"
288 def favourited_by(%{assigns: %{user: user}} = conn, %{"id" => id}) do
289 with %Activity{} = activity <- Activity.get_by_id_with_object(id),
290 {:visible, true} <- {:visible, Visibility.visible_for_user?(activity, user)},
291 %Object{data: %{"likes" => likes}} <- Object.normalize(activity) do
294 |> Ecto.Query.where([u], u.ap_id in ^likes)
296 |> Enum.filter(&(not User.blocks?(user, &1)))
299 |> put_view(AccountView)
300 |> render("index.json", for: user, users: users, as: :user)
302 {:visible, false} -> {:error, :not_found}
307 @doc "GET /api/v1/statuses/:id/reblogged_by"
308 def reblogged_by(%{assigns: %{user: user}} = conn, %{"id" => id}) do
309 with %Activity{} = activity <- Activity.get_by_id_with_object(id),
310 {:visible, true} <- {:visible, Visibility.visible_for_user?(activity, user)},
311 %Object{data: %{"announcements" => announces, "id" => ap_id}} <-
312 Object.normalize(activity) do
315 |> Activity.Queries.by_type()
316 |> Ecto.Query.where([a], a.actor in ^announces)
317 # this is to use the index
318 |> Activity.Queries.by_object_id(ap_id)
320 |> Enum.filter(&Visibility.visible_for_user?(&1, user))
321 |> Enum.map(& &1.actor)
326 |> Ecto.Query.where([u], u.ap_id in ^announces)
328 |> Enum.filter(&(not User.blocks?(user, &1)))
331 |> put_view(AccountView)
332 |> render("index.json", for: user, users: users, as: :user)
334 {:visible, false} -> {:error, :not_found}
339 @doc "GET /api/v1/statuses/:id/context"
340 def context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
341 with %Activity{} = activity <- Activity.get_by_id(id) do
343 ActivityPub.fetch_activities_for_context(activity.data["context"], %{
344 "blocking_user" => user,
346 "exclude_id" => activity.id
349 render(conn, "context.json", activity: activity, activities: activities, user: user)
353 @doc "GET /api/v1/favourites"
354 def favourites(%{assigns: %{user: user}} = conn, params) do
356 ActivityPub.fetch_favourites(
358 Map.take(params, Pleroma.Pagination.page_keys())
362 |> add_link_headers(activities)
363 |> render("index.json", activities: activities, for: user, as: :activity)
366 @doc "GET /api/v1/bookmarks"
367 def bookmarks(%{assigns: %{user: user}} = conn, params) do
368 user = User.get_cached_by_id(user.id)
372 |> Bookmark.for_user_query()
373 |> Pleroma.Pagination.fetch_paginated(params)
377 |> Enum.map(fn b -> Map.put(b.activity, :bookmark, Map.delete(b, :activity)) end)
380 |> add_link_headers(bookmarks)
381 |> render("index.json", %{activities: activities, for: user, as: :activity})