Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2023-10-17 16:41:31 +02:00
parent 0613f7f736
commit b5672cee7e
108 changed files with 5221 additions and 1318 deletions

View File

@@ -9,6 +9,7 @@ defmodule Mobilizon.Discussions.Comment do
import Mobilizon.Storage.Ecto, only: [maybe_add_published_at: 1]
alias Mobilizon.Actors.Actor
alias Mobilizon.Conversations.Conversation
alias Mobilizon.Discussions.{Comment, CommentVisibility, Discussion}
alias Mobilizon.Events.{Event, Tag}
alias Mobilizon.Medias.Media
@@ -49,7 +50,9 @@ defmodule Mobilizon.Discussions.Comment do
:local,
:is_announcement,
:discussion_id,
:language
:conversation_id,
:language,
:visibility
]
@attrs @required_attrs ++ @optional_attrs
@@ -71,6 +74,7 @@ defmodule Mobilizon.Discussions.Comment do
belongs_to(:in_reply_to_comment, Comment, foreign_key: :in_reply_to_comment_id)
belongs_to(:origin_comment, Comment, foreign_key: :origin_comment_id)
belongs_to(:discussion, Discussion, type: :binary_id)
belongs_to(:conversation, Conversation)
has_many(:replies, Comment, foreign_key: :origin_comment_id)
many_to_many(:tags, Tag, join_through: "comments_tags", on_replace: :delete)
has_many(:mentions, Mention)
@@ -80,7 +84,7 @@ defmodule Mobilizon.Discussions.Comment do
end
@doc """
Returns the id of the first comment in the discussion.
Returns the id of the first comment in the discussion or conversation.
"""
@spec get_thread_id(t) :: integer
def get_thread_id(%__MODULE__{id: id, origin_comment_id: origin_comment_id}) do
@@ -181,7 +185,7 @@ defmodule Mobilizon.Discussions.Comment do
Tag.changeset(%Tag{}, tag)
end
defp process_mention(tag) do
Mention.changeset(%Mention{}, tag)
defp process_mention(mention) do
Mention.changeset(%Mention{}, mention)
end
end

View File

@@ -42,9 +42,9 @@ defmodule Mobilizon.Discussions do
:origin_comment,
:replies,
:tags,
:mentions,
:discussion,
:media
:media,
mentions: [:actor]
]
@discussion_preloads [
@@ -76,6 +76,7 @@ defmodule Mobilizon.Discussions do
Comment
|> join(:left, [c], r in Comment, on: r.origin_comment_id == c.id)
|> where([c, _], is_nil(c.in_reply_to_comment_id))
|> where([c], c.visibility in ^@public_visibility)
# TODO: This was added because we don't want to count deleted comments in total_replies.
# However, it also excludes all top-level comments with deleted replies from being selected
# |> where([_, r], is_nil(r.deleted_at))
@@ -197,9 +198,13 @@ defmodule Mobilizon.Discussions do
"""
@spec update_comment(Comment.t(), map) :: {:ok, Comment.t()} | {:error, Changeset.t()}
def update_comment(%Comment{} = comment, attrs) do
comment
|> Comment.update_changeset(attrs)
|> Repo.update()
with {:ok, %Comment{} = comment} <-
comment
|> Comment.update_changeset(attrs)
|> Repo.update(),
%Comment{} = comment <- Repo.preload(comment, @comment_preloads) do
{:ok, comment}
end
end
@doc """
@@ -272,6 +277,19 @@ defmodule Mobilizon.Discussions do
|> Page.build_page(page, limit)
end
@doc """
Get all the comments contained into a discussion
"""
@spec get_comments_in_reply_to_comment_id(integer, integer | nil, integer | nil) ::
Page.t(Comment.t())
def get_comments_in_reply_to_comment_id(origin_comment_id, page \\ nil, limit \\ nil) do
Comment
|> where([c], c.id == ^origin_comment_id)
|> or_where([c], c.origin_comment_id == ^origin_comment_id)
|> order_by(asc: :inserted_at)
|> Page.build_page(page, limit)
end
@doc """
Counts local comments under events
"""