2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Events.Comment do
|
2018-06-14 18:15:27 +02:00
|
|
|
@moduledoc """
|
2019-09-13 01:01:17 +02:00
|
|
|
Represents an actor comment (for instance on an event or on a group).
|
2018-06-14 18:15:27 +02:00
|
|
|
"""
|
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
use Ecto.Schema
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
import Ecto.Changeset
|
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-10-25 17:43:37 +02:00
|
|
|
alias Mobilizon.Events.{Comment, CommentVisibility, Event, Tag}
|
|
|
|
alias Mobilizon.Mention
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2019-09-17 22:10:22 +02:00
|
|
|
alias MobilizonWeb.Endpoint
|
2019-09-22 16:26:23 +02:00
|
|
|
alias MobilizonWeb.Router.Helpers, as: Routes
|
2019-09-17 22:10:22 +02:00
|
|
|
|
2019-09-13 01:01:17 +02:00
|
|
|
@type t :: %__MODULE__{
|
|
|
|
text: String.t(),
|
|
|
|
url: String.t(),
|
|
|
|
local: boolean,
|
|
|
|
visibility: CommentVisibility.t(),
|
|
|
|
uuid: Ecto.UUID.t(),
|
|
|
|
actor: Actor.t(),
|
|
|
|
attributed_to: Actor.t(),
|
|
|
|
event: Event.t(),
|
2019-10-25 17:43:37 +02:00
|
|
|
tags: [Tag.t()],
|
|
|
|
mentions: [Mention.t()],
|
2019-09-13 01:01:17 +02:00
|
|
|
in_reply_to_comment: t,
|
|
|
|
origin_comment: t
|
|
|
|
}
|
|
|
|
|
|
|
|
@required_attrs [:text, :actor_id, :url]
|
|
|
|
@optional_attrs [:event_id, :in_reply_to_comment_id, :origin_comment_id, :attributed_to_id]
|
|
|
|
@attrs @required_attrs ++ @optional_attrs
|
2018-05-17 11:32:23 +02:00
|
|
|
|
|
|
|
schema "comments" do
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:text, :string)
|
|
|
|
field(:url, :string)
|
|
|
|
field(:local, :boolean, default: true)
|
2019-09-13 01:01:17 +02:00
|
|
|
field(:visibility, CommentVisibility, default: :public)
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:uuid, Ecto.UUID)
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
belongs_to(:actor, Actor, foreign_key: :actor_id)
|
|
|
|
belongs_to(:attributed_to, Actor, foreign_key: :attributed_to_id)
|
|
|
|
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)
|
2019-10-25 17:43:37 +02:00
|
|
|
many_to_many(:tags, Tag, join_through: "comments_tags", on_replace: :delete)
|
|
|
|
has_many(:mentions, Mention)
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-08-24 11:34:00 +02:00
|
|
|
timestamps(type: :utc_datetime)
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
|
|
|
|
2019-09-13 01:01:17 +02:00
|
|
|
@doc """
|
|
|
|
Returns the id of the first comment in the conversation.
|
|
|
|
"""
|
|
|
|
@spec get_thread_id(t) :: integer
|
2019-09-13 01:55:45 +02:00
|
|
|
def get_thread_id(%__MODULE__{id: id, origin_comment_id: origin_comment_id}) do
|
2019-09-13 01:01:17 +02:00
|
|
|
origin_comment_id || id
|
|
|
|
end
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2019-09-13 01:01:17 +02:00
|
|
|
@doc false
|
|
|
|
@spec changeset(t, map) :: Ecto.Changeset.t()
|
2019-09-13 01:55:45 +02:00
|
|
|
def changeset(%__MODULE__{} = comment, attrs) do
|
2019-10-25 17:43:37 +02:00
|
|
|
uuid = Map.get(attrs, :uuid) || Ecto.UUID.generate()
|
|
|
|
url = Map.get(attrs, :url) || generate_url(uuid)
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
comment
|
2019-09-13 01:01:17 +02:00
|
|
|
|> cast(attrs, @attrs)
|
2018-06-14 17:25:55 +02:00
|
|
|
|> put_change(:uuid, uuid)
|
2018-08-24 11:34:00 +02:00
|
|
|
|> put_change(:url, url)
|
2019-10-25 17:43:37 +02:00
|
|
|
|> put_tags(attrs)
|
|
|
|
|> put_mentions(attrs)
|
2019-09-13 01:01:17 +02:00
|
|
|
|> validate_required(@required_attrs)
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|
2018-11-08 15:57:46 +01:00
|
|
|
|
2019-09-13 01:01:17 +02:00
|
|
|
@spec generate_url(String.t()) :: String.t()
|
2019-09-17 22:10:22 +02:00
|
|
|
defp generate_url(uuid), do: Routes.page_url(Endpoint, :comment, uuid)
|
2019-10-25 17:43:37 +02:00
|
|
|
|
|
|
|
@spec put_tags(Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
|
|
|
|
defp put_tags(changeset, %{"tags" => tags}),
|
|
|
|
do: put_assoc(changeset, :tags, Enum.map(tags, &process_tag/1))
|
|
|
|
|
|
|
|
defp put_tags(changeset, %{tags: tags}),
|
|
|
|
do: put_assoc(changeset, :tags, Enum.map(tags, &process_tag/1))
|
|
|
|
|
|
|
|
defp put_tags(changeset, _), do: changeset
|
|
|
|
|
|
|
|
@spec put_mentions(Ecto.Changeset.t(), map) :: Ecto.Changeset.t()
|
|
|
|
defp put_mentions(changeset, %{"mentions" => mentions}),
|
|
|
|
do: put_assoc(changeset, :mentions, Enum.map(mentions, &process_mention/1))
|
|
|
|
|
|
|
|
defp put_mentions(changeset, %{mentions: mentions}),
|
|
|
|
do: put_assoc(changeset, :mentions, Enum.map(mentions, &process_mention/1))
|
|
|
|
|
|
|
|
defp put_mentions(changeset, _), do: changeset
|
|
|
|
|
|
|
|
# We need a changeset instead of a raw struct because of slug which is generated in changeset
|
|
|
|
defp process_tag(tag) do
|
|
|
|
Tag.changeset(%Tag{}, tag)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp process_mention(tag) do
|
|
|
|
Mention.changeset(%Mention{}, tag)
|
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|