Add logic for keeping follow_request_count up-to-date on the `follow`,
authoreugenijm <eugenijm@protonmail.com>
Sat, 9 Feb 2019 23:26:29 +0000 (02:26 +0300)
committereugenijm <eugenijm@protonmail.com>
Fri, 15 Feb 2019 09:20:20 +0000 (12:20 +0300)
`approve_friend_request`, and `deny_friend_request` actions.
Add follow_request_count to the user view.

lib/pleroma/user.ex
lib/pleroma/user/info.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/activity_pub/transmogrifier.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
lib/pleroma/web/twitter_api/views/user_view.ex
test/web/mastodon_api/mastodon_api_controller_test.exs
test/web/twitter_api/twitter_api_controller_test.exs

index 3232cb8421e50f7596c778b1482f0b92c70f2f36..854787a2bf10db54f4d4e67cf7adc10f1123beaf 100644 (file)
@@ -618,6 +618,32 @@ defmodule Pleroma.User do
     )
   end
 
+  def update_follow_request_count(%User{} = user) do
+    subquery =
+      user
+      |> User.get_follow_requests_query()
+      |> select([a], %{count: count(a.id)})
+
+    User
+    |> where(id: ^user.id)
+    |> join(:inner, [u], s in subquery(subquery))
+    |> update([u, s],
+      set: [
+        info:
+          fragment(
+            "jsonb_set(?, '{follow_request_count}', ?::varchar::jsonb, true)",
+            u.info,
+            s.count
+          )
+      ]
+    )
+    |> Repo.update_all([], returning: true)
+    |> case do
+      {1, [user]} -> {:ok, user}
+      _ -> {:error, user}
+    end
+  end
+
   def get_follow_requests(%User{} = user) do
     q = get_follow_requests_query(user)
     reqs = Repo.all(q)
index 9d8779fabb46be3ec8e4d05daa3f66ebd81d3632..c59e74c4507937725848dce84c42550307e55727 100644 (file)
@@ -12,6 +12,7 @@ defmodule Pleroma.User.Info do
     field(:source_data, :map, default: %{})
     field(:note_count, :integer, default: 0)
     field(:follower_count, :integer, default: 0)
+    field(:follow_request_count, :integer, default: 0)
     field(:locked, :boolean, default: false)
     field(:confirmation_pending, :boolean, default: false)
     field(:confirmation_token, :string, default: nil)
index c46d8233e04aa77dfef3a4da5e0d49d66679454f..975f9fde3f0fb98b8918e3c13c1b8e8ff444d6ad 100644 (file)
@@ -172,9 +172,10 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     # only accept false as false value
     local = !(params[:local] == false)
 
-    with data <- %{"to" => to, "type" => "Accept", "actor" => actor, "object" => object},
+    with data <- %{"to" => to, "type" => "Accept", "actor" => actor.ap_id, "object" => object},
          {:ok, activity} <- insert(data, local),
-         :ok <- maybe_federate(activity) do
+         :ok <- maybe_federate(activity),
+         _ <- User.update_follow_request_count(actor) do
       {:ok, activity}
     end
   end
@@ -183,9 +184,10 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     # only accept false as false value
     local = !(params[:local] == false)
 
-    with data <- %{"to" => to, "type" => "Reject", "actor" => actor, "object" => object},
+    with data <- %{"to" => to, "type" => "Reject", "actor" => actor.ap_id, "object" => object},
          {:ok, activity} <- insert(data, local),
-         :ok <- maybe_federate(activity) do
+         :ok <- maybe_federate(activity),
+         _ <- User.update_follow_request_count(actor) do
       {:ok, activity}
     end
   end
@@ -283,7 +285,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   def follow(follower, followed, activity_id \\ nil, local \\ true) do
     with data <- make_follow_data(follower, followed, activity_id),
          {:ok, activity} <- insert(data, local),
-         :ok <- maybe_federate(activity) do
+         :ok <- maybe_federate(activity),
+         _ <- User.update_follow_request_count(followed) do
       {:ok, activity}
     end
   end
@@ -293,7 +296,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
          {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
          unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
          {:ok, activity} <- insert(unfollow_data, local),
-         :ok <- maybe_federate(activity) do
+         :ok <- maybe_federate(activity),
+         _ <- User.update_follow_request_count(followed) do
       {:ok, activity}
     end
   end
index 26b2dd575432b29b0868e8f295650af2e74367a3..41d89a02b145d6594c7f60558ad643ff9e61beaf 100644 (file)
@@ -406,7 +406,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
       if not User.locked?(followed) do
         ActivityPub.accept(%{
           to: [follower.ap_id],
-          actor: followed.ap_id,
+          actor: followed,
           object: data,
           local: true
         })
@@ -432,7 +432,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
            ActivityPub.accept(%{
              to: follow_activity.data["to"],
              type: "Accept",
-             actor: followed.ap_id,
+             actor: followed,
              object: follow_activity.data["id"],
              local: false
            }) do
@@ -458,7 +458,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
            ActivityPub.reject(%{
              to: follow_activity.data["to"],
              type: "Reject",
-             actor: followed.ap_id,
+             actor: followed,
              object: follow_activity.data["id"],
              local: false
            }) do
index dcaeccac698d0bee14dee817dcc48a11b8dc06fe..f0bbe5b3f1e9a9ac5664cdc8244a53fedb33b2bc 100644 (file)
@@ -680,7 +680,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
          {:ok, _activity} <-
            ActivityPub.accept(%{
              to: [follower.ap_id],
-             actor: followed.ap_id,
+             actor: followed,
              object: follow_activity.data["id"],
              type: "Accept"
            }) do
@@ -702,7 +702,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
          {:ok, _activity} <-
            ActivityPub.reject(%{
              to: [follower.ap_id],
-             actor: followed.ap_id,
+             actor: followed,
              object: follow_activity.data["id"],
              type: "Reject"
            }) do
index c2f0dc2a9e331b361bb1820af6a79e743134f430..70ae4068a31d957adc24762b97bafc26dbbde53c 100644 (file)
@@ -570,7 +570,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
          {:ok, _activity} <-
            ActivityPub.accept(%{
              to: [follower.ap_id],
-             actor: followed.ap_id,
+             actor: followed,
              object: follow_activity.data["id"],
              type: "Accept"
            }) do
@@ -590,7 +590,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
          {:ok, _activity} <-
            ActivityPub.reject(%{
              to: [follower.ap_id],
-             actor: followed.ap_id,
+             actor: followed,
              object: follow_activity.data["id"],
              type: "Reject"
            }) do
index a09450df74629f63d92746aee30309cd1286712f..df73844761af4cfb3e02044ca8b8da7ef827e6af 100644 (file)
@@ -113,10 +113,12 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
       "fields" => fields,
 
       # Pleroma extension
-      "pleroma" => %{
-        "confirmation_pending" => user_info.confirmation_pending,
-        "tags" => user.tags
-      }
+      "pleroma" =>
+        %{
+          "confirmation_pending" => user_info.confirmation_pending,
+          "tags" => user.tags
+        }
+        |> maybe_with_follow_request_count(user, for_user)
     }
 
     data =
