ChatMessageValidator: Require `published` field
[akkoma] / lib / pleroma / web / activity_pub / object_validators / chat_message_validator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator do
6 use Ecto.Schema
7
8 alias Pleroma.User
9 alias Pleroma.Web.ActivityPub.ObjectValidators.Types
10
11 import Ecto.Changeset
12
13 @primary_key false
14 @derive Jason.Encoder
15
16 embedded_schema do
17 field(:id, Types.ObjectID, primary_key: true)
18 field(:to, Types.Recipients, default: [])
19 field(:type, :string)
20 field(:content, :string)
21 field(:actor, Types.ObjectID)
22 field(:published, Types.DateTime)
23 end
24
25 def cast_and_apply(data) do
26 data
27 |> cast_data
28 |> apply_action(:insert)
29 end
30
31 def cast_and_validate(data) do
32 data
33 |> cast_data()
34 |> validate_data()
35 end
36
37 def cast_data(data) do
38 %__MODULE__{}
39 |> changeset(data)
40 end
41
42 def fix(data) do
43 data
44 |> Map.put_new("actor", data["attributedTo"])
45 end
46
47 def changeset(struct, data) do
48 data = fix(data)
49
50 struct
51 |> cast(data, __schema__(:fields))
52 end
53
54 def validate_data(data_cng) do
55 data_cng
56 |> validate_inclusion(:type, ["ChatMessage"])
57 |> validate_required([:id, :actor, :to, :type, :content, :published])
58 |> validate_length(:to, is: 1)
59 |> validate_length(:content, max: Pleroma.Config.get([:instance, :remote_limit]))
60 |> validate_local_concern()
61 end
62
63 @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."
64 def validate_local_concern(cng) do
65 with actor_ap <- get_field(cng, :actor),
66 {_, %User{} = actor} <- {:find_actor, User.get_cached_by_ap_id(actor_ap)},
67 {_, %User{} = recipient} <-
68 {:find_recipient, User.get_cached_by_ap_id(get_field(cng, :to) |> hd())},
69 {_, true} <- {:local?, Enum.any?([actor, recipient], & &1.local)} do
70 cng
71 else
72 {:local?, false} ->
73 cng
74 |> add_error(:actor, "actor and recipient are both remote")
75
76 {:find_actor, _} ->
77 cng
78 |> add_error(:actor, "can't find user")
79
80 {:find_recipient, _} ->
81 cng
82 |> add_error(:to, "can't find user")
83 end
84 end
85 end