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
17 field(:id, Types.ObjectID, primary_key: true)
18 field(:to, Types.Recipients, default: [])
20 field(:content, :string)
21 field(:actor, Types.ObjectID)
22 field(:published, Types.DateTime)
23 field(:emoji, :map, default: %{})
26 def cast_and_apply(data) do
29 |> apply_action(:insert)
32 def cast_and_validate(data) do
38 def cast_data(data) do
45 |> Map.put_new("actor", data["attributedTo"])
48 def changeset(struct, data) do
52 |> cast(data, __schema__(:fields))
55 def validate_data(data_cng) do
57 |> validate_inclusion(:type, ["ChatMessage"])
58 |> validate_required([:id, :actor, :to, :type, :content, :published])
59 |> validate_length(:to, is: 1)
60 |> validate_length(:content, max: Pleroma.Config.get([:instance, :remote_limit]))
61 |> validate_local_concern()
64 @doc "Validates if at least one of the users in this ChatMessage is a local user, otherwise we don't want the message in our system. It also validates the presence of both users in our system."
65 def validate_local_concern(cng) do
66 with actor_ap <- get_field(cng, :actor),
67 {_, %User{} = actor} <- {:find_actor, User.get_cached_by_ap_id(actor_ap)},
68 {_, %User{} = recipient} <-
69 {:find_recipient, User.get_cached_by_ap_id(get_field(cng, :to) |> hd())},
70 {_, true} <- {:local?, Enum.any?([actor, recipient], & &1.local)} do
75 |> add_error(:actor, "actor and recipient are both remote")
79 |> add_error(:actor, "can't find user")
81 {:find_recipient, _} ->
83 |> add_error(:to, "can't find user")