[#2332] Misc. improvements per code change requests.
This commit is contained in:
parent
9c94b6a327
commit
ea9c57b26e
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
import EctoEnum
|
import EctoEnum
|
||||||
|
|
||||||
defenum(UserRelationshipTypeEnum,
|
defenum(Pleroma.UserRelationship.Type,
|
||||||
block: 1,
|
block: 1,
|
||||||
mute: 2,
|
mute: 2,
|
||||||
reblog_mute: 3,
|
reblog_mute: 3,
|
||||||
|
@ -12,7 +12,7 @@
|
||||||
inverse_subscription: 5
|
inverse_subscription: 5
|
||||||
)
|
)
|
||||||
|
|
||||||
defenum(FollowingRelationshipStateEnum,
|
defenum(Pleroma.FollowingRelationship.State,
|
||||||
follow_pending: 1,
|
follow_pending: 1,
|
||||||
follow_accept: 2,
|
follow_accept: 2,
|
||||||
follow_reject: 3
|
follow_reject: 3
|
||||||
|
|
|
@ -8,12 +8,13 @@ defmodule Pleroma.FollowingRelationship do
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
|
alias Ecto.Changeset
|
||||||
alias FlakeId.Ecto.CompatType
|
alias FlakeId.Ecto.CompatType
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
|
|
||||||
schema "following_relationships" do
|
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(:follower, User, type: CompatType)
|
||||||
belongs_to(:following, User, type: CompatType)
|
belongs_to(:following, User, type: CompatType)
|
||||||
|
@ -33,13 +34,12 @@ def changeset(%__MODULE__{} = following_relationship, attrs) do
|
||||||
|> validate_not_self_relationship()
|
|> validate_not_self_relationship()
|
||||||
end
|
end
|
||||||
|
|
||||||
def state_to_enum(state) when is_binary(state) do
|
def state_to_enum(state) when state in ["pending", "accept", "reject"] do
|
||||||
case state do
|
String.to_existing_atom("follow_#{state}")
|
||||||
"pending" -> :follow_pending
|
end
|
||||||
"accept" -> :follow_accept
|
|
||||||
"reject" -> :follow_reject
|
def state_to_enum(state) do
|
||||||
_ -> raise "State is not convertible to FollowingRelationshipStateEnum: #{state}"
|
raise "State is not convertible to Pleroma.FollowingRelationship.State: #{state}"
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(%User{} = follower, %User{} = following) do
|
def get(%User{} = follower, %User{} = following) do
|
||||||
|
@ -171,16 +171,14 @@ def find(following_relationships, follower, following) do
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
|
defp validate_not_self_relationship(%Changeset{} = changeset) do
|
||||||
changeset
|
changeset
|
||||||
|> validate_change(:following_id, fn _, following_id ->
|
|> validate_follower_id_following_id_inequality()
|
||||||
if following_id == get_field(changeset, :follower_id) do
|
|> validate_following_id_follower_id_inequality()
|
||||||
[target_id: "can't be equal to follower_id"]
|
end
|
||||||
else
|
|
||||||
[]
|
defp validate_follower_id_following_id_inequality(%Changeset{} = changeset) do
|
||||||
end
|
validate_change(changeset, :follower_id, fn _, follower_id ->
|
||||||
end)
|
|
||||||
|> validate_change(:follower_id, fn _, follower_id ->
|
|
||||||
if follower_id == get_field(changeset, :following_id) do
|
if follower_id == get_field(changeset, :following_id) do
|
||||||
[source_id: "can't be equal to following_id"]
|
[source_id: "can't be equal to following_id"]
|
||||||
else
|
else
|
||||||
|
@ -188,4 +186,14 @@ defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
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
|
||||||
|
end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -769,7 +769,7 @@ def unfollow(%User{} = follower, %User{} = followed) do
|
||||||
|
|
||||||
defdelegate following?(follower, followed), to: FollowingRelationship
|
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
|
def get_follow_state(%User{} = follower, %User{} = following) do
|
||||||
following_relationship = FollowingRelationship.get(follower, following)
|
following_relationship = FollowingRelationship.get(follower, following)
|
||||||
get_follow_state(follower, following, following_relationship)
|
get_follow_state(follower, following, following_relationship)
|
||||||
|
|
|
@ -8,6 +8,7 @@ defmodule Pleroma.UserRelationship do
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
|
alias Ecto.Changeset
|
||||||
alias Pleroma.FollowingRelationship
|
alias Pleroma.FollowingRelationship
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
|
@ -16,12 +17,12 @@ defmodule Pleroma.UserRelationship do
|
||||||
schema "user_relationships" do
|
schema "user_relationships" do
|
||||||
belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
|
belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
|
||||||
belongs_to(:target, 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)
|
timestamps(updated_at: false)
|
||||||
end
|
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_block/2`, `def create_mute/2`, `def create_reblog_mute/2`,
|
||||||
# `def create_notification_mute/2`, `def create_inverse_subscription/2`
|
# `def create_notification_mute/2`, `def create_inverse_subscription/2`
|
||||||
def unquote(:"create_#{relationship_type}")(source, target),
|
def unquote(:"create_#{relationship_type}")(source, target),
|
||||||
|
@ -40,7 +41,7 @@ def unquote(:"#{relationship_type}_exists?")(source, target),
|
||||||
|
|
||||||
def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
|
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
|
def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
|
||||||
user_relationship
|
user_relationship
|
||||||
|
@ -147,16 +148,14 @@ def view_relationships_option(%User{} = reading_user, actors) do
|
||||||
%{user_relationships: user_relationships, following_relationships: following_relationships}
|
%{user_relationships: user_relationships, following_relationships: following_relationships}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
|
defp validate_not_self_relationship(%Changeset{} = changeset) do
|
||||||
changeset
|
changeset
|
||||||
|> validate_change(:target_id, fn _, target_id ->
|
|> validate_source_id_target_id_inequality()
|
||||||
if target_id == get_field(changeset, :source_id) do
|
|> validate_target_id_source_id_inequality()
|
||||||
[target_id: "can't be equal to source_id"]
|
end
|
||||||
else
|
|
||||||
[]
|
defp validate_source_id_target_id_inequality(%Changeset{} = changeset) do
|
||||||
end
|
validate_change(changeset, :source_id, fn _, source_id ->
|
||||||
end)
|
|
||||||
|> validate_change(:source_id, fn _, source_id ->
|
|
||||||
if source_id == get_field(changeset, :target_id) do
|
if source_id == get_field(changeset, :target_id) do
|
||||||
[source_id: "can't be equal to target_id"]
|
[source_id: "can't be equal to target_id"]
|
||||||
else
|
else
|
||||||
|
@ -164,4 +163,14 @@ defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
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
|
||||||
|
end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
defmodule Pleroma.Repo.Migrations.ChangeFollowingRelationshipsStateToInteger do
|
defmodule Pleroma.Repo.Migrations.ChangeFollowingRelationshipsStateToInteger do
|
||||||
use Ecto.Migration
|
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
|
def up do
|
||||||
execute("""
|
execute("""
|
||||||
#{@alter_apps_scopes} TYPE integer USING
|
#{@alter_following_relationship_state} TYPE integer USING
|
||||||
CASE
|
CASE
|
||||||
WHEN state = 'pending' THEN 1
|
WHEN state = 'pending' THEN 1
|
||||||
WHEN state = 'accept' THEN 2
|
WHEN state = 'accept' THEN 2
|
||||||
|
@ -17,7 +17,7 @@ def up do
|
||||||
|
|
||||||
def down do
|
def down do
|
||||||
execute("""
|
execute("""
|
||||||
#{@alter_apps_scopes} TYPE varchar(255) USING
|
#{@alter_following_relationship_state} TYPE varchar(255) USING
|
||||||
CASE
|
CASE
|
||||||
WHEN state = 1 THEN 'pending'
|
WHEN state = 1 THEN 'pending'
|
||||||
WHEN state = 2 THEN 'accept'
|
WHEN state = 2 THEN 'accept'
|
||||||
|
|
Loading…
Reference in a new issue