Check if mogrify available before calling it
[akkoma] / lib / pleroma / web / activity_pub / object_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.ObjectValidator do
6 @moduledoc """
7 This module is responsible for validating an object (which can be an activity)
8 and checking if it is both well formed and also compatible with our view of
9 the system.
10 """
11
12 alias Pleroma.EctoType.ActivityPub.ObjectValidators
13 alias Pleroma.Object
14 alias Pleroma.User
15 alias Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator
16 alias Pleroma.Web.ActivityPub.ObjectValidators.BlockValidator
17 alias Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator
18 alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
19 alias Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator
20 alias Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator
21 alias Pleroma.Web.ActivityPub.ObjectValidators.FollowValidator
22 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
23 alias Pleroma.Web.ActivityPub.ObjectValidators.UndoValidator
24 alias Pleroma.Web.ActivityPub.ObjectValidators.UpdateValidator
25
26 @spec validate(map(), keyword()) :: {:ok, map(), keyword()} | {:error, any()}
27 def validate(object, meta)
28
29 def validate(%{"type" => "Follow"} = object, meta) do
30 with {:ok, object} <-
31 object
32 |> FollowValidator.cast_and_validate()
33 |> Ecto.Changeset.apply_action(:insert) do
34 object = stringify_keys(object)
35 {:ok, object, meta}
36 end
37 end
38
39 def validate(%{"type" => "Block"} = block_activity, meta) do
40 with {:ok, block_activity} <-
41 block_activity
42 |> BlockValidator.cast_and_validate()
43 |> Ecto.Changeset.apply_action(:insert) do
44 block_activity = stringify_keys(block_activity)
45 outgoing_blocks = Pleroma.Config.get([:activitypub, :outgoing_blocks])
46
47 meta =
48 if !outgoing_blocks do
49 Keyword.put(meta, :do_not_federate, true)
50 else
51 meta
52 end
53
54 {:ok, block_activity, meta}
55 end
56 end
57
58 def validate(%{"type" => "Update"} = update_activity, meta) do
59 with {:ok, update_activity} <-
60 update_activity
61 |> UpdateValidator.cast_and_validate()
62 |> Ecto.Changeset.apply_action(:insert) do
63 update_activity = stringify_keys(update_activity)
64 {:ok, update_activity, meta}
65 end
66 end
67
68 def validate(%{"type" => "Undo"} = object, meta) do
69 with {:ok, object} <-
70 object
71 |> UndoValidator.cast_and_validate()
72 |> Ecto.Changeset.apply_action(:insert) do
73 object = stringify_keys(object)
74 {:ok, object, meta}
75 end
76 end
77
78 def validate(%{"type" => "Delete"} = object, meta) do
79 with cng <- DeleteValidator.cast_and_validate(object),
80 do_not_federate <- DeleteValidator.do_not_federate?(cng),
81 {:ok, object} <- Ecto.Changeset.apply_action(cng, :insert) do
82 object = stringify_keys(object)
83 meta = Keyword.put(meta, :do_not_federate, do_not_federate)
84 {:ok, object, meta}
85 end
86 end
87
88 def validate(%{"type" => "Like"} = object, meta) do
89 with {:ok, object} <-
90 object
91 |> LikeValidator.cast_and_validate()
92 |> Ecto.Changeset.apply_action(:insert) do
93 object = stringify_keys(object)
94 {:ok, object, meta}
95 end
96 end
97
98 def validate(%{"type" => "ChatMessage"} = object, meta) do
99 with {:ok, object} <-
100 object
101 |> ChatMessageValidator.cast_and_validate()
102 |> Ecto.Changeset.apply_action(:insert) do
103 object = stringify_keys(object)
104 {:ok, object, meta}
105 end
106 end
107
108 def validate(%{"type" => "EmojiReact"} = object, meta) do
109 with {:ok, object} <-
110 object
111 |> EmojiReactValidator.cast_and_validate()
112 |> Ecto.Changeset.apply_action(:insert) do
113 object = stringify_keys(object |> Map.from_struct())
114 {:ok, object, meta}
115 end
116 end
117
118 def validate(%{"type" => "Create", "object" => object} = create_activity, meta) do
119 with {:ok, object_data} <- cast_and_apply(object),
120 meta = Keyword.put(meta, :object_data, object_data |> stringify_keys),
121 {:ok, create_activity} <-
122 create_activity
123 |> CreateChatMessageValidator.cast_and_validate(meta)
124 |> Ecto.Changeset.apply_action(:insert) do
125 create_activity = stringify_keys(create_activity)
126 {:ok, create_activity, meta}
127 end
128 end
129
130 def validate(%{"type" => "Announce"} = object, meta) do
131 with {:ok, object} <-
132 object
133 |> AnnounceValidator.cast_and_validate()
134 |> Ecto.Changeset.apply_action(:insert) do
135 object = stringify_keys(object |> Map.from_struct())
136 {:ok, object, meta}
137 end
138 end
139
140 def cast_and_apply(%{"type" => "ChatMessage"} = object) do
141 ChatMessageValidator.cast_and_apply(object)
142 end
143
144 def cast_and_apply(o), do: {:error, {:validator_not_set, o}}
145
146 def stringify_keys(%{__struct__: _} = object) do
147 object
148 |> Map.from_struct()
149 |> stringify_keys
150 end
151
152 def stringify_keys(object) when is_map(object) do
153 object
154 |> Map.new(fn {key, val} -> {to_string(key), stringify_keys(val)} end)
155 end
156
157 def stringify_keys(object) when is_list(object) do
158 object
159 |> Enum.map(&stringify_keys/1)
160 end
161
162 def stringify_keys(object), do: object
163
164 def fetch_actor(object) do
165 with {:ok, actor} <- ObjectValidators.ObjectID.cast(object["actor"]) do
166 User.get_or_fetch_by_ap_id(actor)
167 end
168 end
169
170 def fetch_actor_and_object(object) do
171 fetch_actor(object)
172 Object.normalize(object["object"], true)
173 :ok
174 end
175 end