[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>
master
Thomas Citharel 4 years ago
parent e3a8343112
commit c1e6612405
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
  1. 20
      lib/mix/tasks/toot.ex
  2. 25
      lib/mobilizon/actors/actor.ex
  3. 77
      lib/mobilizon/actors/actors.ex
  4. 4
      lib/mobilizon/actors/follower.ex
  5. 4
      lib/mobilizon/addresses/addresses.ex
  6. 120
      lib/mobilizon/events/events.ex
  7. 58
      lib/mobilizon_web/api/comments.ex
  8. 54
      lib/mobilizon_web/api/events.ex
  9. 58
      lib/mobilizon_web/api/groups.ex
  10. 123
      lib/mobilizon_web/api/utils.ex
  11. 3
      lib/mobilizon_web/resolvers/category.ex
  12. 25
      lib/mobilizon_web/resolvers/comment.ex
  13. 25
      lib/mobilizon_web/resolvers/event.ex
  14. 41
      lib/mobilizon_web/resolvers/group.ex
  15. 17
      lib/mobilizon_web/schema.ex
  16. 145
      lib/service/activity_pub/activity_pub.ex
  17. 250
      lib/service/activity_pub/transmogrifier.ex
  18. 240
      lib/service/activity_pub/utils.ex
  19. 157
      lib/service/formatter/formatter.ex
  20. 37
      test/fixtures/mastodon-announce.json
  21. 33
      test/fixtures/mastodon-delete.json
  22. 29
      test/fixtures/mastodon-follow-activity.json
  23. 29
      test/fixtures/mastodon-like.json
  24. 70
      test/fixtures/mastodon-post-activity-hashtag.json
  25. 18
      test/fixtures/mastodon-post-activity.json
  26. 47
      test/fixtures/mastodon-undo-announce.json
  27. 34
      test/fixtures/mastodon-undo-like.json
  28. 34
      test/fixtures/mastodon-unfollow-activity.json
  29. 43
      test/fixtures/mastodon-update.json
  30. 65
      test/fixtures/prismo-url-map.json
  31. 118
      test/fixtures/vcr_cassettes/activity_pub/fetch_reply_to_framatube.json
  32. 156
      test/fixtures/vcr_cassettes/activity_pub/fetch_social_tcit_fr_reply.json
  33. 12
      test/fixtures/vcr_cassettes/activity_pub_controller/mastodon-post-activity_actor_call.json
  34. 3
      test/mobilizon/addresses/addresses_test.exs
  35. 13
      test/mobilizon/events/events_test.exs
  36. 25
      test/mobilizon/service/activitypub/activitypub_test.exs
  37. 894
      test/mobilizon/service/activitypub/transmogrifier_test.exs
  38. 40
      test/mobilizon/service/activitypub/utils_test.exs
  39. 41
      test/mobilizon_web/resolvers/comment_resolver_test.exs
  40. 8
      test/mobilizon_web/resolvers/event_resolver_test.exs
  41. 6
      test/mobilizon_web/resolvers/group_resolver_test.exs

@ -4,28 +4,12 @@ defmodule Mix.Tasks.Toot do
"""
use Mix.Task
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Service.ActivityPub.Utils
require Logger
@shortdoc "Toot to an user"
def run([from, to, content]) do
def run([from, content]) do
Mix.Task.run("app.start")
with %Actor{} = from <- Actors.get_actor_by_name(from),
{:ok, %Actor{} = to} <- ActivityPub.find_or_make_actor_from_nickname(to) do
comment = Utils.make_comment_data(from.url, [to.url], content)
ActivityPub.create(%{
to: [to.url],
actor: from,
object: comment,
local: true
})
else
e -> Logger.error(inspect(e))
end
MobilizonWeb.API.Comments.create_comment(from, content)
end
end

@ -167,6 +167,7 @@ defmodule Mobilizon.Actors.Actor do
])
|> build_urls(:Group)
|> put_change(:domain, nil)
|> put_change(:keys, Actors.create_keys())
|> put_change(:type, :Group)
|> validate_required([:url, :outbox_url, :inbox_url, :type, :preferred_username])
|> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_type_index)
@ -292,7 +293,7 @@ defmodule Mobilizon.Actors.Actor do
{:already_following, false} <- {:already_following, following?(follower, followed)} do
do_follow(follower, followed, approved)
else
{:already_following, _} ->
{:already_following, %Follower{}} ->
{:error,
"Could not follow actor: you are already following #{followed.preferred_username}"}
@ -301,6 +302,17 @@ defmodule Mobilizon.Actors.Actor do
end
end
@spec unfollow(struct(), struct()) :: {:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
def unfollow(%Actor{} = followed, %Actor{} = follower) do
with {:already_following, %Follower{} = follow} <-
{:already_following, following?(follower, followed)} do
Actors.delete_follower(follow)
else
{:already_following, false} ->
{:error, "Could not unfollow actor: you are not following #{followed.preferred_username}"}
end
end
defp do_follow(%Actor{} = follower, %Actor{} = followed, approved) do
Actors.create_follower(%{
"actor_id" => follower.id,
@ -311,12 +323,13 @@ defmodule Mobilizon.Actors.Actor do
@spec following?(struct(), struct()) :: boolean()
def following?(
%Actor{id: follower_actor_id} = _follower_actor,
%Actor{followers: followers} = _followed
%Actor{} = follower_actor,
%Actor{} = followed_actor
) do
followers
|> Enum.map(& &1.actor_id)
|> Enum.member?(follower_actor_id)
case Actors.get_follower(followed_actor, follower_actor) do
nil -> false
%Follower{} = follow -> follow
end
end
@spec actor_acct_from_actor(struct()) :: String.t()

@ -162,16 +162,41 @@ defmodule Mobilizon.Actors do
)
end
def get_group_by_name(name) do
case String.split(name, "@") do
[name] ->
Repo.get_by(Actor, preferred_username: name, type: :Group)
[name, domain] ->
Repo.get_by(Actor, preferred_username: name, domain: domain, type: :Group)
@doc """
Get a group by it's title
"""
@spec get_group_by_title(String.t()) :: Actor.t() | nil
def get_group_by_title(title) do
case String.split(title, "@") do
[title] ->
get_local_group_by_title(title)
[title, domain] ->
Repo.one(
from(a in Actor,
where: a.preferred_username == ^title and a.type == "Group" and a.domain == ^domain
)
)
end
end
@doc """
Get a local group by it's title
"""
@spec get_local_group_by_title(String.t()) :: Actor.t() | nil
def get_local_group_by_title(title) do
title
|> do_get_local_group_by_title
|> Repo.one()
end
@spec do_get_local_group_by_title(String.t()) :: Ecto.Query.t()
defp do_get_local_group_by_title(title) do
from(a in Actor,
where: a.preferred_username == ^title and a.type == "Group" and is_nil(a.domain)
)
end
@doc """
Creates a group.
@ -185,8 +210,6 @@ defmodule Mobilizon.Actors do
"""
def create_group(attrs \\ %{}) do
attrs = Map.put(attrs, :keys, create_keys())
%Actor{}
|> Actor.group_creation(attrs)
|> Repo.insert()
@ -218,10 +241,11 @@ defmodule Mobilizon.Actors do
keys: data.keys,
avatar_url: data.avatar_url,
banner_url: data.banner_url,
name: data.name
name: data.name,
summary: data.summary
]
],
conflict_target: [:preferred_username, :domain, :type]
conflict_target: [:url]
)
if preload, do: {:ok, Repo.preload(actor, [:followers])}, else: {:ok, actor}
@ -516,9 +540,11 @@ defmodule Mobilizon.Actors do
end
end
# Create a new RSA key
@doc """
Create a new RSA key
"""
@spec create_keys() :: String.t()
defp create_keys() do
def create_keys() do
key = :public_key.generate_key({:rsa, 2048, 65_537})
entry = :public_key.pem_entry_encode(:RSAPrivateKey, key)
[entry] |> :public_key.pem_encode() |> String.trim_trailing()
@ -958,6 +984,13 @@ defmodule Mobilizon.Actors do
|> Repo.preload([:actor, :target_actor])
end
@spec get_follower(Actor.t(), Actor.t()) :: Follower.t()
def get_follower(%Actor{id: followed_id}, %Actor{id: follower_id}) do
Repo.one(
from(f in Follower, where: f.target_actor_id == ^followed_id and f.actor_id == ^follower_id)
)
end
@doc """
Creates a follower.
@ -1013,6 +1046,24 @@ defmodule Mobilizon.Actors do
Repo.delete(follower)
end
@doc """
Delete a follower by followed and follower actors
## Examples
iex> delete_follower(%Actor{}, %Actor{})
{:ok, %Mobilizon.Actors.Follower{}}
iex> delete_follower(%Actor{}, %Actor{})
{:error, %Ecto.Changeset{}}
"""
@spec delete_follower(Actor.t(), Actor.t()) ::
{:ok, Follower.t()} | {:error, Ecto.Changeset.t()}
def delete_follower(%Actor{} = followed, %Actor{} = follower) do
get_follower(followed, follower) |> Repo.delete()
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking follower changes.

@ -21,4 +21,8 @@ defmodule Mobilizon.Actors.Follower do
|> validate_required([:score, :approved, :target_actor_id, :actor_id])
|> unique_constraint(:target_actor_id, name: :followers_actor_target_actor_unique_index)
end
def url(%Follower{id: id}) do
"#{MobilizonWeb.Endpoint.url()}/follow/#{id}/activity"
end
end

@ -116,7 +116,7 @@ defmodule Mobilizon.Addresses do
rescue
e in ArgumentError ->
Logger.error("#{type_input} is not an existing atom : #{inspect(e)}")
nil
:invalid_type
end
else
type_input
@ -128,7 +128,7 @@ defmodule Mobilizon.Addresses do
process_point(data["latitude"], data["longitude"])
end
else
{:error, nil}
{:error, :invalid_type}
end
end

@ -117,18 +117,20 @@ defmodule Mobilizon.Events do
Repo.get_by!(Event, url: url)
end
@doc """
Gets an event by it's UUID
"""
def get_event_by_uuid(uuid) do
Repo.get_by(Event, uuid: uuid)
end
# @doc """
# Gets an event by it's UUID
# """
# @depreciated "Use get_event_full_by_uuid/3 instead"
# def get_event_by_uuid(uuid) do
# Repo.get_by(Event, uuid: uuid)
# end
@doc """
Gets a single event, with all associations loaded.
Gets a full event by it's UUID
"""
def get_event_full!(id) do
event = Repo.get!(Event, id)
@spec get_event_full_by_uuid(String.t()) :: Event.t()
def get_event_full_by_uuid(uuid) do
event = Repo.get_by(Event, uuid: uuid)
Repo.preload(event, [
:organizer_actor,
@ -142,10 +144,10 @@ defmodule Mobilizon.Events do
end
@doc """
Gets an event by it's URL
Gets a single event, with all associations loaded.
"""
def get_event_full_by_url!(url) do
event = Repo.get_by!(Event, url: url)
def get_event_full!(id) do
event = Repo.get!(Event, id)
Repo.preload(event, [
:organizer_actor,
@ -159,10 +161,33 @@ defmodule Mobilizon.Events do
end
@doc """
Gets a full event by it's UUID
Gets an event by it's URL
"""
def get_event_full_by_uuid(uuid) do
event = Repo.get_by(Event, uuid: uuid)
def get_event_full_by_url(url) do
case Repo.one(
from(e in Event,
where: e.url == ^url,
preload: [
:organizer_actor,
:category,
:sessions,
:tracks,
:tags,
:participants,
:physical_address
]
)
) do
nil -> {:error, :event_not_found}
event -> {:ok, event}
end
end
@doc """
Gets an event by it's URL
"""
def get_event_full_by_url!(url) do
event = Repo.get_by!(Event, url: url)
Repo.preload(event, [
:organizer_actor,
@ -233,7 +258,7 @@ defmodule Mobilizon.Events do
{:ok, %Participant{} = _participant} <-
%Participant{}
|> Participant.changeset(%{
actor_id: attrs.organizer_actor_id,
actor_id: event.organizer_actor_id,
role: 4,
event_id: event.id
})
@ -609,8 +634,12 @@ defmodule Mobilizon.Events do
Participant.changeset(participant, %{})
end
def list_requests_for_actor(%Actor{} = actor) do
Repo.all(from(p in Participant, where: p.actor_id == ^actor.id and p.approved == false))
@doc """
List event participation requests for an actor
"""
@spec list_requests_for_actor(Actor.t()) :: list(Participant.t())
def list_requests_for_actor(%Actor{id: actor_id}) do
Repo.all(from(p in Participant, where: p.actor_id == ^actor_id and p.approved == false))
end
alias Mobilizon.Events.Session
@ -631,24 +660,18 @@ defmodule Mobilizon.Events do
@doc """
Returns the list of sessions for an event
"""
def list_sessions_for_event(event_uuid) do
@spec list_sessions_for_event(Event.t()) :: list(Session.t())
def list_sessions_for_event(%Event{id: event_id}) do
Repo.all(
from(
s in Session,
join: e in Event,
on: s.event_id == e.id,
where: e.uuid == ^event_uuid
where: e.id == ^event_id
)
)
end
@doc """
Returns the list of sessions for a track
"""
def list_sessions_for_track(track_id) do
Repo.all(from(s in Session, where: s.track_id == ^track_id))
end
@doc """
Gets a single session.
@ -745,6 +768,14 @@ defmodule Mobilizon.Events do
Repo.all(Track)
end
@doc """
Returns the list of sessions for a track
"""
@spec list_sessions_for_track(Track.t()) :: list(Session.t())
def list_sessions_for_track(%Track{id: track_id}) do
Repo.all(from(s in Session, where: s.track_id == ^track_id))
end
@doc """
Gets a single track.
@ -880,9 +911,29 @@ defmodule Mobilizon.Events do
"""
def get_comment!(id), do: Repo.get!(Comment, id)
def get_comment_from_uuid(uuid), do: Repo.get_by(Comment, uuid: uuid)
# @doc """
# Gets a single comment from it's UUID
# """
# @spec get_comment_from_uuid(String.t) :: {:ok, Comment.t} | {:error, nil}
# def get_comment_from_uuid(uuid), do: Repo.get_by(Comment, uuid: uuid)
# @doc """
# Gets a single comment by it's UUID.
# Raises `Ecto.NoResultsError` if the Comment does not exist.
def get_comment_from_uuid!(uuid), do: Repo.get_by!(Comment, uuid: uuid)
# ## Examples
# iex> get_comment_from_uuid!("123AFV13")
# %Comment{}
# iex> get_comment_from_uuid!("20R9HKDJHF")
# ** (Ecto.NoResultsError)
# """
# @spec get_comment_from_uuid(String.t) :: Comment.t
# def get_comment_from_uuid!(uuid), do: Repo.get_by!(Comment, uuid: uuid)
def get_comment_full_from_uuid(uuid) do
with %Comment{} = comment <- Repo.get_by!(Comment, uuid: uuid) do
@ -894,9 +945,18 @@ defmodule Mobilizon.Events do
def get_comment_from_url!(url), do: Repo.get_by!(Comment, url: url)
def get_comment_full_from_url(url) do
case Repo.one(
from(c in Comment, where: c.url == ^url, preload: [:actor, :in_reply_to_comment])
) do
nil -> {:error, :comment_not_found}
comment -> {:ok, comment}
end
end
def get_comment_full_from_url!(url) do
with %Comment{} = comment <- Repo.get_by!(Comment, url: url) do
Repo.preload(comment, :actor)
Repo.preload(comment, [:actor, :in_reply_to_comment])
end
end

@ -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

@ -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

@ -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

@ -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)

@ -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)"
)

@ -13,6 +13,7 @@ defmodule Mobilizon.Service.ActivityPub do
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Actors.Follower
alias Mobilizon.Service.Federator
alias Mobilizon.Service.HTTPSignatures
@ -36,7 +37,7 @@ defmodule Mobilizon.Service.ActivityPub do
@spec insert(map(), boolean()) :: {:ok, %Activity{}} | {:error, any()}
def insert(map, local \\ true) when is_map(map) do
with map <- lazy_put_activity_defaults(map),
:ok <- insert_full_object(map, local) do
:ok <- insert_full_object(map) do
object_id =
cond do
is_map(map["object"]) ->
@ -46,7 +47,7 @@ defmodule Mobilizon.Service.ActivityPub do
map["id"]
end
map = Map.put(map, "id", "#{object_id}/activity")
map = if local, do: Map.put(map, "id", "#{object_id}/activity"), else: map
activity = %Activity{
data: map,
@ -69,6 +70,8 @@ defmodule Mobilizon.Service.ActivityPub do
"""
@spec fetch_object_from_url(String.t()) :: {:ok, %Event{}} | {:ok, %Comment{}} | {:error, any()}
def fetch_object_from_url(url) do
Logger.info("Fetching object from url #{url}")
with true <- String.starts_with?(url, "http"),
nil <- Events.get_event_by_url(url),
nil <- Events.get_comment_from_url(url),
@ -94,17 +97,22 @@ defmodule Mobilizon.Service.ActivityPub do
{:ok, Events.get_event_by_url!(activity.data["object"]["id"])}
"Note" ->
{:ok, Events.get_comment_from_url!(activity.data["object"]["id"])}
{:ok, Events.get_comment_full_from_url!(activity.data["object"]["id"])}
other ->
{:error, other}
end
else
object = %Event{} -> {:ok, object}
object = %Comment{} -> {:ok, object}
%Event{url: event_url} -> {:ok, Events.get_event_by_url!(event_url)}
%Comment{url: comment_url} -> {:ok, Events.get_comment_full_from_url!(comment_url)}
e -> {:error, e}
end
end
def create(%{to: to, actor: actor, object: object} = params) do
Logger.debug("creating an activity")
Logger.debug(inspect(params))
Logger.debug(inspect(object))
additional = params[:additional] || %{}
# only accept false as false value
local = !(params[:local] == false)
@ -115,6 +123,7 @@ defmodule Mobilizon.Service.ActivityPub do
%{to: to, actor: actor, published: published, object: object},
additional
),
:ok <- Logger.debug(inspect(create_data)),
{:ok, activity} <- insert(create_data, local),
:ok <- maybe_federate(activity) do
# {:ok, actor} <- Actors.increase_event_count(actor) do
@ -123,6 +132,7 @@ defmodule Mobilizon.Service.ActivityPub do
err ->
Logger.error("Something went wrong")
Logger.error(inspect(err))
err
end
end
@ -154,9 +164,82 @@ defmodule Mobilizon.Service.ActivityPub do
end
end
def follow(%Actor{} = follower, %Actor{} = followed, _activity_id \\ nil, local \\ true) do
with {:ok, follow} <- Actor.follow(followed, follower, true),
data <- make_follow_data(follower, followed, follow.id),
# TODO: This is weird, maybe we shouldn't check here if we can make the activity.
# def like(
# %Actor{url: url} = actor,
# object,
# activity_id \\ nil,
# local \\ true
# ) do
# with nil <- get_existing_like(url, object),
# like_data <- make_like_data(user, object, activity_id),
# {:ok, activity} <- insert(like_data, local),
# {:ok, object} <- add_like_to_object(activity, object),
# :ok <- maybe_federate(activity) do
# {:ok, activity, object}
# else
# %Activity{} = activity -> {:ok, activity, object}
# error -> {:error, error}
# end
# end
# def unlike(
# %User{} = actor,
# %Object{} = object,
# activity_id \\ nil,
# local \\ true
# ) do
# with %Activity{} = like_activity <- get_existing_like(actor.ap_id, object),
# unlike_data <- make_unlike_data(actor, like_activity, activity_id),
# {:ok, unlike_activity} <- insert(unlike_data, local),
# {:ok, _activity} <- Repo.delete(like_activity),
# {:ok, object} <- remove_like_from_object(like_activity, object),
# :ok <- maybe_federate(unlike_activity) do
# {:ok, unlike_activity, like_activity, object}
# else
# _e -> {:ok, object}
# end
# end
# def announce(
# %Actor{} = actor,
# object,
# activity_id \\ nil,
# local \\ true
# ) do
# #with true <- is_public?(object),
# with announce_data <- make_announce_data(actor, object, activity_id),
# {:ok, activity} <- insert(announce_data, local),
# # {:ok, object} <- add_announce_to_object(activity, object),
# :ok <- maybe_federate(activity) do
# {:ok, activity, object}
# else
# error -> {:error, error}
# end
# end
# def unannounce(
# %Actor{} = actor,
# object,
# activity_id \\ nil,
# local \\ true
# ) do
# with %Activity{} = announce_activity <- get_existing_announce(actor.ap_id, object),
# unannounce_data <- make_unannounce_data(actor, announce_activity, activity_id),
# {:ok, unannounce_activity} <- insert(unannounce_data, local),
# :ok <- maybe_federate(unannounce_activity),
# {:ok, _activity} <- Repo.delete(announce_activity),
# {:ok, object} <- remove_announce_from_object(announce_activity, object) do
# {:ok, unannounce_activity, object}
# else
# _e -> {:ok, object}
# end
# end
def follow(%Actor{} = follower, %Actor{} = followed, activity_id \\ nil, local \\ true) do
with {:ok, %Follower{} = follow} <- Actor.follow(followed, follower, true),
activity_follow_id <- activity_id || Follower.url(follow),
data <- make_follow_data(followed, follower, activity_follow_id),
{:ok, activity} <- insert(data, local),
:ok <- maybe_federate(activity) do
{:ok, activity}
@ -166,6 +249,23 @@ defmodule Mobilizon.Service.ActivityPub do
end
end
@spec unfollow(Actor.t(), Actor.t(), String.t(), boolean()) :: {:ok, map()} | any()
def unfollow(%Actor{} = followed, %Actor{} = follower, activity_id \\ nil, local \\ true) do
with {:ok, %Follower{id: follow_id}} <- Actor.unfollow(followed, follower),
# We recreate the follow activity
data <- make_follow_data(followed, follower, follow_id),
{:ok, follow_activity} <- insert(data, local),
unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
{:ok, activity} <- insert(unfollow_data, local),
:ok <- maybe_federate(activity) do
{:ok, activity}
else
err ->
Logger.error(inspect(err))
err
end
end
def delete(object, local \\ true)
def delete(%Event{url: url, organizer_actor: actor} = event, local) do
@ -198,6 +298,21 @@ defmodule Mobilizon.Service.ActivityPub do
end
end
def delete(%Actor{url: url} = actor, local) do
data = %{
"type" => "Delete",
"actor" => url,
"object" => url,
"to" => [url <> "/followers", "https://www.w3.org/ns/activitystreams#Public"]
}
with Actors.delete_actor(actor),
{:ok, activity} <- insert(data, local),
:ok <- maybe_federate(activity) do
{:ok, activity}
end
end
@doc """
Create an actor locally by it's URL (AP ID)
"""
@ -278,7 +393,7 @@ defmodule Mobilizon.Service.ActivityPub do
def publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do
Logger.info("Federating #{id} to #{inbox}")
{host, path} = URI.parse(inbox)
%URI{host: host, path: path} = URI.parse(inbox)
digest = HTTPSignatures.build_digest(json)
date = HTTPSignatures.generate_date_header()
@ -333,15 +448,10 @@ defmodule Mobilizon.Service.ActivityPub do
def actor_data_from_actor_object(data) when is_map(data) do
actor_data = %{
url: data["id"],
info: %{
"ap_enabled" => true,
"source_data" => data
},
avatar_url: data["icon"]["url"],
banner_url: data["image"]["url"],
name: data["name"],
preferred_username: data["preferredUsername"],
follower_address: data["followers"],
summary: data["summary"],
keys: data["publicKey"]["publicKeyPem"],
inbox_url: data["inbox"],
@ -416,7 +526,7 @@ defmodule Mobilizon.Service.ActivityPub do
# Create an activity from a comment
@spec comment_to_activity(%Comment{}, boolean()) :: Activity.t()
defp comment_to_activity(%Comment{} = comment, local \\ true) do
def comment_to_activity(%Comment{} = comment, local \\ true) do
%Activity{
recipients: ["https://www.w3.org/ns/activitystreams#Public"],
actor: comment.actor.url,
@ -471,4 +581,9 @@ defmodule Mobilizon.Service.ActivityPub do
defp sanitize_ical_event_strings(nil) do
nil
end
def is_public?(activity) do
"https://www.w3.org/ns/activitystreams#Public" in (activity.data["to"] ++
(activity.data["cc"] || []))
end
end

@ -2,14 +2,36 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
@moduledoc """
A module to handle coding from internal to wire ActivityPub and back.
"""
alias Mobilizon.Actors.Actor
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Events
alias Mobilizon.Events.{Event, Comment}
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Service.ActivityPub.Utils
require Logger
def get_actor(%{"actor" => actor}) when is_binary(actor) do
actor
end
def get_actor(%{"actor" => actor}) when is_list(actor) do
if is_binary(Enum.at(actor, 0)) do
Enum.at(actor, 0)
else
Enum.find(actor, fn %{"type" => type} -> type in ["Person", "Service", "Application"] end)
|> Map.get("id")
end
end
def get_actor(%{"actor" => %{"id" => id}}) when is_bitstring(id) do
id
end
def get_actor(%{"actor" => nil, "attributedTo" => actor}) when not is_nil(actor) do
get_actor(%{"actor" => actor})
end
@doc """
Modifies an incoming AP object (mastodon format) to our internal format.
"""
@ -48,6 +70,10 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
object
|> Map.put("inReplyTo", replied_object.url)
{:error, {:error, :not_supported}} ->
Logger.info("Object reply origin has not a supported type")
object
e ->
Logger.error("Couldn't fetch #{in_reply_to_id} #{inspect(e)}")
object