1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.FollowingRelationship do
12 alias FlakeId.Ecto.CompatType
16 schema "following_relationships" do
17 field(:state, Pleroma.FollowingRelationship.State, default: :follow_pending)
19 belongs_to(:follower, User, type: CompatType)
20 belongs_to(:following, User, type: CompatType)
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
34 |> validate_not_self_relationship()
37 def state_to_enum(state) when state in ["pending", "accept", "reject"] do
38 String.to_existing_atom("follow_#{state}")
41 def state_to_enum(state) do
42 raise "State is not convertible to Pleroma.FollowingRelationship.State: #{state}"
45 def get(%User{} = follower, %User{} = following) do
47 |> where(follower_id: ^follower.id, following_id: ^following.id)
51 def update(follower, following, :follow_reject), do: unfollow(follower, following)
53 def update(%User{} = follower, %User{} = following, state) do
54 case get(follower, following) do
56 follow(follower, following, state)
58 following_relationship ->
59 following_relationship
60 |> cast(%{state: state}, [:state])
61 |> validate_required([:state])
66 def follow(%User{} = follower, %User{} = following, state \\ :follow_accept) do
68 |> changeset(%{follower: follower, following: following, state: state})
69 |> Repo.insert(on_conflict: :nothing)
72 def unfollow(%User{} = follower, %User{} = following) do
73 case get(follower, following) do
74 %__MODULE__{} = following_relationship -> Repo.delete(following_relationship)
79 def follower_count(%User{} = user) do
80 %{followers: user, deactivated: false}
82 |> Repo.aggregate(:count, :id)
85 def following_count(%User{id: nil}), do: 0
87 def following_count(%User{} = user) do
88 %{friends: user, deactivated: false}
90 |> Repo.aggregate(:count, :id)
93 def get_follow_requests(%User{id: id}) do
95 |> join(:inner, [r], f in assoc(r, :follower))
96 |> where([r], r.state == ^:follow_pending)
97 |> where([r], r.following_id == ^id)
102 def following?(%User{id: follower_id}, %User{id: followed_id}) do
104 |> where(follower_id: ^follower_id, following_id: ^followed_id, state: ^:follow_accept)
108 def following(%User{} = user) do
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)
117 if not user.local or user.invisible do
120 [user.follower_address | following]
124 def move_following(origin, target) do
126 |> join(:inner, [r], f in assoc(r, :follower))
127 |> where(following_id: ^origin.id)
128 |> where([r, f], f.allow_following_move == true)
130 |> preload([:follower])
132 |> Enum.map(fn following_relationship ->
133 Repo.delete(following_relationship)
134 Pleroma.Web.CommonAPI.follow(following_relationship.follower, target)
138 User.update_follower_count(origin)
142 move_following(origin, target)
146 def all_between_user_sets(
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)
157 "(follower_id = ANY(?) AND following_id = ANY(?)) OR \
158 (follower_id = ANY(?) AND following_id = ANY(?))",
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
174 defp validate_not_self_relationship(%Changeset{} = changeset) do
176 |> validate_follower_id_following_id_inequality()
177 |> validate_following_id_follower_id_inequality()
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"]
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"]