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
index caf2138a78fe2cf75579fb51273e7fae93454374..e87c1ac2e217bdb48cac07f3a6c2775a4a463531 100644 (file)
@@ -5,10 +5,11 @@
 defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator do
   use Ecto.Schema
 
-  alias Pleroma.Web.ActivityPub.ObjectValidators.Types
   alias Pleroma.User
+  alias Pleroma.Web.ActivityPub.ObjectValidators.Types
 
   import Ecto.Changeset
+  import Pleroma.Web.ActivityPub.Transmogrifier, only: [fix_emoji: 1]
 
   @primary_key false
   @derive Jason.Encoder
@@ -17,9 +18,10 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator do
     field(:id, Types.ObjectID, primary_key: true)
     field(:to, Types.Recipients, default: [])
     field(:type, :string)
-    field(:content, :string)
+    field(:content, Types.SafeText)
     field(:actor, Types.ObjectID)
     field(:published, Types.DateTime)
+    field(:emoji, :map, default: %{})
   end
 
   def cast_and_apply(data) do
@@ -41,6 +43,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator do
 
   def fix(data) do
     data
+    |> fix_emoji()
     |> Map.put_new("actor", data["attributedTo"])
   end
 
@@ -54,21 +57,31 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator do
   def validate_data(data_cng) do
     data_cng
     |> validate_inclusion(:type, ["ChatMessage"])
-    |> validate_required([:id, :actor, :to, :type, :content])
+    |> validate_required([:id, :actor, :to, :type, :content, :published])
     |> validate_length(:to, is: 1)
     |> validate_length(:content, max: Pleroma.Config.get([:instance, :remote_limit]))
     |> validate_local_concern()
   end
 
-  @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."
+  @doc """
+  Validates the following
+  - If both users are in our system
+  - If at least one of the users in this ChatMessage is a local user
+  - If the recipient is not blocking the actor
+  """
   def validate_local_concern(cng) do
     with actor_ap <- get_field(cng, :actor),
          {_, %User{} = actor} <- {:find_actor, User.get_cached_by_ap_id(actor_ap)},
          {_, %User{} = recipient} <-
            {:find_recipient, User.get_cached_by_ap_id(get_field(cng, :to) |> hd())},
+         {_, false} <- {:blocking_actor?, User.blocks?(recipient, actor)},
          {_, true} <- {:local?, Enum.any?([actor, recipient], & &1.local)} do
       cng
     else
+      {:blocking_actor?, true} ->
+        cng
+        |> add_error(:actor, "actor is blocked by recipient")
+
       {:local?, false} ->
         cng
         |> add_error(:actor, "actor and recipient are both remote")