From 9ca91cbb8764ef4f8fe5303705dd98984e4e90c0 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 16:51:45 +0100 Subject: [PATCH] Change relationship direction of subscriptions --- lib/pleroma/user.ex | 42 +++++++++---------- lib/pleroma/user/info.ex | 18 ++++---- lib/pleroma/web/common_api/utils.ex | 2 +- .../mastodon_api/mastodon_api_controller.ex | 20 +++++++++ .../web/mastodon_api/views/account_view.ex | 1 + lib/pleroma/web/router.ex | 6 +-- lib/pleroma/web/twitter_api/twitter_api.ex | 14 ------- .../web/twitter_api/twitter_api_controller.ex | 24 ----------- .../web/twitter_api/views/user_view.ex | 10 ++--- test/web/mastodon_api/account_view_test.exs | 2 + .../mastodon_api_controller_test.exs | 19 +++++++++ test/web/twitter_api/views/user_view_test.exs | 20 ++------- 12 files changed, 84 insertions(+), 94 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index de7ed6e72..f1565ade7 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -924,23 +924,27 @@ def unmute(muter, %{ap_id: ap_id}) do end def subscribe(subscriber, %{ap_id: ap_id}) do - info_cng = - subscriber.info - |> User.Info.add_to_subscriptions(ap_id) + with %User{} = user <- get_or_fetch_by_ap_id(ap_id) do + info_cng = + user.info + |> User.Info.add_to_subscribers(subscriber.ap_id) - change(subscriber) - |> put_embed(:info, info_cng) - |> update_and_set_cache() + change(user) + |> put_embed(:info, info_cng) + |> update_and_set_cache() + end end def unsubscribe(unsubscriber, %{ap_id: ap_id}) do - info_cng = - unsubscriber.info - |> User.Info.remove_from_subscriptions(ap_id) + with %User{} = user <- get_or_fetch_by_ap_id(ap_id) do + info_cng = + user.info + |> User.Info.remove_from_subscribers(unsubscriber.ap_id) - change(unsubscriber) - |> put_embed(:info, info_cng) - |> update_and_set_cache() + change(user) + |> put_embed(:info, info_cng) + |> update_and_set_cache() + end end def block(blocker, %User{ap_id: ap_id} = blocked) do @@ -1000,8 +1004,9 @@ def blocks?(user, %{ap_id: ap_id}) do end def subscribed_to?(user, %{ap_id: ap_id}) do - subs = user.info.subscriptions - Enum.member?(subs, ap_id) + with %User{} = target <- User.get_by_ap_id(ap_id) do + Enum.member?(target.info.subscribers, user.ap_id) + end end def muted_users(user), @@ -1010,13 +1015,8 @@ def muted_users(user), def blocked_users(user), do: Repo.all(from(u in User, where: u.ap_id in ^user.info.blocks)) - def subscribed_users(user), - do: - Repo.all( - from(u in User, - where: fragment("?->'subscriptions' @> ?", u.info, ^user.ap_id) - ) - ) + def subscribers(user), + do: Repo.all(from(u in User, where: u.ap_id in ^user.info.subscribers)) def block_domain(user, domain) do info_cng = diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index 1746da576..1cf46feb3 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -22,7 +22,7 @@ defmodule Pleroma.User.Info do field(:domain_blocks, {:array, :string}, default: []) field(:mutes, {:array, :string}, default: []) field(:muted_reblogs, {:array, :string}, default: []) - field(:subscriptions, {:array, :string}, default: []) + field(:subscribers, {:array, :string}, default: []) field(:deactivated, :boolean, default: false) field(:no_rich_text, :boolean, default: false) field(:ap_enabled, :boolean, default: false) @@ -94,12 +94,12 @@ def set_blocks(info, blocks) do |> validate_required([:blocks]) end - def set_subscriptions(info, subscriptions) do - params = %{subscriptions: subscriptions} + def set_subscribers(info, subscribers) do + params = %{subscribers: subscribers} info - |> cast(params, [:subscriptions]) - |> validate_required([:subscriptions]) + |> cast(params, [:subscribers]) + |> validate_required([:subscribers]) end def add_to_mutes(info, muted) do @@ -118,12 +118,12 @@ def remove_from_block(info, blocked) do set_blocks(info, List.delete(info.blocks, blocked)) end - def add_to_subscriptions(info, subscribed) do - set_subscriptions(info, Enum.uniq([subscribed | info.subscriptions])) + def add_to_subscribers(info, subscribed) do + set_subscribers(info, Enum.uniq([subscribed | info.subscribers])) end - def remove_from_subscriptions(info, subscribed) do - set_subscriptions(info, List.delete(info.subscriptions, subscribed)) + def remove_from_subscribers(info, subscribed) do + set_subscribers(info, List.delete(info.subscribers, subscribed)) end def set_domain_blocks(info, domain_blocks) do diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 087778dfe..4e0a6b2d9 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -343,7 +343,7 @@ def maybe_notify_subscribers( with %User{} = user <- User.get_by_ap_id(actor) do subscriber_ids = user - |> User.subscribed_users() + |> User.subscribers() |> Enum.map(& &1.ap_id) recipients ++ subscriber_ids diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 89fd7629a..e848895f1 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -863,6 +863,26 @@ def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) d json(conn, %{}) end + def subscribe(%{assigns: %{user: user}} = conn, %{"id" => id}) do + with %User{} = subscription_target <- User.get_by_id(id) do + {:ok, subscription_target} = User.subscribe(user, subscription_target) + + conn + |> put_view(AccountView) + |> render("relationship.json", %{user: user, target: subscription_target}) + end + end + + def unsubscribe(%{assigns: %{user: user}} = conn, %{"id" => id}) do + with %User{} = subscription_target <- User.get_by_id(id) do + {:ok, subscription_target} = User.unsubscribe(user, subscription_target) + + conn + |> put_view(AccountView) + |> render("relationship.json", %{user: user, target: subscription_target}) + end + end + def status_search(user, query) do fetched = if Regex.match?(~r/https?:/, query) do diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index b5f3bbb9d..42595b0b5 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -53,6 +53,7 @@ def render("relationship.json", %{user: %User{} = user, target: %User{} = target blocking: User.blocks?(user, target), muting: User.mutes?(user, target), muting_notifications: false, + subscribing: User.subscribed_to?(user, target), requested: requested, domain_blocking: false, showing_reblogs: User.showing_reblogs?(user, target), diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 0b571fc0b..da988e5f2 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -311,6 +311,9 @@ defmodule Pleroma.Web.Router do post("/domain_blocks", MastodonAPIController, :block_domain) delete("/domain_blocks", MastodonAPIController, :unblock_domain) + + post("/pleroma/accounts/:id/subscribe", MastodonAPIController, :subscribe) + post("/pleroma/accounts/:id/unsubscribe", MastodonAPIController, :unsubscribe) end scope [] do @@ -495,9 +498,6 @@ defmodule Pleroma.Web.Router do post("/pleroma/friendships/approve", TwitterAPI.Controller, :approve_friend_request) post("/pleroma/friendships/deny", TwitterAPI.Controller, :deny_friend_request) - post("/pleroma/subscriptions/create", TwitterAPI.Controller, :subscribe) - post("/pleroma/subscriptions/destroy", TwitterAPI.Controller, :unsubscribe) - post("/friendships/create", TwitterAPI.Controller, :follow) post("/friendships/destroy", TwitterAPI.Controller, :unfollow) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 5537680ad..9b081a316 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -59,20 +59,6 @@ def unblock(%User{} = blocker, params) do end end - def subscribe(%User{} = subscriber, params) do - with {:ok, %User{} = subscribed} <- get_user(params), - {:ok, subscriber} <- User.subscribe(subscriber, subscribed) do - {:ok, subscriber, subscribed} - end - end - - def unsubscribe(%User{} = unsubscriber, params) do - with {:ok, %User{} = unsubscribed} <- get_user(params), - {:ok, unsubscriber} <- User.unsubscribe(unsubscriber, unsubscribed) do - {:ok, unsubscriber, unsubscribed} - end - end - def repeat(%User{} = user, ap_id_or_id) do with {:ok, _announce, %{data: %{"id" => id}}} <- CommonAPI.repeat(ap_id_or_id, user), %Activity{} = activity <- Activity.get_create_by_object_ap_id(id) do diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 0732705e6..a7ec9949c 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -269,30 +269,6 @@ def unfollow(%{assigns: %{user: user}} = conn, params) do end end - def subscribe(%{assigns: %{user: user}} = conn, params) do - case TwitterAPI.subscribe(user, params) do - {:ok, user, subscribed} -> - conn - |> put_view(UserView) - |> render("show.json", %{user: subscribed, for: user}) - - {:error, msg} -> - forbidden_json_reply(conn, msg) - end - end - - def unsubscribe(%{assigns: %{user: user}} = conn, params) do - case TwitterAPI.unsubscribe(user, params) do - {:ok, user, unsubscribed} -> - conn - |> put_view(UserView) - |> render("show.json", %{user: unsubscribed, for: user}) - - {:error, msg} -> - forbidden_json_reply(conn, msg) - end - end - def fetch_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do with %Activity{} = activity <- Activity.get_by_id(id), true <- Visibility.visible_for_user?(activity, user) do diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index c59570d3e..0791ed760 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -47,16 +47,15 @@ defp do_render("user.json", %{user: user = %User{}} = assigns) do for_user = assigns[:for] image = User.avatar_url(user) |> MediaProxy.url() - {following, follows_you, statusnet_blocking, subscribed} = + {following, follows_you, statusnet_blocking} = if for_user do { User.following?(for_user, user), User.following?(user, for_user), - User.blocks?(for_user, user), - User.subscribed_to?(for_user, user) + User.blocks?(for_user, user) } else - {false, false, false, false} + {false, false, false} end user_info = User.get_cached_user_info(user) @@ -117,8 +116,7 @@ defp do_render("user.json", %{user: user = %User{}} = assigns) do "pleroma" => %{ "confirmation_pending" => user_info.confirmation_pending, - "tags" => user.tags, - "subscribed" => subscribed + "tags" => user.tags } |> maybe_with_activation_status(user, for_user) } diff --git a/test/web/mastodon_api/account_view_test.exs b/test/web/mastodon_api/account_view_test.exs index 6dc60afe9..48580ff1e 100644 --- a/test/web/mastodon_api/account_view_test.exs +++ b/test/web/mastodon_api/account_view_test.exs @@ -142,6 +142,7 @@ test "represent a relationship" do blocking: true, muting: false, muting_notifications: false, + subscribing: false, requested: false, domain_blocking: false, showing_reblogs: true, @@ -198,6 +199,7 @@ test "represent an embedded relationship" do following: false, followed_by: false, blocking: true, + subscribing: false, muting: false, muting_notifications: false, requested: false, diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 6060cc97f..811a2dd7b 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1555,6 +1555,25 @@ test "muting / unmuting a user", %{conn: conn} do assert %{"id" => _id, "muting" => false} = json_response(conn, 200) end + test "subscribing / unsubscribing to a user", %{conn: conn} do + user = insert(:user) + subscription_target = insert(:user) + + conn = + conn + |> assign(:user, user) + |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe") + + assert %{"id" => _id, "subscribing" => true} = json_response(conn, 200) + + conn = + build_conn() + |> assign(:user, user) + |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe") + + assert %{"id" => _id, "subscribing" => false} = json_response(conn, 200) + end + test "getting a list of mutes", %{conn: conn} do user = insert(:user) other_user = insert(:user) diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 69d9c5da5..0feaf4b64 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -105,8 +105,7 @@ test "A user" do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [], - "subscribed" => false + "tags" => [] } } @@ -154,8 +153,7 @@ test "A user for a given other follower", %{user: user} do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [], - "subscribed" => false + "tags" => [] } } @@ -204,22 +202,13 @@ test "A user that follows you", %{user: user} do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [], - "subscribed" => false + "tags" => [] } } assert represented == UserView.render("show.json", %{user: follower, for: user}) end - test "a user that you are subscribed to" do - user = insert(:user) - subscriber = insert(:user) - {:ok, subscriber} = User.subscribe(subscriber, user) - represented = UserView.render("show.json", %{user: user, for: subscriber}) - assert represented["pleroma"]["subscribed"] == true - end - test "a user that is a moderator" do user = insert(:user, %{info: %{is_moderator: true}}) represented = UserView.render("show.json", %{user: user, for: user}) @@ -299,8 +288,7 @@ test "A blocked user for the blocker" do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [], - "subscribed" => false + "tags" => [] } }