1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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)
26 |> Enum.reduce(cng, fn field, cng ->
28 |> add_error(field, "none of #{inspect(fields)} present")
33 @spec validate_actor_presence(Ecto.Changeset.t(), keyword()) :: Ecto.Changeset.t()
34 def validate_actor_presence(cng, options \\ []) do
35 field_name = Keyword.get(options, :field_name, :actor)
38 |> validate_change(field_name, fn field_name, actor ->
39 case User.get_cached_by_ap_id(actor) do
40 %User{is_active: false} ->
41 [{field_name, "user is deactivated"}]
47 [{field_name, "can't find user"}]
52 @spec validate_object_presence(Ecto.Changeset.t(), keyword()) :: Ecto.Changeset.t()
53 def validate_object_presence(cng, options \\ []) do
54 field_name = Keyword.get(options, :field_name, :object)
55 allowed_types = Keyword.get(options, :allowed_types, false)
58 |> validate_change(field_name, fn field_name, object_id ->
59 object = Object.get_cached_by_ap_id(object_id) || Activity.get_by_ap_id(object_id)
63 [{field_name, "can't find object"}]
65 object && allowed_types && object.data["type"] not in allowed_types ->
66 [{field_name, "object not in allowed types"}]
74 @spec validate_object_or_user_presence(Ecto.Changeset.t(), keyword()) :: Ecto.Changeset.t()
75 def validate_object_or_user_presence(cng, options \\ []) do
76 field_name = Keyword.get(options, :field_name, :object)
77 options = Keyword.put(options, :field_name, field_name)
81 |> validate_actor_presence(options)
85 |> validate_object_presence(options)
87 if actor_cng.valid?, do: actor_cng, else: object_cng
90 @spec validate_host_match(Ecto.Changeset.t(), [atom()]) :: Ecto.Changeset.t()
91 def validate_host_match(cng, fields \\ [:id, :actor]) do
92 if same_domain?(cng, fields) do
96 |> Enum.reduce(cng, fn field, cng ->
98 |> add_error(field, "hosts of #{inspect(fields)} aren't matching")
103 @spec validate_fields_match(Ecto.Changeset.t(), [atom()]) :: Ecto.Changeset.t()
104 def validate_fields_match(cng, fields) do
105 if map_unique?(cng, fields) do
109 |> Enum.reduce(cng, fn field, cng ->
111 |> add_error(field, "Fields #{inspect(fields)} aren't matching")
116 defp map_unique?(cng, fields, func \\ & &1) do
117 Enum.reduce_while(fields, nil, fn field, acc ->
124 {value, nil} -> {:cont, value}
125 {value, value} -> {:cont, value}
131 @spec same_domain?(Ecto.Changeset.t(), [atom()]) :: boolean()
132 def same_domain?(cng, fields \\ [:actor, :object]) do
133 map_unique?(cng, fields, fn value -> URI.parse(value).host end)
136 # This figures out if a user is able to create, delete or modify something
137 # based on the domain and superuser status
138 @spec validate_modification_rights(Ecto.Changeset.t()) :: Ecto.Changeset.t()
139 def validate_modification_rights(cng) do
140 actor = User.get_cached_by_ap_id(get_field(cng, :actor))
142 if User.superuser?(actor) || same_domain?(cng) do
146 |> add_error(:actor, "is not allowed to modify object")