ChatMessageValidator: Additional validation.

This commit is contained in:
lain 2020-07-03 15:13:27 +02:00
parent 3ca9af1f9f
commit 4a7b89e372
2 changed files with 17 additions and 0 deletions

View file

@ -93,12 +93,14 @@ def validate_content_or_attachment(cng) do
- If both users are in our system - If both users are in our system
- If at least one of the users in this ChatMessage is a local user - If at least one of the users in this ChatMessage is a local user
- If the recipient is not blocking the actor - If the recipient is not blocking the actor
- If the recipient is explicitly not accepting chat messages
""" """
def validate_local_concern(cng) do def validate_local_concern(cng) do
with actor_ap <- get_field(cng, :actor), with actor_ap <- get_field(cng, :actor),
{_, %User{} = actor} <- {:find_actor, User.get_cached_by_ap_id(actor_ap)}, {_, %User{} = actor} <- {:find_actor, User.get_cached_by_ap_id(actor_ap)},
{_, %User{} = recipient} <- {_, %User{} = recipient} <-
{:find_recipient, User.get_cached_by_ap_id(get_field(cng, :to) |> hd())}, {:find_recipient, User.get_cached_by_ap_id(get_field(cng, :to) |> hd())},
{_, false} <- {:not_accepting_chats?, recipient.accepts_chat_messages == false},
{_, false} <- {:blocking_actor?, User.blocks?(recipient, actor)}, {_, false} <- {:blocking_actor?, User.blocks?(recipient, actor)},
{_, true} <- {:local?, Enum.any?([actor, recipient], & &1.local)} do {_, true} <- {:local?, Enum.any?([actor, recipient], & &1.local)} do
cng cng
@ -107,6 +109,10 @@ def validate_local_concern(cng) do
cng cng
|> add_error(:actor, "actor is blocked by recipient") |> add_error(:actor, "actor is blocked by recipient")
{:not_accepting_chats?, true} ->
cng
|> add_error(:to, "recipient does not accept chat messages")
{:local?, false} -> {:local?, false} ->
cng cng
|> add_error(:actor, "actor and recipient are both remote") |> add_error(:actor, "actor and recipient are both remote")

View file

@ -223,6 +223,17 @@ test "does not validate if the recipient is blocking the actor", %{
refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, [])) refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
end end
test "does not validate if the recipient is not accepting chat messages", %{
valid_chat_message: valid_chat_message,
recipient: recipient
} do
recipient
|> Ecto.Changeset.change(%{accepts_chat_messages: false})
|> Pleroma.Repo.update!()
refute match?({:ok, _object, _meta}, ObjectValidator.validate(valid_chat_message, []))
end
test "does not validate if the actor or the recipient is not in our system", %{ test "does not validate if the actor or the recipient is not in our system", %{
valid_chat_message: valid_chat_message valid_chat_message: valid_chat_message
} do } do