X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fweb%2Factivity_pub%2Fobject_validators%2Fcommon_validations.ex;h=d9a629a342ec00b20760fef15b7c494e723f388f;hb=1fb383f368b861d7aea77770ba7be6e3dfe3468e;hp=db0e2072dbd53198b875ab034a4afce9835e03a1;hpb=3d1b445cbf001f76af614441c241dcc299e76af7;p=akkoma
diff --git a/lib/pleroma/web/activity_pub/object_validators/common_validations.ex b/lib/pleroma/web/activity_pub/object_validators/common_validations.ex
index db0e2072d..d9a629a34 100644
--- a/lib/pleroma/web/activity_pub/object_validators/common_validations.ex
+++ b/lib/pleroma/web/activity_pub/object_validators/common_validations.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
@@ -8,6 +8,26 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
alias Pleroma.Object
alias Pleroma.User
+ def validate_recipients_presence(cng, fields \\ [:to, :cc]) do
+ non_empty =
+ fields
+ |> Enum.map(fn field -> get_field(cng, field) end)
+ |> Enum.any?(fn
+ [] -> false
+ _ -> true
+ end)
+
+ if non_empty do
+ cng
+ else
+ fields
+ |> Enum.reduce(cng, fn field, cng ->
+ cng
+ |> add_error(field, "no recipients in any field")
+ end)
+ end
+ end
+
def validate_actor_presence(cng, field_name \\ :actor) do
cng
|> validate_change(field_name, fn field_name, actor ->
@@ -21,11 +41,22 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations do
def validate_object_presence(cng, field_name \\ :object) do
cng
- |> validate_change(field_name, fn field_name, actor ->
- if Object.get_cached_by_ap_id(actor) do
+ |> validate_change(field_name, fn field_name, object ->
+ if Object.get_cached_by_ap_id(object) do
[]
else
- [{field_name, "can't find user"}]
+ [{field_name, "can't find object"}]
+ end
+ end)
+ end
+
+ def validate_object_or_user_presence(cng, field_name \\ :object) do
+ cng
+ |> validate_change(field_name, fn field_name, object ->
+ if Object.get_cached_by_ap_id(object) || User.get_cached_by_ap_id(object) do
+ []
+ else
+ [{field_name, "can't find object"}]
end
end)
end