Merge branch 'develop' into gun
[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.FollowingRelationship
12 alias Pleroma.Repo
13 alias Pleroma.User
14 alias Pleroma.UserRelationship
15
16 schema "user_relationships" do
17 belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
18 belongs_to(:target, User, type: FlakeId.Ecto.CompatType)
19 field(:relationship_type, UserRelationshipTypeEnum)
20
21 timestamps(updated_at: false)
22 end
23
24 for relationship_type <- Keyword.keys(UserRelationshipTypeEnum.__enum_map__()) do
25 # `def create_block/2`, `def create_mute/2`, `def create_reblog_mute/2`,
26 # `def create_notification_mute/2`, `def create_inverse_subscription/2`
27 def unquote(:"create_#{relationship_type}")(source, target),
28 do: create(unquote(relationship_type), source, target)
29
30 # `def delete_block/2`, `def delete_mute/2`, `def delete_reblog_mute/2`,
31 # `def delete_notification_mute/2`, `def delete_inverse_subscription/2`
32 def unquote(:"delete_#{relationship_type}")(source, target),
33 do: delete(unquote(relationship_type), source, target)
34
35 # `def block_exists?/2`, `def mute_exists?/2`, `def reblog_mute_exists?/2`,
36 # `def notification_mute_exists?/2`, `def inverse_subscription_exists?/2`
37 def unquote(:"#{relationship_type}_exists?")(source, target),
38 do: exists?(unquote(relationship_type), source, target)
39 end
40
41 def user_relationship_types, do: Keyword.keys(user_relationship_mappings())
42
43 def user_relationship_mappings, do: UserRelationshipTypeEnum.__enum_map__()
44
45 def changeset(%UserRelationship{} = user_relationship, params \\ %{}) do
46 user_relationship
47 |> cast(params, [:relationship_type, :source_id, :target_id])
48 |> validate_required([:relationship_type, :source_id, :target_id])
49 |> unique_constraint(:relationship_type,
50 name: :user_relationships_source_id_relationship_type_target_id_index
51 )
52 |> validate_not_self_relationship()
53 end
54
55 def exists?(relationship_type, %User{} = source, %User{} = target) do
56 UserRelationship
57 |> where(relationship_type: ^relationship_type, source_id: ^source.id, target_id: ^target.id)
58 |> Repo.exists?()
59 end
60
61 def create(relationship_type, %User{} = source, %User{} = target) do
62 %UserRelationship{}
63 |> changeset(%{
64 relationship_type: relationship_type,
65 source_id: source.id,
66 target_id: target.id
67 })
68 |> Repo.insert(
69 on_conflict: {:replace_all_except, [:id]},
70 conflict_target: [:source_id, :relationship_type, :target_id]
71 )
72 end
73
74 def delete(relationship_type, %User{} = source, %User{} = target) do
75 attrs = %{relationship_type: relationship_type, source_id: source.id, target_id: target.id}
76
77 case Repo.get_by(UserRelationship, attrs) do
78 %UserRelationship{} = existing_record -> Repo.delete(existing_record)
79 nil -> {:ok, nil}
80 end
81 end
82
83 def dictionary(
84 source_users,
85 target_users,
86 source_to_target_rel_types \\ nil,
87 target_to_source_rel_types \\ nil
88 )
89 when is_list(source_users) and is_list(target_users) do
90 source_user_ids = User.binary_id(source_users)
91 target_user_ids = User.binary_id(target_users)
92
93 get_rel_type_codes = fn rel_type -> user_relationship_mappings()[rel_type] end
94
95 source_to_target_rel_types =
96 Enum.map(source_to_target_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
97
98 target_to_source_rel_types =
99 Enum.map(target_to_source_rel_types || user_relationship_types(), &get_rel_type_codes.(&1))
100
101 __MODULE__
102 |> where(
103 fragment(
104 "(source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?)) OR \
105 (source_id = ANY(?) AND target_id = ANY(?) AND relationship_type = ANY(?))",
106 ^source_user_ids,
107 ^target_user_ids,
108 ^source_to_target_rel_types,
109 ^target_user_ids,
110 ^source_user_ids,
111 ^target_to_source_rel_types
112 )
113 )
114 |> select([ur], [ur.relationship_type, ur.source_id, ur.target_id])
115 |> Repo.all()
116 end
117
118 def exists?(dictionary, rel_type, source, target, func) do
119 cond do
120 is_nil(source) or is_nil(target) ->
121 false
122
123 dictionary ->
124 [rel_type, source.id, target.id] in dictionary
125
126 true ->
127 func.(source, target)
128 end
129 end
130
131 @doc ":relationships option for StatusView / AccountView / NotificationView"
132 def view_relationships_option(nil = _reading_user, _actors) do
133 %{user_relationships: [], following_relationships: []}
134 end
135
136 def view_relationships_option(%User{} = reading_user, actors) do
137 user_relationships =
138 UserRelationship.dictionary(
139 [reading_user],
140 actors,
141 [:block, :mute, :notification_mute, :reblog_mute],
142 [:block, :inverse_subscription]
143 )
144
145 following_relationships = FollowingRelationship.all_between_user_sets([reading_user], actors)
146
147 %{user_relationships: user_relationships, following_relationships: following_relationships}
148 end
149
150 defp validate_not_self_relationship(%Ecto.Changeset{} = changeset) do
151 changeset
152 |> validate_change(:target_id, fn _, target_id ->
153 if target_id == get_field(changeset, :source_id) do
154 [target_id: "can't be equal to source_id"]
155 else
156 []
157 end
158 end)
159 |> validate_change(:source_id, fn _, source_id ->
160 if source_id == get_field(changeset, :target_id) do
161 [source_id: "can't be equal to target_id"]
162 else
163 []
164 end
165 end)
166 end
167 end