Merge branch 'release/2.0.0' into 'stable'
[akkoma] / lib / pleroma / user_relationship.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.UserRelationship do
6 use Ecto.Schema
7
8 import Ecto.Changeset
9 import Ecto.Query
10
11 alias Pleroma.Repo
12 alias Pleroma.User
13 alias Pleroma.UserRelationship
14
15 schema "user_relationships" do
16 belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
17 belongs_to(:target, User, type: FlakeId.Ecto.CompatType)
18 field(:relationship_type, UserRelationshipTypeEnum)
19
20 timestamps(updated_at: false)
21 end
22
23 for relationship_type <- Keyword.keys(UserRelationshipTypeEnum.__enum_map__()) do
24 # Definitions of `create_block/2`, `create_mute/2` etc.
25 def unquote(:"create_#{relationship_type}")(source, target),
26 do: create(unquote(relationship_type), source, target)
27
28 # Definitions of `delete_block/2`, `delete_mute/2` etc.
29 def unquote(:"delete_#{relationship_type}")(source, target),
30 do: delete(unquote(relationship_type), source, target)
31
32 # Definitions of `block_exists?/2`, `mute_exists?/2` etc.
33 def unquote(:"#{relationship_type}_exists?")(source, target),
34 do: exists?(unquote(relationship_type), source, target)
35 end
36
37 def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
38 user_relationship
39 |> cast(params, [:relationship_type, :source_id, :target_id])
40 |> validate_required([:relationship_type, :source_id, :target_id])
41 |> unique_constraint(:relationship_type,
42 name: :user_relationships_source_id_relationship_type_target_id_index
43 )
44 |> validate_not_self_relationship()
45 end
46
47 def exists?(relationship_type, %User{} = source, %User{} = target) do
48 UserRelationship
49 |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
50 |> Repo.exists?()
51 end
52
53 def create(relationship_type, %User{} = source, %User{} = target) do
54 %UserRelationship{}
55 |> changeset(%{
56 relationship_type: relationship_type,
57 source_id: source.id,
58 target_id: target.id
59 })
60 |> Repo.insert(
61 on_conflict: {:replace_all_except, [:id]},
62 conflict_target: [:source_id, :relationship_type, :target_id]
63 )
64 end
65
66 def delete(relationship_type, %User{} = source, %User{} = target) do
67 attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
68
69 case Repo.get_by(UserRelationship, attrs) do
70 %UserRelationship{} = existing_record -> Repo.delete(existing_record)
71 nil -> {:ok, nil}
72 end
73 end
74
75 defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
76 changeset
77 |> validate_change(:target_id, fn _, target_id ->
78 if target_id == get_field(changeset, :source_id) do
79 [target_id: "can't be equal to source_id"]
80 else
81 []
82 end
83 end)
84 |> validate_change(:source_id, fn _, source_id ->
85 if source_id == get_field(changeset, :target_id) do
86 [source_id: "can't be equal to target_id"]
87 else
88 []
89 end
90 end)
91 end
92 end