2018-05-17 11:32:23 +02:00
|
|
|
defmodule Eventos.Events.Comment do
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
|
|
|
|
|
|
|
alias Eventos.Events.Event
|
2018-05-18 09:56:21 +02:00
|
|
|
alias Eventos.Actors.Actor
|
|
|
|
alias Eventos.Actors.Comment
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
schema "comments" do
|
|
|
|
field :text, :string
|
|
|
|
field :url, :string
|
|
|
|
field :local, :boolean, default: true
|
2018-06-14 17:25:55 +02:00
|
|
|
field :uuid, Ecto.UUID
|
2018-05-18 09:56:21 +02:00
|
|
|
belongs_to :actor, Actor, [foreign_key: :actor_id]
|
2018-06-14 17:25:55 +02:00
|
|
|
belongs_to :attributed_to, Actor, [foreign_key: :attributed_to_id]
|
2018-05-17 11:32:23 +02:00
|
|
|
belongs_to :event, Event, [foreign_key: :event_id]
|
|
|
|
belongs_to :in_reply_to_comment, Comment, [foreign_key: :in_reply_to_comment_id]
|
|
|
|
belongs_to :origin_comment, Comment, [foreign_key: :origin_comment_id]
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(comment, attrs) do
|
2018-06-14 17:25:55 +02:00
|
|
|
uuid = Ecto.UUID.generate()
|
2018-05-17 11:32:23 +02:00
|
|
|
comment
|
2018-06-14 17:25:55 +02:00
|
|
|
|> cast(attrs, [:url, :text, :actor_id, :event_id, :in_reply_to_comment_id, :attributed_to_id])
|
|
|
|
|> validate_required([:text, :actor_id])
|
|
|
|
|> put_change(:uuid, uuid)
|
|
|
|
|> put_change(:url, "#{EventosWeb.Endpoint.url()}/comments/#{uuid}")
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
end
|