From e80d91c64a53a9d64e48a528f40987c5b36f2ca5 Mon Sep 17 00:00:00 2001
From: William Pitcock <nenolod@dereferenced.org>
Date: Fri, 25 May 2018 09:31:42 +0000
Subject: [PATCH] introduce User.maybe_direct_follow() and use it where we used
 to call User.follow()

---
 lib/pleroma/user.ex                           | 29 +++++++++++++++++++
 .../mastodon_api/mastodon_api_controller.ex   |  2 ++
 lib/pleroma/web/twitter_api/twitter_api.ex    |  1 +
 3 files changed, 32 insertions(+)

diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index 2e57f2b43..e4fb57308 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -168,6 +168,35 @@ defmodule Pleroma.User do
     end
   end
 
+  def maybe_direct_follow(%User{} = follower, %User{info: info} = followed) do
+    user_info = user_info(followed)
+
+    should_direct_follow =
+      cond do
+        # if the account is locked, don't pre-create the relationship
+        user_info.locked == true ->
+          false
+
+        # if the users are blocking each other, we shouldn't even be here, but check for it anyway
+        User.blocks?(follower, followed) == true or User.blocks?(followed, follower) == true ->
+          false
+
+        # if OStatus, then there is no three-way handshake to follow
+        User.ap_enabled?(followed) != true ->
+          true
+
+        # if there are no other reasons not to, just pre-create the relationship
+        true ->
+          true
+      end
+
+    if should_direct_follow do
+      follow(follower, followed)
+    else
+      follower
+    end
+  end
+
   def follow(%User{} = follower, %User{info: info} = followed) do
     ap_followers = followed.follower_address
 
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
index d50d2d9b5..e12d3fb5b 100644
--- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
+++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -429,6 +429,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
 
   def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
     with %User{} = followed <- Repo.get(User, id),
+         {:ok, follower} <- User.maybe_direct_follow(follower, followed),
          {:ok, _activity} <- ActivityPub.follow(follower, followed) do
       render(conn, AccountView, "relationship.json", %{user: follower, target: followed})
     else
@@ -441,6 +442,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
 
   def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
     with %User{} = followed <- Repo.get_by(User, nickname: uri),
+         {:ok, follower} <- User.maybe_direct_follow(follower, followed),
          {:ok, _activity} <- ActivityPub.follow(follower, followed) do
       render(conn, AccountView, "account.json", %{user: followed})
     else
diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex
index 903c99a8e..331efa90b 100644
--- a/lib/pleroma/web/twitter_api/twitter_api.ex
+++ b/lib/pleroma/web/twitter_api/twitter_api.ex
@@ -25,6 +25,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
 
   def follow(%User{} = follower, params) do
     with {:ok, %User{} = followed} <- get_user(params),
+         {:ok, follower} <- User.maybe_direct_follow(follower, followed),
          {:ok, activity} <- ActivityPub.follow(follower, followed) do
       {:ok, follower, followed, activity}
     else