2019-10-10 20:35:32 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-10-10 20:35:32 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.FollowingRelationship do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Ecto.Query
|
|
|
|
|
2020-03-31 07:21:42 +01:00
|
|
|
alias Ecto.Changeset
|
2019-10-10 20:35:32 +01:00
|
|
|
alias FlakeId.Ecto.CompatType
|
2020-04-13 11:24:31 +01:00
|
|
|
alias Pleroma.FollowingRelationship.State
|
2019-10-10 20:35:32 +01:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
|
|
|
|
2022-12-14 12:38:48 +00:00
|
|
|
@type follow_state :: :follow_pending | :follow_accept | :follow_reject | :unfollow
|
|
|
|
|
2019-10-10 20:35:32 +01:00
|
|
|
schema "following_relationships" do
|
2020-04-13 11:24:31 +01:00
|
|
|
field(:state, State, default: :follow_pending)
|
2019-10-10 20:35:32 +01:00
|
|
|
|
|
|
|
belongs_to(:follower, User, type: CompatType)
|
|
|
|
belongs_to(:following, User, type: CompatType)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2020-04-13 11:24:31 +01:00
|
|
|
@doc "Returns underlying integer code for state atom"
|
|
|
|
def state_int_code(state_atom), do: State.__enum_map__() |> Keyword.fetch!(state_atom)
|
|
|
|
|
|
|
|
def accept_state_code, do: state_int_code(:follow_accept)
|
|
|
|
|
2019-10-10 20:35:32 +01:00
|
|
|
def changeset(%__MODULE__{} = following_relationship, attrs) do
|
|
|
|
following_relationship
|
|
|
|
|> cast(attrs, [:state])
|
|
|
|
|> put_assoc(:follower, attrs.follower)
|
|
|
|
|> put_assoc(:following, attrs.following)
|
|
|
|
|> validate_required([:state, :follower, :following])
|
2020-03-28 15:49:03 +00:00
|
|
|
|> unique_constraint(:follower_id,
|
|
|
|
name: :following_relationships_follower_id_following_id_index
|
|
|
|
)
|
|
|
|
|> validate_not_self_relationship()
|
|
|
|
end
|
|
|
|
|
2020-03-31 07:21:42 +01:00
|
|
|
def state_to_enum(state) when state in ["pending", "accept", "reject"] do
|
|
|
|
String.to_existing_atom("follow_#{state}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def state_to_enum(state) do
|
|
|
|
raise "State is not convertible to Pleroma.FollowingRelationship.State: #{state}"
|
2019-10-10 20:35:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def get(%User{} = follower, %User{} = following) do
|
2020-02-07 12:17:34 +00:00
|
|
|
__MODULE__
|
|
|
|
|> where(follower_id: ^follower.id, following_id: ^following.id)
|
|
|
|
|> Repo.one()
|
2019-10-10 20:35:32 +01:00
|
|
|
end
|
|
|
|
|
2020-03-28 15:49:03 +00:00
|
|
|
def update(follower, following, :follow_reject), do: unfollow(follower, following)
|
2019-10-10 20:35:32 +01:00
|
|
|
|
|
|
|
def update(%User{} = follower, %User{} = following, state) do
|
|
|
|
case get(follower, following) do
|
|
|
|
nil ->
|
|
|
|
follow(follower, following, state)
|
|
|
|
|
|
|
|
following_relationship ->
|
2020-12-01 20:17:52 +00:00
|
|
|
with {:ok, _following_relationship} <-
|
|
|
|
following_relationship
|
|
|
|
|> cast(%{state: state}, [:state])
|
|
|
|
|> validate_required([:state])
|
|
|
|
|> Repo.update() do
|
|
|
|
after_update(state, follower, following)
|
|
|
|
end
|
2019-10-10 20:35:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-14 12:38:48 +00:00
|
|
|
@spec follow(User.t(), User.t()) :: {:ok, User.t(), User.t()} | {:error, any}
|
2020-03-28 15:49:03 +00:00
|
|
|
def follow(%User{} = follower, %User{} = following, state \\ :follow_accept) do
|
2020-12-01 20:17:52 +00:00
|
|
|
with {:ok, _following_relationship} <-
|
|
|
|
%__MODULE__{}
|
|
|
|
|> changeset(%{follower: follower, following: following, state: state})
|
|
|
|
|> Repo.insert(on_conflict: :nothing) do
|
|
|
|
after_update(state, follower, following)
|
|
|
|
end
|
2019-10-10 20:35:32 +01:00
|
|
|
end
|
|
|
|
|
2022-12-14 12:38:48 +00:00
|
|
|
@spec unfollow(User.t(), User.t()) :: {:ok, User.t(), User.t()} | {:error, any}
|
2019-10-10 20:35:32 +01:00
|
|
|
def unfollow(%User{} = follower, %User{} = following) do
|
|
|
|
case get(follower, following) do
|
2020-12-01 20:17:52 +00:00
|
|
|
%__MODULE__{} = following_relationship ->
|
|
|
|
with {:ok, _following_relationship} <- Repo.delete(following_relationship) do
|
|
|
|
after_update(:unfollow, follower, following)
|
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
2022-12-14 12:38:48 +00:00
|
|
|
{:ok, follower, following}
|
2020-12-01 20:17:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-14 12:38:48 +00:00
|
|
|
@spec after_update(follow_state(), User.t(), User.t()) ::
|
|
|
|
{:ok, User.t(), User.t()} | {:error, any()}
|
2020-12-01 20:17:52 +00:00
|
|
|
defp after_update(state, %User{} = follower, %User{} = following) do
|
|
|
|
with {:ok, following} <- User.update_follower_count(following),
|
|
|
|
{:ok, follower} <- User.update_following_count(follower) do
|
2020-12-02 15:16:36 +00:00
|
|
|
Pleroma.Web.Streamer.stream("follow_relationship", %{
|
2020-12-01 20:17:52 +00:00
|
|
|
state: state,
|
|
|
|
following: following,
|
|
|
|
follower: follower
|
|
|
|
})
|
|
|
|
|
|
|
|
{:ok, follower, following}
|
2022-12-14 12:38:48 +00:00
|
|
|
else
|
|
|
|
err -> {:error, err}
|
2019-10-10 20:35:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def follower_count(%User{} = user) do
|
|
|
|
%{followers: user, deactivated: false}
|
|
|
|
|> User.Query.build()
|
|
|
|
|> Repo.aggregate(:count, :id)
|
|
|
|
end
|
|
|
|
|
2020-04-12 19:53:03 +01:00
|
|
|
def followers_query(%User{} = user) do
|
|
|
|
__MODULE__
|
|
|
|
|> join(:inner, [r], u in User, on: r.follower_id == u.id)
|
|
|
|
|> where([r], r.following_id == ^user.id)
|
2020-04-13 11:24:31 +01:00
|
|
|
|> where([r], r.state == ^:follow_accept)
|
2020-04-12 19:53:03 +01:00
|
|
|
end
|
|
|
|
|
2020-07-31 16:18:04 +01:00
|
|
|
def followers_ap_ids(user, from_ap_ids \\ nil)
|
|
|
|
|
|
|
|
def followers_ap_ids(_, []), do: []
|
|
|
|
|
|
|
|
def followers_ap_ids(%User{} = user, from_ap_ids) do
|
2020-04-12 19:53:03 +01:00
|
|
|
query =
|
|
|
|
user
|
|
|
|
|> followers_query()
|
|
|
|
|> select([r, u], u.ap_id)
|
|
|
|
|
|
|
|
query =
|
|
|
|
if from_ap_ids do
|
|
|
|
where(query, [r, u], u.ap_id in ^from_ap_ids)
|
|
|
|
else
|
|
|
|
query
|
|
|
|
end
|
|
|
|
|
|
|
|
Repo.all(query)
|
|
|
|
end
|
|
|
|
|
2019-10-10 20:35:32 +01:00
|
|
|
def following_count(%User{id: nil}), do: 0
|
|
|
|
|
|
|
|
def following_count(%User{} = user) do
|
|
|
|
%{friends: user, deactivated: false}
|
|
|
|
|> User.Query.build()
|
|
|
|
|> Repo.aggregate(:count, :id)
|
|
|
|
end
|
|
|
|
|
2023-02-04 20:51:17 +00:00
|
|
|
def get_follow_requests_query(%User{id: id}) do
|
2019-10-10 20:35:32 +01:00
|
|
|
__MODULE__
|
2023-02-04 20:51:17 +00:00
|
|
|
|> join(:inner, [r], f in assoc(r, :follower), as: :follower)
|
2020-03-28 15:49:03 +00:00
|
|
|
|> where([r], r.state == ^:follow_pending)
|
2019-10-10 20:35:32 +01:00
|
|
|
|> where([r], r.following_id == ^id)
|
2023-02-04 20:51:17 +00:00
|
|
|
|> where([r, follower: f], f.is_active == true)
|
|
|
|
|> select([r, follower: f], f)
|
2019-10-10 20:35:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def following?(%User{id: follower_id}, %User{id: followed_id}) do
|
|
|
|
__MODULE__
|
2020-03-28 15:49:03 +00:00
|
|
|
|> where(follower_id: ^follower_id, following_id: ^followed_id, state: ^:follow_accept)
|
2019-10-10 20:35:32 +01:00
|
|
|
|> Repo.exists?()
|
|
|
|
end
|
|
|
|
|
2020-04-12 19:53:03 +01:00
|
|
|
def following_query(%User{} = user) do
|
|
|
|
__MODULE__
|
|
|
|
|> join(:inner, [r], u in User, on: r.following_id == u.id)
|
|
|
|
|> where([r], r.follower_id == ^user.id)
|
2020-04-13 07:23:50 +01:00
|
|
|
|> where([r], r.state == ^:follow_accept)
|
2020-04-12 19:53:03 +01:00
|
|
|
end
|
|
|
|
|
2020-06-07 16:31:37 +01:00
|
|
|
def outgoing_pending_follow_requests_query(%User{} = follower) do
|
|
|
|
__MODULE__
|
|
|
|
|> where([r], r.follower_id == ^follower.id)
|
|
|
|
|> where([r], r.state == ^:follow_pending)
|
|
|
|
end
|
|
|
|
|
2019-10-10 20:35:32 +01:00
|
|
|
def following(%User{} = user) do
|
|
|
|
following =
|
2020-04-12 19:53:03 +01:00
|
|
|
following_query(user)
|
2019-10-10 20:35:32 +01:00
|
|
|
|> select([r, u], u.follower_address)
|
|
|
|
|> Repo.all()
|
|
|
|
|
2019-11-05 07:50:03 +00:00
|
|
|
if not user.local or user.invisible do
|
2019-10-10 20:35:32 +01:00
|
|
|
following
|
|
|
|
else
|
|
|
|
[user.follower_address | following]
|
|
|
|
end
|
|
|
|
end
|
2019-10-30 11:21:49 +00:00
|
|
|
|
|
|
|
def move_following(origin, target) do
|
2019-11-12 11:36:50 +00:00
|
|
|
__MODULE__
|
|
|
|
|> join(:inner, [r], f in assoc(r, :follower))
|
|
|
|
|> where(following_id: ^origin.id)
|
|
|
|
|> where([r, f], f.allow_following_move == true)
|
2022-07-04 17:29:39 +01:00
|
|
|
|> where([r, f], f.local == true)
|
2019-11-12 11:36:50 +00:00
|
|
|
|> limit(50)
|
|
|
|
|> preload([:follower])
|
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.map(fn following_relationship ->
|
|
|
|
Pleroma.Web.CommonAPI.follow(following_relationship.follower, target)
|
2022-07-04 17:29:39 +01:00
|
|
|
Pleroma.Web.CommonAPI.unfollow(following_relationship.follower, origin)
|
2019-11-12 11:36:50 +00:00
|
|
|
end)
|
|
|
|
|> case do
|
2019-12-03 15:31:12 +00:00
|
|
|
[] ->
|
|
|
|
User.update_follower_count(origin)
|
|
|
|
:ok
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
move_following(origin, target)
|
2019-10-30 11:21:49 +00:00
|
|
|
end
|
|
|
|
end
|
2020-03-23 09:01:11 +00:00
|
|
|
|
|
|
|
def all_between_user_sets(
|
|
|
|
source_users,
|
|
|
|
target_users
|
|
|
|
)
|
|
|
|
when is_list(source_users) and is_list(target_users) do
|
2020-03-25 14:01:45 +00:00
|
|
|
source_user_ids = User.binary_id(source_users)
|
|
|
|
target_user_ids = User.binary_id(target_users)
|
2020-03-23 09:01:11 +00:00
|
|
|
|
|
|
|
__MODULE__
|
|
|
|
|> where(
|
|
|
|
fragment(
|
|
|
|
"(follower_id = ANY(?) AND following_id = ANY(?)) OR \
|
|
|
|
(follower_id = ANY(?) AND following_id = ANY(?))",
|
|
|
|
^source_user_ids,
|
|
|
|
^target_user_ids,
|
|
|
|
^target_user_ids,
|
|
|
|
^source_user_ids
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
2020-03-25 17:33:34 +00:00
|
|
|
|
|
|
|
def find(following_relationships, follower, following) do
|
|
|
|
Enum.find(following_relationships, fn
|
|
|
|
fr -> fr.follower_id == follower.id and fr.following_id == following.id
|
|
|
|
end)
|
|
|
|
end
|
2020-03-28 15:49:03 +00:00
|
|
|
|
2020-03-31 07:21:42 +01:00
|
|
|
defp validate_not_self_relationship(%Changeset{} = changeset) do
|
2020-03-28 15:49:03 +00:00
|
|
|
changeset
|
2020-03-31 07:21:42 +01:00
|
|
|
|> validate_follower_id_following_id_inequality()
|
|
|
|
|> validate_following_id_follower_id_inequality()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp validate_follower_id_following_id_inequality(%Changeset{} = changeset) do
|
|
|
|
validate_change(changeset, :follower_id, fn _, follower_id ->
|
|
|
|
if follower_id == get_field(changeset, :following_id) do
|
|
|
|
[source_id: "can't be equal to following_id"]
|
2020-03-28 15:49:03 +00:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end)
|
2020-03-31 07:21:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp validate_following_id_follower_id_inequality(%Changeset{} = changeset) do
|
|
|
|
validate_change(changeset, :following_id, fn _, following_id ->
|
|
|
|
if following_id == get_field(changeset, :follower_id) do
|
|
|
|
[target_id: "can't be equal to follower_id"]
|
2020-03-28 15:49:03 +00:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2020-08-18 16:21:34 +01:00
|
|
|
|
|
|
|
@spec following_ap_ids(User.t()) :: [String.t()]
|
|
|
|
def following_ap_ids(%User{} = user) do
|
|
|
|
user
|
|
|
|
|> following_query()
|
|
|
|
|> select([r, u], u.ap_id)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
2019-10-10 20:35:32 +01:00
|
|
|
end
|