[WIP] Test transmogrifier
Introduce MobilizonWeb.API namespace Signed-off-by: Thomas Citharel <tcit@tcit.fr> Format Signed-off-by: Thomas Citharel <tcit@tcit.fr> WIP Signed-off-by: Thomas Citharel <tcit@tcit.fr> remove unneeded code Signed-off-by: Thomas Citharel <tcit@tcit.fr> Fix tests Signed-off-by: Thomas Citharel <tcit@tcit.fr> Fix warnings Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
58
lib/mobilizon_web/api/comments.ex
Normal file
58
lib/mobilizon_web/api/comments.ex
Normal file
@@ -0,0 +1,58 @@
|
||||
defmodule MobilizonWeb.API.Comments do
|
||||
@moduledoc """
|
||||
API for Comments
|
||||
"""
|
||||
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events.Comment
|
||||
alias Mobilizon.Service.Formatter
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
|
||||
import MobilizonWeb.API.Utils
|
||||
|
||||
@doc """
|
||||
Create a comment
|
||||
|
||||
Creates a comment from an actor and a status
|
||||
"""
|
||||
@spec create_comment(String.t(), String.t(), String.t()) :: {:ok, Activity.t()} | any()
|
||||
def create_comment(from_username, status, visibility \\ "public", inReplyToCommentURL \\ nil) do
|
||||
with %Actor{url: url} = actor <- Actors.get_local_actor_by_name(from_username),
|
||||
status <- String.trim(status),
|
||||
mentions <- Formatter.parse_mentions(status),
|
||||
inReplyToComment <- get_in_reply_to_comment(inReplyToCommentURL),
|
||||
{to, cc} <- to_for_actor_and_mentions(actor, mentions, inReplyToComment, visibility),
|
||||
tags <- Formatter.parse_tags(status),
|
||||
content_html <-
|
||||
make_content_html(
|
||||
status,
|
||||
mentions,
|
||||
tags,
|
||||
"text/plain"
|
||||
),
|
||||
comment <-
|
||||
ActivityPubUtils.make_comment_data(
|
||||
url,
|
||||
to,
|
||||
content_html,
|
||||
inReplyToComment,
|
||||
tags,
|
||||
cc
|
||||
) do
|
||||
ActivityPub.create(%{
|
||||
to: ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
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()
|
||||
defp get_in_reply_to_comment(inReplyToCommentURL) do
|
||||
ActivityPub.fetch_object_from_url(inReplyToCommentURL)
|
||||
end
|
||||
end
|
||||
54
lib/mobilizon_web/api/events.ex
Normal file
54
lib/mobilizon_web/api/events.ex
Normal file
@@ -0,0 +1,54 @@
|
||||
defmodule MobilizonWeb.API.Events do
|
||||
@moduledoc """
|
||||
API for Events
|
||||
"""
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.Formatter
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
|
||||
import MobilizonWeb.API.Utils
|
||||
|
||||
@spec create_event(map()) :: {:ok, Activity.t()} | any()
|
||||
def create_event(
|
||||
%{
|
||||
title: title,
|
||||
description: description,
|
||||
organizer_actor_username: organizer_actor_username,
|
||||
begins_on: begins_on,
|
||||
category: category
|
||||
} = args
|
||||
) do
|
||||
with %Actor{url: url} = actor <- Actors.get_local_actor_by_name(organizer_actor_username),
|
||||
title <- String.trim(title),
|
||||
mentions <- Formatter.parse_mentions(description),
|
||||
visibility <- Map.get(args, :visibility, "public"),
|
||||
{to, cc} <- to_for_actor_and_mentions(actor, mentions, nil, visibility),
|
||||
tags <- Formatter.parse_tags(description),
|
||||
content_html <-
|
||||
make_content_html(
|
||||
description,
|
||||
mentions,
|
||||
tags,
|
||||
"text/plain"
|
||||
),
|
||||
event <-
|
||||
ActivityPubUtils.make_event_data(
|
||||
url,
|
||||
to,
|
||||
title,
|
||||
content_html,
|
||||
tags,
|
||||
cc,
|
||||
%{begins_on: begins_on},
|
||||
category
|
||||
) do
|
||||
ActivityPub.create(%{
|
||||
to: ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
actor: actor,
|
||||
object: event,
|
||||
local: true
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
58
lib/mobilizon_web/api/groups.ex
Normal file
58
lib/mobilizon_web/api/groups.ex
Normal file
@@ -0,0 +1,58 @@
|
||||
defmodule MobilizonWeb.API.Groups do
|
||||
@moduledoc """
|
||||
API for Events
|
||||
"""
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.Formatter
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils
|
||||
import MobilizonWeb.API.Utils
|
||||
|
||||
@spec create_group(map()) :: {:ok, Activity.t()} | any()
|
||||
def create_group(
|
||||
%{
|
||||
preferred_username: title,
|
||||
description: description,
|
||||
admin_actor_username: admin_actor_username
|
||||
} = args
|
||||
) do
|
||||
with {:bad_actor, %Actor{url: url} = actor} <-
|
||||
{:bad_actor, Actors.get_local_actor_by_name(admin_actor_username)},
|
||||
{:existing_group, nil} <- {:existing_group, Actors.get_group_by_title(title)},
|
||||
title <- String.trim(title),
|
||||
mentions <- Formatter.parse_mentions(description),
|
||||
visibility <- Map.get(args, :visibility, "public"),
|
||||
{to, cc} <- to_for_actor_and_mentions(actor, mentions, nil, visibility),
|
||||
tags <- Formatter.parse_tags(description),
|
||||
content_html <-
|
||||
make_content_html(
|
||||
description,
|
||||
mentions,
|
||||
tags,
|
||||
"text/plain"
|
||||
),
|
||||
group <-
|
||||
ActivityPubUtils.make_group_data(
|
||||
url,
|
||||
to,
|
||||
title,
|
||||
content_html,
|
||||
tags,
|
||||
cc
|
||||
) do
|
||||
ActivityPub.create(%{
|
||||
to: ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
actor: actor,
|
||||
object: group,
|
||||
local: true
|
||||
})
|
||||
else
|
||||
{:existing_group, _} ->
|
||||
{:error, :existing_group_name}
|
||||
|
||||
{:bad_actor} ->
|
||||
{:error, :bad_admin_actor}
|
||||
end
|
||||
end
|
||||
end
|
||||
123
lib/mobilizon_web/api/utils.ex
Normal file
123
lib/mobilizon_web/api/utils.ex
Normal file
@@ -0,0 +1,123 @@
|
||||
defmodule MobilizonWeb.API.Utils do
|
||||
@moduledoc """
|
||||
Utils for API
|
||||
"""
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.Formatter
|
||||
|
||||
@doc """
|
||||
Determines the full audience based on mentions for a public audience
|
||||
|
||||
Audience is:
|
||||
* `to` : the mentionned actors, the eventual actor we're replying to and the public
|
||||
* `cc` : the actor's followers
|
||||
"""
|
||||
@spec to_for_actor_and_mentions(Actor.t(), list(), map(), String.t()) :: {list(), list()}
|
||||
def to_for_actor_and_mentions(%Actor{} = actor, mentions, inReplyTo, "public") do
|
||||
mentioned_actors = Enum.map(mentions, fn {_, %{url: url}} -> url end)
|
||||
|
||||
to = ["https://www.w3.org/ns/activitystreams#Public" | mentioned_actors]
|
||||
cc = [actor.followers_url]
|
||||
|
||||
if inReplyTo do
|
||||
{Enum.uniq([inReplyTo.actor | to]), cc}
|
||||
else
|
||||
{to, cc}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Determines the full audience based on mentions based on a unlisted audience
|
||||
|
||||
Audience is:
|
||||
* `to` : the mentionned actors, actor's followers and the eventual actor we're replying to
|
||||
* `cc` : public
|
||||
"""
|
||||
@spec to_for_actor_and_mentions(Actor.t(), list(), map(), String.t()) :: {list(), list()}
|
||||
def to_for_actor_and_mentions(%Actor{} = actor, mentions, inReplyTo, "unlisted") do
|
||||
mentioned_actors = Enum.map(mentions, fn {_, %{url: url}} -> url end)
|
||||
|
||||
to = [actor.followers_url | mentioned_actors]
|
||||
cc = ["https://www.w3.org/ns/activitystreams#Public"]
|
||||
|
||||
if inReplyTo do
|
||||
{Enum.uniq([inReplyTo.actor | to]), cc}
|
||||
else
|
||||
{to, cc}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Determines the full audience based on mentions based on a private audience
|
||||
|
||||
Audience is:
|
||||
* `to` : the mentionned actors, actor's followers and the eventual actor we're replying to
|
||||
* `cc` : none
|
||||
"""
|
||||
@spec to_for_actor_and_mentions(Actor.t(), list(), map(), String.t()) :: {list(), list()}
|
||||
def to_for_actor_and_mentions(%Actor{} = actor, mentions, inReplyTo, "private") do
|
||||
{to, cc} = to_for_actor_and_mentions(actor, mentions, inReplyTo, "direct")
|
||||
{[actor.followers_url | to], cc}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Determines the full audience based on mentions based on a direct audience
|
||||
|
||||
Audience is:
|
||||
* `to` : the mentionned actors and the eventual actor we're replying to
|
||||
* `cc` : none
|
||||
"""
|
||||
@spec to_for_actor_and_mentions(Actor.t(), list(), map(), String.t()) :: {list(), list()}
|
||||
def to_for_actor_and_mentions(_actor, mentions, inReplyTo, "direct") do
|
||||
mentioned_actors = Enum.map(mentions, fn {_, %{url: url}} -> url end)
|
||||
|
||||
if inReplyTo do
|
||||
{Enum.uniq([inReplyTo.actor | mentioned_actors]), []}
|
||||
else
|
||||
{mentioned_actors, []}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates HTML content from text and mentions
|
||||
"""
|
||||
@spec make_content_html(String.t(), list(), list(), String.t()) :: String.t()
|
||||
def make_content_html(
|
||||
status,
|
||||
mentions,
|
||||
tags,
|
||||
content_type
|
||||
),
|
||||
do: format_input(status, mentions, tags, content_type)
|
||||
|
||||
def format_input(text, mentions, tags, "text/plain") do
|
||||
text
|
||||
|> Formatter.html_escape("text/plain")
|
||||
|> String.replace(~r/\r?\n/, "<br>")
|
||||
|> (&{[], &1}).()
|
||||
|> Formatter.add_links()
|
||||
|> Formatter.add_actor_links(mentions)
|
||||
|> Formatter.add_hashtag_links(tags)
|
||||
|> Formatter.finalize()
|
||||
end
|
||||
|
||||
def format_input(text, mentions, _tags, "text/html") do
|
||||
text
|
||||
|> Formatter.html_escape("text/html")
|
||||
|> String.replace(~r/\r?\n/, "<br>")
|
||||
|> (&{[], &1}).()
|
||||
|> Formatter.add_actor_links(mentions)
|
||||
|> Formatter.finalize()
|
||||
end
|
||||
|
||||
def format_input(text, mentions, tags, "text/markdown") do
|
||||
text
|
||||
|> Earmark.as_html!()
|
||||
|> Formatter.html_escape("text/html")
|
||||
|> String.replace(~r/\r?\n/, "")
|
||||
|> (&{[], &1}).()
|
||||
|> Formatter.add_actor_links(mentions)
|
||||
|> Formatter.add_hashtag_links(tags)
|
||||
|> Formatter.finalize()
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user