[#2456] Added support for `embed_relationships` param, nailed down endpoints which should support it. Fixed :source_mutes relationships subset fetching.

This commit is contained in:
Ivan Tashkinov 2020-05-12 19:14:35 +03:00
parent b960a9430d
commit 63a1a82f38
7 changed files with 99 additions and 13 deletions

View File

@ -87,6 +87,22 @@ def dictionary(
source_to_target_rel_types \\ nil, source_to_target_rel_types \\ nil,
target_to_source_rel_types \\ nil target_to_source_rel_types \\ nil
) )
def dictionary(
_source_users,
_target_users,
[] = _source_to_target_rel_types,
[] = _target_to_source_rel_types
) do
[]
end
def dictionary(
source_users,
target_users,
source_to_target_rel_types,
target_to_source_rel_types
)
when is_list(source_users) and is_list(target_users) do when is_list(source_users) and is_list(target_users) do
source_user_ids = User.binary_id(source_users) source_user_ids = User.binary_id(source_users)
target_user_ids = User.binary_id(target_users) target_user_ids = User.binary_id(target_users)
@ -138,11 +154,16 @@ def view_relationships_option(nil = _reading_user, _actors, _opts) do
def view_relationships_option(%User{} = reading_user, actors, opts) do def view_relationships_option(%User{} = reading_user, actors, opts) do
{source_to_target_rel_types, target_to_source_rel_types} = {source_to_target_rel_types, target_to_source_rel_types} =
if opts[:source_mutes_only] do case opts[:subset] do
# This option is used for rendering statuses (FE needs `muted` flag for each one anyways) :source_mutes ->
{[:mute], []} # Used for statuses rendering (FE needs `muted` flag for each status when statuses load)
else {[:mute], []}
{[:block, :mute, :notification_mute, :reblog_mute], [:block, :inverse_subscription]}
nil ->
{[:block, :mute, :notification_mute, :reblog_mute], [:block, :inverse_subscription]}
unknown ->
raise "Unsupported :subset option value: #{inspect(unknown)}"
end end
user_relationships = user_relationships =
@ -153,7 +174,17 @@ def view_relationships_option(%User{} = reading_user, actors, opts) do
target_to_source_rel_types target_to_source_rel_types
) )
following_relationships = FollowingRelationship.all_between_user_sets([reading_user], actors) following_relationships =
case opts[:subset] do
:source_mutes ->
[]
nil ->
FollowingRelationship.all_between_user_sets([reading_user], actors)
unknown ->
raise "Unsupported :subset option value: #{inspect(unknown)}"
end
%{user_relationships: user_relationships, following_relationships: following_relationships} %{user_relationships: user_relationships, following_relationships: following_relationships}
end end

View File

@ -103,4 +103,9 @@ def try_render(conn, _, _) do
def put_if_exist(map, _key, nil), do: map def put_if_exist(map, _key, nil), do: map
def put_if_exist(map, key, value), do: Map.put(map, key, value) def put_if_exist(map, key, value), do: Map.put(map, key, value)
def embed_relationships?(params) do
# To do: change to `truthy_param?(params["embed_relationships"])` once PleromaFE supports it
not explicitly_falsy_param?(params["embed_relationships"])
end
end end

View File

@ -10,6 +10,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
add_link_headers: 2, add_link_headers: 2,
truthy_param?: 1, truthy_param?: 1,
assign_account_by_id: 2, assign_account_by_id: 2,
embed_relationships?: 1,
json_response: 3 json_response: 3
] ]
@ -269,7 +270,13 @@ def followers(%{assigns: %{user: for_user, account: user}} = conn, params) do
conn conn
|> add_link_headers(followers) |> add_link_headers(followers)
|> render("index.json", for: for_user, users: followers, as: :user) # https://git.pleroma.social/pleroma/pleroma-fe/-/issues/838#note_59223
|> render("index.json",
for: for_user,
users: followers,
as: :user,
embed_relationships: embed_relationships?(params)
)
end end
@doc "GET /api/v1/accounts/:id/following" @doc "GET /api/v1/accounts/:id/following"
@ -288,7 +295,13 @@ def following(%{assigns: %{user: for_user, account: user}} = conn, params) do
conn conn
|> add_link_headers(followers) |> add_link_headers(followers)
|> render("index.json", for: for_user, users: followers, as: :user) # https://git.pleroma.social/pleroma/pleroma-fe/-/issues/838#note_59223
|> render("index.json",
for: for_user,
users: followers,
as: :user,
embed_relationships: embed_relationships?(params)
)
end end
@doc "GET /api/v1/accounts/:id/lists" @doc "GET /api/v1/accounts/:id/lists"

