Merge branch 'remake-remodel' into develop
[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.Object
13 alias Pleroma.User
14 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
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 {:ok, object} <-
21 object |> LikeValidator.cast_and_validate() |> Ecto.Changeset.apply_action(:insert) do
22 object = stringify_keys(object |> Map.from_struct())
23 {:ok, object, meta}
24 end
25 end
26
27 def stringify_keys(object) do
28 object
29 |> Enum.map(fn {key, val} -> {to_string(key), val} end)
30 |> Enum.into(%{})
31 end
32
33 def fetch_actor_and_object(object) do
34 User.get_or_fetch_by_ap_id(object["actor"])
35 Object.normalize(object["object"])
36 :ok
37 end
38 end