2019-11-18 17:38:56 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-11-18 17:38:56 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.UserRelationship do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.UserRelationship
|
|
|
|
|
|
|
|
schema "user_relationships" do
|
|
|
|
belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
|
|
|
|
belongs_to(:target, User, type: FlakeId.Ecto.CompatType)
|
|
|
|
field(:relationship_type, UserRelationshipTypeEnum)
|
|
|
|
|
|
|
|
timestamps(updated_at: false)
|
|
|
|
end
|
|
|
|
|
2019-11-19 20:22:10 +00:00
|
|
|
for relationship_type <- Keyword.keys(UserRelationshipTypeEnum.__enum_map__()) do
|
2020-03-15 18:00:12 +00:00
|
|
|
# `def create_block/2`, `def create_mute/2`, `def create_reblog_mute/2`,
|
|
|
|
# `def create_notification_mute/2`, `def create_inverse_subscription/2`
|
2019-11-19 20:22:10 +00:00
|
|
|
def unquote(:"create_#{relationship_type}")(source, target),
|
|
|
|
do: create(unquote(relationship_type), source, target)
|
|
|
|
|
2020-03-15 18:00:12 +00:00
|
|
|
# `def delete_block/2`, `def delete_mute/2`, `def delete_reblog_mute/2`,
|
|
|
|
# `def delete_notification_mute/2`, `def delete_inverse_subscription/2`
|
2019-11-19 20:22:10 +00:00
|
|
|
def unquote(:"delete_#{relationship_type}")(source, target),
|
|
|
|
do: delete(unquote(relationship_type), source, target)
|
|
|
|
|
2020-03-15 18:00:12 +00:00
|
|
|
# `def block_exists?/2`, `def mute_exists?/2`, `def reblog_mute_exists?/2`,
|
|
|
|
# `def notification_mute_exists?/2`, `def inverse_subscription_exists?/2`
|
2019-11-19 20:22:10 +00:00
|
|
|
def unquote(:"#{relationship_type}_exists?")(source, target),
|
|
|
|
do: exists?(unquote(relationship_type), source, target)
|
|
|
|
end
|
|
|
|
|
2019-11-18 17:38:56 +00:00
|
|
|
def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
|
|
|
|
user_relationship
|
|
|
|
|> cast(params, [:relationship_type, :source_id, :target_id])
|
|
|
|
|> validate_required([:relationship_type, :source_id, :target_id])
|
|
|
|
|> unique_constraint(:relationship_type,
|
|
|
|
name: :user_relationships_source_id_relationship_type_target_id_index
|
|
|
|
)
|
|
|
|
|> validate_not_self_relationship()
|
|
|
|
end
|
|
|
|
|
|
|
|
def exists?(relationship_type, %User{} = source, %User{} = target) do
|
|
|
|
UserRelationship
|
|
|
|
|> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
|
|
|
|
|> Repo.exists?()
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(relationship_type, %User{} = source, %User{} = target) do
|
|
|
|
%UserRelationship{}
|
|
|
|
|> changeset(%{
|
|
|
|
relationship_type: relationship_type,
|
|
|
|
source_id: source.id,
|
|
|
|
target_id: target.id
|
|
|
|
})
|
|
|
|
|> Repo.insert(
|
2020-01-28 07:43:47 +00:00
|
|
|
on_conflict: {:replace_all_except, [:id]},
|
2019-11-18 17:38:56 +00:00
|
|
|
conflict_target: [:source_id, :relationship_type, :target_id]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(relationship_type, %User{} = source, %User{} = target) do
|
|
|
|
attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
|
|
|
|
|
|
|
|
case Repo.get_by(UserRelationship, attrs) do
|
|
|
|
%UserRelationship{} = existing_record -> Repo.delete(existing_record)
|
|
|
|
nil -> {:ok, nil}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp validate_not_self_relationship(%Ecto.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"]
|
|
|
|
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"]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|