Fix following groups + Add interface to manage followers

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-01-20 18:16:44 +01:00
parent 58fe7b74a5
commit 4fbdc94e7c
25 changed files with 929 additions and 83 deletions

View File

@@ -1022,6 +1022,16 @@ defmodule Mobilizon.Actors do
@spec list_bots :: [Bot.t()]
def list_bots, do: Repo.all(Bot)
@doc """
Gets a single follower.
"""
@spec get_follower(integer | String.t()) :: Follower.t() | nil
def get_follower(id) do
Follower
|> Repo.get(id)
|> Repo.preload([:actor, :target_actor])
end
@doc """
Gets a single follower.
Raises `Ecto.NoResultsError` if the follower does not exist.
@@ -1149,6 +1159,25 @@ defmodule Mobilizon.Actors do
|> Repo.aggregate(:count)
end
@doc """
Returns a paginated list of followers for an actor.
"""
@spec list_paginated_followers_for_actor(Actor.t(), boolean | nil, integer | nil, integer | nil) ::
Page.t()
def list_paginated_followers_for_actor(
%Actor{id: actor_id},
approved \\ nil,
page \\ nil,
limit \\ nil
) do
actor_id
|> follower_for_actor_query()
|> filter_followed_by_approved_status(approved)
|> order_by(desc: :updated_at)
|> preload([:actor, :target_actor])
|> Page.build_page(page, limit)
end
@doc """
Returns the list of followings for an actor.
If actor A follows actor B and C, actor A's followings are B and C.
@@ -1688,6 +1717,13 @@ defmodule Mobilizon.Actors do
from(a in query, where: a.preferred_username == ^name and a.domain == ^domain)
end
@spec filter_by_name(Ecto.Query.t(), boolean | nil) :: Ecto.Query.t()
defp filter_followed_by_approved_status(query, nil), do: query
defp filter_followed_by_approved_status(query, approved) do
from(f in query, where: f.approved == ^approved)
end
@spec preload_followers(Actor.t(), boolean) :: Actor.t()
defp preload_followers(actor, true), do: Repo.preload(actor, [:followers])
defp preload_followers(actor, false), do: actor