2019-01-14 14:56:07 +00:00
|
|
|
import EctoEnum
|
|
|
|
|
|
|
|
defenum(Mobilizon.Events.CommentVisibilityEnum, :comment_visibility_type, [
|
|
|
|
:public,
|
|
|
|
:unlisted,
|
2019-01-14 15:21:13 +00:00
|
|
|
:private,
|
2019-01-14 14:56:07 +00:00
|
|
|
:moderated,
|
2019-01-14 15:21:13 +00:00
|
|
|
:invite
|
2019-01-14 14:56:07 +00:00
|
|
|
])
|
|
|
|
|
2018-10-11 15:37:39 +00:00
|
|
|
defmodule Mobilizon.Events.Comment do
|
2018-06-14 16:15:27 +00:00
|
|
|
@moduledoc """
|
|
|
|
An actor comment (for instance on an event or on a group)
|
|
|
|
"""
|
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
|
|
|
|
2018-10-11 15:37:39 +00:00
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Actors.Actor
|
2018-11-08 15:13:47 +00:00
|
|
|
alias Mobilizon.Events.Comment
|
2019-04-25 17:05:05 +00:00
|
|
|
alias MobilizonWeb.Router.Helpers, as: Routes
|
|
|
|
alias MobilizonWeb.Endpoint
|
2018-05-17 09:32:23 +00:00
|
|
|
|
|
|
|
schema "comments" do
|
2018-07-27 08:45:35 +00:00
|
|
|
field(:text, :string)
|
|
|
|
field(:url, :string)
|
|
|
|
field(:local, :boolean, default: true)
|
2019-01-14 15:21:13 +00:00
|
|
|
field(:visibility, Mobilizon.Events.CommentVisibilityEnum, default: :public)
|
2018-07-27 08:45:35 +00:00
|
|
|
field(:uuid, Ecto.UUID)
|
|
|
|
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)
|
2018-05-17 09:32:23 +00:00
|
|
|
|
2018-08-24 09:34:00 +00:00
|
|
|
timestamps(type: :utc_datetime)
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(comment, attrs) do
|
2018-11-12 08:05:31 +00:00
|
|
|
uuid =
|
|
|
|
if Map.has_key?(attrs, "uuid"),
|
|
|
|
do: attrs["uuid"],
|
|
|
|
else: Ecto.UUID.generate()
|
2018-07-27 08:45:35 +00:00
|
|
|
|
2018-08-24 09:34:00 +00:00
|
|
|
# TODO : really change me right away
|
|
|
|
url =
|
|
|
|
if Map.has_key?(attrs, "url"),
|
|
|
|
do: attrs["url"],
|
2019-04-25 17:05:05 +00:00
|
|
|
else: Routes.page_url(Endpoint, :comment, uuid)
|
2018-08-24 09:34:00 +00:00
|
|
|
|
2018-05-17 09:32:23 +00:00
|
|
|
comment
|
2019-01-14 14:56:07 +00:00
|
|
|
|> Ecto.Changeset.cast(attrs, [
|
2018-11-12 08:05:31 +00:00
|
|
|
:url,
|
|
|
|
:text,
|
|
|
|
:actor_id,
|
|
|
|
:event_id,
|
|
|
|
:in_reply_to_comment_id,
|
|
|
|
:origin_comment_id,
|
|
|
|
:attributed_to_id
|
|
|
|
])
|
2018-06-14 15:25:55 +00:00
|
|
|
|> put_change(:uuid, uuid)
|
2018-08-24 09:34:00 +00:00
|
|
|
|> put_change(:url, url)
|
|
|
|
|> validate_required([:text, :actor_id, :url])
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|
2018-11-08 14:57:46 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns the id of the first comment in the conversation
|
|
|
|
"""
|
|
|
|
def get_thread_id(%Comment{id: id, origin_comment_id: origin_comment_id}) do
|
|
|
|
origin_comment_id || id
|
|
|
|
end
|
2018-05-17 09:32:23 +00:00
|
|
|
end
|