From 16ce129e382cc5fa0cf5d86fc0785457a0dedd76 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Sun, 3 Feb 2019 21:24:09 +0300 Subject: [PATCH 1/3] Split hide_network into hide_followers & hide_followings (fixed) --- lib/pleroma/user/info.ex | 6 +- .../web/activity_pub/views/user_view.ex | 8 +-- .../mastodon_api/mastodon_api_controller.ex | 4 +- .../web/twitter_api/twitter_api_controller.ex | 6 +- .../web/twitter_api/views/user_view.ex | 3 +- .../20190203185340_split_hide_network.exs | 11 ++++ .../activity_pub_controller_test.exs | 8 +-- .../mastodon_api_controller_test.exs | 16 ++--- .../twitter_api_controller_test.exs | 60 +++++++++++++------ test/web/twitter_api/views/user_view_test.exs | 12 ++-- 10 files changed, 89 insertions(+), 45 deletions(-) create mode 100644 priv/repo/migrations/20190203185340_split_hide_network.exs diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index c6c923aac..2186190a0 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -30,7 +30,8 @@ defmodule Pleroma.User.Info do field(:topic, :string, default: nil) field(:hub, :string, default: nil) field(:salmon, :string, default: nil) - field(:hide_network, :boolean, default: false) + field(:hide_followers, :boolean, default: false) + field(:hide_followings, :boolean, default: false) field(:pinned_activities, {:array, :string}, default: []) # Found in the wild @@ -143,7 +144,8 @@ def profile_update(info, params) do :no_rich_text, :default_scope, :banner, - :hide_network, + :hide_followings, + :hide_followers, :background ]) end diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex index dcf681b6d..b9588bee5 100644 --- a/lib/pleroma/web/activity_pub/views/user_view.ex +++ b/lib/pleroma/web/activity_pub/views/user_view.ex @@ -86,7 +86,7 @@ def render("following.json", %{user: user, page: page}) do query = from(user in query, select: [:ap_id]) following = Repo.all(query) - collection(following, "#{user.ap_id}/following", page, !user.info.hide_network) + collection(following, "#{user.ap_id}/following", page, !user.info.hide_followings) |> Map.merge(Utils.make_json_ld_header()) end @@ -99,7 +99,7 @@ def render("following.json", %{user: user}) do "id" => "#{user.ap_id}/following", "type" => "OrderedCollection", "totalItems" => length(following), - "first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_network) + "first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_followings) } |> Map.merge(Utils.make_json_ld_header()) end @@ -109,7 +109,7 @@ def render("followers.json", %{user: user, page: page}) do query = from(user in query, select: [:ap_id]) followers = Repo.all(query) - collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_network) + collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_followers) |> Map.merge(Utils.make_json_ld_header()) end @@ -122,7 +122,7 @@ def render("followers.json", %{user: user}) do "id" => "#{user.ap_id}/followers", "type" => "OrderedCollection", "totalItems" => length(followers), - "first" => collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_network) + "first" => collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_followers) } |> Map.merge(Utils.make_json_ld_header()) end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 7f3fbff4a..85769c3d7 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -605,7 +605,7 @@ def followers(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do followers = cond do for_user && user.id == for_user.id -> followers - user.info.hide_network -> [] + user.info.hide_followers -> [] true -> followers end @@ -621,7 +621,7 @@ def following(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do followers = cond do for_user && user.id == for_user.id -> followers - user.info.hide_network -> [] + user.info.hide_followings -> [] true -> followers end diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 3064d61ea..5e3fe9352 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -503,7 +503,7 @@ def followers(%{assigns: %{user: for_user}} = conn, params) do followers = cond do for_user && user.id == for_user.id -> followers - user.info.hide_network -> [] + user.info.hide_followers -> [] true -> followers end @@ -523,7 +523,7 @@ def friends(%{assigns: %{user: for_user}} = conn, params) do friends = cond do for_user && user.id == for_user.id -> friends - user.info.hide_network -> [] + user.info.hide_followings -> [] true -> friends end @@ -618,7 +618,7 @@ def raw_empty_array(conn, _params) do defp build_info_cng(user, params) do info_params = - ["no_rich_text", "locked", "hide_network"] + ["no_rich_text", "locked", "hide_followers", "hide_followings"] |> Enum.reduce(%{}, fn key, res -> if value = params[key] do Map.put(res, key, value == "true") diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index 15682db8f..cd7c4349c 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -108,7 +108,8 @@ defp do_render("user.json", %{user: user = %User{}} = assigns) do "locked" => user.info.locked, "default_scope" => user.info.default_scope, "no_rich_text" => user.info.no_rich_text, - "hide_network" => user.info.hide_network, + "hide_followers" => user.info.hide_followers, + "hide_followings" => user.info.hide_followings, "fields" => fields, # Pleroma extension diff --git a/priv/repo/migrations/20190203185340_split_hide_network.exs b/priv/repo/migrations/20190203185340_split_hide_network.exs new file mode 100644 index 000000000..9cc362d17 --- /dev/null +++ b/priv/repo/migrations/20190203185340_split_hide_network.exs @@ -0,0 +1,11 @@ +defmodule Pleroma.Repo.Migrations.SplitHideNetwork do + use Ecto.Migration + + def up do + execute("UPDATE users SET info = jsonb_set(info, '{hide_followings}'::text[], info->'hide_network')") + execute("UPDATE users SET info = jsonb_set(info, '{hide_followers}'::text[], info->'hide_network')") + end + + def down do + end +end diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs index d3dd160dd..0a0103793 100644 --- a/test/web/activity_pub/activity_pub_controller_test.exs +++ b/test/web/activity_pub/activity_pub_controller_test.exs @@ -386,9 +386,9 @@ test "it returns the followers in a collection", %{conn: conn} do assert result["first"]["orderedItems"] == [user.ap_id] end - test "it returns returns empty if the user has 'hide_network' set", %{conn: conn} do + test "it returns returns empty if the user has 'hide_followers' set", %{conn: conn} do user = insert(:user) - user_two = insert(:user, %{info: %{hide_network: true}}) + user_two = insert(:user, %{info: %{hide_followers: true}}) User.follow(user, user_two) result = @@ -441,8 +441,8 @@ test "it returns the following in a collection", %{conn: conn} do assert result["first"]["orderedItems"] == [user_two.ap_id] end - test "it returns returns empty if the user has 'hide_network' set", %{conn: conn} do - user = insert(:user, %{info: %{hide_network: true}}) + test "it returns returns empty if the user has 'hide_followings' set", %{conn: conn} do + user = insert(:user, %{info: %{hide_followings: true}}) user_two = insert(:user) User.follow(user, user_two) diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 8528d4f64..d4e2acae3 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1101,9 +1101,9 @@ test "getting followers", %{conn: conn} do assert id == to_string(user.id) end - test "getting followers, hide_network", %{conn: conn} do + test "getting followers, hide_followers", %{conn: conn} do user = insert(:user) - other_user = insert(:user, %{info: %{hide_network: true}}) + other_user = insert(:user, %{info: %{hide_followers: true}}) {:ok, _user} = User.follow(user, other_user) conn = @@ -1113,9 +1113,9 @@ test "getting followers, hide_network", %{conn: conn} do assert [] == json_response(conn, 200) end - test "getting followers, hide_network, same user requesting", %{conn: conn} do + test "getting followers, hide_followers, same user requesting", %{conn: conn} do user = insert(:user) - other_user = insert(:user, %{info: %{hide_network: true}}) + other_user = insert(:user, %{info: %{hide_followers: true}}) {:ok, _user} = User.follow(user, other_user) conn = @@ -1139,8 +1139,8 @@ test "getting following", %{conn: conn} do assert id == to_string(other_user.id) end - test "getting following, hide_network", %{conn: conn} do - user = insert(:user, %{info: %{hide_network: true}}) + test "getting following, hide_followings", %{conn: conn} do + user = insert(:user, %{info: %{hide_followings: true}}) other_user = insert(:user) {:ok, user} = User.follow(user, other_user) @@ -1151,8 +1151,8 @@ test "getting following, hide_network", %{conn: conn} do assert [] == json_response(conn, 200) end - test "getting following, hide_network, same user requesting", %{conn: conn} do - user = insert(:user, %{info: %{hide_network: true}}) + test "getting following, hide_followings, same user requesting", %{conn: conn} do + user = insert(:user, %{info: %{hide_followings: true}}) other_user = insert(:user) {:ok, user} = User.follow(user, other_user) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 863abd10f..6777354c3 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1132,8 +1132,8 @@ test "it returns a given user's followers with user_id", %{conn: conn} do ) end - test "it returns empty for a hidden network", %{conn: conn} do - user = insert(:user, %{info: %{hide_network: true}}) + test "it returns empty when hide_followers is set to true", %{conn: conn} do + user = insert(:user, %{info: %{hide_followers: true}}) follower_one = insert(:user) follower_two = insert(:user) not_follower = insert(:user) @@ -1150,10 +1150,11 @@ test "it returns empty for a hidden network", %{conn: conn} do assert [] == response end - test "it returns the followers for a hidden network if requested by the user themselves", %{ - conn: conn - } do - user = insert(:user, %{info: %{hide_network: true}}) + test "it returns the followers when hide_followers is set to true if requested by the user themselves", + %{ + conn: conn + } do + user = insert(:user, %{info: %{hide_followers: true}}) follower_one = insert(:user) follower_two = insert(:user) _not_follower = insert(:user) @@ -1256,8 +1257,8 @@ test "it returns a given user's friends with user_id", %{conn: conn} do ) end - test "it returns empty for a hidden network", %{conn: conn} do - user = insert(:user, %{info: %{hide_network: true}}) + test "it returns empty when hide_followings is set to true", %{conn: conn} do + user = insert(:user, %{info: %{hide_followings: true}}) followed_one = insert(:user) followed_two = insert(:user) not_followed = insert(:user) @@ -1273,10 +1274,11 @@ test "it returns empty for a hidden network", %{conn: conn} do assert [] == json_response(conn, 200) end - test "it returns friends for a hidden network if the user themselves request it", %{ - conn: conn - } do - user = insert(:user, %{info: %{hide_network: true}}) + test "it returns friends when hide_followings is set to true if the user themselves request it", + %{ + conn: conn + } do + user = insert(:user, %{info: %{hide_followings: true}}) followed_one = insert(:user) followed_two = insert(:user) _not_followed = insert(:user) @@ -1364,27 +1366,51 @@ test "it updates a user's profile", %{conn: conn} do assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) end - test "it sets and un-sets hide_network", %{conn: conn} do + test "it sets and un-sets hide_followings", %{conn: conn} do user = insert(:user) conn |> assign(:user, user) |> post("/api/account/update_profile.json", %{ - "hide_network" => "true" + "hide_followings" => "true" }) user = Repo.get!(User, user.id) - assert user.info.hide_network == true + assert user.info.hide_followings == true conn = conn |> assign(:user, user) |> post("/api/account/update_profile.json", %{ - "hide_network" => "false" + "hide_followings" => "false" }) user = Repo.get!(User, user.id) - assert user.info.hide_network == false + assert user.info.hide_followings == false + assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) + end + + test "it sets and un-sets hide_followers", %{conn: conn} do + user = insert(:user) + + conn + |> assign(:user, user) + |> post("/api/account/update_profile.json", %{ + "hide_followers" => "true" + }) + + user = Repo.get!(User, user.id) + assert user.info.hide_followers == true + + conn = + conn + |> assign(:user, user) + |> post("/api/account/update_profile.json", %{ + "hide_followers" => "false" + }) + + user = Repo.get!(User, user.id) + assert user.info.hide_followers == false assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) end diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index daf18c1c5..0885afaec 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -100,7 +100,8 @@ test "A user" do "locked" => false, "default_scope" => "public", "no_rich_text" => false, - "hide_network" => false, + "hide_followings" => false, + "hide_followers" => false, "fields" => [], "pleroma" => %{ "confirmation_pending" => false, @@ -147,7 +148,8 @@ test "A user for a given other follower", %{user: user} do "locked" => false, "default_scope" => "public", "no_rich_text" => false, - "hide_network" => false, + "hide_followings" => false, + "hide_followers" => false, "fields" => [], "pleroma" => %{ "confirmation_pending" => false, @@ -195,7 +197,8 @@ test "A user that follows you", %{user: user} do "locked" => false, "default_scope" => "public", "no_rich_text" => false, - "hide_network" => false, + "hide_followings" => false, + "hide_followers" => false, "fields" => [], "pleroma" => %{ "confirmation_pending" => false, @@ -257,7 +260,8 @@ test "A blocked user for the blocker" do "locked" => false, "default_scope" => "public", "no_rich_text" => false, - "hide_network" => false, + "hide_followings" => false, + "hide_followers" => false, "fields" => [], "pleroma" => %{ "confirmation_pending" => false, From cd6606b7f69d1604a08cb31fa539cbd549591e23 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Mon, 4 Feb 2019 22:23:09 +0300 Subject: [PATCH 2/3] Initialize hide_network with false --- priv/repo/migrations/20190203185340_split_hide_network.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/priv/repo/migrations/20190203185340_split_hide_network.exs b/priv/repo/migrations/20190203185340_split_hide_network.exs index 9cc362d17..fd6fd940a 100644 --- a/priv/repo/migrations/20190203185340_split_hide_network.exs +++ b/priv/repo/migrations/20190203185340_split_hide_network.exs @@ -2,6 +2,7 @@ defmodule Pleroma.Repo.Migrations.SplitHideNetwork do use Ecto.Migration def up do + execute("UPDATE users SET info = '{\"hide_network\": false}'::jsonb WHERE NOT(info::jsonb ? 'hide_network')") execute("UPDATE users SET info = jsonb_set(info, '{hide_followings}'::text[], info->'hide_network')") execute("UPDATE users SET info = jsonb_set(info, '{hide_followers}'::text[], info->'hide_network')") end From c48aba3c2a3e594e5d68e8b4aaa31db1823384ad Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Tue, 5 Feb 2019 10:22:31 +0300 Subject: [PATCH 3/3] Fix SQL --- priv/repo/migrations/20190203185340_split_hide_network.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/priv/repo/migrations/20190203185340_split_hide_network.exs b/priv/repo/migrations/20190203185340_split_hide_network.exs index fd6fd940a..9c44e8aff 100644 --- a/priv/repo/migrations/20190203185340_split_hide_network.exs +++ b/priv/repo/migrations/20190203185340_split_hide_network.exs @@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.SplitHideNetwork do use Ecto.Migration def up do - execute("UPDATE users SET info = '{\"hide_network\": false}'::jsonb WHERE NOT(info::jsonb ? 'hide_network')") + execute("UPDATE users SET info = jsonb_set(info, '{hide_network}'::text[], 'false'::jsonb) WHERE NOT(info::jsonb ? 'hide_network')") execute("UPDATE users SET info = jsonb_set(info, '{hide_followings}'::text[], info->'hide_network')") execute("UPDATE users SET info = jsonb_set(info, '{hide_followers}'::text[], info->'hide_network')") end