[#2456] Added support for `embed_relationships` param, nailed down endpoints which...
authorIvan Tashkinov <ivantashkinov@gmail.com>
Tue, 12 May 2020 16:14:35 +0000 (19:14 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Tue, 12 May 2020 16:14:35 +0000 (19:14 +0300)
lib/pleroma/user_relationship.ex
lib/pleroma/web/controller_helper.ex
lib/pleroma/web/mastodon_api/controllers/account_controller.ex
lib/pleroma/web/mastodon_api/controllers/search_controller.ex
lib/pleroma/web/mastodon_api/views/account_view.ex
lib/pleroma/web/mastodon_api/views/notification_view.ex
lib/pleroma/web/mastodon_api/views/status_view.ex

index 235ad427ce8887df7903f31c8d7cabef3810332d..6dfdd2860b1a08429c883ee59e6ef491770391b7 100644 (file)
@@ -87,6 +87,22 @@ defmodule Pleroma.UserRelationship do
         source_to_target_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
     source_user_ids = User.binary_id(source_users)
     target_user_ids = User.binary_id(target_users)
@@ -138,11 +154,16 @@ defmodule Pleroma.UserRelationship do
 
   def view_relationships_option(%User{} = reading_user, actors, opts) do
     {source_to_target_rel_types, target_to_source_rel_types} =
-      if opts[:source_mutes_only] do
-        # This option is used for rendering statuses (FE needs `muted` flag for each one anyways)
-        {[:mute], []}
-      else
-        {[:block, :mute, :notification_mute, :reblog_mute], [:block, :inverse_subscription]}
+      case opts[:subset] do
+        :source_mutes ->
+          # Used for statuses rendering (FE needs `muted` flag for each status when statuses load)
+          {[:mute], []}
+
+        nil ->
+          {[:block, :mute, :notification_mute, :reblog_mute], [:block, :inverse_subscription]}
+
+        unknown ->
+          raise "Unsupported :subset option value: #{inspect(unknown)}"
       end
 
     user_relationships =
@@ -153,7 +174,17 @@ defmodule Pleroma.UserRelationship do
         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}
   end
index 61fdec0304aa989738576b8d083d2860ab92aa40..ae9b265b11850dba433582546cb21231ddf57878 100644 (file)
@@ -103,4 +103,9 @@ defmodule Pleroma.Web.ControllerHelper do
   def put_if_exist(map, _key, nil), do: map
 
   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
index 489441da50da2cf8545172e63c8dbd228ca53d2a..ef41f9e96db2ba3c7564b81200db473f54c26eae 100644 (file)
@@ -10,6 +10,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
       add_link_headers: 2,
       truthy_param?: 1,
       assign_account_by_id: 2,
+      embed_relationships?: 1,
       json_response: 3
     ]
 
@@ -269,7 +270,13 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
 
     conn
     |> 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
 
   @doc "GET /api/v1/accounts/:id/following"
@@ -288,7 +295,13 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
 
     conn
     |> 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
 
   @doc "GET /api/v1/accounts/:id/lists"
index c30ae1c7aba23cd4a3cf0b665a86c3fd4fe389c2..632c4590f7da8c7bc592fa0c85b35daaa196e0ed 100644 (file)
@@ -11,6 +11,7 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
   alias Pleroma.Repo
   alias Pleroma.User
   alias Pleroma.Web
+  alias Pleroma.Web.ControllerHelper
   alias Pleroma.Web.MastodonAPI.AccountView
   alias Pleroma.Web.MastodonAPI.StatusView
 
@@ -32,7 +33,13 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
 
     conn
     |> 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
 
   def search2(conn, params), do: do_search(:v2, conn, params)
@@ -75,6 +82,7 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
       offset: params[:offset],
       type: params[:type],
       author: get_author(params),
+      embed_relationships: ControllerHelper.embed_relationships?(params),
       for_user: user
     ]
     |> Enum.filter(&elem(&1, 1))
@@ -86,7 +94,9 @@ defmodule Pleroma.Web.MastodonAPI.SearchController do
     AccountView.render("index.json",
       users: accounts,
       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
 
index b3a14d255966e4b38e956aebee789dd2ae8a4373..6304d77ca8892b0b82bcf9189dfaf6c3b3bc1183 100644 (file)
@@ -13,6 +13,22 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
   alias Pleroma.Web.MediaProxy
 
   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
     |> render_many(AccountView, "show.json", opts)
     |> Enum.filter(&Enum.any?/1)
@@ -175,6 +191,17 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
         }
       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),
       username: username_from_nickname(user.nickname),
@@ -213,7 +240,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
         hide_followers: user.hide_followers,
         hide_follows: user.hide_follows,
         hide_favorites: user.hide_favorites,
-        relationship: %{},
+        relationship: relationship,
         skip_thread_containment: user.skip_thread_containment,
         background_image: image_url(user.background) |> MediaProxy.url()
       }
index a53218d59db8701f29f4ae0ec1b54753d9c01f6e..c46ddcf5578f456b30c18cdce99ff3a9b0baccd0 100644 (file)
@@ -51,7 +51,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
             |> Enum.filter(& &1)
             |> 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
 
     opts =
index f7895c514aad3740d5f6e5dbdeb77415936a2cf7..05a26017a801c593b3bb2db86e783f57b0f008ad 100644 (file)
@@ -107,7 +107,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
             |> Enum.map(&get_user(&1.data["actor"], false))
             |> 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
 
     opts =