From 79910ce5cc85f83e58c5f49e53b4b6263925d110 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 13:49:33 +0100 Subject: [PATCH 01/19] Add API endpoints and user column for subscription --- lib/pleroma/user.ex | 20 ++++++++++++++++ lib/pleroma/user/info.ex | 17 +++++++++++++ lib/pleroma/web/router.ex | 3 +++ lib/pleroma/web/twitter_api/twitter_api.ex | 12 ++++++++++ .../web/twitter_api/twitter_api_controller.ex | 24 +++++++++++++++++++ 5 files changed, 76 insertions(+) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 5012aef77..b7cab8642 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -923,6 +923,26 @@ def unmute(muter, %{ap_id: ap_id}) do update_and_set_cache(cng) end + def subscribe(subscriber, %{ap_id: ap_id}) do + info_cng = + subscriber.info + |> User.Info.add_to_subscriptions(ap_id) + + change(subscriber) + |> put_embed(:info, info_cng) + |> update_and_set_cache() + end + + def unsubscribe(unsubscriber, %{ap_id: ap_id}) do + info_cng = + subscriber.info + |> User.Info.remove_from_subscriptions(ap_id) + + change(subscriber) + |> put_embed(:info, info_cng) + |> update_and_set_cache() + end + def block(blocker, %User{ap_id: ap_id} = blocked) do # sever any follow relationships to prevent leaks per activitypub (Pleroma issue #213) blocker = diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index 740a46727..30c594f64 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -22,6 +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(:subscribed_to, {:array, :string}, default: []) field(:deactivated, :boolean, default: false) field(:no_rich_text, :boolean, default: false) field(:ap_enabled, :boolean, default: false) @@ -93,6 +94,14 @@ def set_blocks(info, blocks) do |> validate_required([:blocks]) end + def set_subscriptions(info, subscriptions) do + params = %{subscriptions: subscriptions} + + info + |> cast(params, [:subscribed_to]) + |> validate_required([:subscribed_to]) + end + def add_to_mutes(info, muted) do set_mutes(info, Enum.uniq([muted | info.mutes])) end @@ -109,6 +118,14 @@ 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.subscribed_to])) + end + + def remove_from_subscriptions(info, subscribed) do + set_subscriptions(info, List.delete(info.subscribed_to, subscribed)) + end + def set_domain_blocks(info, domain_blocks) do params = %{domain_blocks: domain_blocks} diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 605a327fc..10037b24f 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -500,6 +500,9 @@ defmodule Pleroma.Web.Router do post("/blocks/create", TwitterAPI.Controller, :block) post("/blocks/destroy", TwitterAPI.Controller, :unblock) + + post("/subscriptions/create", TwitterAPI.Controller, :subscribe) + post("/subscriptions/destroy", TwitterAPI.Controller, :unsubscribe) end end diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 9b081a316..c0a0a500f 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -59,6 +59,18 @@ def unblock(%User{} = blocker, params) do end end + def subscribe(%User{} = subscriber, params) do + with {:ok, %User{} = subscribed} <- get_user(params) do + User.subscribe(subscriber, subscribed) + end + end + + def unsubscribe(%User{} = unsubscriber, params) do + with {:ok, %User{} = unsubscribed} <- get_user(params) do + User.unsubscribe(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 a7ec9949c..af4c0a60e 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -269,6 +269,30 @@ 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 From 007762e767fcd734bacd9700f5268591854a446a Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 14:20:13 +0100 Subject: [PATCH 02/19] Add notification checks --- lib/pleroma/notification.ex | 1 + lib/pleroma/user.ex | 7 +++++-- lib/pleroma/user/info.ex | 10 +++++----- lib/pleroma/web/common_api/utils.ex | 16 ++++++++++++++++ lib/pleroma/web/twitter_api/twitter_api.ex | 1 + 5 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index cac10f24a..7c5856438 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -148,6 +148,7 @@ def get_notified_from_activity( [] |> Utils.maybe_notify_to_recipients(activity) |> Utils.maybe_notify_mentioned_recipients(activity) + |> Utils.maybe_notify_subscribers(activity) |> Enum.uniq() User.get_users_from_set(recipients, local_only) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index b7cab8642..70db520ca 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -935,10 +935,10 @@ def subscribe(subscriber, %{ap_id: ap_id}) do def unsubscribe(unsubscriber, %{ap_id: ap_id}) do info_cng = - subscriber.info + unsubscriber.info |> User.Info.remove_from_subscriptions(ap_id) - change(subscriber) + change(unsubscriber) |> put_embed(:info, info_cng) |> update_and_set_cache() end @@ -1005,6 +1005,9 @@ 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: u.ap_id in ^user.info.subscriptions)) + def block_domain(user, domain) do info_cng = user.info diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index 30c594f64..1746da576 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(:subscribed_to, {:array, :string}, default: []) + field(:subscriptions, {:array, :string}, default: []) field(:deactivated, :boolean, default: false) field(:no_rich_text, :boolean, default: false) field(:ap_enabled, :boolean, default: false) @@ -98,8 +98,8 @@ def set_subscriptions(info, subscriptions) do params = %{subscriptions: subscriptions} info - |> cast(params, [:subscribed_to]) - |> validate_required([:subscribed_to]) + |> cast(params, [:subscriptions]) + |> validate_required([:subscriptions]) end def add_to_mutes(info, muted) do @@ -119,11 +119,11 @@ def remove_from_block(info, blocked) do end def add_to_subscriptions(info, subscribed) do - set_subscriptions(info, Enum.uniq([subscribed | info.subscribed_to])) + set_subscriptions(info, Enum.uniq([subscribed | info.subscriptions])) end def remove_from_subscriptions(info, subscribed) do - set_subscriptions(info, List.delete(info.subscribed_to, subscribed)) + set_subscriptions(info, List.delete(info.subscriptions, 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 9cd8b3758..effc49a01 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -335,6 +335,22 @@ def maybe_notify_mentioned_recipients( def maybe_notify_mentioned_recipients(recipients, _), do: recipients + def maybe_notify_subscribers( + recipients, + %Activity{data: %{"actor" => actor, "type" => type}} + ) when type == "Create" do + with %User{} = user <- User.get_by_ap_id(actor) do + subscriber_ids = + user + |> User.subscribed_users() + |> Enum.map(& &1.ap_id) + + recipients ++ subscriber_ids + end + end + + def maybe_notify_subscribers(recipients, _), do: recipients + def maybe_extract_mentions(%{"tag" => tag}) do tag |> Enum.filter(fn x -> is_map(x) end) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index c0a0a500f..7720367ad 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -62,6 +62,7 @@ def unblock(%User{} = blocker, params) do def subscribe(%User{} = subscriber, params) do with {:ok, %User{} = subscribed} <- get_user(params) do User.subscribe(subscriber, subscribed) + |> IO.inspect end end From 23c4f4949436003f6ccd7fd283492a6598f88482 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 14:27:26 +0100 Subject: [PATCH 03/19] Fix subscription endpoint return formats --- lib/pleroma/web/twitter_api/twitter_api.ex | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 7720367ad..5537680ad 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -60,15 +60,16 @@ def unblock(%User{} = blocker, params) do end def subscribe(%User{} = subscriber, params) do - with {:ok, %User{} = subscribed} <- get_user(params) do - User.subscribe(subscriber, subscribed) - |> IO.inspect + 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) do - User.unsubscribe(unsubscriber, unsubscribed) + with {:ok, %User{} = unsubscribed} <- get_user(params), + {:ok, unsubscriber} <- User.unsubscribe(unsubscriber, unsubscribed) do + {:ok, unsubscriber, unsubscribed} end end From 316fe20d864711c2854401eaa2cb08f627d5e536 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 14:59:34 +0100 Subject: [PATCH 04/19] Make subscriptions the same direction as blocks That being, user - subscribes to -> users, rather than user - has subscribers -> users --- lib/pleroma/user.ex | 7 ++++++- lib/pleroma/web/common_api/utils.ex | 9 +++++---- lib/pleroma/web/twitter_api/twitter_api_controller.ex | 4 ++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 70db520ca..6b213df58 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1006,7 +1006,12 @@ 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: u.ap_id in ^user.info.subscriptions)) + do: + Repo.all( + from(u in User, + where: fragment("?->'subscriptions' @> ?", u.info, ^user.ap_id) + ) + ) def block_domain(user, domain) do info_cng = diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index effc49a01..087778dfe 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -336,10 +336,11 @@ def maybe_notify_mentioned_recipients( def maybe_notify_mentioned_recipients(recipients, _), do: recipients def maybe_notify_subscribers( - recipients, - %Activity{data: %{"actor" => actor, "type" => type}} - ) when type == "Create" do - with %User{} = user <- User.get_by_ap_id(actor) do + recipients, + %Activity{data: %{"actor" => actor, "type" => type}} + ) + when type == "Create" do + with %User{} = user <- User.get_by_ap_id(actor) do subscriber_ids = user |> User.subscribed_users() diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index af4c0a60e..0732705e6 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -279,7 +279,7 @@ def subscribe(%{assigns: %{user: user}} = conn, params) do {:error, msg} -> forbidden_json_reply(conn, msg) end - end + end def unsubscribe(%{assigns: %{user: user}} = conn, params) do case TwitterAPI.unsubscribe(user, params) do @@ -287,7 +287,7 @@ def unsubscribe(%{assigns: %{user: user}} = conn, params) do conn |> put_view(UserView) |> render("show.json", %{user: unsubscribed, for: user}) - + {:error, msg} -> forbidden_json_reply(conn, msg) end From 75c4ceb4a2e5b59607a4688a2519e2f784aaa8f6 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 15:14:19 +0100 Subject: [PATCH 05/19] Add tests for subscriptions --- test/notification_test.exs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/notification_test.exs b/test/notification_test.exs index 12b4292aa..3a11d493b 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -29,6 +29,18 @@ test "notifies someone when they are directly addressed" do assert notification.activity_id == activity.id assert other_notification.activity_id == activity.id end + + + test "it creates a notification for subscribed users" do + user = insert(:user) + subscriber = insert(:user) + + {:ok, _, _} = TwitterAPI.subscribe(subscriber, %{"user_id" => user.id}) + {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"}) + {:ok, [notification]} = Notification.create_notifications(status) + + assert notification.user_id == subscriber.id + end end describe "create_notification" do @@ -84,6 +96,16 @@ test "it doesn't create a notification for repeat-unrepeat-repeat chains" do {:ok, dupe} = TwitterAPI.repeat(user, status.id) assert nil == Notification.create_notification(dupe, retweeted_user) end + + test "it doesn't create duplicate notifications for follow+subscribed users" do + user = insert(:user) + subscriber = insert(:user) + + {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id}) + {:ok, _, _} = TwitterAPI.subscribe(subscriber, %{"user_id" => user.id}) + {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"}) + {:ok, [_notif]} = Notification.create_notifications(status) + end end describe "get notification" do From d56866c824d8023c9ca162ffef4c2eff150de399 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 15:21:33 +0100 Subject: [PATCH 06/19] Add subscribed status to user view Added in pleroma extensions, but can be moved whenever --- lib/pleroma/user.ex | 5 +++++ lib/pleroma/web/twitter_api/views/user_view.ex | 10 ++++++---- test/web/twitter_api/views/user_view_test.exs | 17 ++++++++++++++--- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 6b213df58..de7ed6e72 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -999,6 +999,11 @@ def blocks?(user, %{ap_id: ap_id}) do end) end + def subscribed_to?(user, %{ap_id: ap_id}) do + subs = user.info.subscriptions + Enum.member?(subs, ap_id) + end + def muted_users(user), do: Repo.all(from(u in User, where: u.ap_id in ^user.info.mutes)) diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index 0791ed760..c59570d3e 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -47,15 +47,16 @@ 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} = + {following, follows_you, statusnet_blocking, subscribed} = if for_user do { User.following?(for_user, user), User.following?(user, for_user), - User.blocks?(for_user, user) + User.blocks?(for_user, user), + User.subscribed_to?(for_user, user) } else - {false, false, false} + {false, false, false, false} end user_info = User.get_cached_user_info(user) @@ -116,7 +117,8 @@ defp do_render("user.json", %{user: user = %User{}} = assigns) do "pleroma" => %{ "confirmation_pending" => user_info.confirmation_pending, - "tags" => user.tags + "tags" => user.tags, + "subscribed" => subscribed } |> maybe_with_activation_status(user, for_user) } diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 0feaf4b64..f9ae5888c 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -105,7 +105,8 @@ test "A user" do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [] + "tags" => [], + "subscribed" => false } } @@ -153,7 +154,8 @@ test "A user for a given other follower", %{user: user} do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [] + "tags" => [], + "subscribed" => false, } } @@ -202,13 +204,22 @@ test "A user that follows you", %{user: user} do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [] + "tags" => [], + "subscribed" => false } } 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}) From 06cd547fc6562b00d505fcc8e78ba7c734b1adf0 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 15:22:48 +0100 Subject: [PATCH 07/19] Format subscriptions related stuff --- test/notification_test.exs | 3 +-- test/web/twitter_api/views/user_view_test.exs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/test/notification_test.exs b/test/notification_test.exs index 3a11d493b..96a8d372a 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -30,7 +30,6 @@ test "notifies someone when they are directly addressed" do assert other_notification.activity_id == activity.id end - test "it creates a notification for subscribed users" do user = insert(:user) subscriber = insert(:user) @@ -38,7 +37,7 @@ test "it creates a notification for subscribed users" do {:ok, _, _} = TwitterAPI.subscribe(subscriber, %{"user_id" => user.id}) {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"}) {:ok, [notification]} = Notification.create_notifications(status) - + assert notification.user_id == subscriber.id end end diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index f9ae5888c..8986112cc 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -155,7 +155,7 @@ test "A user for a given other follower", %{user: user} do "pleroma" => %{ "confirmation_pending" => false, "tags" => [], - "subscribed" => false, + "subscribed" => false } } From 73afd13c2114dde0f9939d2f8c8516d2693b7e95 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 15:37:00 +0100 Subject: [PATCH 08/19] Add subscription key to one test I forgot about --- test/web/twitter_api/views/user_view_test.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 8986112cc..69d9c5da5 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -299,7 +299,8 @@ test "A blocked user for the blocker" do "fields" => [], "pleroma" => %{ "confirmation_pending" => false, - "tags" => [] + "tags" => [], + "subscribed" => false } } From d35f6551c1e9b11dec81b622c9ed2d9cdd6ac389 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 15:58:44 +0100 Subject: [PATCH 09/19] Move subscriptions endpoint to be under /pleroma --- lib/pleroma/web/router.ex | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 10037b24f..0b571fc0b 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -495,14 +495,14 @@ 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) post("/blocks/create", TwitterAPI.Controller, :block) post("/blocks/destroy", TwitterAPI.Controller, :unblock) - - post("/subscriptions/create", TwitterAPI.Controller, :subscribe) - post("/subscriptions/destroy", TwitterAPI.Controller, :unsubscribe) end end From 9ca91cbb8764ef4f8fe5303705dd98984e4e90c0 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 16:51:45 +0100 Subject: [PATCH 10/19] 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" => [] } } From f6e131eeb9b9e75e66c5e64084f46e57b9b1358f Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 17:00:41 +0100 Subject: [PATCH 11/19] Don't use twitterAPI for subscription tests --- test/notification_test.exs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/notification_test.exs b/test/notification_test.exs index 96a8d372a..2163c95c0 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -33,8 +33,9 @@ test "notifies someone when they are directly addressed" do test "it creates a notification for subscribed users" do user = insert(:user) subscriber = insert(:user) + + User.subscribe(subscriber, user) - {:ok, _, _} = TwitterAPI.subscribe(subscriber, %{"user_id" => user.id}) {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"}) {:ok, [notification]} = Notification.create_notifications(status) @@ -101,7 +102,7 @@ test "it doesn't create duplicate notifications for follow+subscribed users" do subscriber = insert(:user) {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id}) - {:ok, _, _} = TwitterAPI.subscribe(subscriber, %{"user_id" => user.id}) + User.subscribe(subscriber, user) {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"}) {:ok, [_notif]} = Notification.create_notifications(status) end From a270e7a1915c0d36004e16fc9a166a42ea3eff8a Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 17:07:58 +0100 Subject: [PATCH 12/19] Mix format everything --- test/notification_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/notification_test.exs b/test/notification_test.exs index 2163c95c0..8109623af 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -33,7 +33,7 @@ test "notifies someone when they are directly addressed" do test "it creates a notification for subscribed users" do user = insert(:user) subscriber = insert(:user) - + User.subscribe(subscriber, user) {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"}) From ffac2593eaefeb3000a69f44566dfe5b2a574ae9 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 17:30:54 +0100 Subject: [PATCH 13/19] Add index on subscribers --- .../20190405160700_add_index_on_subscribers.exs | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 priv/repo/migrations/20190405160700_add_index_on_subscribers.exs diff --git a/priv/repo/migrations/20190405160700_add_index_on_subscribers.exs b/priv/repo/migrations/20190405160700_add_index_on_subscribers.exs new file mode 100644 index 000000000..232f75c92 --- /dev/null +++ b/priv/repo/migrations/20190405160700_add_index_on_subscribers.exs @@ -0,0 +1,8 @@ +defmodule Pleroma.Repo.Migrations.AddIndexOnSubscribers do + use Ecto.Migration + + @disable_ddl_transaction true + def change do + create index(:users, ["(info->'subscribers')"], name: :users_subscribers_index, using: :gin, concurrently: true) + end +end From c05fe4da0a9ad119891d2fc6cf82ea3beb59fec7 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Sat, 6 Apr 2019 16:20:06 +0100 Subject: [PATCH 14/19] Document subscription endpoints, fix typos Also adds a quick error case on the subscription endpoints to avoid 500s --- docs/api/pleroma_api.md | 52 ++++++++++++++++++- .../mastodon_api/mastodon_api_controller.ex | 20 ++++--- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/docs/api/pleroma_api.md b/docs/api/pleroma_api.md index 478c9d874..410f2a955 100644 --- a/docs/api/pleroma_api.md +++ b/docs/api/pleroma_api.md @@ -52,7 +52,7 @@ Request parameters can be passed via [query strings](https://en.wikipedia.org/wi * `confirm` * `captcha_solution`: optional, contains provider-specific captcha solution, * `captcha_token`: optional, contains provider-specific captcha token - * `token`: invite token required when the registerations aren't public. + * `token`: invite token required when the registrations aren't public. * Response: JSON. Returns a user object on success, otherwise returns `{"error": "error_msg"}` * Example response: ``` @@ -114,5 +114,53 @@ See [Admin-API](Admin-API.md) * Method `POST` * Authentication: required * Params: - * `id`: notifications's id + * `id`: notification's id * Response: JSON. Returns `{"status": "success"}` if the reading was successful, otherwise returns `{"error": "error_msg"}` + +## `/api/v1/pleroma/accounts/:id/subscribe` +### Subscribe to receive notifications for all statuses posted by a user +* Method `POST` +* Authentication: required +* Params: + * `id`: account id to subscribe to +* Response: JSON, returns a mastodon relationship object on success, otherwise returns `{"error": "error_msg"}` +* Example response: +```json +{ + id: "abcdefg", + following: true, + followed_by: false, + blocking: false, + muting: false, + muting_notifications: false, + subscribing: true, + requested: false, + domain_blocking: false, + showing_reblogs: true, + endorsed: false +} +``` + +## `/api/v1/pleroma/accounts/:id/unsubscribe` +### Unsubscribe to stop receiving notifications from user statuses +* Method `POST` +* Authentication: required +* Params: + * `id`: account id to unsubscribe from +* Response: JSON, returns a mastodon relationship object on success, otherwise returns `{"error": "error_msg"}` +* Example response: +```json +{ + id: "abcdefg", + following: true, + followed_by: false, + blocking: false, + muting: false, + muting_notifications: false, + subscribing: false, + requested: false, + domain_blocking: false, + showing_reblogs: true, + endorsed: false +} +``` diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index e848895f1..a7c9c4735 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -864,22 +864,30 @@ def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) d 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) - + with %User{} = subscription_target <- User.get_by_id(id), + {:ok, subscription_target} = User.subscribe(user, subscription_target) do conn |> put_view(AccountView) |> render("relationship.json", %{user: user, target: subscription_target}) + else + {:error, message} -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Jason.encode!(%{"error" => message})) 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) - + with %User{} = subscription_target <- User.get_by_id(id), + {:ok, subscription_target} = User.unsubscribe(user, subscription_target) do conn |> put_view(AccountView) |> render("relationship.json", %{user: user, target: subscription_target}) + else + {:error, message} -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Jason.encode!(%{"error" => message})) end end From e6778003abcccdf35fe098a571023ed5f5a20323 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Sat, 6 Apr 2019 16:24:21 +0100 Subject: [PATCH 15/19] JSON need quotes! --- docs/api/pleroma_api.md | 44 ++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/docs/api/pleroma_api.md b/docs/api/pleroma_api.md index 410f2a955..75569e092 100644 --- a/docs/api/pleroma_api.md +++ b/docs/api/pleroma_api.md @@ -127,17 +127,17 @@ See [Admin-API](Admin-API.md) * Example response: ```json { - id: "abcdefg", - following: true, - followed_by: false, - blocking: false, - muting: false, - muting_notifications: false, - subscribing: true, - requested: false, - domain_blocking: false, - showing_reblogs: true, - endorsed: false + "id": "abcdefg", + "following": true, + "followed_by": false, + "blocking": false, + "muting": false, + "muting_notifications": false, + "subscribing": true, + "requested": false, + "domain_blocking": false, + "showing_reblogs": true, + "endorsed": false } ``` @@ -151,16 +151,16 @@ See [Admin-API](Admin-API.md) * Example response: ```json { - id: "abcdefg", - following: true, - followed_by: false, - blocking: false, - muting: false, - muting_notifications: false, - subscribing: false, - requested: false, - domain_blocking: false, - showing_reblogs: true, - endorsed: false + "id": "abcdefg", + "following": true, + "followed_by": false, + "blocking": false, + "muting": false, + "muting_notifications": false, + "subscribing": false, + "requested": false, + "domain_blocking": false, + "showing_reblogs": true, + "endorsed": false } ``` From 76fdfd1c7f10332783ef167b7b996edd4813b1c3 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Sun, 7 Apr 2019 15:11:29 +0100 Subject: [PATCH 16/19] Add check on activity visibility in sub notification --- lib/pleroma/web/common_api/utils.ex | 4 +++- test/notification_test.exs | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 4e0a6b2d9..a450a70c3 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -12,6 +12,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do alias Pleroma.Repo alias Pleroma.User alias Pleroma.Web.ActivityPub.Utils + alias Pleroma.Web.ActivityPub.Visibility alias Pleroma.Web.Endpoint alias Pleroma.Web.MediaProxy @@ -337,13 +338,14 @@ def maybe_notify_mentioned_recipients(recipients, _), do: recipients def maybe_notify_subscribers( recipients, - %Activity{data: %{"actor" => actor, "type" => type}} + %Activity{data: %{"actor" => actor, "type" => type}} = activity ) when type == "Create" do with %User{} = user <- User.get_by_ap_id(actor) do subscriber_ids = user |> User.subscribers() + |> Enum.filter(&Visibility.visible_for_user?(activity, &1)) |> Enum.map(& &1.ap_id) recipients ++ subscriber_ids diff --git a/test/notification_test.exs b/test/notification_test.exs index 8109623af..5727620c9 100644 --- a/test/notification_test.exs +++ b/test/notification_test.exs @@ -106,6 +106,18 @@ test "it doesn't create duplicate notifications for follow+subscribed users" do {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"}) {:ok, [_notif]} = Notification.create_notifications(status) end + + test "it doesn't create subscription notifications if the recipient cannot see the status" do + user = insert(:user) + subscriber = insert(:user) + + User.subscribe(subscriber, user) + + {:ok, status} = + TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"}) + + assert {:ok, []} == Notification.create_notifications(status) + end end describe "get notification" do From 13ff312c48c338c93ee9aa85ec2e48c2059d5168 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Mon, 8 Apr 2019 12:46:12 +0100 Subject: [PATCH 17/19] Sever subscription in case of block --- lib/pleroma/user.ex | 31 ++++++++++++++++++++++++------- test/user_test.exs | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index f1565ade7..9a80601c1 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -924,14 +924,23 @@ def unmute(muter, %{ap_id: ap_id}) do end def subscribe(subscriber, %{ap_id: ap_id}) do - with %User{} = user <- get_or_fetch_by_ap_id(ap_id) do - info_cng = - user.info - |> User.Info.add_to_subscribers(subscriber.ap_id) + user_config = Application.get_env(:pleroma, :user) + deny_follow_blocked = Keyword.get(user_config, :deny_follow_blocked) - change(user) - |> put_embed(:info, info_cng) - |> update_and_set_cache() + with %User{} = subscribed <- get_or_fetch_by_ap_id(ap_id) do + blocked = blocks?(subscribed, subscriber) and deny_follow_blocked + + if blocked do + {:error, "Could not subscribe: #{subscribed.nickname} is blocking you"} + else + info_cng = + subscribed.info + |> User.Info.add_to_subscribers(subscriber.ap_id) + + change(subscribed) + |> put_embed(:info, info_cng) + |> update_and_set_cache() + end end end @@ -957,6 +966,14 @@ def block(blocker, %User{ap_id: ap_id} = blocked) do blocker end + blocker = + if subscribed_to?(blocked, blocker) do + {:ok, blocker} = unsubscribe(blocked, blocker) + blocker + else + blocker + end + if following?(blocked, blocker) do unfollow(blocked, blocker) end diff --git a/test/user_test.exs b/test/user_test.exs index 38712cebb..2368f95f5 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -146,6 +146,15 @@ test "can't follow a user who blocked us" do {:error, _} = User.follow(blockee, blocker) end + test "can't subscribe to a user who blocked us" do + blocker = insert(:user) + blocked = insert(:user) + + {:ok, blocker} = User.block(blocker, blocked) + + {:error, _} = User.subscribe(blocked, blocker) + end + test "local users do not automatically follow local locked accounts" do follower = insert(:user, info: %{locked: true}) followed = insert(:user, info: %{locked: true}) @@ -729,6 +738,22 @@ test "blocks tear down blocked->blocker follow relationships" do refute User.following?(blocker, blocked) refute User.following?(blocked, blocker) end + + test "blocks tear down blocked->blocker subscription relationships" do + blocker = insert(:user) + blocked = insert(:user) + + {:ok, blocker} = User.subscribe(blocked, blocker) + + assert User.subscribed_to?(blocked, blocker) + refute User.subscribed_to?(blocker, blocked) + + {:ok, blocker} = User.block(blocker, blocked) + + assert User.blocks?(blocker, blocked) + refute User.subscribed_to?(blocker, blocked) + refute User.subscribed_to?(blocked, blocker) + end end describe "domain blocking" do From 87a01f9ee061d513ede1a18ab494778f4b2eb3a8 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Mon, 8 Apr 2019 13:04:30 +0100 Subject: [PATCH 18/19] Use Pleroma.Config instead of Application.get_env --- lib/pleroma/user.ex | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 9a80601c1..15f606c5f 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -924,8 +924,7 @@ def unmute(muter, %{ap_id: ap_id}) do end def subscribe(subscriber, %{ap_id: ap_id}) do - user_config = Application.get_env(:pleroma, :user) - deny_follow_blocked = Keyword.get(user_config, :deny_follow_blocked) + deny_follow_blocked = Pleroma.Config.get([:user, :deny_follow_blocked]) with %User{} = subscribed <- get_or_fetch_by_ap_id(ap_id) do blocked = blocks?(subscribed, subscriber) and deny_follow_blocked From 589629cf3e1bee9a4f6e92b8456d8614eff73c89 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Mon, 8 Apr 2019 15:56:14 +0100 Subject: [PATCH 19/19] Used cached user resources in subscriptions --- lib/pleroma/user.ex | 4 ++-- lib/pleroma/web/common_api/utils.ex | 2 +- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 15f606c5f..8a0243604 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -926,7 +926,7 @@ def unmute(muter, %{ap_id: ap_id}) do def subscribe(subscriber, %{ap_id: ap_id}) do deny_follow_blocked = Pleroma.Config.get([:user, :deny_follow_blocked]) - with %User{} = subscribed <- get_or_fetch_by_ap_id(ap_id) do + with %User{} = subscribed <- get_cached_by_ap_id(ap_id) do blocked = blocks?(subscribed, subscriber) and deny_follow_blocked if blocked do @@ -944,7 +944,7 @@ def subscribe(subscriber, %{ap_id: ap_id}) do end def unsubscribe(unsubscriber, %{ap_id: ap_id}) do - with %User{} = user <- get_or_fetch_by_ap_id(ap_id) do + with %User{} = user <- get_cached_by_ap_id(ap_id) do info_cng = user.info |> User.Info.remove_from_subscribers(unsubscriber.ap_id) diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index a450a70c3..a830ea5d2 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -341,7 +341,7 @@ def maybe_notify_subscribers( %Activity{data: %{"actor" => actor, "type" => type}} = activity ) when type == "Create" do - with %User{} = user <- User.get_by_ap_id(actor) do + with %User{} = user <- User.get_cached_by_ap_id(actor) do subscriber_ids = user |> User.subscribers() diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index a7c9c4735..d9c0cd55e 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -864,7 +864,7 @@ def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) d end def subscribe(%{assigns: %{user: user}} = conn, %{"id" => id}) do - with %User{} = subscription_target <- User.get_by_id(id), + with %User{} = subscription_target <- User.get_cached_by_id(id), {:ok, subscription_target} = User.subscribe(user, subscription_target) do conn |> put_view(AccountView) @@ -878,7 +878,7 @@ def subscribe(%{assigns: %{user: user}} = conn, %{"id" => id}) do end def unsubscribe(%{assigns: %{user: user}} = conn, %{"id" => id}) do - with %User{} = subscription_target <- User.get_by_id(id), + with %User{} = subscription_target <- User.get_cached_by_id(id), {:ok, subscription_target} = User.unsubscribe(user, subscription_target) do conn |> put_view(AccountView)