[#2332] Misc. improvements per code change requests.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Tue, 31 Mar 2020 06:21:42 +0000 (09:21 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Tue, 31 Mar 2020 06:21:42 +0000 (09:21 +0300)
lib/pleroma/ecto_enums.ex
lib/pleroma/following_relationship.ex
lib/pleroma/user.ex
lib/pleroma/user_relationship.ex
priv/repo/migrations/20200328124805_change_following_relationships_state_to_integer.exs

index b98ac4ba132caa335b7c0be9513aa441525c862b..6fc47620c7760c74d85a4b0be04656920a924550 100644 (file)
@@ -4,7 +4,7 @@
 
 import EctoEnum
 
-defenum(UserRelationshipTypeEnum,
+defenum(Pleroma.UserRelationship.Type,
   block: 1,
   mute: 2,
   reblog_mute: 3,
@@ -12,7 +12,7 @@ defenum(UserRelationshipTypeEnum,
   inverse_subscription: 5
 )
 
-defenum(FollowingRelationshipStateEnum,
+defenum(Pleroma.FollowingRelationship.State,
   follow_pending: 1,
   follow_accept: 2,
   follow_reject: 3
index a28da8bec183560aa97373919870b3358358d4ca..9ccf4049571f4303f8e952874dd4e940572f2eff 100644 (file)
@@ -8,12 +8,13 @@ defmodule Pleroma.FollowingRelationship do
   import Ecto.Changeset
   import Ecto.Query
 
+  alias Ecto.Changeset
   alias FlakeId.Ecto.CompatType
   alias Pleroma.Repo
   alias Pleroma.User
 
   schema "following_relationships" do
-    field(:state, FollowingRelationshipStateEnum, default: :follow_pending)
+    field(:state, Pleroma.FollowingRelationship.State, default: :follow_pending)
 
     belongs_to(:follower, User, type: CompatType)
     belongs_to(:following, User, type: CompatType)
@@ -33,13 +34,12 @@ defmodule Pleroma.FollowingRelationship do
     |> validate_not_self_relationship()
   end
 
-  def state_to_enum(state) when is_binary(state) do
-    case state do
-      "pending" -> :follow_pending
-      "accept" -> :follow_accept
-      "reject" -> :follow_reject
-      _ -> raise "State is not convertible to FollowingRelationshipStateEnum: #{state}"
-    end
+  def state_to_enum(state) when state in ["pending", "accept", "reject"] do
+    String.to_existing_atom("follow_#{state}")
+  end
+
+  def state_to_enum(state) do
+    raise "State is not convertible to Pleroma.FollowingRelationship.State: #{state}"
   end
 
   def get(%User{} = follower, %User{} = following) do
@@ -171,18 +171,26 @@ defmodule Pleroma.FollowingRelationship do
     end)
   end
 
-  defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
+  defp validate_not_self_relationship(%Changeset{} = changeset) do
     changeset
-    |> validate_change(:following_id, fn _, following_id ->
-      if following_id == get_field(changeset, :follower_id) do
-        [target_id: "can't be equal to follower_id"]
+    |> validate_follower_id_following_id_inequality()
+    |> validate_following_id_follower_id_inequality()
+  end
+
+  defp validate_follower_id_following_id_inequality(%Changeset{} = changeset) do
+    validate_change(changeset, :follower_id, fn _, follower_id ->
+      if follower_id == get_field(changeset, :following_id) do
+        [source_id: "can't be equal to following_id"]
       else
         []
       end
     end)
-    |> validate_change(:follower_id, fn _, follower_id ->
-      if follower_id == get_field(changeset, :following_id) do
-        [source_id: "can't be equal to following_id"]
+  end
+
+  defp validate_following_id_follower_id_inequality(%Changeset{} = changeset) do
+    validate_change(changeset, :following_id, fn _, following_id ->
+      if following_id == get_field(changeset, :follower_id) do
+        [target_id: "can't be equal to follower_id"]
       else
         []
       end
index 6ffb820459a675bb7282acc50e3f271c3a4ccf4a..4f3abd7d5c521cb258e79758eba6fe8e4e8914d7 100644 (file)
@@ -769,7 +769,7 @@ defmodule Pleroma.User do
 
   defdelegate following?(follower, followed), to: FollowingRelationship
 
-  @doc "Returns follow state as FollowingRelationshipStateEnum value"
+  @doc "Returns follow state as Pleroma.FollowingRelationship.State value"
   def get_follow_state(%User{} = follower, %User{} = following) do
     following_relationship = FollowingRelationship.get(follower, following)
     get_follow_state(follower, following, following_relationship)
index 18a5eec7262bc1816753dea928b64025ebe08deb..ad0d303b1c2973ce8f62ca8d9936cc83cbc05e80 100644 (file)
@@ -8,6 +8,7 @@ defmodule Pleroma.UserRelationship do
   import Ecto.Changeset
   import Ecto.Query
 
+  alias Ecto.Changeset
   alias Pleroma.FollowingRelationship
   alias Pleroma.Repo
   alias Pleroma.User
@@ -16,12 +17,12 @@ defmodule Pleroma.UserRelationship do
   schema "user_relationships" do
     belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
     belongs_to(:target, User, type: FlakeId.Ecto.CompatType)
-    field(:relationship_type, UserRelationshipTypeEnum)
+    field(:relationship_type, Pleroma.UserRelationship.Type)
 
     timestamps(updated_at: false)
   end
 
-  for relationship_type <- Keyword.keys(UserRelationshipTypeEnum.__enum_map__()) do
+  for relationship_type <- Keyword.keys(Pleroma.UserRelationship.Type.__enum_map__()) do
     # `def create_block/2`, `def create_mute/2`, `def create_reblog_mute/2`,
     #   `def create_notification_mute/2`, `def create_inverse_subscription/2`
     def unquote(:"create_#{relationship_type}")(source, target),
@@ -40,7 +41,7 @@ defmodule Pleroma.UserRelationship do
 
   def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
 
-  def user_relationship_mappings, do: UserRelationshipTypeEnum.__enum_map__()
+  def user_relationship_mappings, do: Pleroma.UserRelationship.Type.__enum_map__()
 
   def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
     user_relationship
@@ -147,18 +148,26 @@ defmodule Pleroma.UserRelationship do
     %{user_relationships: user_relationships, following_relationships: following_relationships}
   end
 
-  defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
+  defp validate_not_self_relationship(%Changeset{} = changeset) do
     changeset
-    |> validate_change(:target_id, fn _, target_id ->
-      if target_id == get_field(changeset, :source_id) do
-        [target_id: "can't be equal to source_id"]
+    |> validate_source_id_target_id_inequality()
+    |> validate_target_id_source_id_inequality()
+  end
+
+  defp validate_source_id_target_id_inequality(%Changeset{} = changeset) do
+    validate_change(changeset, :source_id, fn _, source_id ->
+      if source_id == get_field(changeset, :target_id) do
+        [source_id: "can't be equal to target_id"]
       else
         []
       end
     end)
-    |> validate_change(:source_id, fn _, source_id ->
-      if source_id == get_field(changeset, :target_id) do
-        [source_id: "can't be equal to target_id"]
+  end
+
+  defp validate_target_id_source_id_inequality(%Changeset{} = changeset) do
+    validate_change(changeset, :target_id, fn _, target_id ->
+      if target_id == get_field(changeset, :source_id) do
+        [target_id: "can't be equal to source_id"]
       else
         []
       end
index d5a431c0000b14520d72af18b19151d9c72ac26c..2b0820f3f9be3ae1bb0e4ac8ed50cab3100707e1 100644 (file)
@@ -1,11 +1,11 @@
 defmodule Pleroma.Repo.Migrations.ChangeFollowingRelationshipsStateToInteger do
   use Ecto.Migration
 
-  @alter_apps_scopes "ALTER TABLE following_relationships ALTER COLUMN state"
+  @alter_following_relationship_state "ALTER TABLE following_relationships ALTER COLUMN state"
 
   def up do
     execute("""
-    #{@alter_apps_scopes} TYPE integer USING
+    #{@alter_following_relationship_state} TYPE integer USING
     CASE
       WHEN state = 'pending' THEN 1
       WHEN state = 'accept' THEN 2
@@ -17,7 +17,7 @@ defmodule Pleroma.Repo.Migrations.ChangeFollowingRelationshipsStateToInteger do
 
   def down do
     execute("""
-    #{@alter_apps_scopes} TYPE varchar(255) USING
+    #{@alter_following_relationship_state} TYPE varchar(255) USING
     CASE
       WHEN state = 1 THEN 'pending'
       WHEN state = 2 THEN 'accept'