1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
12 @spec validate_any_presence(Ecto.Changeset.t(), [atom()]) :: Ecto.Changeset.t()
13 def validate_any_presence(cng, fields) do
16 |> Enum.map(fn field -> get_field(cng, field) end)
27 |> Enum.reduce(cng, fn field, cng ->
29 |> add_error(field, "none of #{inspect(fields)} present")
34 @spec validate_actor_presence(Ecto.Changeset.t(), keyword()) :: Ecto.Changeset.t()
35 def validate_actor_presence(cng, options \\ []) do
36 field_name = Keyword.get(options, :field_name, :actor)
39 |> validate_change(field_name, fn field_name, actor ->
40 case User.get_cached_by_ap_id(actor) do
41 %User{is_active: false} ->
42 [{field_name, "user is deactivated"}]
48 [{field_name, "can't find user"}]
53 @spec validate_object_presence(Ecto.Changeset.t(), keyword()) :: Ecto.Changeset.t()
54 def validate_object_presence(cng, options \\ []) do
55 field_name = Keyword.get(options, :field_name, :object)
56 allowed_types = Keyword.get(options, :allowed_types, false)
59 |> validate_change(field_name, fn field_name, object_id ->
60 object = Object.get_cached_by_ap_id(object_id) || Activity.get_by_ap_id(object_id)
64 [{field_name, "can't find object"}]
66 object && allowed_types && object.data["type"] not in allowed_types ->
67 [{field_name, "object not in allowed types"}]
75 @spec validate_object_or_user_presence(Ecto.Changeset.t(), keyword()) :: Ecto.Changeset.t()
76 def validate_object_or_user_presence(cng, options \\ []) do
77 field_name = Keyword.get(options, :field_name, :object)
78 options = Keyword.put(options, :field_name, field_name)
82 |> validate_actor_presence(options)
86 |> validate_object_presence(options)
88 if actor_cng.valid?, do: actor_cng, else: object_cng
91 @spec validate_host_match(Ecto.Changeset.t(), [atom()]) :: Ecto.Changeset.t()
92 def validate_host_match(cng, fields \\ [:id, :actor]) do
93 if same_domain?(cng, fields) do
97 |> Enum.reduce(cng, fn field, cng ->
99 |> add_error(field, "hosts of #{inspect(fields)} aren't matching")
104 @spec validate_fields_match(Ecto.Changeset.t(), [atom()]) :: Ecto.Changeset.t()
105 def validate_fields_match(cng, fields) do
106 if map_unique?(cng, fields) do
110 |> Enum.reduce(cng, fn field, cng ->
112 |> add_error(field, "Fields #{inspect(fields)} aren't matching")
117 defp map_unique?(cng, fields, func \\ & &1) do
118 Enum.reduce_while(fields, nil, fn field, acc ->
125 {value, nil} -> {:cont, value}
126 {value, value} -> {:cont, value}
132 @spec same_domain?(Ecto.Changeset.t(), [atom()]) :: boolean()
133 def same_domain?(cng, fields \\ [:actor, :object]) do
134 map_unique?(cng, fields, fn value -> URI.parse(value).host end)
137 # This figures out if a user is able to create, delete or modify something
138 # based on the domain and superuser status
139 @spec validate_modification_rights(Ecto.Changeset.t(), atom()) :: Ecto.Changeset.t()
140 def validate_modification_rights(cng, privilege) do
141 actor = User.get_cached_by_ap_id(get_field(cng, :actor))
143 if User.privileged?(actor, privilege) || same_domain?(cng) do
147 |> add_error(:actor, "is not allowed to modify object")