Page MenuHomePhorge

No OneTemporary

Size
9 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/pleroma/web/activity_pub/object_validators/tag_validator.ex b/lib/pleroma/web/activity_pub/object_validators/tag_validator.ex
index dd1bc53ed..e7a7c1fc2 100644
--- a/lib/pleroma/web/activity_pub/object_validators/tag_validator.ex
+++ b/lib/pleroma/web/activity_pub/object_validators/tag_validator.ex
@@ -1,124 +1,83 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.ObjectValidators.TagValidator do
use Ecto.Schema
alias Pleroma.EctoType.ActivityPub.ObjectValidators
import Ecto.Changeset
@primary_key false
embedded_schema do
# Common
field(:type, :string)
field(:name, :string)
# Mention, Hashtag
field(:href, ObjectValidators.Uri)
# Emoji
embeds_one :icon, IconObjectValidator, primary_key: false do
field(:type, :string)
field(:url, ObjectValidators.Uri)
end
field(:updated, ObjectValidators.DateTime)
field(:id, ObjectValidators.Uri)
end
def cast_and_validate(data) do
data
|> cast_data()
end
def cast_data(data) do
%__MODULE__{}
|> changeset(data)
end
def changeset(struct, %{"type" => "Mention"} = data) do
struct
|> cast(data, [:type, :name, :href])
|> validate_required([:type, :href])
end
def changeset(struct, %{"type" => "Hashtag", "name" => name} = data) do
name =
cond do
"#" <> name -> name
name -> name
end
|> String.downcase()
data = Map.put(data, "name", name)
struct
|> cast(data, [:type, :name, :href])
|> validate_required([:type, :name])
end
def changeset(struct, %{"type" => "Emoji"} = data) do
data = Map.put(data, "name", String.trim(data["name"], ":"))
struct
|> cast(data, [:type, :name, :updated, :id])
|> cast_embed(:icon, with: &icon_changeset/2)
|> validate_required([:type, :name, :icon])
end
- def changeset(struct, %{"type" => "Book"} = data) do
- data =
- if Map.has_key?(data, "title") do
- Map.put(data, "name", data["title"])
- else
- data
- end
-
- struct
- |> cast(data, [:type, :name])
- |> validate_required([:type, :name])
- end
-
- def changeset(struct, %{"type" => "Edition"} = data) do
- # Sometimes it's title, sometimes name
- # Don't ask me.
- data =
- if Map.has_key?(data, "title") do
- Map.put(data, "name", data["title"])
- else
- data
- end
-
+ def changeset(struct, %{"type" => _} = data) do
struct
- |> cast(data, [:type, :name])
- |> validate_required([:type, :name])
- end
-
- def changeset(struct, %{"type" => "Work"} = data) do
- data =
- if Map.has_key?(data, "title") do
- Map.put(data, "name", data["title"])
- else
- data
- end
-
- struct
- |> cast(data, [:type, :name])
- |> validate_required([:type, :name])
- end
-
- def changeset(struct, %{"type" => "Author"} = data) do
- struct
- |> cast(data, [:type, :name])
- |> validate_required([:type, :name])
+ |> cast(data, [])
+ |> Map.put(:action, :ignore)
end
def icon_changeset(struct, data) do
struct
|> cast(data, [:type, :url])
|> validate_inclusion(:type, ~w[Image])
|> validate_required([:type, :url])
end
end
diff --git a/test/fixtures/bookwyrm-article.json b/test/fixtures/bookwyrm-article.json
index 66b34b688..d0f520ec3 100644
--- a/test/fixtures/bookwyrm-article.json
+++ b/test/fixtures/bookwyrm-article.json
@@ -1,35 +1,35 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"attachment": [
{
"id": null,
"name": "Death's End (The Three-Body Problem) (2018, Head of Zeus)",
"type": "Document",
"url": "https://bookwyrm.com/images/covers/e7a6a777-b3fa-44be-a819-33f3aa5187dd.jpeg"
}
],
"attributedTo": "https://bookwyrm.com/user/TestUser",
"cc": [
"https://bookwyrm.com/user/TestUser/followers"
],
"content": "<p>review</p>",
"id": "https://bookwyrm.com/user/TestUser/review/17",
"inReplyToBook": "https://bookwyrm.com/book/2",
"name": "Review of \"Death's End (The Three-Body Problem)\": ab",
"published": "2022-01-07T16:07:43.665392+00:00",
"replies": {
"@context": "https://www.w3.org/ns/activitystreams",
"first": "https://bookwyrm.com/user/TestUser/review/17/replies?page=1",
"id": "https://bookwyrm.com/user/TestUser/review/17/replies",
"last": "https://bookwyrm.com/user/TestUser/review/17/replies?page=1",
"totalItems": 0,
"type": "OrderedCollection"
},
"sensitive": false,
- "tag": [],
+ "tag": [{"type": "Edition"}],
"to": [
"https://www.w3.org/ns/activitystreams#Public"
],
"type": "Article",
"updated": "2022-01-07T16:14:08.267337+00:00"
}
diff --git a/test/pleroma/web/activity_pub/object_validators/tag_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/tag_validator_test.exs
index bca7a32b9..443dd308d 100644
--- a/test/pleroma/web/activity_pub/object_validators/tag_validator_test.exs
+++ b/test/pleroma/web/activity_pub/object_validators/tag_validator_test.exs
@@ -1,112 +1,19 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.ObjectValidators.TagValidatorTest do
use Pleroma.DataCase, async: true
alias Pleroma.Web.ActivityPub.ObjectValidators.TagValidator
- test "it validates an Edition" do
+ test "it doesn't error on unusual objects" do
edition = %{
"@context" => "https://www.w3.org/ns/activitystreams",
- "asin" => "",
- "authors" => ["https://bookwyrm.com/author/3"],
- "cover" => %{
- "@context" => "https://www.w3.org/ns/activitystreams",
- "name" => "Piranesi (2020, Bloomsbury Publishing)",
- "type" => "Document",
- "url" => "https://bookwyrm.com/images/covers/9fd28af7-ebb8-4df3-80c8-28488fc5349f.jpeg"
- },
- "description" => "",
- "editionRank" => 7,
- "firstPublishedDate" => "",
- "goodreadsKey" => "",
- "id" => "https://bookwyrm.com/book/10",
- "isbn10" => "163557563X",
- "isbn13" => "9781635575637",
- "languages" => ["English"],
- "librarythingKey" => "",
- "oclcNumber" => "",
- "openlibraryKey" => "OL28300471M",
- "pages" => 272,
- "physicalFormat" => "",
- "physicalFormatDetail" => "hardcover",
- "publishedDate" => "2020-09-15T00:00:00+00:00",
- "publishers" => ["Bloomsbury Publishing"],
- "series" => "",
- "seriesNumber" => "",
- "sortTitle" => "",
- "subjectPlaces" => [],
- "subjects" => [],
- "subtitle" => "",
- "title" => "Piranesi",
"type" => "Edition",
"work" => "https://bookwyrm.com/book/9"
}
- assert %{valid?: true, changes: %{name: "Piranesi"}} = TagValidator.cast_and_validate(edition)
- end
-
- test "it should validate an author" do
- author = %{
- "@context" => "https://www.w3.org/ns/activitystreams",
- "aliases" => [],
- "bio" => "snipped",
- "bnfId" => "14603397h",
- "born" => "1959-11-01T00:00:00+00:00",
- "goodreadsKey" => "",
- "id" => "https://bookwyrm.com/author/3",
- "isni" => "0000 0001 0877 1086",
- "librarythingKey" => "",
- "name" => "Susanna Clarke",
- "openlibraryKey" => "OL1387961A",
- "type" => "Author",
- "viafId" => "19931023",
- "wikipediaLink" => ""
- }
-
- assert %{valid?: true, changes: %{name: "Susanna Clarke"}} =
- TagValidator.cast_and_validate(author)
- end
-
- test "it should validate a work" do
- work = %{
- "@context" => "https://www.w3.org/ns/activitystreams",
- "authors" => ["https://bookwyrm.com/author/3"],
- "cover" => %{
- "@context" => "https://www.w3.org/ns/activitystreams",
- "name" => "Piranesi",
- "type" => "Document",
- "url" => "https://bookwyrm.com/images/covers/e950ac10-feaf-4c3e-b2d3-de20d3a28329.jpeg"
- },
- "description" => "snipped",
- "editions" => [
- "https://bookwyrm.com/book/12",
- "https://bookwyrm.com/book/10",
- "https://bookwyrm.com/book/14",
- "https://bookwyrm.com/book/13",
- "https://bookwyrm.com/book/11",
- "https://bookwyrm.com/book/15"
- ],
- "firstPublishedDate" => "",
- "goodreadsKey" => "",
- "id" => "https://bookwyrm.com/book/9",
- "languages" => [],
- "lccn" => "",
- "librarythingKey" => "",
- "openlibraryKey" => "OL20893680W",
- "publishedDate" => "",
- "series" => "",
- "seriesNumber" => "",
- "sortTitle" => "",
- "subjectPlaces" => [],
- "subjects" => ["English literature"],
- "subtitle" => "",
- "title" => "Piranesi",
- "type" => "Work"
- }
-
- assert %{valid?: true, changes: %{name: "Piranesi"}} = TagValidator.cast_and_validate(work)
+ assert %{valid?: true, action: :ignore} = TagValidator.cast_and_validate(edition)
end
end

File Metadata

Mime Type
text/x-diff
Expires
Tue, Oct 13, 4:24 AM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1786595
Default Alt Text
(9 KB)

Event Timeline