move user disable into deactivation
authorEgor Kislitsyn <egor@kislitsyn.com>
Thu, 11 Apr 2019 10:22:42 +0000 (17:22 +0700)
committerEgor Kislitsyn <egor@kislitsyn.com>
Thu, 11 Apr 2019 10:22:42 +0000 (17:22 +0700)
12 files changed:
lib/mix/tasks/pleroma/user.ex
lib/pleroma/activity.ex
lib/pleroma/notification.ex
lib/pleroma/user.ex
lib/pleroma/user/info.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/admin_api/admin_api_controller.ex
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/controllers/util_controller.ex
lib/pleroma/web/twitter_api/twitter_api.ex
priv/repo/migrations/20190228121252_users_add_disabled_index.exs [deleted file]
priv/repo/migrations/20190411094120_add_index_on_user_info_deactivated.exs [new file with mode: 0644]

index 78493231cf07c3cf763c5a49bf455ce37516903d..441168df2da7953c8a5a45e5cc3b25c16c2c92d6 100644 (file)
@@ -53,10 +53,6 @@ defmodule Mix.Tasks.Pleroma.User do
 
       mix pleroma.user toggle_activated NICKNAME
 
-  ## Disable or enable the user's account.
-
-      mix pleroma.user toggle_disabled NICKNAME
-
   ## Unsubscribe local users from user's account and deactivate it
 
       mix pleroma.user unsubscribe NICKNAME
@@ -190,20 +186,6 @@ defmodule Mix.Tasks.Pleroma.User do
     end
   end
 
-  def run(["toggle_disabled", nickname]) do
-    Common.start_pleroma()
-
-    case User.get_by_nickname(nickname) do
-      %User{} = user ->
-        {:ok, user} = User.disable(user, !user.info.disabled)
-        status = if(user.info.disabled, do: "ON", else: "OFF")
-        Mix.shell().info("Disabled status of #{nickname}: #{status}")
-
-      _ ->
-        Mix.shell().error("No user #{nickname}")
-    end
-  end
-
   def run(["reset_password", nickname]) do
     Common.start_pleroma()
 
index c8c7f0d040bd0726f50f26ef78634c7b9feb7526..d06fd917d9921d06c12f93de02ba016bd13ed001 100644 (file)
@@ -100,7 +100,7 @@ defmodule Pleroma.Activity do
   def get_by_id(id) do
     Activity
     |> where([a], a.id == ^id)
-    |> restrict_disabled_users()
+    |> restrict_deactivated_users()
     |> Repo.one()
   end
 
@@ -169,7 +169,7 @@ defmodule Pleroma.Activity do
 
   def get_create_by_object_ap_id(ap_id) when is_binary(ap_id) do
     create_by_object_ap_id(ap_id)
-    |> restrict_disabled_users()
+    |> restrict_deactivated_users()
     |> Repo.one()
   end
 
@@ -296,11 +296,11 @@ defmodule Pleroma.Activity do
     end
   end
 
-  def restrict_disabled_users(query) do
+  def restrict_deactivated_users(query) do
     from(activity in query,
       where:
         fragment(
-          "? not in (SELECT ap_id FROM users WHERE info->'disabled' @> 'true')",
+          "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
           activity.actor
         )
     )
index 7de2d4c189b47633fffd97aac4fd553f77b6f959..941218eea1368abd81d41d8c4c8ad35cb50b6dc6 100644 (file)
@@ -36,7 +36,7 @@ defmodule Pleroma.Notification do
     |> where(
       [n, a],
       fragment(
-        "? not in (SELECT ap_id FROM users WHERE info->'disabled' @> 'true')",
+        "? not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')",
         a.actor
       )
     )
index 1f2aca235b05d9710c6a045e4831f060b1e43528..c08d3a171711af43f3077cdd51c2e287dd171297 100644 (file)
@@ -117,9 +117,9 @@ defmodule Pleroma.User do
     }
   end
 
-  defp restrict_disabled(query) do
+  defp restrict_deactivated(query) do
     from(u in query,
-      where: not fragment("? \\? 'disabled' AND ?->'disabled' @> 'true'", u.info, u.info)
+      where: not fragment("? \\? 'deactivated' AND ?->'deactivated' @> 'true'", u.info, u.info)
     )
   end
 
