2020-01-26 20:11:16 +00:00
|
|
|
defmodule Mobilizon.GraphQL.API.Comments do
|
2018-12-14 16:41:55 +00:00
|
|
|
@moduledoc """
|
2020-11-26 10:41:13 +00:00
|
|
|
API for discussions and comments.
|
2018-12-14 16:41:55 +00:00
|
|
|
"""
|
2020-01-26 20:11:16 +00:00
|
|
|
|
2020-07-09 15:24:28 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2023-10-17 14:41:31 +00:00
|
|
|
alias Mobilizon.Conversations.Conversation
|
|
|
|
alias Mobilizon.Discussions.{Comment, Discussion}
|
2021-09-28 17:40:37 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.{Actions, Activity}
|
2020-11-26 10:41:13 +00:00
|
|
|
alias Mobilizon.GraphQL.API.Utils
|
2018-12-14 16:41:55 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Create a comment
|
|
|
|
"""
|
2021-10-05 15:43:45 +00:00
|
|
|
@spec create_comment(map) ::
|
|
|
|
{:ok, Activity.t(), Comment.t()}
|
|
|
|
| {:error, :entity_tombstoned | atom() | Ecto.Changeset.t()}
|
2019-10-25 15:43:37 +00:00
|
|
|
def create_comment(args) do
|
2020-11-26 10:41:13 +00:00
|
|
|
args = extract_pictures_from_comment_body(args)
|
2021-09-28 17:40:37 +00:00
|
|
|
Actions.Create.create(:comment, args, true)
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
2019-11-15 17:36:47 +00:00
|
|
|
|
2020-11-26 10:41:13 +00:00
|
|
|
@doc """
|
|
|
|
Updates a comment
|
|
|
|
"""
|
2021-10-05 15:43:45 +00:00
|
|
|
@spec update_comment(Comment.t(), map()) ::
|
|
|
|
{:ok, Activity.t(), Comment.t()} | {:error, atom() | Ecto.Changeset.t()}
|
2020-02-18 07:57:00 +00:00
|
|
|
def update_comment(%Comment{} = comment, args) do
|
2020-11-26 10:41:13 +00:00
|
|
|
args = extract_pictures_from_comment_body(args)
|
2021-09-28 17:40:37 +00:00
|
|
|
Actions.Update.update(comment, args, true)
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
2019-11-15 17:36:47 +00:00
|
|
|
@doc """
|
|
|
|
Deletes a comment
|
|
|
|
"""
|
2020-07-09 15:24:28 +00:00
|
|
|
@spec delete_comment(Comment.t(), Actor.t()) :: {:ok, Activity.t(), Comment.t()} | any
|
|
|
|
def delete_comment(%Comment{} = comment, %Actor{} = actor) do
|
2021-09-28 17:40:37 +00:00
|
|
|
Actions.Delete.delete(comment, actor, true)
|
2019-11-15 17:36:47 +00:00
|
|
|
end
|
2020-11-26 10:41:13 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Creates a discussion (or reply to a discussion)
|
|
|
|
"""
|
2021-10-05 15:43:45 +00:00
|
|
|
@spec create_discussion(map()) ::
|
|
|
|
{:ok, Activity.t(), Discussion.t()}
|
|
|
|
| {:error, :entity_tombstoned | atom | Ecto.Changeset.t()}
|
2020-11-26 10:41:13 +00:00
|
|
|
def create_discussion(args) do
|
|
|
|
args = extract_pictures_from_comment_body(args)
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
Actions.Create.create(
|
2020-11-26 10:41:13 +00:00
|
|
|
:discussion,
|
|
|
|
args,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-10-17 14:41:31 +00:00
|
|
|
@doc """
|
|
|
|
Creates a conversation (or reply to a conversation)
|
|
|
|
"""
|
|
|
|
@spec create_conversation(map()) ::
|
|
|
|
{:ok, Activity.t(), Conversation.t()}
|
|
|
|
| {:error, :entity_tombstoned | atom | Ecto.Changeset.t()}
|
|
|
|
def create_conversation(args) do
|
|
|
|
args = extract_pictures_from_comment_body(args)
|
|
|
|
|
|
|
|
Actions.Create.create(
|
|
|
|
:conversation,
|
|
|
|
args,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-11-26 10:41:13 +00:00
|
|
|
@spec extract_pictures_from_comment_body(map()) :: map()
|
|
|
|
defp extract_pictures_from_comment_body(%{text: text, actor_id: actor_id} = args) do
|
|
|
|
pictures = Utils.extract_pictures_from_body(text, actor_id)
|
|
|
|
Map.put(args, :media, pictures)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp extract_pictures_from_comment_body(args), do: args
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|