UserView: Add acceptsChatMessages field
authorlain <lain@soykaf.club>
Fri, 3 Jul 2020 11:38:59 +0000 (13:38 +0200)
committerlain <lain@soykaf.club>
Fri, 3 Jul 2020 11:38:59 +0000 (13:38 +0200)
lib/pleroma/web/activity_pub/views/user_view.ex
test/web/activity_pub/views/user_view_test.exs

index 4a02b09a17dd3f731edf5d74daf62b8bcea839a6..d062d6230c2e8ad5c7565e8504522ca30fe72b35 100644 (file)
@@ -81,6 +81,15 @@ defmodule Pleroma.Web.ActivityPub.UserView do
 
     fields = Enum.map(user.fields, &Map.put(&1, "type", "PropertyValue"))
 
+    chat_message_acceptance =
+      if is_boolean(user.accepts_chat_messages) do
+        %{
+          "acceptsChatMessages" => user.accepts_chat_messages
+        }
+      else
+        %{}
+      end
+
     %{
       "id" => user.ap_id,
       "type" => user.actor_type,
@@ -103,6 +112,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do
       "tag" => emoji_tags,
       "discoverable" => user.discoverable
     }
+    |> Map.merge(chat_message_acceptance)
     |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
     |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
     |> Map.merge(Utils.make_json_ld_header())
index bec15a996fd256ba56300402679a1cef60820b41..3b4a1bcdeb1fcfdc10a1da2cea5c768b1a9a090d 100644 (file)
@@ -158,4 +158,16 @@ defmodule Pleroma.Web.ActivityPub.UserViewTest do
       assert %{"totalItems" => 1} = UserView.render("following.json", %{user: user})
     end
   end
+
+  describe "acceptsChatMessages" do
+    test "it returns this value if it is set" do
+      true_user = insert(:user, accepts_chat_messages: true)
+      false_user = insert(:user, accepts_chat_messages: false)
+      nil_user = insert(:user, accepts_chat_messages: nil)
+
+      assert %{"acceptsChatMessages" => true} = UserView.render("user.json", user: true_user)
+      assert %{"acceptsChatMessages" => false} = UserView.render("user.json", user: false_user)
+      refute Map.has_key?(UserView.render("user.json", user: nil_user), "acceptsChatMessages")
+    end
+  end
 end