aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Tashkinov <ivantashkinov@gmail.com>2020-03-31 09:21:42 +0300
committerIvan Tashkinov <ivantashkinov@gmail.com>2020-03-31 09:21:42 +0300
commitea9c57b26ed463622e4489736fcddb8fca1b3341 (patch)
treebc22eaaadc465865517e344385171d64ed445358
parent9c94b6a327118d8c7ea21355d6c378ef31c54321 (diff)
downloadpleroma-ea9c57b26ed463622e4489736fcddb8fca1b3341.tar.gz
[#2332] Misc. improvements per code change requests.
-rw-r--r--lib/pleroma/ecto_enums.ex4
-rw-r--r--lib/pleroma/following_relationship.ex38
-rw-r--r--lib/pleroma/user.ex2
-rw-r--r--lib/pleroma/user_relationship.ex29
-rw-r--r--priv/repo/migrations/20200328124805_change_following_relationships_state_to_integer.exs6
5 files changed, 48 insertions, 31 deletions
diff --git a/lib/pleroma/ecto_enums.ex b/lib/pleroma/ecto_enums.ex
index b98ac4ba1..6fc47620c 100644
--- a/lib/pleroma/ecto_enums.ex
+++ b/lib/pleroma/ecto_enums.ex
@@ -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
diff --git a/lib/pleroma/following_relationship.ex b/lib/pleroma/following_relationship.ex
index a28da8bec..9ccf40495 100644
--- a/lib/pleroma/following_relationship.ex
+++ b/lib/pleroma/following_relationship.ex
@@ -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
diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex
index 6ffb82045..4f3abd7d5 100644
--- a/lib/pleroma/user.ex
+++ b/lib/pleroma/user.ex
@@ -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)
diff --git a/lib/pleroma/user_relationship.ex b/lib/pleroma/user_relationship.ex
index 18a5eec72..ad0d303b1 100644
--- a/lib/pleroma/user_relationship.ex
+++ b/lib/pleroma/user_relationship.ex
@@ -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
diff --git a/priv/repo/migrations/20200328124805_change_following_relationships_state_to_integer.exs b/priv/repo/migrations/20200328124805_change_following_relationships_state_to_integer.exs
index d5a431c00..2b0820f3f 100644
--- a/priv/repo/migrations/20200328124805_change_following_relationships_state_to_integer.exs
+++ b/priv/repo/migrations/20200328124805_change_following_relationships_state_to_integer.exs
@@ -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'