From 6a1cd42d2c27cd2f6b6522251ed381eb5693c7cd Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 23 Nov 2020 10:38:01 +0100 Subject: [PATCH] Add backend to list an user's pictures Signed-off-by: Thomas Citharel --- lib/graphql/resolvers/user.ex | 22 ++++++++++++++++++++++ lib/graphql/schema/picture.ex | 8 ++++++++ lib/graphql/schema/post.ex | 2 +- lib/graphql/schema/user.ex | 10 ++++++++++ lib/mobilizon/media/media.ex | 22 +++++++++++++++++++++- 5 files changed, 62 insertions(+), 2 deletions(-) diff --git a/lib/graphql/resolvers/user.ex b/lib/graphql/resolvers/user.ex index fef85730..bcd283b9 100644 --- a/lib/graphql/resolvers/user.ex +++ b/lib/graphql/resolvers/user.ex @@ -525,6 +525,28 @@ defmodule Mobilizon.GraphQL.Resolvers.User do end end + def user_medias(%User{id: user_id}, %{page: page, limit: limit}, %{ + context: %{current_user: %User{id: logged_in_user_id}} + }) + when user_id == logged_in_user_id do + %{elements: elements, total: total} = Mobilizon.Media.pictures_for_user(user_id, page, limit) + + {:ok, + %{ + elements: + Enum.map(elements, fn element -> + %{ + name: element.file.name, + url: element.file.url, + id: element.id, + content_type: element.file.content_type, + size: element.file.size + } + end), + total: total + }} + end + @spec update_user_login_information(User.t(), map()) :: {:ok, User.t()} | {:error, Ecto.Changeset.t()} defp update_user_login_information( diff --git a/lib/graphql/schema/picture.ex b/lib/graphql/schema/picture.ex index 02f76a96..f1fde46d 100644 --- a/lib/graphql/schema/picture.ex +++ b/lib/graphql/schema/picture.ex @@ -16,6 +16,14 @@ defmodule Mobilizon.GraphQL.Schema.PictureType do field(:size, :integer, description: "The picture's size") end + @desc """ + A paginated list of pictures + """ + object :paginated_picture_list do + field(:elements, list_of(:picture), description: "The list of pictures") + field(:total, :integer, description: "The total number of pictures in the list") + end + @desc "An attached picture or a link to a picture" input_object :picture_input do # Either a full picture object diff --git a/lib/graphql/schema/post.ex b/lib/graphql/schema/post.ex index e796e620..b3af7764 100644 --- a/lib/graphql/schema/post.ex +++ b/lib/graphql/schema/post.ex @@ -26,7 +26,7 @@ defmodule Mobilizon.GraphQL.Schema.PostType do ) field(:picture, :picture, - description: "The event's picture", + description: "The posts's picture", resolve: &Picture.picture/3 ) end diff --git a/lib/graphql/schema/user.ex b/lib/graphql/schema/user.ex index 4986aa10..668a9c1a 100644 --- a/lib/graphql/schema/user.ex +++ b/lib/graphql/schema/user.ex @@ -110,6 +110,16 @@ defmodule Mobilizon.GraphQL.Schema.UserType do field(:current_sign_in_ip, :string, description: "The IP adress the user's currently signed-in with" ) + + field(:media, :paginated_picture_list, description: "The user's media objects") do + arg(:page, :integer, + default_value: 1, + description: "The page in the paginated user media list" + ) + + arg(:limit, :integer, default_value: 10, description: "The limit of user media per page") + resolve(&User.user_medias/3) + end end @desc "The list of roles an user can have" diff --git a/lib/mobilizon/media/media.ex b/lib/mobilizon/media/media.ex index 549f84bd..3af6665f 100644 --- a/lib/mobilizon/media/media.ex +++ b/lib/mobilizon/media/media.ex @@ -7,8 +7,10 @@ defmodule Mobilizon.Media do alias Ecto.Multi + alias Mobilizon.Actors.Actor alias Mobilizon.Media.{File, Picture} - alias Mobilizon.Storage.Repo + alias Mobilizon.Storage.{Page, Repo} + alias Mobilizon.Users.User alias Mobilizon.Web.Upload @@ -35,6 +37,16 @@ defmodule Mobilizon.Media do |> Repo.one() end + @doc """ + List the paginated picture for user + """ + @spec pictures_for_user(integer | String.t(), integer | nil, integer | nil) :: Page.t() + def pictures_for_user(user_id, page, limit) do + user_id + |> pictures_for_user_query() + |> Page.build_page(page, limit) + end + @doc """ Creates a picture. """ @@ -84,4 +96,12 @@ defmodule Mobilizon.Media do where: fragment("? @> ?", p.file, ~s|{"url": "#{url}"}|) ) end + + @spec pictures_for_user_query(integer() | String.t()) :: Ecto.Query.t() + defp pictures_for_user_query(user_id) do + Picture + |> join(:inner, [p], a in Actor, on: p.actor_id == a.id) + |> join(:inner, [_p, a], u in User, on: a.user_id == u.id) + |> where([_p, _a, u], u.id == ^user_id) + end end