Add subscribed status to user view
authorSadposter <hannah+pleroma@coffee-and-dreams.uk>
Fri, 5 Apr 2019 14:21:33 +0000 (15:21 +0100)
committerHannah Ward <Hannah.ward9001@gmail.com>
Fri, 5 Apr 2019 14:30:31 +0000 (15:30 +0100)
Added in pleroma extensions, but can be moved whenever

lib/pleroma/user.ex
lib/pleroma/web/twitter_api/views/user_view.ex
test/web/twitter_api/views/user_view_test.exs

index 6b213df580f0a47d298f8d1f3e93ca206f446bad..de7ed6e72fb96221ec5a08849a66cfa9b073f999 100644 (file)
@@ -999,6 +999,11 @@ defmodule Pleroma.User 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))
 
index 0791ed7608e1be3b3318d63a456f067d4d41ea02..c59570d3e08824ce2df59858833f192c5d0a0464 100644 (file)
@@ -47,15 +47,16 @@ defmodule Pleroma.Web.TwitterAPI.UserView 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 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
       "pleroma" =>
         %{
           "confirmation_pending" => user_info.confirmation_pending,
-          "tags" => user.tags
+          "tags" => user.tags,
+          "subscribed" => subscribed
         }
         |> maybe_with_activation_status(user, for_user)
     }
index 0feaf4b64837ab032214cf33c08fbf383c72f1d6..f9ae5888ca664af261c6613d32cdf62f66f84c3c 100644 (file)
@@ -105,7 +105,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
       "fields" => [],
       "pleroma" => %{
         "confirmation_pending" => false,
-        "tags" => []
+        "tags" => [],
+        "subscribed" => false
       }
     }
 
@@ -153,7 +154,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
       "fields" => [],
       "pleroma" => %{
         "confirmation_pending" => false,
-        "tags" => []
+        "tags" => [],
+        "subscribed" => false,
       }
     }
 
@@ -202,13 +204,22 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest 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})