View File

@ -11,6 +11,7 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web alias Pleroma.Web
alias Pleroma.Web.ControllerHelper
alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.MastodonAPI.StatusView alias Pleroma.Web.MastodonAPI.StatusView
@ -32,7 +33,13 @@ def account_search(%{assigns: %{user: user}} = conn, %{q: query} = params) do
conn conn
|> put_view(AccountView) |> put_view(AccountView)
|> render("index.json", users: accounts, for: user, as: :user) # https://git.pleroma.social/pleroma/pleroma-fe/-/issues/838#note_59223
|> render("index.json",
users: accounts,
for: user,
as: :user,
embed_relationships: ControllerHelper.embed_relationships?(params)
)
end end
def search2(conn, params), do: do_search(:v2, conn, params) def search2(conn, params), do: do_search(:v2, conn, params)
@ -75,6 +82,7 @@ defp search_options(params, user) do
offset: params[:offset], offset: params[:offset],
type: params[:type], type: params[:type],
author: get_author(params), author: get_author(params),
embed_relationships: ControllerHelper.embed_relationships?(params),
for_user: user for_user: user
] ]
|> Enum.filter(&elem(&1, 1)) |> Enum.filter(&elem(&1, 1))
@ -86,7 +94,9 @@ defp resource_search(_, "accounts", query, options) do
AccountView.render("index.json", AccountView.render("index.json",
users: accounts, users: accounts,
for: options[:for_user], for: options[:for_user],
as: :user as: :user,
# https://git.pleroma.social/pleroma/pleroma-fe/-/issues/838#note_59223
embed_relationships: options[:embed_relationships]
) )
end end

View File

@ -13,6 +13,22 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
alias Pleroma.Web.MediaProxy alias Pleroma.Web.MediaProxy
def render("index.json", %{users: users} = opts) do def render("index.json", %{users: users} = opts) do
reading_user = opts[:for]
relationships_opt =
cond do
Map.has_key?(opts, :relationships) ->
opts[:relationships]
is_nil(reading_user) || !opts[:embed_relationships] ->
UserRelationship.view_relationships_option(nil, [])
true ->
UserRelationship.view_relationships_option(reading_user, users)
end
opts = Map.put(opts, :relationships, relationships_opt)
users users
|> render_many(AccountView, "show.json", opts) |> render_many(AccountView, "show.json", opts)
|> Enum.filter(&Enum.any?/1) |> Enum.filter(&Enum.any?/1)
@ -175,6 +191,17 @@ defp do_render("show.json", %{user: user} = opts) do
} }
end) end)
relationship =
if opts[:embed_relationships] do
render("relationship.json", %{
user: opts[:for],
target: user,
relationships: opts[:relationships]
})
else
%{}
end
%{ %{
id: to_string(user.id), id: to_string(user.id),
username: username_from_nickname(user.nickname), username: username_from_nickname(user.nickname),
@ -213,7 +240,7 @@ defp do_render("show.json", %{user: user} = opts) do
hide_followers: user.hide_followers, hide_followers: user.hide_followers,
hide_follows: user.hide_follows, hide_follows: user.hide_follows,
hide_favorites: user.hide_favorites, hide_favorites: user.hide_favorites,
relationship: %{}, relationship: relationship,
skip_thread_containment: user.skip_thread_containment, skip_thread_containment: user.skip_thread_containment,
background_image: image_url(user.background) |> MediaProxy.url() background_image: image_url(user.background) |> MediaProxy.url()
} }

View File

@ -51,7 +51,7 @@ def render("index.json", %{notifications: notifications, for: reading_user} = op
|> Enum.filter(& &1) |> Enum.filter(& &1)
|> Kernel.++(move_activities_targets) |> Kernel.++(move_activities_targets)
UserRelationship.view_relationships_option(reading_user, actors, source_mutes_only: true) UserRelationship.view_relationships_option(reading_user, actors, subset: :source_mutes)
end end
opts = opts =

View File

@ -107,7 +107,7 @@ def render("index.json", opts) do
|> Enum.map(&get_user(&1.data["actor"], false)) |> Enum.map(&get_user(&1.data["actor"], false))
|> Enum.filter(& &1) |> Enum.filter(& &1)
UserRelationship.view_relationships_option(reading_user, actors, source_mutes_only: true) UserRelationship.view_relationships_option(reading_user, actors, subset: :source_mutes)
end end
opts = opts =