[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
|
||||
@@ -2,6 +2,9 @@ defmodule MobilizonWeb.Resolvers.Category do
|
||||
require Logger
|
||||
alias Mobilizon.Actors.User
|
||||
|
||||
###
|
||||
# TODO : Refactor this into MobilizonWeb.API.Categories when a standard AS category is defined
|
||||
###
|
||||
def list_categories(_parent, %{page: page, limit: limit}, _resolution) do
|
||||
categories =
|
||||
Mobilizon.Events.list_categories(page, limit)
|
||||
|
||||
25
lib/mobilizon_web/resolvers/comment.ex
Normal file
25
lib/mobilizon_web/resolvers/comment.ex
Normal file
@@ -0,0 +1,25 @@
|
||||
defmodule MobilizonWeb.Resolvers.Comment do
|
||||
require Logger
|
||||
alias Mobilizon.Events.Comment
|
||||
alias Mobilizon.Activity
|
||||
alias Mobilizon.Actors.User
|
||||
alias MobilizonWeb.API.Comments
|
||||
|
||||
def create_comment(_parent, %{text: comment, actor_username: username}, %{
|
||||
context: %{current_user: %User{} = _user}
|
||||
}) do
|
||||
with {:ok, %Activity{data: %{"object" => %{"type" => "Note"} = object}}} <-
|
||||
Comments.create_comment(username, comment) do
|
||||
{:ok,
|
||||
%Comment{
|
||||
text: object["content"],
|
||||
url: object["id"],
|
||||
uuid: object["uuid"]
|
||||
}}
|
||||
end
|
||||
end
|
||||
|
||||
def create_comment(_parent, _args, %{}) do
|
||||
{:error, "You are not allowed to create a comment if not connected"}
|
||||
end
|
||||
end
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule MobilizonWeb.Resolvers.Event do
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Activity
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Events.Event
|
||||
|
||||
def list_events(_parent, %{page: page, limit: limit}, _resolution) do
|
||||
{:ok, Mobilizon.Events.list_events(page, limit)}
|
||||
@@ -63,10 +65,27 @@ defmodule MobilizonWeb.Resolvers.Event do
|
||||
{:ok, found}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Create an event
|
||||
"""
|
||||
def create_event(_parent, args, %{context: %{current_user: user}}) do
|
||||
organizer_actor_id = Map.get(args, :organizer_actor_id) || Actors.get_actor_for_user(user).id
|
||||
args = args |> Map.put(:organizer_actor_id, organizer_actor_id)
|
||||
Mobilizon.Events.create_event(args)
|
||||
with {:ok, %Activity{data: %{"object" => %{"type" => "Event"} = object}}} <-
|
||||
args
|
||||
# Set default organizer_actor_id if none set
|
||||
|> Map.update(
|
||||
:organizer_actor_username,
|
||||
Actors.get_actor_for_user(user).preferred_username,
|
||||
& &1
|
||||
)
|
||||
|> MobilizonWeb.API.Events.create_event() do
|
||||
{:ok,
|
||||
%Event{
|
||||
title: object["name"],
|
||||
description: object["content"],
|
||||
uuid: object["uuid"],
|
||||
url: object["id"]
|
||||
}}
|
||||
end
|
||||
end
|
||||
|
||||
def create_event(_parent, _args, _resolution) do
|
||||
|
||||
@@ -2,6 +2,7 @@ defmodule MobilizonWeb.Resolvers.Group do
|
||||
alias Mobilizon.Actors
|
||||
alias Mobilizon.Actors.{Actor}
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
alias Mobilizon.Activity
|
||||
require Logger
|
||||
|
||||
@doc """
|
||||
@@ -29,24 +30,36 @@ defmodule MobilizonWeb.Resolvers.Group do
|
||||
"""
|
||||
def create_group(
|
||||
_parent,
|
||||
%{preferred_username: preferred_username, creator_username: actor_username},
|
||||
args,
|
||||
%{
|
||||
context: %{current_user: user}
|
||||
context: %{current_user: _user}
|
||||
}
|
||||
) do
|
||||
with %Actor{id: actor_id} <- Actors.get_local_actor_by_name(actor_username),
|
||||
{:user_actor, true} <-
|
||||
{:user_actor, actor_id in Enum.map(Actors.get_actors_for_user(user), & &1.id)},
|
||||
{:ok, %Actor{} = group} <- Actors.create_group(%{preferred_username: preferred_username}) do
|
||||
{:ok, group}
|
||||
else
|
||||
{:error, %Ecto.Changeset{errors: [url: {"has already been taken", []}]}} ->
|
||||
{:error, :group_name_not_available}
|
||||
|
||||
err ->
|
||||
Logger.error(inspect(err))
|
||||
err
|
||||
with {:ok, %Activity{data: %{"object" => %{"type" => "Group"} = object}}} <-
|
||||
MobilizonWeb.API.Groups.create_group(args) do
|
||||
{:ok,
|
||||
%Actor{
|
||||
preferred_username: object["preferredUsername"],
|
||||
summary: object["summary"],
|
||||
type: :Group,
|
||||
# uuid: object["uuid"],
|
||||
url: object["id"]
|
||||
}}
|
||||
end
|
||||
|
||||
# with %Actor{id: actor_id} <- Actors.get_local_actor_by_name(actor_username),
|
||||
# {:user_actor, true} <-
|
||||
# {:user_actor, actor_id in Enum.map(Actors.get_actors_for_user(user), & &1.id)},
|
||||
# {:ok, %Actor{} = group} <- Actors.create_group(%{preferred_username: preferred_username}) do
|
||||
# {:ok, group}
|
||||
# else
|
||||
# {:error, %Ecto.Changeset{errors: [url: {"has already been taken", []}]}} ->
|
||||
# {:error, :group_name_not_available}
|
||||
|
||||
# err ->
|
||||
# Logger.error(inspect(err))
|
||||
# err
|
||||
# end
|
||||
end
|
||||
|
||||
def create_group(_parent, _args, _resolution) do
|
||||
|
||||
@@ -253,7 +253,7 @@ defmodule MobilizonWeb.Schema do
|
||||
field(:uuid, :uuid)
|
||||
field(:url, :string)
|
||||
field(:local, :boolean)
|
||||
field(:content, :string)
|
||||
field(:text, :string)
|
||||
field(:primaryLanguage, :string)
|
||||
field(:replies, list_of(:comment))
|
||||
field(:threadLanguages, non_null(list_of(:string)))
|
||||
@@ -484,12 +484,20 @@ defmodule MobilizonWeb.Schema do
|
||||
arg(:address_type, non_null(:address_type))
|
||||
arg(:online_address, :string)
|
||||
arg(:phone, :string)
|
||||
arg(:organizer_actor_id, non_null(:integer))
|
||||
arg(:category_id, non_null(:integer))
|
||||
arg(:organizer_actor_username, non_null(:string))
|
||||
arg(:category, non_null(:string))
|
||||
|
||||
resolve(&Resolvers.Event.create_event/3)
|
||||
end
|
||||
|
||||
@desc "Create a comment"
|
||||
field :create_comment, type: :comment do
|
||||
arg(:text, non_null(:string))
|
||||
arg(:actor_username, non_null(:string))
|
||||
|
||||
resolve(&Resolvers.Comment.create_comment/3)
|
||||
end
|
||||
|
||||
@desc "Create a category with a title, description and picture"
|
||||
field :create_category, type: :category do
|
||||
arg(:title, non_null(:string))
|
||||
@@ -552,8 +560,9 @@ defmodule MobilizonWeb.Schema do
|
||||
field :create_group, :group do
|
||||
arg(:preferred_username, non_null(:string), description: "The name for the group")
|
||||
arg(:name, :string, description: "The displayed name for the group")
|
||||
arg(:description, :string, description: "The summary for the group", default_value: "")
|
||||
|
||||
arg(:creator_username, :string,
|
||||
arg(:admin_actor_username, :string,
|
||||
description: "The actor's username which will be the admin (otherwise user's default one)"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user