Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[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 field(:emoji, :map, default: %{})
24 end
25
26 def cast_and_apply(data) do
27 data
28 |> cast_data
29 |> apply_action(:insert)
30 end
31
32 def cast_and_validate(data) do
33 data
34 |> cast_data()
35 |> validate_data()
36 end
37
38 def cast_data(data) do
39 %__MODULE__{}
40 |> changeset(data)
41 end
42
43 def fix(data) do
44 data
45 |> Map.put_new("actor", data["attributedTo"])
46 end
47
48 def changeset(struct, data) do
49 data = fix(data)
50
51 struct
52 |> cast(data, __schema__(:fields))
53 end
54
55 def validate_data(data_cng) do
56 data_cng
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()
62 end
63
64 @doc """
65 Validates the following
66 - If both users are in our system
67 - If at least one of the users in this ChatMessage is a local user
68 - If the recipient is not blocking the actor
69 """
70 def validate_local_concern(cng) do
71 with actor_ap <- get_field(cng, :actor),
72 {_, %User{} = actor} <- {:find_actor, User.get_cached_by_ap_id(actor_ap)},
73 {_, %User{} = recipient} <-
74 {:find_recipient, User.get_cached_by_ap_id(get_field(cng, :to) |> hd())},
75 {_, false} <- {:blocking_actor?, User.blocks?(recipient, actor)},
76 {_, true} <- {:local?, Enum.any?([actor, recipient], & &1.local)} do
77 cng
78 else
79 {:blocking_actor?, true} ->
80 cng
81 |> add_error(:actor, "actor is blocked by recipient")
82
83 {:local?, false} ->
84 cng
85 |> add_error(:actor, "actor and recipient are both remote")
86
87 {:find_actor, _} ->
88 cng
89 |> add_error(:actor, "can't find user")
90
91 {:find_recipient, _} ->
92 cng
93 |> add_error(:to, "can't find user")
94 end
95 end
96 end