From 6a23f03b70423ac1426f22d9877410b57955acd7 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 8 Mar 2021 15:58:25 +0100 Subject: [PATCH] Federate draft status Signed-off-by: Thomas Citharel --- .../activity_stream/converter/event.ex | 4 +- .../activity_stream/converter/post.ex | 7 +- .../transmogrifier/posts_test.exs | 92 +++++++++++++++++++ 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 test/federation/activity_pub/transmogrifier/posts_test.exs diff --git a/lib/federation/activity_stream/converter/event.ex b/lib/federation/activity_stream/converter/event.ex index a45200b5a..e1b8efa87 100644 --- a/lib/federation/activity_stream/converter/event.ex +++ b/lib/federation/activity_stream/converter/event.ex @@ -70,7 +70,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do status: object |> Map.get("ical:status", "CONFIRMED") |> String.downcase(), online_address: object |> Map.get("attachment", []) |> get_online_address(), phone_address: object["phoneAddress"], - draft: false, + draft: object["draft"] == true, url: object["id"], uuid: object["uuid"], tags: tags, @@ -119,7 +119,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do "commentsEnabled" => event.options.comment_moderation == :allow_all, "anonymousParticipationEnabled" => event.options.anonymous_participation, "attachment" => [], - # "draft" => event.draft, + "draft" => event.draft, "ical:status" => event.status |> to_string |> String.upcase(), "id" => event.url, "url" => event.url diff --git a/lib/federation/activity_stream/converter/post.ex b/lib/federation/activity_stream/converter/post.ex index 79e725d00..020fda4aa 100644 --- a/lib/federation/activity_stream/converter/post.ex +++ b/lib/federation/activity_stream/converter/post.ex @@ -51,7 +51,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Post do "content" => post.body, "attributedTo" => creator_url, "published" => (post.publish_at || post.inserted_at) |> to_date(), - "attachment" => [] + "attachment" => [], + "draft" => post.draft } |> Map.merge(audience) |> maybe_add_post_picture(post) @@ -79,7 +80,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Post do local: false, publish_at: object["published"], picture_id: picture_id, - medias: medias + medias: medias, + draft: object["draft"] == true } else {:error, err} -> {:error, err} @@ -91,6 +93,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Post do defp get_actor(nil), do: {:error, "nil property found for actor data"} defp get_actor(actor), do: actor |> Utils.get_url() |> ActivityPub.get_or_fetch_actor_by_url() + defp to_date(nil), do: nil defp to_date(%DateTime{} = date), do: DateTime.to_iso8601(date) defp to_date(%NaiveDateTime{} = date), do: NaiveDateTime.to_iso8601(date) diff --git a/test/federation/activity_pub/transmogrifier/posts_test.exs b/test/federation/activity_pub/transmogrifier/posts_test.exs new file mode 100644 index 000000000..2ccbb4fe2 --- /dev/null +++ b/test/federation/activity_pub/transmogrifier/posts_test.exs @@ -0,0 +1,92 @@ +defmodule Mobilizon.Federation.ActivityPub.Transmogrifier.PostsTest do + use Mobilizon.DataCase + + import Mobilizon.Factory + import Mox + alias Mobilizon.Actors.Actor + alias Mobilizon.Federation.ActivityPub.{Activity, Transmogrifier} + alias Mobilizon.Federation.ActivityStream.Convertible + alias Mobilizon.Posts.Post + alias Mobilizon.Service.HTTP.ActivityPub.Mock + + describe "handle incoming posts" do + setup :verify_on_exit! + + test "it ignores an incoming post if we already have it" do + post = insert(:post) + post = Repo.preload(post, [:author, :attributed_to, :picture, :media]) + + activity = %{ + "type" => "Create", + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "actor" => post.author.url, + "attributedTo" => post.attributed_to.url, + "object" => Convertible.model_to_as(post) + } + + data = + File.read!("test/fixtures/mobilizon-post-activity-group.json") + |> Jason.decode!() + |> Map.merge(activity) + + assert {:ok, nil, _} = Transmogrifier.handle_incoming(data) + end + + test "it receives a draft post correctly as a member" do + %Actor{} = group = insert(:group, domain: "remote.tld", url: "https://remote.tld/@group") + %Actor{} = author = insert(:actor, domain: "remote.tld", url: "https://remote.tld/@author") + insert(:member, parent: group, actor: author, role: :moderator) + insert(:member, parent: group, role: :member) + + object = + Convertible.model_to_as(%Post{ + url: "https://remote.tld/@group/some-slug", + author: author, + attributed_to: group, + picture: nil, + media: [], + body: "my body", + title: "my title", + draft: true + }) + + data = + File.read!("test/fixtures/mobilizon-post-activity-group.json") + |> Jason.decode!() + |> Map.put("object", object) + + assert {:ok, %Activity{}, %Post{draft: true}} = Transmogrifier.handle_incoming(data) + end + + test "it publishes a previously draft post correctly as a member" do + %Actor{} = group = insert(:group, domain: "remote.tld", url: "https://remote.tld/@group") + %Actor{} = author = insert(:actor, domain: "remote.tld", url: "https://remote.tld/@author") + insert(:member, parent: group, actor: author, role: :moderator) + insert(:member, parent: group, role: :member) + + %Post{} = + post = + insert(:post, + url: "https://remote.tld/@group/some-slug", + author: author, + attributed_to: group, + draft: true + ) + + activity = %{ + "type" => "Update", + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "actor" => post.author.url, + "attributedTo" => post.attributed_to.url, + "object" => Convertible.model_to_as(%Post{post | draft: false}) + } + + data = + File.read!("test/fixtures/mobilizon-post-activity-group.json") + |> Jason.decode!() + |> Map.merge(activity) + + assert {:ok, %Activity{}, %Post{draft: false}} = Transmogrifier.handle_incoming(data) + end + end +end