1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
11 def validate_recipients_presence(cng, fields \\ [:to, :cc]) do
14 |> Enum.map(fn field -> get_field(cng, field) end)
24 |> Enum.reduce(cng, fn field, cng ->
26 |> add_error(field, "no recipients in any field")
31 def validate_actor_presence(cng, options \\ []) do
32 field_name = Keyword.get(options, :field_name, :actor)
35 |> validate_change(field_name, fn field_name, actor ->
36 if User.get_cached_by_ap_id(actor) do
39 [{field_name, "can't find user"}]
44 def validate_object_presence(cng, options \\ []) do
45 field_name = Keyword.get(options, :field_name, :object)
46 allowed_types = Keyword.get(options, :allowed_types, false)
49 |> validate_change(field_name, fn field_name, object_id ->
50 object = Object.get_cached_by_ap_id(object_id)
54 [{field_name, "can't find object"}]
56 object && allowed_types && object.data["type"] not in allowed_types ->
57 [{field_name, "object not in allowed types"}]
65 def validate_object_or_user_presence(cng, options \\ []) do
66 field_name = Keyword.get(options, :field_name, :object)
67 options = Keyword.put(options, :field_name, field_name)
71 |> validate_actor_presence(options)
75 |> validate_object_presence(options)
77 if actor_cng.valid?, do: actor_cng, else: object_cng