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.ChatMessageValidator do
9 alias Pleroma.Web.ActivityPub.ObjectValidators.Types
12 import Pleroma.Web.ActivityPub.Transmogrifier, only: [fix_emoji: 1]
18 field(:id, Types.ObjectID, primary_key: true)
19 field(:to, Types.Recipients, default: [])
21 field(:content, Types.SafeText)
22 field(:actor, Types.ObjectID)
23 field(:published, Types.DateTime)
24 field(:emoji, :map, default: %{})
27 def cast_and_apply(data) do
30 |> apply_action(:insert)
33 def cast_and_validate(data) do
39 def cast_data(data) do
47 |> Map.put_new("actor", data["attributedTo"])
50 def changeset(struct, data) do
54 |> cast(data, __schema__(:fields))
57 def validate_data(data_cng) do
59 |> validate_inclusion(:type, ["ChatMessage"])
60 |> validate_required([:id, :actor, :to, :type, :content, :published])
61 |> validate_length(:to, is: 1)
62 |> validate_length(:content, max: Pleroma.Config.get([:instance, :remote_limit]))
63 |> validate_local_concern()
67 Validates the following
68 - If both users are in our system
69 - If at least one of the users in this ChatMessage is a local user
70 - If the recipient is not blocking the actor
72 def validate_local_concern(cng) do
73 with actor_ap <- get_field(cng, :actor),
74 {_, %User{} = actor} <- {:find_actor, User.get_cached_by_ap_id(actor_ap)},
75 {_, %User{} = recipient} <-
76 {:find_recipient, User.get_cached_by_ap_id(get_field(cng, :to) |> hd())},
77 {_, false} <- {:blocking_actor?, User.blocks?(recipient, actor)},
78 {_, true} <- {:local?, Enum.any?([actor, recipient], & &1.local)} do
81 {:blocking_actor?, true} ->
83 |> add_error(:actor, "actor is blocked by recipient")
87 |> add_error(:actor, "actor and recipient are both remote")
91 |> add_error(:actor, "can't find user")
93 {:find_recipient, _} ->
95 |> add_error(:to, "can't find user")