Merge remote-tracking branch 'remotes/origin/develop' into 1559-follow-request-notifi...
[akkoma] / lib / pleroma / following_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.FollowingRelationship do
6 use Ecto.Schema
7
8 import Ecto.Changeset
9 import Ecto.Query
10
11 alias Ecto.Changeset
12 alias FlakeId.Ecto.CompatType
13 alias Pleroma.Repo
14 alias Pleroma.User
15
16 schema "following_relationships" do
17 field(:state, Pleroma.FollowingRelationship.State, default: :follow_pending)
18
19 belongs_to(:follower, User, type: CompatType)
20 belongs_to(:following, User, type: CompatType)
21
22 timestamps()
23 end
24
25 def changeset(%__MODULE__{} = following_relationship, attrs) do
26 following_relationship
27 |> cast(attrs, [:state])
28 |> put_assoc(:follower, attrs.follower)
29 |> put_assoc(:following, attrs.following)
30 |> validate_required([:state, :follower, :following])
31 |> unique_constraint(:follower_id,
32 name: :following_relationships_follower_id_following_id_index
33 )
34 |> validate_not_self_relationship()
35 end
36
37 def state_to_enum(state) when state in ["pending", "accept", "reject"] do
38 String.to_existing_atom("follow_#{state}")
39 end
40
41 def state_to_enum(state) do
42 raise "State is not convertible to Pleroma.FollowingRelationship.State: #{state}"
43 end
44
45 def get(%User{} = follower, %User{} = following) do
46 __MODULE__
47 |> where(follower_id: ^follower.id, following_id: ^following.id)
48 |> Repo.one()
49 end
50
51 def update(follower, following, :follow_reject), do: unfollow(follower, following)
52
53 def update(%User{} = follower, %User{} = following, state) do
54 case get(follower, following) do
55 nil ->
56 follow(follower, following, state)
57
58 following_relationship ->
59 following_relationship
60 |> cast(%{state: state}, [:state])
61 |> validate_required([:state])
62 |> Repo.update()
63 end
64 end
65
66 def follow(%User{} = follower, %User{} = following, state \\ :follow_accept) do
67 %__MODULE__{}
68 |> changeset(%{follower: follower, following: following, state: state})
69 |> Repo.insert(on_conflict: :nothing)
70 end
71
72 def unfollow(%User{} = follower, %User{} = following) do
73 case get(follower, following) do
74 %__MODULE__{} = following_relationship -> Repo.delete(following_relationship)
75 _ -> {:ok, nil}
76 end
77 end
78
79 def follower_count(%User{} = user) do
80 %{followers: user, deactivated: false}
81 |> User.Query.build()
82 |> Repo.aggregate(:count, :id)
83 end
84
85 def following_count(%User{id: nil}), do: 0
86
87 def following_count(%User{} = user) do
88 %{friends: user, deactivated: false}
89 |> User.Query.build()
90 |> Repo.aggregate(:count, :id)
91 end
92
93 def get_follow_requests(%User{id: id}) do
94 __MODULE__
95 |> join(:inner, [r], f in assoc(r, :follower))
96 |> where([r], r.state == ^:follow_pending)
97 |> where([r], r.following_id == ^id)
98 |> select([r, f], f)
99 |> Repo.all()
100 end
101
102 def following?(%User{id: follower_id}, %User{id: followed_id}) do
103 __MODULE__
104 |> where(follower_id: ^follower_id, following_id: ^followed_id, state: ^:follow_accept)
105 |> Repo.exists?()
106 end
107
108 def following(%User{} = user) do
109 following =
110 __MODULE__
111 |> join(:inner, [r], u in User, on: r.following_id == u.id)
112 |> where([r], r.follower_id == ^user.id)
113 |> where([r], r.state == ^:follow_accept)
114 |> select([r, u], u.follower_address)
115 |> Repo.all()
116
117 if not user.local or user.invisible do
118 following
119 else
120 [user.follower_address | following]
121 end
122 end
123
124 def move_following(origin, target) do
125 __MODULE__
126 |> join(:inner, [r], f in assoc(r, :follower))
127 |> where(following_id: ^origin.id)
128 |> where([r, f], f.allow_following_move == true)
129 |> limit(50)
130 |> preload([:follower])
131 |> Repo.all()
132 |> Enum.map(fn following_relationship ->
133 Repo.delete(following_relationship)
134 Pleroma.Web.CommonAPI.follow(following_relationship.follower, target)
135 end)
136 |> case do
137 [] ->
138 User.update_follower_count(origin)
139 :ok
140
141 _ ->
142 move_following(origin, target)
143 end
144 end
145
146 def all_between_user_sets(
147 source_users,
148 target_users
149 )
150 when is_list(source_users) and is_list(target_users) do
151 source_user_ids = User.binary_id(source_users)
152 target_user_ids = User.binary_id(target_users)
153
154 __MODULE__
155 |> where(
156 fragment(
157 "(follower_id = ANY(?) AND following_id = ANY(?)) OR \
158 (follower_id = ANY(?) AND following_id = ANY(?))",
159 ^source_user_ids,
160 ^target_user_ids,
161 ^target_user_ids,
162 ^source_user_ids
163 )
164 )
165 |> Repo.all()
166 end
167
168 def find(following_relationships, follower, following) do
169 Enum.find(following_relationships, fn
170 fr -> fr.follower_id == follower.id and fr.following_id == following.id
171 end)
172 end
173
174 defp validate_not_self_relationship(%Changeset{} = changeset) do
175 changeset
176 |> validate_follower_id_following_id_inequality()
177 |> validate_following_id_follower_id_inequality()
178 end
179
180 defp validate_follower_id_following_id_inequality(%Changeset{} = changeset) do
181 validate_change(changeset, :follower_id, fn _, follower_id ->
182 if follower_id == get_field(changeset, :following_id) do
183 [source_id: "can't be equal to following_id"]
184 else
185 []
186 end
187 end)
188 end
189
190 defp validate_following_id_follower_id_inequality(%Changeset{} = changeset) do
191 validate_change(changeset, :following_id, fn _, following_id ->
192 if following_id == get_field(changeset, :follower_id) do
193 [target_id: "can't be equal to follower_id"]
194 else
195 []
196 end
197 end)
198 end
199 end