Federate draft status

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-03-08 15:58:25 +01:00
parent 3bffabccb6
commit 6a23f03b70
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
3 changed files with 99 additions and 4 deletions

View File

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

View File

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

View File

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