2020-11-26 11:41:13 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Schema.MediaType do
|
|
|
|
@moduledoc """
|
|
|
|
Schema representation for Medias
|
|
|
|
"""
|
|
|
|
use Absinthe.Schema.Notation
|
|
|
|
|
|
|
|
alias Mobilizon.GraphQL.Resolvers.Media
|
|
|
|
|
2023-03-24 12:13:42 +01:00
|
|
|
@env Application.compile_env(:mobilizon, :env)
|
|
|
|
@media_rate_limiting 60
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
@desc "A media"
|
|
|
|
object :media do
|
2023-03-17 18:10:59 +01:00
|
|
|
meta(:authorize, :all)
|
2020-11-26 11:41:13 +01:00
|
|
|
field(:id, :id, description: "The media's ID")
|
|
|
|
field(:alt, :string, description: "The media's alternative text")
|
|
|
|
field(:name, :string, description: "The media's name")
|
|
|
|
field(:url, :string, description: "The media's full URL")
|
|
|
|
field(:content_type, :string, description: "The media's detected content type")
|
|
|
|
field(:size, :integer, description: "The media's size")
|
2021-06-10 09:36:25 +02:00
|
|
|
field(:metadata, :media_metadata, description: "The media's metadata")
|
2020-11-26 11:41:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@desc """
|
|
|
|
A paginated list of medias
|
|
|
|
"""
|
|
|
|
object :paginated_media_list do
|
2023-03-17 18:10:59 +01:00
|
|
|
meta(:authorize, :all)
|
2020-11-26 11:41:13 +01:00
|
|
|
field(:elements, list_of(:media), description: "The list of medias")
|
|
|
|
field(:total, :integer, description: "The total number of medias in the list")
|
|
|
|
end
|
|
|
|
|
2021-06-10 09:36:25 +02:00
|
|
|
@desc """
|
|
|
|
Some metadata associated with a media
|
|
|
|
"""
|
|
|
|
object :media_metadata do
|
2023-03-17 18:10:59 +01:00
|
|
|
meta(:authorize, :all)
|
2021-06-10 09:36:25 +02:00
|
|
|
field(:width, :integer, description: "The media width (if a picture)")
|
|
|
|
field(:height, :integer, description: "The media width (if a height)")
|
|
|
|
field(:blurhash, :string, description: "The media blurhash (if a picture")
|
|
|
|
end
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
@desc "An attached media or a link to a media"
|
|
|
|
input_object :media_input do
|
|
|
|
# Either a full media object
|
|
|
|
field(:media, :media_input_object, description: "A full media attached")
|
|
|
|
# Or directly the ID of an existing media
|
|
|
|
field(:media_id, :id, description: "The ID of an existing media")
|
|
|
|
end
|
|
|
|
|
|
|
|
@desc "An attached media"
|
|
|
|
input_object :media_input_object do
|
|
|
|
field(:name, non_null(:string), description: "The media's name")
|
|
|
|
field(:alt, :string, description: "The media's alternative text")
|
2024-04-10 14:36:21 +02:00
|
|
|
field(:file, :upload, description: "The media file")
|
2020-11-26 11:41:13 +01:00
|
|
|
field(:actor_id, :id, description: "The media owner")
|
2024-04-10 14:36:21 +02:00
|
|
|
field(:url, :string, description: "The media URL")
|
2020-11-26 11:41:13 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
object :media_queries do
|
|
|
|
@desc "Get a media"
|
|
|
|
field :media, :media do
|
|
|
|
arg(:id, non_null(:id), description: "The media ID")
|
2023-03-17 18:10:59 +01:00
|
|
|
middleware(Rajska.QueryAuthorization, permit: :all)
|
2020-11-26 11:41:13 +01:00
|
|
|
resolve(&Media.media/3)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
object :media_mutations do
|
|
|
|
@desc "Upload a media"
|
|
|
|
field :upload_media, :media do
|
|
|
|
arg(:name, non_null(:string), description: "The media's name")
|
|
|
|
arg(:alt, :string, description: "The media's alternative text")
|
|
|
|
arg(:file, non_null(:upload), description: "The media file")
|
2023-03-17 18:10:59 +01:00
|
|
|
arg(:actor_id, :id, description: "The actor that uploads the media")
|
|
|
|
|
|
|
|
middleware(Rajska.QueryAuthorization,
|
|
|
|
permit: :user,
|
|
|
|
scope: Mobilizon.Medias.Media,
|
|
|
|
rule: :"write:media:upload",
|
|
|
|
args: %{}
|
|
|
|
)
|
|
|
|
|
2023-03-24 12:13:42 +01:00
|
|
|
middleware(Rajska.RateLimiter, limit: media_rate_limiting(@env))
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
resolve(&Media.upload_media/3)
|
|
|
|
end
|
|
|
|
|
|
|
|
@desc """
|
|
|
|
Remove a media
|
|
|
|
"""
|
|
|
|
field :remove_media, :deleted_object do
|
|
|
|
arg(:id, non_null(:id), description: "The media's ID")
|
2023-03-17 18:10:59 +01:00
|
|
|
|
|
|
|
middleware(Rajska.QueryAuthorization,
|
|
|
|
permit: :user,
|
|
|
|
scope: Mobilizon.Medias.Media,
|
|
|
|
rule: :"write:media:remove"
|
|
|
|
)
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
resolve(&Media.remove_media/3)
|
|
|
|
end
|
|
|
|
end
|
2023-03-24 12:13:42 +01:00
|
|
|
|
|
|
|
defp media_rate_limiting(:test), do: @media_rate_limiting * 1000
|
|
|
|
defp media_rate_limiting(_), do: @media_rate_limiting
|
2020-11-26 11:41:13 +01:00
|
|
|
end
|