Merge remote-tracking branch 'upstream/bookwyrm-entities' into develop
[akkoma] / lib / pleroma / web / activity_pub / object_validators / attachment_validator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator do
6 use Ecto.Schema
7
8 alias Pleroma.EctoType.ActivityPub.ObjectValidators
9
10 import Ecto.Changeset
11
12 @primary_key false
13 embedded_schema do
14 field(:type, :string)
15 field(:mediaType, :string, default: "application/octet-stream")
16 field(:name, :string)
17 field(:blurhash, :string)
18
19 embeds_many :url, UrlObjectValidator, primary_key: false do
20 field(:type, :string)
21 field(:href, ObjectValidators.Uri)
22 field(:mediaType, :string, default: "application/octet-stream")
23 field(:width, :integer)
24 field(:height, :integer)
25 end
26 end
27
28 def cast_and_validate(data) do
29 data
30 |> cast_data()
31 |> validate_data()
32 end
33
34 def cast_data(data) do
35 %__MODULE__{}
36 |> changeset(data)
37 end
38
39 def changeset(struct, data) do
40 data =
41 data
42 |> fix_media_type()
43 |> fix_url()
44
45 struct
46 |> cast(data, [:type, :mediaType, :name, :blurhash])
47 |> cast_embed(:url, with: &url_changeset/2)
48 |> validate_inclusion(:type, ~w[Link Document Audio Image Video])
49 |> validate_required([:type, :mediaType, :url])
50 end
51
52 def url_changeset(struct, data) do
53 data = fix_media_type(data)
54
55 struct
56 |> cast(data, [:type, :href, :mediaType, :width, :height])
57 |> validate_inclusion(:type, ["Link"])
58 |> validate_required([:type, :href, :mediaType])
59 end
60
61 def fix_media_type(data) do
62 data = Map.put_new(data, "mediaType", data["mimeType"])
63
64 if is_bitstring(data["mediaType"]) && MIME.extensions(data["mediaType"]) != [] do
65 data
66 else
67 Map.put(data, "mediaType", "application/octet-stream")
68 end
69 end
70
71 defp handle_href(href, mediaType, data) do
72 [
73 %{
74 "href" => href,
75 "type" => "Link",
76 "mediaType" => mediaType,
77 "width" => data["width"],
78 "height" => data["height"]
79 }
80 ]
81 end
82
83 defp fix_url(data) do
84 cond do
85 is_binary(data["url"]) ->
86 Map.put(data, "url", handle_href(data["url"], data["mediaType"], data))
87
88 is_binary(data["href"]) and data["url"] == nil ->
89 Map.put(data, "url", handle_href(data["href"], data["mediaType"], data))
90
91 true ->
92 data
93 end
94 end
95
96 defp validate_data(cng) do
97 cng
98 |> validate_inclusion(:type, ~w[Document Audio Image Video])
99 |> validate_required([:mediaType, :url, :type])
100 end
101 end