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
12 def validate_any_presence(cng, fields) do
15 |> Enum.map(fn field -> get_field(cng, field) end)
25 |> Enum.reduce(cng, fn field, cng ->
27 |> add_error(field, "none of #{inspect(fields)} present")
32 def validate_actor_presence(cng, options \\ []) do
33 field_name = Keyword.get(options, :field_name, :actor)
36 |> validate_change(field_name, fn field_name, actor ->
37 case User.get_cached_by_ap_id(actor) do
38 %User{deactivated: true} ->
39 [{field_name, "user is deactivated"}]
45 [{field_name, "can't find user"}]
50 def validate_object_presence(cng, options \\ []) do
51 field_name = Keyword.get(options, :field_name, :object)
52 allowed_types = Keyword.get(options, :allowed_types, false)
55 |> validate_change(field_name, fn field_name, object_id ->
56 object = Object.get_cached_by_ap_id(object_id) || Activity.get_by_ap_id(object_id)
60 [{field_name, "can't find object"}]
62 object && allowed_types && object.data["type"] not in allowed_types ->
63 [{field_name, "object not in allowed types"}]
71 def validate_object_or_user_presence(cng, options \\ []) do
72 field_name = Keyword.get(options, :field_name, :object)
73 options = Keyword.put(options, :field_name, field_name)
77 |> validate_actor_presence(options)
81 |> validate_object_presence(options)
83 if actor_cng.valid?, do: actor_cng, else: object_cng
86 def validate_host_match(cng, fields \\ [:id, :actor]) do
89 |> Enum.map(fn field ->
100 if unique_hosts == 1 do
104 |> Enum.reduce(cng, fn field, cng ->
106 |> add_error(field, "hosts of #{inspect(fields)} aren't matching")
111 def validate_fields_match(cng, fields) do
114 |> Enum.map(fn field -> get_field(cng, field) end)
118 if unique_fields == 1 do
122 |> Enum.reduce(cng, fn field, cng ->
124 |> add_error(field, "Fields #{inspect(fields)} aren't matching")
129 def same_domain?(cng, fields \\ [:actor, :object]) do
132 |> Enum.map(fn field ->
146 # This figures out if a user is able to create, delete or modify something
147 # based on the domain and superuser status
148 def validate_modification_rights(cng) do
149 actor = User.get_cached_by_ap_id(get_field(cng, :actor))
151 if User.superuser?(actor) || same_domain?(cng) do
155 |> add_error(:actor, "is not allowed to modify object")