From 79910ce5cc85f83e58c5f49e53b4b6263925d110 Mon Sep 17 00:00:00 2001 From: Sadposter Date: Fri, 5 Apr 2019 13:49:33 +0100 Subject: [PATCH] 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