2018-12-14 16:41:55 +00:00
|
|
|
defmodule MobilizonWeb.API.Comments do
|
|
|
|
@moduledoc """
|
2019-09-22 14:26:23 +00:00
|
|
|
API for Comments.
|
2018-12-14 16:41:55 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Events.Comment
|
|
|
|
alias Mobilizon.Service.ActivityPub
|
|
|
|
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
|
2019-09-22 14:26:23 +00:00
|
|
|
|
2019-07-26 09:30:28 +00:00
|
|
|
alias MobilizonWeb.API.Utils
|
2018-12-14 16:41:55 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Create a comment
|
|
|
|
|
|
|
|
Creates a comment from an actor and a status
|
|
|
|
"""
|
2019-07-30 08:35:29 +00:00
|
|
|
@spec create_comment(String.t(), String.t(), String.t()) ::
|
|
|
|
{:ok, Activity.t(), Comment.t()} | any()
|
2019-01-03 13:59:59 +00:00
|
|
|
def create_comment(
|
|
|
|
from_username,
|
|
|
|
status,
|
2019-07-26 09:30:28 +00:00
|
|
|
visibility \\ :public,
|
2019-01-03 13:59:59 +00:00
|
|
|
in_reply_to_comment_URL \\ nil
|
|
|
|
) do
|
2019-01-03 10:34:31 +00:00
|
|
|
with {:local_actor, %Actor{url: url} = actor} <-
|
|
|
|
{:local_actor, Actors.get_local_actor_by_name(from_username)},
|
2019-01-03 13:59:59 +00:00
|
|
|
in_reply_to_comment <- get_in_reply_to_comment(in_reply_to_comment_URL),
|
2019-07-26 09:30:28 +00:00
|
|
|
{content_html, tags, to, cc} <-
|
|
|
|
Utils.prepare_content(actor, status, visibility, [], in_reply_to_comment),
|
2018-12-14 16:41:55 +00:00
|
|
|
comment <-
|
|
|
|
ActivityPubUtils.make_comment_data(
|
|
|
|
url,
|
|
|
|
to,
|
|
|
|
content_html,
|
2019-01-03 13:59:59 +00:00
|
|
|
in_reply_to_comment,
|
2018-12-14 16:41:55 +00:00
|
|
|
tags,
|
|
|
|
cc
|
|
|
|
) do
|
|
|
|
ActivityPub.create(%{
|
2018-12-21 08:03:10 +00:00
|
|
|
to: to,
|
2018-12-14 16:41:55 +00:00
|
|
|
actor: actor,
|
|
|
|
object: comment,
|
|
|
|
local: true
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec get_in_reply_to_comment(nil) :: nil
|
|
|
|
defp get_in_reply_to_comment(nil), do: nil
|
|
|
|
@spec get_in_reply_to_comment(String.t()) :: Comment.t()
|
2019-03-19 10:16:03 +00:00
|
|
|
defp get_in_reply_to_comment(in_reply_to_comment_url) do
|
|
|
|
ActivityPub.fetch_object_from_url(in_reply_to_comment_url)
|
2018-12-14 16:41:55 +00:00
|
|
|
end
|
|
|
|
end
|