2020-11-26 11:41:13 +01:00
|
|
|
defmodule Mobilizon.Medias.Media do
|
|
|
|
@moduledoc """
|
|
|
|
Represents a media entity.
|
|
|
|
"""
|
|
|
|
|
|
|
|
use Ecto.Schema
|
|
|
|
|
2021-06-10 09:36:25 +02:00
|
|
|
import Ecto.Changeset, only: [cast: 3, cast_embed: 2, cast_embed: 3]
|
2020-11-26 11:41:13 +01:00
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Discussions.Comment
|
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Medias.File
|
2021-06-10 09:36:25 +02:00
|
|
|
alias Mobilizon.Medias.Media.Metadata
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.Posts.Post
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{
|
|
|
|
file: File.t(),
|
2021-06-10 09:36:25 +02:00
|
|
|
metadata: Metadata.t(),
|
2020-11-26 11:41:13 +01:00
|
|
|
actor: Actor.t()
|
|
|
|
}
|
|
|
|
|
2021-06-10 09:36:25 +02:00
|
|
|
@metadata_attrs [:height, :width, :blurhash]
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
schema "medias" do
|
|
|
|
embeds_one(:file, File, on_replace: :update)
|
2021-06-10 09:36:25 +02:00
|
|
|
|
|
|
|
embeds_one :metadata, Metadata, on_replace: :update do
|
|
|
|
field(:height, :integer)
|
|
|
|
field(:width, :integer)
|
|
|
|
field(:blurhash, :string)
|
|
|
|
end
|
|
|
|
|
2020-11-26 11:41:13 +01:00
|
|
|
belongs_to(:actor, Actor)
|
|
|
|
has_many(:event_picture, Event, foreign_key: :picture_id)
|
|
|
|
many_to_many(:events, Event, join_through: "events_medias")
|
|
|
|
has_many(:posts_picture, Post, foreign_key: :picture_id)
|
|
|
|
many_to_many(:posts, Post, join_through: "posts_medias")
|
|
|
|
many_to_many(:comments, Comment, join_through: "comments_medias")
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec changeset(t | Ecto.Schema.t(), map) :: Ecto.Changeset.t()
|
2020-11-26 11:41:13 +01:00
|
|
|
def changeset(%__MODULE__{} = media, attrs) do
|
|
|
|
media
|
|
|
|
|> cast(attrs, [:actor_id])
|
|
|
|
|> cast_embed(:file)
|
2021-06-10 09:36:25 +02:00
|
|
|
|> cast_embed(:metadata, with: &metadata_changeset/2)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2021-09-10 11:27:59 +02:00
|
|
|
@spec metadata_changeset(Metadata.t(), map) :: Ecto.Changeset.t()
|
2021-06-10 09:36:25 +02:00
|
|
|
def metadata_changeset(metadata, attrs) do
|
|
|
|
metadata
|
|
|
|
|> cast(attrs, @metadata_attrs)
|
2020-11-26 11:41:13 +01:00
|
|
|
end
|
|
|
|
end
|