Backend support to get used media size for users and actors
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -37,6 +37,16 @@ defmodule Mobilizon.Media do
|
||||
|> Repo.one()
|
||||
end
|
||||
|
||||
@doc """
|
||||
List the paginated picture for an actor
|
||||
"""
|
||||
@spec pictures_for_actor(integer | String.t(), integer | nil, integer | nil) :: Page.t()
|
||||
def pictures_for_actor(actor_id, page, limit) do
|
||||
actor_id
|
||||
|> pictures_for_actor_query()
|
||||
|> Page.build_page(page, limit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
List the paginated picture for user
|
||||
"""
|
||||
@@ -47,6 +57,32 @@ defmodule Mobilizon.Media do
|
||||
|> Page.build_page(page, limit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculate the sum of media size used by the user
|
||||
"""
|
||||
@spec media_size_for_actor(integer | String.t()) :: integer()
|
||||
def media_size_for_actor(actor_id) do
|
||||
actor_id
|
||||
|> pictures_for_actor_query()
|
||||
|> select([:file])
|
||||
|> Repo.all()
|
||||
|> Enum.map(& &1.file.size)
|
||||
|> Enum.sum()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculate the sum of media size used by the user
|
||||
"""
|
||||
@spec media_size_for_user(integer | String.t()) :: integer()
|
||||
def media_size_for_user(user_id) do
|
||||
user_id
|
||||
|> pictures_for_user_query()
|
||||
|> select([:file])
|
||||
|> Repo.all()
|
||||
|> Enum.map(& &1.file.size)
|
||||
|> Enum.sum()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a picture.
|
||||
"""
|
||||
@@ -97,6 +133,13 @@ defmodule Mobilizon.Media do
|
||||
)
|
||||
end
|
||||
|
||||
@spec pictures_for_actor_query(integer() | String.t()) :: Ecto.Query.t()
|
||||
defp pictures_for_actor_query(actor_id) do
|
||||
Picture
|
||||
|> join(:inner, [p], a in Actor, on: p.actor_id == a.id)
|
||||
|> where([_p, a], a.id == ^actor_id)
|
||||
end
|
||||
|
||||
@spec pictures_for_user_query(integer() | String.t()) :: Ecto.Query.t()
|
||||
defp pictures_for_user_query(user_id) do
|
||||
Picture
|
||||
|
||||
Reference in New Issue
Block a user