Transmogrifier: Make proper use of the LikeValidator.
[akkoma] / lib / pleroma / web / activity_pub / object_validator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Web.ActivityPub.ObjectValidators.LikeValidator
13 alias Pleroma.User
14 alias Pleroma.Object
15
16 @spec validate(map(), keyword()) :: {:ok, map(), keyword()} | {:error, any()}
17 def validate(object, meta)
18
19 def validate(%{"type" => "Like"} = object, meta) do
20 with {_, %{valid?: true, changes: object}} <-
21 {:validate_object, LikeValidator.cast_and_validate(object)} do
22 object = stringify_keys(object)
23 {:ok, object, meta}
24 else
25 e -> {:error, e}
26 end
27 end
28
29 def stringify_keys(object) do
30 object
31 |> Enum.map(fn {key, val} -> {to_string(key), val} end)
32 |> Enum.into(%{})
33 end
34
35 def fetch_actor_and_object(object) do
36 User.get_or_fetch_by_ap_id(object["actor"])
37 Object.normalize(object["object"])
38 :ok
39 end
40 end