ChatMessage: Support emoji.
authorlain <lain@soykaf.club>
Mon, 20 Apr 2020 12:08:54 +0000 (14:08 +0200)
committerlain <lain@soykaf.club>
Mon, 20 Apr 2020 12:08:54 +0000 (14:08 +0200)
lib/pleroma/web/activity_pub/builder.ex
lib/pleroma/web/activity_pub/object_validators/chat_message_validator.ex
test/fixtures/create-chat-message.json
test/web/activity_pub/object_validator_test.exs
test/web/common_api/common_api_test.exs

index b67166a30720e99c81cce7971e04b6d98223416d..7576ed2782f689c2a05bcdc523f3bd77f50f6646 100644 (file)
@@ -5,6 +5,7 @@ defmodule Pleroma.Web.ActivityPub.Builder do
   This module encodes our addressing policies and general shape of our objects.
   """
 
+  alias Pleroma.Emoji
   alias Pleroma.Object
   alias Pleroma.User
   alias Pleroma.Web.ActivityPub.Utils
@@ -30,7 +31,8 @@ defmodule Pleroma.Web.ActivityPub.Builder do
        "type" => "ChatMessage",
        "to" => [recipient],
        "content" => content,
-       "published" => DateTime.utc_now() |> DateTime.to_iso8601()
+       "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
+       "emoji" => Emoji.Formatter.get_emoji_map(content)
      }, []}
   end
 
index 9b8262553c553b7b9ecc6c2094c1720b280e25ba..2feb65f29d99b106d1e58290d6b36e95da130e0b 100644 (file)
@@ -20,6 +20,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator do
     field(:content, :string)
     field(:actor, Types.ObjectID)
     field(:published, Types.DateTime)
+    field(:emoji, :map, default: %{})
   end
 
   def cast_and_apply(data) do
index 2e4608f4335204bf2af5d881491b9bb2c3237bdd..6db5b9f5caae9389bfad33e605d428d15fa5063c 100644 (file)
@@ -1,19 +1,19 @@
 {
-    "actor": "http://2hu.gensokyo/users/raymoo",
-    "id": "http://2hu.gensokyo/objects/1",
-    "object": {
-        "attributedTo": "http://2hu.gensokyo/users/raymoo",
-        "content": "You expected a cute girl? Too bad. <script>alert('XSS')</script>",
-        "id": "http://2hu.gensokyo/objects/2",
-        "published": "2020-02-12T14:08:20Z",
-        "to": [
-           "http://2hu.gensokyo/users/marisa"
-        ],
-        "type": "ChatMessage"
-    },
-    "published": "2018-02-12T14:08:20Z",
+  "actor": "http://2hu.gensokyo/users/raymoo",
+  "id": "http://2hu.gensokyo/objects/1",
+  "object": {
+    "attributedTo": "http://2hu.gensokyo/users/raymoo",
+    "content": "You expected a cute girl? Too bad. <script>alert('XSS')</script>",
+    "id": "http://2hu.gensokyo/objects/2",
+    "published": "2020-02-12T14:08:20Z",
     "to": [
-        "http://2hu.gensokyo/users/marisa"
+      "http://2hu.gensokyo/users/marisa"
     ],
-    "type": "Create"
+    "type": "ChatMessage"
+  },
+  "published": "2018-02-12T14:08:20Z",
+  "to": [
+    "http://2hu.gensokyo/users/marisa"
+  ],
+  "type": "Create"
 }
index 3ac5ecaf4ae6573949644f0921a477684092f6e0..8230ae0d983e72fa99076e5c10d6d74ea981a45e 100644 (file)
@@ -15,13 +15,15 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
       user = insert(:user)
       recipient = insert(:user, local: false)
 
-      {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey")
+      {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "hey :firefox:")
 
       %{user: user, recipient: recipient, valid_chat_message: valid_chat_message}
     end
 
     test "validates for a basic object we build", %{valid_chat_message: valid_chat_message} do
-      assert {:ok, _object, _meta} = ObjectValidator.validate(valid_chat_message, [])
+      assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
+
+      assert object == valid_chat_message
     end
 
     test "does not validate if the message is longer than the remote_limit", %{
index c17e30210ccb128844694c9cc70a556a9bc18a4c..86b3648ac2f05806707e67955f4c082da818e565 100644 (file)
@@ -33,7 +33,7 @@ defmodule Pleroma.Web.CommonAPITest do
         CommonAPI.post_chat_message(
           author,
           recipient,
-          "a test message <script>alert('uuu')</script>"
+          "a test message <script>alert('uuu')</script> :firefox:"
         )
 
       assert activity.data["type"] == "Create"
@@ -44,7 +44,11 @@ defmodule Pleroma.Web.CommonAPITest do
       assert object.data["to"] == [recipient.ap_id]
 
       assert object.data["content"] ==
-               "a test message &lt;script&gt;alert(&#39;uuu&#39;)&lt;/script&gt;"
+               "a test message &lt;script&gt;alert(&#39;uuu&#39;)&lt;/script&gt; :firefox:"
+
+      assert object.data["emoji"] == %{
+               "firefox" => "http://localhost:4001/emoji/Firefox.gif"
+             }
 
       assert Chat.get(author.id, recipient.ap_id)
       assert Chat.get(recipient.id, author.ap_id)