Mastodon API: Set follower/following counters to 0 when hiding
authorrinpatch <rinpatch@sdf.org>
Fri, 9 Aug 2019 13:49:09 +0000 (16:49 +0300)
committerrinpatch <rinpatch@sdf.org>
Fri, 9 Aug 2019 13:53:55 +0000 (16:53 +0300)
followers/following is enabled

We are already doing that in AP representation, so I think we should do
it here as well for consistency.

CHANGELOG.md
lib/pleroma/web/mastodon_api/views/account_view.ex
test/web/mastodon_api/account_view_test.exs

index dccc3696593c0f1e7eefb487150c96b5f46e92d4..5d08fe75794e304180a14150036cf579e26bb195 100644 (file)
@@ -26,6 +26,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Federation/MediaProxy not working with instances that have wrong certificate order
 - Mastodon API: Handling of search timeouts (`/api/v1/search` and `/api/v2/search`)
 - Mastodon API: Embedded relationships not being properly rendered in the Account entity of Status entity
+- Mastodon API: follower/following counters not being nullified, when `hide_follows`/`hide_followers` is set
 - Mastodon API: Add `account_id`, `type`, `offset`, and `limit` to search API (`/api/v1/search` and `/api/v2/search`)
 - Mastodon API, streaming: Fix filtering of notifications based on blocks/mutes/thread mutes
 - ActivityPub C2S: follower/following collection pages being inaccessible even when authentifucated if `hide_followers`/ `hide_follows` was set
index de084fd6ef0c61fd440914e0c8f34fb20c12f90c..72c092f252e6e8c604b7eb3db956bd856f929a3f 100644 (file)
@@ -72,6 +72,13 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
     image = User.avatar_url(user) |> MediaProxy.url()
     header = User.banner_url(user) |> MediaProxy.url()
     user_info = User.get_cached_user_info(user)
+
+    following_count =
+      ((!user.info.hide_follows or opts[:for] == user) && user_info.following_count) || 0
+
+    followers_count =
+      ((!user.info.hide_followers or opts[:for] == user) && user_info.follower_count) || 0
+
     bot = (user.info.source_data["type"] || "Person") in ["Application", "Service"]
 
     emojis =
@@ -102,8 +109,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
       display_name: display_name,
       locked: user_info.locked,
       created_at: Utils.to_masto_date(user.inserted_at),
-      followers_count: user_info.follower_count,
-      following_count: user_info.following_count,
+      followers_count: followers_count,
+      following_count: following_count,
       statuses_count: user_info.note_count,
       note: bio || "",
       url: User.profile_url(user),
index 905e9af98daa9dac80dfbf08d01ddeb13580659e..a26f514a515a2926d0eb3bd951b17170df92ac80 100644 (file)
@@ -356,4 +356,31 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
     result = AccountView.render("account.json", %{user: user})
     refute result.display_name == "<marquee> username </marquee>"
   end
+
+  describe "hiding follows/following" do
+    test "shows when follows/following are hidden and sets follower/following count to 0" do
+      user = insert(:user, info: %{hide_followers: true, hide_follows: true})
+      other_user = insert(:user)
+      {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
+      {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
+
+      assert %{
+               followers_count: 0,
+               following_count: 0,
+               pleroma: %{hide_follows: true, hide_followers: true}
+             } = AccountView.render("account.json", %{user: user})
+    end
+
+    test "shows actual follower/following count to the account owner" do
+      user = insert(:user, info: %{hide_followers: true, hide_follows: true})
+      other_user = insert(:user)
+      {:ok, user, other_user, _activity} = CommonAPI.follow(user, other_user)
+      {:ok, _other_user, user, _activity} = CommonAPI.follow(other_user, user)
+
+      assert %{
+               followers_count: 1,
+               following_count: 1
+             } = AccountView.render("account.json", %{user: user, for: user})
+    end
+  end
 end