@@ -130,7 +130,7 @@ defmodule Pleroma.User do
       where: u.follower_address in ^following,
       where: u.id != ^id
     )
-    |> restrict_disabled()
+    |> restrict_deactivated()
     |> Repo.aggregate(:count, :id)
   end
 
@@ -584,7 +584,7 @@ defmodule Pleroma.User do
       where: fragment("? <@ ?", ^[follower_address], u.following),
       where: u.id != ^id
     )
-    |> restrict_disabled()
+    |> restrict_deactivated()
   end
 
   def get_followers_query(user, page) do
@@ -612,7 +612,7 @@ defmodule Pleroma.User do
       where: u.follower_address in ^following,
       where: u.id != ^id
     )
-    |> restrict_disabled()
+    |> restrict_deactivated()
   end
 
   def get_friends_query(user, page) do
@@ -736,7 +736,7 @@ defmodule Pleroma.User do
       |> where([u], ^user.follower_address in u.following)
       |> where([u], u.id != ^user.id)
       |> select([u], %{count: count(u.id)})
-      |> restrict_disabled()
+      |> restrict_deactivated()
 
     User
     |> where(id: ^user.id)
@@ -887,7 +887,7 @@ defmodule Pleroma.User do
           ^processed_query
         )
     )
-    |> restrict_disabled()
+    |> restrict_deactivated()
   end
 
   defp trigram_search_subquery(term) do
@@ -906,7 +906,7 @@ defmodule Pleroma.User do
       },
       where: fragment("trim(? || ' ' || coalesce(?, '')) % ?", u.nickname, u.name, ^term)
     )
-    |> restrict_disabled()
+    |> restrict_deactivated()
   end
 
   def blocks_import(%User{} = blocker, blocked_identifiers) when is_list(blocked_identifiers) do
@@ -1150,13 +1150,24 @@ defmodule Pleroma.User do
     )
   end
 
+
+  def deactivate_async(user, status \\ true) do
+    PleromaJobQueue.enqueue(:user, __MODULE__, [:deactivate_async, user, status])
+  end
+
+  def perform(:deactivate_async, user, status), do: deactivate(user, status)
+
   def deactivate(%User{} = user, status \\ true) do
     info_cng = User.Info.set_activation_status(user.info, status)
 
-    user
+    with {:ok, user} <- user
     |> change()
     |> put_embed(:info, info_cng)
-    |> update_and_set_cache()
+    |> update_and_set_cache(),
+    {:ok, friends} <- User.get_friends(user) do
+      Enum.each(friends, &update_follower_count(&1))
+      {:ok, user}
+    end
   end
 
   def update_notification_settings(%User{} = user, settings \\ %{}) do
@@ -1199,26 +1210,6 @@ defmodule Pleroma.User do
     {:ok, user}
   end
 
-  def disable_async(user, status \\ true) do
-    PleromaJobQueue.enqueue(:user, __MODULE__, [:disable_async, user, status])
-  end
-
-  def disable(%User{} = user, status \\ true) do
-    with {:ok, user} <- User.deactivate(user, status),
-         info_cng <- User.Info.set_disabled_status(user.info, status),
-         {:ok, user} <-
-           user
-           |> change()
-           |> put_embed(:info, info_cng)
-           |> update_and_set_cache(),
-         {:ok, friends} <- User.get_friends(user) do
-      Enum.each(friends, &update_follower_count(&1))
-      {:ok, user}
-    end
-  end
-
-  def perform(:disable_async, user, status), do: disable(user, status)
-
   def html_filter_policy(%User{info: %{no_rich_text: true}}) do
     Pleroma.HTML.Scrubber.TwitterText
   end
index 07825a1c4bc1ec5362c634014f1211c4abaf952a..5afa7988cee7287750e2f6e1a0169732c12cd486 100644 (file)
@@ -40,7 +40,6 @@ defmodule Pleroma.User.Info do
     field(:hide_follows, :boolean, default: false)
     field(:pinned_activities, {:array, :string}, default: [])
     field(:flavour, :string, default: nil)
