Add API endpoints and user column for subscription
authorSadposter <hannah+pleroma@coffee-and-dreams.uk>
Fri, 5 Apr 2019 12:49:33 +0000 (13:49 +0100)
committerHannah Ward <Hannah.ward9001@gmail.com>
Fri, 5 Apr 2019 14:30:18 +0000 (15:30 +0100)
lib/pleroma/user.ex
lib/pleroma/user/info.ex
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex

index 5012aef77aa543be08d076e2db3be6a278a51e93..b7cab8642c53bc63cbbade9aebe93ab2b4c4d168 100644 (file)
@@ -923,6 +923,26 @@ defmodule Pleroma.User 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 =
index 740a46727b50ffe1e1ff9a3ed443c5e6205c2af0..30c594f64e68b5df7b45aeea9af24612f2874e0f 100644 (file)
@@ -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 @@ defmodule Pleroma.User.Info 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 @@ defmodule Pleroma.User.Info 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}
 
index 605a327fc382960dee697305a1648244b4a1f66d..10037b24fd4bdd997090c082d7fc5189ab63df10 100644 (file)
@@ -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
 
index 9b081a3167141ce4d2fcdc780df88aa55df21c46..c0a0a500f8c4f79de04543e233ea23730e4f2bdf 100644 (file)
@@ -59,6 +59,18 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI 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
index a7ec9949cb43de42d4313cf8d15b3dd59fb7c4b4..af4c0a60e376cdbd2b2e35e3f1db4fc2f14b7c42 100644 (file)
@@ -269,6 +269,30 @@ defmodule Pleroma.Web.TwitterAPI.Controller 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