Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / web / activity_pub / object_validators / audio_video_validator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.AudioVideoValidator do
6 use Ecto.Schema
7
8 alias Pleroma.EarmarkRenderer
9 alias Pleroma.EctoType.ActivityPub.ObjectValidators
10 alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
11 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
12 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
13 alias Pleroma.Web.ActivityPub.Transmogrifier
14
15 import Ecto.Changeset
16
17 @primary_key false
18 @derive Jason.Encoder
19
20 embedded_schema do
21 field(:id, ObjectValidators.ObjectID, primary_key: true)
22 field(:to, ObjectValidators.Recipients, default: [])
23 field(:cc, ObjectValidators.Recipients, default: [])
24 field(:bto, ObjectValidators.Recipients, default: [])
25 field(:bcc, ObjectValidators.Recipients, default: [])
26 # TODO: Write type
27 field(:tag, {:array, :map}, default: [])
28 field(:type, :string)
29
30 field(:name, :string)
31 field(:summary, :string)
32 field(:content, :string)
33
34 field(:context, :string)
35 # short identifier for PleromaFE to group statuses by context
36 field(:context_id, :integer)
37
38 # TODO: Remove actor on objects
39 field(:actor, ObjectValidators.ObjectID)
40
41 field(:attributedTo, ObjectValidators.ObjectID)
42 field(:published, ObjectValidators.DateTime)
43 field(:emoji, ObjectValidators.Emoji, default: %{})
44 field(:sensitive, :boolean, default: false)
45 embeds_many(:attachment, AttachmentValidator)
46 field(:replies_count, :integer, default: 0)
47 field(:like_count, :integer, default: 0)
48 field(:announcement_count, :integer, default: 0)
49 field(:inReplyTo, ObjectValidators.ObjectID)
50 field(:url, ObjectValidators.Uri)
51
52 field(:likes, {:array, ObjectValidators.ObjectID}, default: [])
53 field(:announcements, {:array, ObjectValidators.ObjectID}, default: [])
54 end
55
56 def cast_and_apply(data) do
57 data
58 |> cast_data
59 |> apply_action(:insert)
60 end
61
62 def cast_and_validate(data) do
63 data
64 |> cast_data()
65 |> validate_data()
66 end
67
68 def cast_data(data) do
69 %__MODULE__{}
70 |> changeset(data)
71 end
72
73 defp fix_url(%{"url" => url} = data) when is_list(url) do
74 attachment =
75 Enum.find(url, fn x ->
76 mime_type = x["mimeType"] || x["mediaType"] || ""
77
78 is_map(x) and String.starts_with?(mime_type, ["video/", "audio/"])
79 end)
80
81 link_element =
82 Enum.find(url, fn x ->
83 mime_type = x["mimeType"] || x["mediaType"] || ""
84
85 is_map(x) and mime_type == "text/html"
86 end)
87
88 data
89 |> Map.put("attachment", [attachment])
90 |> Map.put("url", link_element["href"])
91 end
92
93 defp fix_url(data), do: data
94
95 defp fix_content(%{"mediaType" => "text/markdown", "content" => content} = data)
96 when is_binary(content) do
97 content =
98 content
99 |> Earmark.as_html!(%Earmark.Options{renderer: EarmarkRenderer})
100 |> Pleroma.HTML.filter_tags()
101
102 Map.put(data, "content", content)
103 end
104
105 defp fix_content(data), do: data
106
107 defp fix(data) do
108 data
109 |> CommonFixes.fix_defaults()
110 |> CommonFixes.fix_attribution()
111 |> CommonFixes.fix_actor()
112 |> Transmogrifier.fix_emoji()
113 |> fix_url()
114 |> fix_content()
115 end
116
117 def changeset(struct, data) do
118 data = fix(data)
119
120 struct
121 |> cast(data, __schema__(:fields) -- [:attachment])
122 |> cast_embed(:attachment)
123 end
124
125 def validate_data(data_cng) do
126 data_cng
127 |> validate_inclusion(:type, ["Audio", "Video"])
128 |> validate_required([:id, :actor, :attributedTo, :type, :context, :attachment])
129 |> CommonValidations.validate_any_presence([:cc, :to])
130 |> CommonValidations.validate_fields_match([:actor, :attributedTo])
131 |> CommonValidations.validate_actor_presence()
132 |> CommonValidations.validate_host_match()
133 end
134 end