diff --git a/lib/pleroma/constants.ex b/lib/pleroma/constants.ex index 4d69b840cb..e59b85ea3e 100644 --- a/lib/pleroma/constants.ex +++ b/lib/pleroma/constants.ex @@ -22,7 +22,8 @@ defmodule Pleroma.Constants do "generator", "assigned_account", "rules", - "content_type" + "content_type", + "voters" ] ) diff --git a/lib/pleroma/web/activity_pub/object_validators/question_validator.ex b/lib/pleroma/web/activity_pub/object_validators/question_validator.ex index 4bfa295084..00b85a2148 100644 --- a/lib/pleroma/web/activity_pub/object_validators/question_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/question_validator.ex @@ -29,6 +29,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do field(:closed, ObjectValidators.DateTime) field(:voters, {:array, ObjectValidators.ObjectID}, default: []) + field(:votersCount, :integer) field(:nonAnonymous, :boolean) embeds_many(:anyOf, QuestionOptionsValidator) embeds_many(:oneOf, QuestionOptionsValidator) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 816823b1c1..214a6b587e 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -723,6 +723,12 @@ def replies(%{"replies" => %{"items" => items}}) when not is_nil(items) do def replies(_), do: [] + defp set_voters_count(%{"voters" => [_ | _] = voters} = obj) do + Map.merge(obj, %{"votersCount" => length(voters)}) + end + + defp set_voters_count(obj), do: obj + # Prepares the object of an outgoing create activity. def prepare_object(object) do object @@ -735,6 +741,7 @@ def prepare_object(object) do |> set_reply_to_uri |> set_quote_url |> set_replies + |> set_voters_count |> strip_internal_fields |> strip_internal_tags |> set_type diff --git a/lib/pleroma/web/mastodon_api/views/poll_view.ex b/lib/pleroma/web/mastodon_api/views/poll_view.ex index 1e3c9f36d4..5bc482c8d8 100644 --- a/lib/pleroma/web/mastodon_api/views/poll_view.ex +++ b/lib/pleroma/web/mastodon_api/views/poll_view.ex @@ -71,6 +71,10 @@ defp options_and_votes_count(options) do end) end + defp voters_count(%{data: %{"votersCount" => voters}}) when is_number(voters) do + voters + end + defp voters_count(%{data: %{"voters" => [_ | _] = voters}}) do length(voters) end diff --git a/priv/static/schemas/litepub-0.1.jsonld b/priv/static/schemas/litepub-0.1.jsonld index d47f55c692..970ceabdcf 100644 --- a/priv/static/schemas/litepub-0.1.jsonld +++ b/priv/static/schemas/litepub-0.1.jsonld @@ -41,7 +41,8 @@ }, "vcard": "http://www.w3.org/2006/vcard/ns#", "sm": "http://smithereen.software/ns#", - "nonAnonymous": "sm:nonAnonymous" + "nonAnonymous": "sm:nonAnonymous", + "votersCount" : "toot:votersCount" } ] } diff --git a/test/pleroma/web/activity_pub/transmogrifier/question_handling_test.exs b/test/pleroma/web/activity_pub/transmogrifier/question_handling_test.exs index d22ec400dc..66f8499faf 100644 --- a/test/pleroma/web/activity_pub/transmogrifier/question_handling_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier/question_handling_test.exs @@ -173,4 +173,23 @@ test "accepts a Question with no content" do assert {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data) end + + test "it strips voters list and displays voters count instead" do + user = insert(:user) + other_user = insert(:user) + + {:ok, activity} = + CommonAPI.post(user, %{ + status: "???", + poll: %{expires_in: 10, options: ["yes", "no"]} + }) + + object = Object.normalize(activity, fetch: false) + {:ok, _, _} = CommonAPI.vote(other_user, object, [1]) + + {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data) + + refute Map.has_key?(modified["object"], "voters") + assert modified["object"]["votersCount"] == 1 + end end diff --git a/test/pleroma/web/mastodon_api/views/poll_view_test.exs b/test/pleroma/web/mastodon_api/views/poll_view_test.exs index 3aa73c224e..98f1f365fc 100644 --- a/test/pleroma/web/mastodon_api/views/poll_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/poll_view_test.exs @@ -167,7 +167,14 @@ test "doesn't strips HTML tags" do } = PollView.render("show.json", %{object: object}) end - test "that poll is non anonymous" do + test "displays correct voters count" do + object = Object.normalize("https://friends.grishka.me/posts/54642", fetch: true) + result = PollView.render("show.json", %{object: object}) + + assert result[:voters_count] == 14 + end + + test "detects that poll is non anonymous" do object = Object.normalize("https://friends.grishka.me/posts/54642", fetch: true) result = PollView.render("show.json", %{object: object})