2019-09-09 08:49:02 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-09-09 08:49:02 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.StatusController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
2020-04-01 10:49:09 -06:00
|
|
|
import Pleroma.Web.ControllerHelper,
|
2020-05-09 09:05:44 -06:00
|
|
|
only: [try_render: 3, add_link_headers: 2]
|
2019-09-09 08:49:02 -06:00
|
|
|
|
|
|
|
require Ecto.Query
|
|
|
|
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Bookmark
|
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.Repo
|
2022-08-29 13:42:22 -06:00
|
|
|
alias Pleroma.Config
|
2019-09-09 08:49:02 -06:00
|
|
|
alias Pleroma.ScheduledActivity
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
alias Pleroma.Web.ActivityPub.Visibility
|
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
alias Pleroma.Web.MastodonAPI.AccountView
|
|
|
|
alias Pleroma.Web.MastodonAPI.ScheduledActivityView
|
2021-02-12 11:44:45 -07:00
|
|
|
alias Pleroma.Web.OAuth.Token
|
2020-06-24 04:07:47 -06:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
|
|
|
alias Pleroma.Web.Plugs.RateLimiter
|
2019-09-09 08:49:02 -06:00
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
2020-06-24 01:53:10 -06:00
|
|
|
|
2021-06-08 18:14:12 -06:00
|
|
|
plug(:skip_public_check when action in [:index, :show])
|
2020-04-24 07:52:38 -06:00
|
|
|
|
2019-10-02 11:42:40 -06:00
|
|
|
@unauthenticated_access %{fallback: :proceed_unauthenticated, scopes: []}
|
2022-08-29 13:42:22 -06:00
|
|
|
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
|
2019-10-02 11:42:40 -06:00
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{@unauthenticated_access | scopes: ["read:statuses"]}
|
|
|
|
when action in [
|
|
|
|
:index,
|
|
|
|
:show,
|
2022-08-29 13:42:22 -06:00
|
|
|
:context,
|
2022-09-06 13:24:02 -06:00
|
|
|
:translate,
|
|
|
|
:show_history,
|
|
|
|
:show_source
|
2019-10-02 11:42:40 -06:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["write:statuses"]}
|
|
|
|
when action in [
|
|
|
|
:create,
|
|
|
|
:delete,
|
|
|
|
:reblog,
|
2022-09-06 13:24:02 -06:00
|
|
|
:unreblog,
|
|
|
|
:update
|
2019-10-02 11:42:40 -06:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2019-10-06 08:12:17 -06:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read:favourites"]} when action == :favourites)
|
|
|
|
|
2019-10-02 11:42:40 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["write:favourites"]} when action in [:favourite, :unfavourite]
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["write:mutes"]} when action in [:mute_conversation, :unmute_conversation]
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{@unauthenticated_access | scopes: ["read:accounts"]}
|
|
|
|
when action in [:favourited_by, :reblogged_by]
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(OAuthScopesPlug, %{scopes: ["write:accounts"]} when action in [:pin, :unpin])
|
|
|
|
|
2019-10-06 08:12:17 -06:00
|
|
|
# Note: scope not present in Mastodon: read:bookmarks
|
|
|
|
plug(OAuthScopesPlug, %{scopes: ["read:bookmarks"]} when action == :bookmarks)
|
|
|
|
|
2019-10-02 11:42:40 -06:00
|
|
|
# Note: scope not present in Mastodon: write:bookmarks
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["write:bookmarks"]} when action in [:bookmark, :unbookmark]
|
|
|
|
)
|
|
|
|
|
2019-09-09 08:49:02 -06:00
|
|
|
@rate_limited_status_actions ~w(reblog unreblog favourite unfavourite create delete)a
|
|
|
|
|
|
|
|
plug(
|
|
|
|
RateLimiter,
|
2020-05-26 05:45:54 -06:00
|
|
|
[name: :status_id_action, bucket_name: "status_id_action:reblog_unreblog", params: [:id]]
|
2019-09-09 08:49:02 -06:00
|
|
|
when action in ~w(reblog unreblog)a
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(
|
|
|
|
RateLimiter,
|
2020-05-26 05:45:54 -06:00
|
|
|
[name: :status_id_action, bucket_name: "status_id_action:fav_unfav", params: [:id]]
|
2019-09-09 08:49:02 -06:00
|
|
|
when action in ~w(favourite unfavourite)a
|
|
|
|
)
|
|
|
|
|
2019-11-11 05:13:06 -07:00
|
|
|
plug(RateLimiter, [name: :statuses_actions] when action in @rate_limited_status_actions)
|
2019-09-09 08:49:02 -06:00
|
|
|
|
|
|
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.StatusOperation
|
|
|
|
|
2019-09-09 08:49:02 -06:00
|
|
|
@doc """
|
|
|
|
GET `/api/v1/statuses?ids[]=1&ids[]=2`
|
|
|
|
|
|
|
|
`ids` query param is required
|
|
|
|
"""
|
2020-11-16 11:23:25 -07:00
|
|
|
def index(%{assigns: %{user: user}} = conn, %{ids: ids} = params) do
|
2019-09-09 08:49:02 -06:00
|
|
|
limit = 100
|
|
|
|
|
|
|
|
activities =
|
|
|
|
ids
|
|
|
|
|> Enum.take(limit)
|
|
|
|
|> Activity.all_by_ids_with_object()
|
|
|
|
|> Enum.filter(&Visibility.visible_for_user?(&1, user))
|
|
|
|
|
2020-04-01 10:49:09 -06:00
|
|
|
render(conn, "index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-11-16 11:23:25 -07:00
|
|
|
as: :activity,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
2020-04-01 10:49:09 -06:00
|
|
|
)
|
2019-09-09 08:49:02 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
POST /api/v1/statuses
|
|
|
|
"""
|
2020-10-07 08:58:45 -06:00
|
|
|
# Creates a scheduled status when `scheduled_at` param is present and it's far enough
|
2019-09-09 08:49:02 -06:00
|
|
|
def create(
|
2020-05-12 13:59:26 -06:00
|
|
|
%{
|
2021-02-11 13:15:25 -07:00
|
|
|
assigns: %{user: user},
|
2020-05-12 13:59:26 -06:00
|
|
|
body_params: %{status: _, scheduled_at: scheduled_at} = params
|
|
|
|
} = conn,
|
|
|
|
_
|
2020-04-22 06:26:19 -06:00
|
|
|
)
|
|
|
|
when not is_nil(scheduled_at) do
|
2021-02-09 17:07:15 -07:00
|
|
|
params =
|
|
|
|
Map.put(params, :in_reply_to_status_id, params[:in_reply_to_id])
|
2021-02-11 13:19:53 -07:00
|
|
|
|> put_application(conn)
|
2020-05-12 13:59:26 -06:00
|
|
|
|
|
|
|
attrs = %{
|
|
|
|
params: Map.new(params, fn {key, value} -> {to_string(key), value} end),
|
|
|
|
scheduled_at: scheduled_at
|
|
|
|
}
|
2019-09-09 08:49:02 -06:00
|
|
|
|
2019-12-03 11:30:10 -07:00
|
|
|
with {:far_enough, true} <- {:far_enough, ScheduledActivity.far_enough?(scheduled_at)},
|
|
|
|
{:ok, scheduled_activity} <- ScheduledActivity.create(user, attrs) do
|
|
|
|
conn
|
|
|
|
|> put_view(ScheduledActivityView)
|
|
|
|
|> render("show.json", scheduled_activity: scheduled_activity)
|
2019-09-09 08:49:02 -06:00
|
|
|
else
|
2019-12-03 11:30:10 -07:00
|
|
|
{:far_enough, _} ->
|
2020-05-12 13:59:26 -06:00
|
|
|
params = Map.drop(params, [:scheduled_at])
|
|
|
|
create(%Plug.Conn{conn | body_params: params}, %{})
|
2019-12-03 11:30:10 -07:00
|
|
|
|
|
|
|
error ->
|
|
|
|
error
|
2019-09-09 08:49:02 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-07 08:58:45 -06:00
|
|
|
# Creates a regular status
|
2021-02-11 13:22:58 -07:00
|
|
|
def create(%{assigns: %{user: user}, body_params: %{status: _} = params} = conn, _) do
|
2021-02-09 17:07:15 -07:00
|
|
|
params =
|
|
|
|
Map.put(params, :in_reply_to_status_id, params[:in_reply_to_id])
|
2021-02-11 13:19:53 -07:00
|
|
|
|> put_application(conn)
|
2019-09-09 08:49:02 -06:00
|
|
|
|
2022-11-28 06:34:54 -07:00
|
|
|
expires_in_seconds =
|
|
|
|
if is_nil(user.status_ttl_days),
|
|
|
|
do: nil,
|
|
|
|
else: 60 * 60 * 24 * user.status_ttl_days
|
|
|
|
|
|
|
|
params =
|
|
|
|
if is_nil(expires_in_seconds),
|
|
|
|
do: params,
|
|
|
|
else: Map.put(params, :expires_in, expires_in_seconds)
|
|
|
|
|
2019-09-09 08:49:02 -06:00
|
|
|
with {:ok, activity} <- CommonAPI.post(user, params) do
|
|
|
|
try_render(conn, "show.json",
|
|
|
|
activity: activity,
|
|
|
|
for: user,
|
|
|
|
as: :activity,
|
|
|
|
with_direct_conversation_id: true
|
|
|
|
)
|
|
|
|
else
|
2020-07-15 07:25:33 -06:00
|
|
|
{:error, {:reject, message}} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:unprocessable_entity)
|
|
|
|
|> json(%{error: message})
|
|
|
|
|
2019-09-09 08:49:02 -06:00
|
|
|
{:error, message} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:unprocessable_entity)
|
|
|
|
|> json(%{error: message})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-12 13:59:26 -06:00
|
|
|
def create(%{assigns: %{user: _user}, body_params: %{media_ids: _} = params} = conn, _) do
|
|
|
|
params = Map.put(params, :status, "")
|
|
|
|
create(%Plug.Conn{conn | body_params: params}, %{})
|
2019-09-27 17:21:28 -06:00
|
|
|
end
|
|
|
|
|
2022-09-06 13:24:02 -06:00
|
|
|
@doc "GET /api/v1/statuses/:id/history"
|
|
|
|
def show_history(%{assigns: assigns} = conn, %{id: id} = params) do
|
|
|
|
with user = assigns[:user],
|
|
|
|
%Activity{} = activity <- Activity.get_by_id_with_object(id),
|
|
|
|
true <- Visibility.visible_for_user?(activity, user) do
|
|
|
|
try_render(conn, "history.json",
|
|
|
|
activity: activity,
|
|
|
|
for: user,
|
|
|
|
with_direct_conversation_id: true,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
|
|
|
)
|
|
|
|
else
|
|
|
|
_ -> {:error, :not_found}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/statuses/:id/source"
|
|
|
|
def show_source(%{assigns: assigns} = conn, %{id: id} = _params) do
|
|
|
|
with user = assigns[:user],
|
|
|
|
%Activity{} = activity <- Activity.get_by_id_with_object(id),
|
|
|
|
true <- Visibility.visible_for_user?(activity, user) do
|
|
|
|
try_render(conn, "source.json",
|
|
|
|
activity: activity,
|
|
|
|
for: user
|
|
|
|
)
|
|
|
|
else
|
|
|
|
_ -> {:error, :not_found}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "PUT /api/v1/statuses/:id"
|
|
|
|
def update(%{assigns: %{user: user}, body_params: body_params} = conn, %{id: id} = params) do
|
|
|
|
with {_, %Activity{}} = {_, activity} <- {:activity, Activity.get_by_id_with_object(id)},
|
|
|
|
{_, true} <- {:visible, Visibility.visible_for_user?(activity, user)},
|
|
|
|
{_, true} <- {:is_create, activity.data["type"] == "Create"},
|
|
|
|
actor <- Activity.user_actor(activity),
|
|
|
|
{_, true} <- {:own_status, actor.id == user.id},
|
|
|
|
changes <- body_params |> put_application(conn),
|
|
|
|
{_, {:ok, _update_activity}} <- {:pipeline, CommonAPI.update(user, activity, changes)},
|
|
|
|
{_, %Activity{}} = {_, activity} <- {:refetched, Activity.get_by_id_with_object(id)} do
|
|
|
|
try_render(conn, "show.json",
|
|
|
|
activity: activity,
|
|
|
|
for: user,
|
|
|
|
with_direct_conversation_id: true,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
|
|
|
)
|
|
|
|
else
|
|
|
|
{:own_status, _} -> {:error, :forbidden}
|
|
|
|
{:pipeline, _} -> {:error, :internal_server_error}
|
|
|
|
_ -> {:error, :not_found}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-09 08:49:02 -06:00
|
|
|
@doc "GET /api/v1/statuses/:id"
|
2020-11-16 11:23:25 -07:00
|
|
|
def show(%{assigns: %{user: user}} = conn, %{id: id} = params) do
|
2019-09-09 08:49:02 -06:00
|
|
|
with %Activity{} = activity <- Activity.get_by_id_with_object(id),
|
|
|
|
true <- Visibility.visible_for_user?(activity, user) do
|
2019-10-08 21:51:14 -06:00
|
|
|
try_render(conn, "show.json",
|
|
|
|
activity: activity,
|
|
|
|
for: user,
|
2020-11-16 11:23:25 -07:00
|
|
|
with_direct_conversation_id: true,
|
|
|
|
with_muted: Map.get(params, :with_muted, false)
|
2019-10-08 21:51:14 -06:00
|
|
|
)
|
2020-03-04 10:09:06 -07:00
|
|
|
else
|
|
|
|
_ -> {:error, :not_found}
|
2019-09-09 08:49:02 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "DELETE /api/v1/statuses/:id"
|
2020-05-12 13:59:26 -06:00
|
|
|
def delete(%{assigns: %{user: user}} = conn, %{id: id}) do
|
2020-06-25 23:16:24 -06:00
|
|
|
with %Activity{} = activity <- Activity.get_by_id_with_object(id),
|
|
|
|
{:ok, %Activity{}} <- CommonAPI.delete(id, user) do
|
2020-07-05 15:01:25 -06:00
|
|
|
try_render(conn, "show.json",
|
|
|
|
activity: activity,
|
|
|
|
for: user,
|
|
|
|
with_direct_conversation_id: true,
|
|
|
|
with_source: true
|
|
|
|
)
|
2019-09-09 08:49:02 -06:00
|
|
|
else
|
2020-06-25 23:16:24 -06:00
|
|
|
_e -> {:error, :not_found}
|
2019-09-09 08:49:02 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/statuses/:id/reblog"
|
2020-05-12 13:59:26 -06:00
|
|
|
def reblog(%{assigns: %{user: user}, body_params: params} = conn, %{id: ap_id_or_id}) do
|
2020-05-21 05:16:21 -06:00
|
|
|
with {:ok, announce} <- CommonAPI.repeat(ap_id_or_id, user, params),
|
2019-09-09 08:49:02 -06:00
|
|
|
%Activity{} = announce <- Activity.normalize(announce.data) do
|
|
|
|
try_render(conn, "show.json", %{activity: announce, for: user, as: :activity})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/statuses/:id/unreblog"
|
2020-05-12 13:59:26 -06:00
|
|
|
def unreblog(%{assigns: %{user: user}} = conn, %{id: activity_id}) do
|
2020-05-05 08:42:34 -06:00
|
|
|
with {:ok, _unannounce} <- CommonAPI.unrepeat(activity_id, user),
|
|
|
|
%Activity{} = activity <- Activity.get_by_id(activity_id) do
|
2019-09-09 08:49:02 -06:00
|
|
|
try_render(conn, "show.json", %{activity: activity, for: user, as: :activity})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/statuses/:id/favourite"
|
2020-05-12 13:59:26 -06:00
|
|
|
def favourite(%{assigns: %{user: user}} = conn, %{id: activity_id}) do
|
2019-10-16 08:16:39 -06:00
|
|
|
with {:ok, _fav} <- CommonAPI.favorite(user, activity_id),
|
|
|
|
%Activity{} = activity <- Activity.get_by_id(activity_id) do
|
2019-09-09 08:49:02 -06:00
|
|
|
try_render(conn, "show.json", activity: activity, for: user, as: :activity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/statuses/:id/unfavourite"
|
2020-05-12 13:59:26 -06:00
|
|
|
def unfavourite(%{assigns: %{user: user}} = conn, %{id: activity_id}) do
|
2020-05-05 07:08:41 -06:00
|
|
|
with {:ok, _unfav} <- CommonAPI.unfavorite(activity_id, user),
|
|
|
|
%Activity{} = activity <- Activity.get_by_id(activity_id) do
|
2019-09-09 08:49:02 -06:00
|
|
|
try_render(conn, "show.json", activity: activity, for: user, as: :activity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/statuses/:id/pin"
|
2020-05-12 13:59:26 -06:00
|
|
|
def pin(%{assigns: %{user: user}} = conn, %{id: ap_id_or_id}) do
|
2019-09-09 08:49:02 -06:00
|
|
|
with {:ok, activity} <- CommonAPI.pin(ap_id_or_id, user) do
|
|
|
|
try_render(conn, "show.json", activity: activity, for: user, as: :activity)
|
2021-02-03 06:09:28 -07:00
|
|
|
else
|
|
|
|
{:error, :pinned_statuses_limit_reached} ->
|
|
|
|
{:error, "You have already pinned the maximum number of statuses"}
|
|
|
|
|
|
|
|
{:error, :ownership_error} ->
|
|
|
|
{:error, :unprocessable_entity, "Someone else's status cannot be pinned"}
|
|
|
|
|
|
|
|
{:error, :visibility_error} ->
|
|
|
|
{:error, :unprocessable_entity, "Non-public status cannot be pinned"}
|
|
|
|
|
|
|
|
error ->
|
|
|
|
error
|
2019-09-09 08:49:02 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/statuses/:id/unpin"
|
2020-05-12 13:59:26 -06:00
|
|
|
def unpin(%{assigns: %{user: user}} = conn, %{id: ap_id_or_id}) do
|
2019-09-09 08:49:02 -06:00
|
|
|
with {:ok, activity} <- CommonAPI.unpin(ap_id_or_id, user) do
|
|
|
|
try_render(conn, "show.json", activity: activity, for: user, as: :activity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/statuses/:id/bookmark"
|
2020-05-12 13:59:26 -06:00
|
|
|
def bookmark(%{assigns: %{user: user}} = conn, %{id: id}) do
|
2019-09-09 08:49:02 -06:00
|
|
|
with %Activity{} = activity <- Activity.get_by_id_with_object(id),
|
|
|
|
%User{} = user <- User.get_cached_by_nickname(user.nickname),
|
|
|
|
true <- Visibility.visible_for_user?(activity, user),
|
|
|
|
{:ok, _bookmark} <- Bookmark.create(user.id, activity.id) do
|
|
|
|
try_render(conn, "show.json", activity: activity, for: user, as: :activity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/statuses/:id/unbookmark"
|
2020-05-12 13:59:26 -06:00
|
|
|
def unbookmark(%{assigns: %{user: user}} = conn, %{id: id}) do
|
2019-09-09 08:49:02 -06:00
|
|
|
with %Activity{} = activity <- Activity.get_by_id_with_object(id),
|
|
|
|
%User{} = user <- User.get_cached_by_nickname(user.nickname),
|
|
|
|
true <- Visibility.visible_for_user?(activity, user),
|
|
|
|
{:ok, _bookmark} <- Bookmark.destroy(user.id, activity.id) do
|
|
|
|
try_render(conn, "show.json", activity: activity, for: user, as: :activity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/statuses/:id/mute"
|
2020-09-08 06:26:06 -06:00
|
|
|
def mute_conversation(%{assigns: %{user: user}, body_params: params} = conn, %{id: id}) do
|
2019-09-09 08:49:02 -06:00
|
|
|
with %Activity{} = activity <- Activity.get_by_id(id),
|
2020-09-08 06:26:06 -06:00
|
|
|
{:ok, activity} <- CommonAPI.add_mute(user, activity, params) do
|
2019-09-09 08:49:02 -06:00
|
|
|
try_render(conn, "show.json", activity: activity, for: user, as: :activity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/statuses/:id/unmute"
|
2020-05-12 13:59:26 -06:00
|
|
|
def unmute_conversation(%{assigns: %{user: user}} = conn, %{id: id}) do
|
2019-09-09 08:49:02 -06:00
|
|
|
with %Activity{} = activity <- Activity.get_by_id(id),
|
|
|
|
{:ok, activity} <- CommonAPI.remove_mute(user, activity) do
|
|
|
|
try_render(conn, "show.json", activity: activity, for: user, as: :activity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/statuses/:id/favourited_by"
|
2020-05-12 13:59:26 -06:00
|
|
|
def favourited_by(%{assigns: %{user: user}} = conn, %{id: id}) do
|
2020-07-28 13:49:49 -06:00
|
|
|
with true <- Pleroma.Config.get([:instance, :show_reactions]),
|
|
|
|
%Activity{} = activity <- Activity.get_by_id_with_object(id),
|
2019-09-09 08:49:02 -06:00
|
|
|
{:visible, true} <- {:visible, Visibility.visible_for_user?(activity, user)},
|
2021-01-04 05:38:31 -07:00
|
|
|
%Object{data: %{"likes" => likes}} <- Object.normalize(activity, fetch: false) do
|
2019-09-09 08:49:02 -06:00
|
|
|
users =
|
|
|
|
User
|
|
|
|
|> Ecto.Query.where([u], u.ap_id in ^likes)
|
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.filter(&(not User.blocks?(user, &1)))
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_view(AccountView)
|
2019-09-30 06:10:54 -06:00
|
|
|
|> render("index.json", for: user, users: users, as: :user)
|
2019-09-09 08:49:02 -06:00
|
|
|
else
|
|
|
|
{:visible, false} -> {:error, :not_found}
|
|
|
|
_ -> json(conn, [])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/statuses/:id/reblogged_by"
|
2020-05-12 13:59:26 -06:00
|
|
|
def reblogged_by(%{assigns: %{user: user}} = conn, %{id: id}) do
|
2019-09-09 08:49:02 -06:00
|
|
|
with %Activity{} = activity <- Activity.get_by_id_with_object(id),
|
|
|
|
{:visible, true} <- {:visible, Visibility.visible_for_user?(activity, user)},
|
2019-10-01 13:40:35 -06:00
|
|
|
%Object{data: %{"announcements" => announces, "id" => ap_id}} <-
|
2021-01-04 05:38:31 -07:00
|
|
|
Object.normalize(activity, fetch: false) do
|
2019-10-01 13:40:35 -06:00
|
|
|
announces =
|
|
|
|
"Announce"
|
|
|
|
|> Activity.Queries.by_type()
|
|
|
|
|> Ecto.Query.where([a], a.actor in ^announces)
|
|
|
|
# this is to use the index
|
|
|
|
|> Activity.Queries.by_object_id(ap_id)
|
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.filter(&Visibility.visible_for_user?(&1, user))
|
|
|
|
|> Enum.map(& &1.actor)
|
|
|
|
|> Enum.uniq()
|
|
|
|
|
2019-09-09 08:49:02 -06:00
|
|
|
users =
|
|
|
|
User
|
|
|
|
|> Ecto.Query.where([u], u.ap_id in ^announces)
|
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.filter(&(not User.blocks?(user, &1)))
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_view(AccountView)
|
2019-09-30 06:10:54 -06:00
|
|
|
|> render("index.json", for: user, users: users, as: :user)
|
2019-09-09 08:49:02 -06:00
|
|
|
else
|
|
|
|
{:visible, false} -> {:error, :not_found}
|
|
|
|
_ -> json(conn, [])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/statuses/:id/context"
|
2020-05-12 13:59:26 -06:00
|
|
|
def context(%{assigns: %{user: user}} = conn, %{id: id}) do
|
2019-09-09 08:49:02 -06:00
|
|
|
with %Activity{} = activity <- Activity.get_by_id(id) do
|
|
|
|
activities =
|
2022-06-29 03:33:57 -06:00
|
|
|
activity.data["context"]
|
|
|
|
|> ActivityPub.fetch_activities_for_context(%{
|
2020-06-04 11:33:16 -06:00
|
|
|
blocking_user: user,
|
|
|
|
user: user,
|
|
|
|
exclude_id: activity.id
|
2019-09-09 08:49:02 -06:00
|
|
|
})
|
2022-06-29 03:33:57 -06:00
|
|
|
|> Enum.filter(fn activity -> Visibility.visible_for_user?(activity, user) end)
|
2019-09-09 08:49:02 -06:00
|
|
|
|
2019-09-26 01:28:35 -06:00
|
|
|
render(conn, "context.json", activity: activity, activities: activities, user: user)
|
2019-09-09 08:49:02 -06:00
|
|
|
end
|
|
|
|
end
|
2019-10-02 05:27:01 -06:00
|
|
|
|
|
|
|
@doc "GET /api/v1/favourites"
|
2020-04-24 13:25:27 -06:00
|
|
|
def favourites(%{assigns: %{user: %User{} = user}} = conn, params) do
|
2020-05-12 13:59:26 -06:00
|
|
|
activities = ActivityPub.fetch_favourites(user, params)
|
2019-10-02 05:27:01 -06:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(activities)
|
2020-04-01 10:49:09 -06:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-05-09 09:05:44 -06:00
|
|
|
as: :activity
|
2020-04-01 10:49:09 -06:00
|
|
|
)
|
2019-10-02 05:27:01 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc "GET /api/v1/bookmarks"
|
|
|
|
def bookmarks(%{assigns: %{user: user}} = conn, params) do
|
|
|
|
user = User.get_cached_by_id(user.id)
|
|
|
|
|
|
|
|
bookmarks =
|
|
|
|
user.id
|
|
|
|
|> Bookmark.for_user_query()
|
|
|
|
|> Pleroma.Pagination.fetch_paginated(params)
|
|
|
|
|
|
|
|
activities =
|
|
|
|
bookmarks
|
|
|
|
|> Enum.map(fn b -> Map.put(b.activity, :bookmark, Map.delete(b, :activity)) end)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(bookmarks)
|
2020-04-01 10:49:09 -06:00
|
|
|
|> render("index.json",
|
|
|
|
activities: activities,
|
|
|
|
for: user,
|
2020-05-09 09:05:44 -06:00
|
|
|
as: :activity
|
2020-04-01 10:49:09 -06:00
|
|
|
)
|
2019-10-02 05:27:01 -06:00
|
|
|
end
|
2021-02-09 17:07:15 -07:00
|
|
|
|
2022-08-29 13:42:22 -06:00
|
|
|
@doc "GET /api/v1/statuses/:id/translations/:language"
|
2022-08-30 08:58:54 -06:00
|
|
|
def translate(%{assigns: %{user: user}} = conn, %{id: id, language: language} = params) do
|
2022-08-29 13:42:22 -06:00
|
|
|
with {:enabled, true} <- {:enabled, Config.get([:translator, :enabled])},
|
|
|
|
%Activity{} = activity <- Activity.get_by_id_with_object(id),
|
|
|
|
{:visible, true} <- {:visible, Visibility.visible_for_user?(activity, user)},
|
|
|
|
translation_module <- Config.get([:translator, :module]),
|
|
|
|
{:ok, detected, translation} <-
|
|
|
|
fetch_or_translate(
|
|
|
|
activity.id,
|
|
|
|
activity.object.data["content"],
|
2022-08-30 08:58:54 -06:00
|
|
|
Map.get(params, :from, nil),
|
2022-08-29 13:42:22 -06:00
|
|
|
language,
|
|
|
|
translation_module
|
|
|
|
) do
|
|
|
|
json(conn, %{detected_language: detected, text: translation})
|
|
|
|
else
|
|
|
|
{:enabled, false} ->
|
|
|
|
conn
|
|
|
|
|> put_status(:bad_request)
|
|
|
|
|> json(%{"error" => "Translation is not enabled"})
|
|
|
|
|
|
|
|
{:visible, false} ->
|
|
|
|
{:error, :not_found}
|
|
|
|
|
|
|
|
e ->
|
|
|
|
e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-30 08:58:54 -06:00
|
|
|
defp fetch_or_translate(status_id, text, source_language, target_language, translation_module) do
|
|
|
|
@cachex.fetch!(
|
|
|
|
:translations_cache,
|
|
|
|
"translations:#{status_id}:#{source_language}:#{target_language}",
|
|
|
|
fn _ ->
|
|
|
|
value = translation_module.translate(text, source_language, target_language)
|
|
|
|
|
|
|
|
with {:ok, _, _} <- value do
|
|
|
|
value
|
|
|
|
else
|
|
|
|
_ -> {:ignore, value}
|
|
|
|
end
|
2022-08-29 13:42:22 -06:00
|
|
|
end
|
2022-08-30 08:58:54 -06:00
|
|
|
)
|
2022-08-29 13:42:22 -06:00
|
|
|
end
|
|
|
|
|
2021-02-18 16:23:17 -07:00
|
|
|
defp put_application(params, %{assigns: %{token: %Token{user: %User{} = user} = token}} = _conn) do
|
|
|
|
if user.disclose_client do
|
|
|
|
%{client_name: client_name, website: website} = Repo.preload(token, :app).app
|
2021-03-01 10:29:10 -07:00
|
|
|
Map.put(params, :generator, %{type: "Application", name: client_name, url: website})
|
2021-02-18 16:23:17 -07:00
|
|
|
else
|
2021-03-01 10:29:10 -07:00
|
|
|
Map.put(params, :generator, nil)
|
2021-02-18 16:23:17 -07:00
|
|
|
end
|
2021-02-09 17:07:15 -07:00
|
|
|
end
|
2021-02-11 13:15:25 -07:00
|
|
|
|
2021-03-01 10:29:10 -07:00
|
|
|
defp put_application(params, _), do: Map.put(params, :generator, nil)
|
2019-09-09 08:49:02 -06:00
|
|
|
end
|