2020-07-09 17:24:28 +02:00
|
|
|
defmodule Mobilizon.Discussions.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
|
2020-08-14 11:32:23 +02:00
|
|
|
import Mobilizon.Storage.Ecto, only: [maybe_add_published_at: 1]
|
2018-05-17 11:32:23 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors.Actor
|
2023-10-17 16:41:31 +02:00
|
|
|
alias Mobilizon.Conversations.Conversation
|
2020-07-09 17:24:28 +02:00
|
|
|
alias Mobilizon.Discussions.{Comment, CommentVisibility, Discussion}
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Events.{Event, Tag}
|
2020-11-26 11:41:13 +01:00
|
|
|
alias Mobilizon.Medias.Media
|
2019-10-25 17:43:37 +02:00
|
|
|
alias Mobilizon.Mention
|
2019-09-13 01:01:17 +02:00
|
|
|
|
2023-12-01 09:49:54 +01:00
|
|
|
use Mobilizon.Web, :verified_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(),
|
2021-09-28 19:40:37 +02:00
|
|
|
id: integer(),
|
2019-09-13 01:01:17 +02:00
|
|
|
local: boolean,
|
2022-04-18 14:38:57 +02:00
|
|
|
visibility: atom(),
|
2019-09-13 01:01:17 +02:00
|
|
|
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()],
|
2020-11-26 11:41:13 +01:00
|
|
|
media: [Media.t()],
|
2019-09-13 01:01:17 +02:00
|
|
|
in_reply_to_comment: t,
|
2021-08-19 20:43:35 +02:00
|
|
|
origin_comment: t,
|
|
|
|
language: String.t()
|
2019-09-13 01:01:17 +02:00
|
|
|
}
|
|
|
|
|
2019-11-15 18:36:47 +01:00
|
|
|
# When deleting an event we only nihilify everything
|
|
|
|
@required_attrs [:url]
|
2020-08-14 11:32:23 +02:00
|
|
|
@creation_required_attrs @required_attrs ++ [:text, :actor_id, :published_at]
|
2019-11-15 18:36:47 +01:00
|
|
|
@optional_attrs [
|
|
|
|
:text,
|
|
|
|
:actor_id,
|
|
|
|
:event_id,
|
|
|
|
:in_reply_to_comment_id,
|
|
|
|
:origin_comment_id,
|
|
|
|
:attributed_to_id,
|
2019-12-18 18:25:40 +01:00
|
|
|
:deleted_at,
|
2020-02-18 08:57:00 +01:00
|
|
|
:local,
|
2021-06-01 18:08:03 +02:00
|
|
|
:is_announcement,
|
2021-08-19 20:43:35 +02:00
|
|
|
:discussion_id,
|
2023-10-17 16:41:31 +02:00
|
|
|
:conversation_id,
|
|
|
|
:language,
|
|
|
|
:visibility
|
2019-11-15 18:36:47 +01:00
|
|
|
]
|
2019-09-13 01:01:17 +02:00
|
|
|
@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-11-15 18:36:47 +01:00
|
|
|
field(:total_replies, :integer, virtual: true, default: 0)
|
|
|
|
field(:deleted_at, :utc_datetime)
|
2020-08-14 11:32:23 +02:00
|
|
|
field(:published_at, :utc_datetime)
|
2021-06-01 18:08:03 +02:00
|
|
|
field(:is_announcement, :boolean, default: false)
|
2021-08-19 20:43:35 +02:00
|
|
|
field(:language, :string, default: "und")
|
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)
|
2020-07-09 17:24:28 +02:00
|
|
|
belongs_to(:discussion, Discussion, type: :binary_id)
|
2023-10-17 16:41:31 +02:00
|
|
|
belongs_to(:conversation, Conversation)
|
2021-05-17 11:36:28 +02:00
|
|
|
has_many(:replies, 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)
|
2023-11-20 09:35:21 +01:00
|
|
|
has_many(:mentions, Mention, on_replace: :delete)
|
2020-11-26 11:41:13 +01:00
|
|
|
many_to_many(:media, Media, join_through: "comments_medias", on_replace: :delete)
|
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 """
|
2023-10-17 16:41:31 +02:00
|
|
|
Returns the id of the first comment in the discussion or conversation.
|
2019-09-13 01:01:17 +02:00
|
|
|
"""
|
|
|
|
@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
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec changeset(t | Ecto.Schema.t(), map) :: Ecto.Changeset.t()
|
2019-09-13 01:55:45 +02:00
|
|
|
def changeset(%__MODULE__{} = comment, attrs) do
|
2019-11-15 18:36:47 +01:00
|
|
|
comment
|
|
|
|
|> common_changeset(attrs)
|
|
|
|
|> validate_required(@creation_required_attrs)
|
|
|
|
end
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
def update_changeset(%__MODULE__{} = comment, attrs) do
|
|
|
|
comment
|
|
|
|
|> changeset(attrs)
|
|
|
|
|
|
|
|
# TODO handle comment edits
|
|
|
|
# |> put_change(:edits, comment.edits + 1)
|
|
|
|
end
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
@spec delete_changeset(t) :: Ecto.Changeset.t()
|
|
|
|
def delete_changeset(%__MODULE__{} = comment) do
|
2019-11-15 18:36:47 +01:00
|
|
|
comment
|
2019-12-03 11:29:51 +01:00
|
|
|
|> change()
|
|
|
|
|> put_change(:text, nil)
|
|
|
|
|> put_change(:actor_id, nil)
|
|
|
|
|> put_change(:deleted_at, DateTime.utc_now() |> DateTime.truncate(:second))
|
2019-11-15 18:36:47 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Checks whether an comment can be managed.
|
|
|
|
"""
|
2021-10-05 17:43:45 +02:00
|
|
|
@spec can_be_managed_by?(t, integer | String.t()) :: boolean()
|
|
|
|
def can_be_managed_by?(%__MODULE__{actor_id: creator_actor_id}, actor_id)
|
2019-11-15 18:36:47 +01:00
|
|
|
when creator_actor_id == actor_id do
|
2021-10-05 17:43:45 +02:00
|
|
|
creator_actor_id == actor_id
|
2019-11-15 18:36:47 +01:00
|
|
|
end
|
2018-08-24 11:34:00 +02:00
|
|
|
|
2021-10-05 17:43:45 +02:00
|
|
|
def can_be_managed_by?(_comment, _actor), do: false
|
2019-11-15 18:36:47 +01:00
|
|
|
|
|
|
|
defp common_changeset(%__MODULE__{} = comment, attrs) do
|
2018-05-17 11:32:23 +02:00
|
|
|
comment
|
2019-09-13 01:01:17 +02:00
|
|
|
|> cast(attrs, @attrs)
|
2020-08-14 11:32:23 +02:00
|
|
|
|> maybe_add_published_at()
|
2019-11-15 18:36:47 +01:00
|
|
|
|> maybe_generate_uuid()
|
|
|
|
|> maybe_generate_url()
|
2020-11-26 11:41:13 +01:00
|
|
|
|> put_assoc(:media, Map.get(attrs, :media, []))
|
2019-10-25 17:43:37 +02:00
|
|
|
|> put_tags(attrs)
|
|
|
|
|> put_mentions(attrs)
|
2021-04-28 18:25:21 +02:00
|
|
|
|> unique_constraint(:url, name: :comments_url_index)
|
2019-11-15 18:36:47 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec maybe_generate_uuid(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
|
|
|
defp maybe_generate_uuid(%Ecto.Changeset{} = changeset) do
|
|
|
|
case fetch_field(changeset, :uuid) do
|
|
|
|
:error -> put_change(changeset, :uuid, Ecto.UUID.generate())
|
|
|
|
{:data, nil} -> put_change(changeset, :uuid, Ecto.UUID.generate())
|
|
|
|
_ -> changeset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec maybe_generate_url(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
|
|
|
defp maybe_generate_url(%Ecto.Changeset{} = changeset) do
|
|
|
|
with res when res in [:error, {:data, nil}] <- fetch_field(changeset, :url),
|
|
|
|
{changes, uuid} when changes in [:changes, :data] <- fetch_field(changeset, :uuid),
|
|
|
|
url <- generate_url(uuid) do
|
|
|
|
put_change(changeset, :url, url)
|
|
|
|
else
|
|
|
|
_ -> changeset
|
|
|
|
end
|
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()
|
2023-12-01 09:49:54 +01:00
|
|
|
defp generate_url(uuid), do: url(~p"/comments/#{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
|
|
|
|
|
2023-10-17 16:41:31 +02:00
|
|
|
defp process_mention(mention) do
|
|
|
|
Mention.changeset(%Mention{}, mention)
|
2019-10-25 17:43:37 +02:00
|
|
|
end
|
2018-05-17 11:32:23 +02:00
|
|
|
end
|