@@ -132,6 +134,14 @@ defmodule Pleroma.Web.TwitterAPI.UserView do
     end
   end
 
+  defp maybe_with_follow_request_count(data, %User{id: id, info: %{locked: true}} = user, %User{
+         id: id
+       }) do
+    Map.put(data, "follow_request_count", user.info.follow_request_count)
+  end
+
+  defp maybe_with_follow_request_count(data, _, _), do: data
+
   defp maybe_with_role(data, %User{id: id} = user, %User{id: id}) do
     Map.merge(data, %{"role" => role(user), "show_role" => user.info.show_role})
   end
index 26c9c25a619b7e2fe2d7cb16d06f9d3e82c499e5..7749c5ded228fa37f9fe7222df5c591d7a32b45e 100644 (file)
@@ -937,7 +937,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     end
 
     test "/api/v1/follow_requests/:id/authorize works" do
-      user = insert(:user, %{info: %Pleroma.User.Info{locked: true}})
+      user = insert(:user, %{info: %User.Info{locked: true}})
       other_user = insert(:user)
 
       {:ok, _activity} = ActivityPub.follow(other_user, user)
@@ -946,6 +946,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
       other_user = Repo.get(User, other_user.id)
 
       assert User.following?(other_user, user) == false
+      assert user.info.follow_request_count == 1
 
       conn =
         build_conn()
@@ -959,6 +960,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
       other_user = Repo.get(User, other_user.id)
 
       assert User.following?(other_user, user) == true
+      assert user.info.follow_request_count == 0
     end
 
     test "verify_credentials", %{conn: conn} do
@@ -979,6 +981,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
 
       {:ok, _activity} = ActivityPub.follow(other_user, user)
 
+      user = Repo.get(User, user.id)
+      assert user.info.follow_request_count == 1
+
       conn =
         build_conn()
         |> assign(:user, user)
@@ -991,6 +996,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
       other_user = Repo.get(User, other_user.id)
 
       assert User.following?(other_user, user) == false
+      assert user.info.follow_request_count == 0
     end
   end
 
index acb03b146fee5993f4f24c281902fda4c68ed1a4..50b19fd86d6cfba241b305129e1c9c37f0798c4b 100644 (file)
@@ -640,6 +640,24 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
       assert json_response(conn, 200) ==
                UserView.render("show.json", %{user: followed, for: current_user})
     end
+
+    test "for restricted account", %{conn: conn, user: current_user} do
+      followed = insert(:user, info: %User.Info{locked: true})
+
+      conn =
+        conn
+        |> with_credentials(current_user.nickname, "test")
+        |> post("/api/friendships/create.json", %{user_id: followed.id})
+
+      current_user = Repo.get(User, current_user.id)
+      followed = Repo.get(User, followed.id)
+
+      refute User.ap_followers(followed) in current_user.following
+      assert followed.info.follow_request_count == 1
+
+      assert json_response(conn, 200) ==
+               UserView.render("show.json", %{user: followed, for: current_user})
+    end
   end
 
   describe "POST /friendships/destroy.json" do
@@ -1676,15 +1694,19 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
       other_user = Repo.get(User, other_user.id)
 
       assert User.following?(other_user, user) == false
+      assert user.info.follow_request_count == 1
 
       conn =
         build_conn()
         |> assign(:user, user)
         |> post("/api/pleroma/friendships/approve", %{"user_id" => other_user.id})
 
+      user = Repo.get(User, user.id)
+
       assert relationship = json_response(conn, 200)
       assert other_user.id == relationship["id"]
       assert relationship["follows_you"] == true
+      assert user.info.follow_request_count == 0
     end
   end
 
@@ -1699,15 +1721,19 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
       other_user = Repo.get(User, other_user.id)
 
       assert User.following?(other_user, user) == false
+      assert user.info.follow_request_count == 1
 
       conn =
         build_conn()
         |> assign(:user, user)
         |> post("/api/pleroma/friendships/deny", %{"user_id" => other_user.id})
 
+      user = Repo.get(User, user.id)
+
       assert relationship = json_response(conn, 200)
       assert other_user.id == relationship["id"]
       assert relationship["follows_you"] == false
+      assert user.info.follow_request_count == 0
     end
   end