@@ -10,6 +10,7 @@ defmodule Mobilizon.Resources.Resource do
|
||||
import EctoEnum
|
||||
defenum(TypeEnum, folder: 0, link: 1, picture: 20, pad: 30, calc: 40, visio: 50)
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Resources.Resource.Metadata
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
title: String.t(),
|
||||
@@ -17,7 +18,7 @@ defmodule Mobilizon.Resources.Resource do
|
||||
url: String.t(),
|
||||
resource_url: String.t(),
|
||||
type: atom(),
|
||||
metadata: Mobilizon.Resources.Resource.Metadata.t(),
|
||||
metadata: Metadata.t(),
|
||||
children: list(__MODULE__),
|
||||
parent: __MODULE__,
|
||||
actor: Actor.t(),
|
||||
@@ -37,20 +38,7 @@ defmodule Mobilizon.Resources.Resource do
|
||||
field(:local, :boolean, default: true)
|
||||
field(:published_at, :utc_datetime)
|
||||
|
||||
embeds_one :metadata, Metadata, on_replace: :delete do
|
||||
field(:type, :string)
|
||||
field(:title, :string)
|
||||
field(:description, :string)
|
||||
field(:image_remote_url, :string)
|
||||
field(:width, :integer)
|
||||
field(:height, :integer)
|
||||
field(:author_name, :string)
|
||||
field(:author_url, :string)
|
||||
field(:provider_name, :string)
|
||||
field(:provider_url, :string)
|
||||
field(:html, :string)
|
||||
field(:favicon_url, :string)
|
||||
end
|
||||
embeds_one(:metadata, Metadata, on_replace: :delete)
|
||||
|
||||
has_many(:children, __MODULE__, foreign_key: :parent_id)
|
||||
belongs_to(:parent, __MODULE__, type: :binary_id)
|
||||
@@ -63,27 +51,13 @@ defmodule Mobilizon.Resources.Resource do
|
||||
@required_attrs [:title, :url, :actor_id, :creator_id, :type, :path, :published_at]
|
||||
@optional_attrs [:summary, :parent_id, :resource_url, :local]
|
||||
@attrs @required_attrs ++ @optional_attrs
|
||||
@metadata_attrs [
|
||||
:type,
|
||||
:title,
|
||||
:description,
|
||||
:image_remote_url,
|
||||
:width,
|
||||
:height,
|
||||
:author_name,
|
||||
:author_url,
|
||||
:provider_name,
|
||||
:provider_url,
|
||||
:html,
|
||||
:favicon_url
|
||||
]
|
||||
|
||||
@doc false
|
||||
@spec changeset(t | Ecto.Schema.t(), map) :: Ecto.Changeset.t()
|
||||
def changeset(resource, attrs) do
|
||||
resource
|
||||
|> cast(attrs, @attrs)
|
||||
|> cast_embed(:metadata, with: &metadata_changeset/2)
|
||||
|> cast_embed(:metadata)
|
||||
|> ensure_url(:resource)
|
||||
|> maybe_add_published_at()
|
||||
|> validate_resource_or_folder()
|
||||
@@ -91,11 +65,6 @@ defmodule Mobilizon.Resources.Resource do
|
||||
|> unique_constraint(:url, name: :resource_url_index)
|
||||
end
|
||||
|
||||
defp metadata_changeset(schema, params) do
|
||||
schema
|
||||
|> cast(params, @metadata_attrs)
|
||||
end
|
||||
|
||||
@spec validate_resource_or_folder(Changeset.t()) :: Changeset.t()
|
||||
defp validate_resource_or_folder(%Changeset{} = changeset) do
|
||||
with {status, type} when status in [:changes, :data] <- fetch_field(changeset, :type),
|
||||
|
||||
Reference in New Issue
Block a user