1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.FollowingRelationship do
12 alias FlakeId.Ecto.CompatType
13 alias Pleroma.FollowingRelationship.State
17 @type follow_state :: :follow_pending | :follow_accept | :follow_reject | :unfollow
19 schema "following_relationships" do
20 field(:state, State, default: :follow_pending)
22 belongs_to(:follower, User, type: CompatType)
23 belongs_to(:following, User, type: CompatType)
28 @doc "Returns underlying integer code for state atom"
29 def state_int_code(state_atom), do: State.__enum_map__() |> Keyword.fetch!(state_atom)
31 def accept_state_code, do: state_int_code(:follow_accept)
33 def changeset(%__MODULE__{} = following_relationship, attrs) do
34 following_relationship
35 |> cast(attrs, [:state])
36 |> put_assoc(:follower, attrs.follower)
37 |> put_assoc(:following, attrs.following)
38 |> validate_required([:state, :follower, :following])
39 |> unique_constraint(:follower_id,
40 name: :following_relationships_follower_id_following_id_index
42 |> validate_not_self_relationship()
45 def state_to_enum(state) when state in ["pending", "accept", "reject"] do
46 String.to_existing_atom("follow_#{state}")
49 def state_to_enum(state) do
50 raise "State is not convertible to Pleroma.FollowingRelationship.State: #{state}"
53 def get(%User{} = follower, %User{} = following) do
55 |> where(follower_id: ^follower.id, following_id: ^following.id)
59 def update(follower, following, :follow_reject), do: unfollow(follower, following)
61 def update(%User{} = follower, %User{} = following, state) do
62 case get(follower, following) do
64 follow(follower, following, state)
66 following_relationship ->
67 with {:ok, _following_relationship} <-
68 following_relationship
69 |> cast(%{state: state}, [:state])
70 |> validate_required([:state])
72 after_update(state, follower, following)
77 @spec follow(User.t(), User.t()) :: {:ok, User.t(), User.t()} | {:error, any}
78 def follow(%User{} = follower, %User{} = following, state \\ :follow_accept) do
79 with {:ok, _following_relationship} <-
81 |> changeset(%{follower: follower, following: following, state: state})
82 |> Repo.insert(on_conflict: :nothing) do
83 after_update(state, follower, following)
87 @spec unfollow(User.t(), User.t()) :: {:ok, User.t(), User.t()} | {:error, any}
88 def unfollow(%User{} = follower, %User{} = following) do
89 case get(follower, following) do
90 %__MODULE__{} = following_relationship ->
91 with {:ok, _following_relationship} <- Repo.delete(following_relationship) do
92 after_update(:unfollow, follower, following)
96 {:ok, follower, following}
100 @spec after_update(follow_state(), User.t(), User.t()) ::
101 {:ok, User.t(), User.t()} | {:error, any()}
102 defp after_update(state, %User{} = follower, %User{} = following) do
103 with {:ok, following} <- User.update_follower_count(following),
104 {:ok, follower} <- User.update_following_count(follower) do
105 Pleroma.Web.Streamer.stream("follow_relationship", %{
107 following: following,
111 {:ok, follower, following}
117 def follower_count(%User{} = user) do
118 %{followers: user, deactivated: false}
119 |> User.Query.build()
120 |> Repo.aggregate(:count, :id)
123 def followers_query(%User{} = user) do
125 |> join(:inner, [r], u in User, on: r.follower_id == u.id)
126 |> where([r], r.following_id == ^user.id)
127 |> where([r], r.state == ^:follow_accept)
130 def followers_ap_ids(user, from_ap_ids \\ nil)
132 def followers_ap_ids(_, []), do: []
134 def followers_ap_ids(%User{} = user, from_ap_ids) do
138 |> select([r, u], u.ap_id)
142 where(query, [r, u], u.ap_id in ^from_ap_ids)
150 def following_count(%User{id: nil}), do: 0
152 def following_count(%User{} = user) do
153 %{friends: user, deactivated: false}
154 |> User.Query.build()
155 |> Repo.aggregate(:count, :id)
158 def get_follow_requests(%User{id: id}) do
160 |> join(:inner, [r], f in assoc(r, :follower))
161 |> where([r], r.state == ^:follow_pending)
162 |> where([r], r.following_id == ^id)
163 |> where([r, f], f.is_active == true)
168 def following?(%User{id: follower_id}, %User{id: followed_id}) do
170 |> where(follower_id: ^follower_id, following_id: ^followed_id, state: ^:follow_accept)
174 def following_query(%User{} = user) do
176 |> join(:inner, [r], u in User, on: r.following_id == u.id)
177 |> where([r], r.follower_id == ^user.id)
178 |> where([r], r.state == ^:follow_accept)
181 def outgoing_pending_follow_requests_query(%User{} = follower) do
183 |> where([r], r.follower_id == ^follower.id)
184 |> where([r], r.state == ^:follow_pending)
187 def following(%User{} = user) do
189 following_query(user)
190 |> select([r, u], u.follower_address)
193 if not user.local or user.invisible do
196 [user.follower_address | following]
200 def move_following(origin, target) do
202 |> join(:inner, [r], f in assoc(r, :follower))
203 |> where(following_id: ^origin.id)
204 |> where([r, f], f.allow_following_move == true)
205 |> where([r, f], f.local == true)
207 |> preload([:follower])
209 |> Enum.map(fn following_relationship ->
210 Pleroma.Web.CommonAPI.follow(following_relationship.follower, target)
211 Pleroma.Web.CommonAPI.unfollow(following_relationship.follower, origin)
215 User.update_follower_count(origin)
219 move_following(origin, target)
223 def all_between_user_sets(
227 when is_list(source_users) and is_list(target_users) do
228 source_user_ids = User.binary_id(source_users)
229 target_user_ids = User.binary_id(target_users)
234 "(follower_id = ANY(?) AND following_id = ANY(?)) OR \
235 (follower_id = ANY(?) AND following_id = ANY(?))",
245 def find(following_relationships, follower, following) do
246 Enum.find(following_relationships, fn
247 fr -> fr.follower_id == follower.id and fr.following_id == following.id
251 defp validate_not_self_relationship(%Changeset{} = changeset) do
253 |> validate_follower_id_following_id_inequality()
254 |> validate_following_id_follower_id_inequality()
257 defp validate_follower_id_following_id_inequality(%Changeset{} = changeset) do
258 validate_change(changeset, :follower_id, fn _, follower_id ->
259 if follower_id == get_field(changeset, :following_id) do
260 [source_id: "can't be equal to following_id"]
267 defp validate_following_id_follower_id_inequality(%Changeset{} = changeset) do
268 validate_change(changeset, :following_id, fn _, following_id ->
269 if following_id == get_field(changeset, :follower_id) do
270 [target_id: "can't be equal to follower_id"]
277 @spec following_ap_ids(User.t()) :: [String.t()]
278 def following_ap_ids(%User{} = user) do
281 |> select([r, u], u.ap_id)