-    field(:disabled, :boolean, default: false)
 
     field(:notification_settings, :map,
       default: %{"remote" => true, "local" => true, "followers" => true, "follows" => true}
@@ -76,14 +75,6 @@ defmodule Pleroma.User.Info do
     |> validate_required([:notification_settings])
   end
 
-  def set_disabled_status(info, disabled) do
-    params = %{disabled: disabled}
-
-    info
-    |> cast(params, [:disabled])
-    |> validate_required([:disabled])
-  end
-
   def add_to_note_count(info, number) do
     set_note_count(info, info.note_count + number)
   end
index dd51d63c8bb446db6ed01434680fb22a8bca2fa6..e749a80aa65db3671bebd1ee4f24873303c89080 100644 (file)
@@ -804,7 +804,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     |> restrict_reblogs(opts)
     |> restrict_pinned(opts)
     |> restrict_muted_reblogs(opts)
-    |> Activity.restrict_disabled_users()
+    |> Activity.restrict_deactivated_users()
   end
 
   def fetch_activities(recipients, opts \\ %{}) do
index fb43d0b011674c0f9748594c376eba7150ca241b..70a5b5c5d7dbf4cfaf3821f9978c0453c0345175 100644 (file)
@@ -75,16 +75,6 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     end
   end
 
-  def user_toggle_disabled(conn, %{"nickname" => nickname}) do
-    user = User.get_by_nickname(nickname)
-
-    {:ok, updated_user} = User.disable(user, !user.info.disabled)
-
-    conn
-    |> put_view(AccountView)
-    |> render("show.json", %{user: updated_user})
-  end
-
   def user_toggle_activation(conn, %{"nickname" => nickname}) do
     user = User.get_by_nickname(nickname)
 
index dd23d7fd5a68fee4f6719fb0f0f666fdd4113eef..c331098b453d73fba120001d7e542cff8bcef786 100644 (file)
@@ -153,7 +153,6 @@ defmodule Pleroma.Web.Router do
 
     delete("/user", AdminAPIController, :user_delete)
     patch("/users/:nickname/toggle_activation", AdminAPIController, :user_toggle_activation)
-    patch("/users/:nickname/toggle_disabled", AdminAPIController, :user_toggle_disabled)
     post("/user", AdminAPIController, :user_create)
     put("/users/tag", AdminAPIController, :tag_users)
     delete("/users/tag", AdminAPIController, :untag_users)
index 317f2b0ffa0b6cdb99004054fd54165f2191a5b1..44f4b183b1aed884f5775bd442e386a89b07f522 100644 (file)
@@ -358,7 +358,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
   def disable_account(%{assigns: %{user: user}} = conn, params) do
     case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
       {:ok, user} ->
-        User.disable_async(user)
+        User.deactivate_async(user)
         json(conn, %{status: "success"})
 
       {:error, msg} ->
index 156f5d40f4a3ebba40085ccfee980bc183a987d2..bf1051afdd57583bce85e66424041a6f9b0862f0 100644 (file)
@@ -235,7 +235,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
           nil ->
             {:error, "No user with such user_id"}
 
-          %User{info: %{disabled: true}} ->
+          %User{info: %{deactivated: true}} ->
             {:error, "User has been disabled"}
 
           user ->
diff --git a/priv/repo/migrations/20190228121252_users_add_disabled_index.exs b/priv/repo/migrations/20190228121252_users_add_disabled_index.exs
deleted file mode 100644 (file)
index 7b921d3..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-defmodule Pleroma.Repo.Migrations.UsersAddDisabledIndex do
-  use Ecto.Migration
-
-  def change do
-    create(index(:users, ["(info->'disabled')"], name: :users_disabled_index, using: :gin))
-  end
-end
diff --git a/priv/repo/migrations/20190411094120_add_index_on_user_info_deactivated.exs b/priv/repo/migrations/20190411094120_add_index_on_user_info_deactivated.exs
new file mode 100644 (file)
index 0000000..d701dce
--- /dev/null
@@ -0,0 +1,7 @@
+defmodule Pleroma.Repo.Migrations.AddIndexOnUserInfoDeactivated do
+  use Ecto.Migration
+
+  def change do
+    create(index(:users, ["(info->'deactivated')"], name: :users_deactivated_index, using: :gin))
+  end
+end