AttachmentValidator: Handle empty mediatypes

This commit is contained in:
lain 2020-05-18 20:17:28 +02:00
parent be4db41d71
commit d19c716770
3 changed files with 50 additions and 5 deletions

View file

@ -12,7 +12,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
@primary_key false @primary_key false
embedded_schema do embedded_schema do
field(:type, :string) field(:type, :string)
field(:mediaType, :string) field(:mediaType, :string, default: "application/octet-stream")
field(:name, :string) field(:name, :string)
embeds_many(:url, UrlObjectValidator) embeds_many(:url, UrlObjectValidator)
@ -41,8 +41,16 @@ def changeset(struct, data) do
end end
def fix_media_type(data) do def fix_media_type(data) do
data data =
|> Map.put_new("mediaType", data["mimeType"]) data
|> Map.put_new("mediaType", data["mimeType"])
if data["mediaType"] == "" do
data
|> Map.put("mediaType", "application/octet-stream")
else
data
end
end end
def fix_url(data) do def fix_url(data) do

View file

@ -15,8 +15,8 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
describe "attachments" do describe "attachments" do
test "works with honkerific attachments" do test "works with honkerific attachments" do
attachment = %{ attachment = %{
"mediaType" => "image/jpeg", "mediaType" => "",
"name" => "298p3RG7j27tfsZ9RQ.jpg", "name" => "",
"summary" => "298p3RG7j27tfsZ9RQ.jpg", "summary" => "298p3RG7j27tfsZ9RQ.jpg",
"type" => "Document", "type" => "Document",
"url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg" "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"

View file

@ -13,6 +13,43 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageTest do
alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Web.ActivityPub.Transmogrifier
describe "handle_incoming" do describe "handle_incoming" do
test "handles this" do
data = %{
"@context" => "https://www.w3.org/ns/activitystreams",
"actor" => "https://honk.tedunangst.com/u/tedu",
"id" => "https://honk.tedunangst.com/u/tedu/honk/x6gt8X8PcyGkQcXxzg1T",
"object" => %{
"attachment" => [
%{
"mediaType" => "image/jpeg",
"name" => "298p3RG7j27tfsZ9RQ.jpg",
"summary" => "298p3RG7j27tfsZ9RQ.jpg",
"type" => "Document",
"url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
}
],
"attributedTo" => "https://honk.tedunangst.com/u/tedu",
"content" => "",
"id" => "https://honk.tedunangst.com/u/tedu/chonk/26L4wl5yCbn4dr4y1b",
"published" => "2020-05-18T01:13:03Z",
"to" => [
"https://dontbulling.me/users/lain"
],
"type" => "ChatMessage"
},
"published" => "2020-05-18T01:13:03Z",
"to" => [
"https://dontbulling.me/users/lain"
],
"type" => "Create"
}
_user = insert(:user, ap_id: data["actor"])
_user = insert(:user, ap_id: hd(data["to"]))
assert {:ok, _activity} = Transmogrifier.handle_incoming(data)
end
test "it rejects messages that don't contain content" do test "it rejects messages that don't contain content" do
data = data =
File.read!("test/fixtures/create-chat-message.json") File.read!("test/fixtures/create-chat-message.json")