diff --git a/CHANGELOG.md b/CHANGELOG.md index d1e3a42c0..0d451dca8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## Fixed - Meilisearch: order of results returned from our REST API now actually matches how Meilisearch ranks results - AP objects with additional JSON-LD profiles beyond ActivityStreams can now be fetched +- Single-selection polls no longer expose the voter_count; MastoAPI demands it be null + and this confused some clients leading to vote distributions >100% ## Changed - Refactored Rich Media to cache the content in the database. Fetching operations that could block status rendering have been eliminated. diff --git a/lib/pleroma/web/api_spec/schemas/poll.ex b/lib/pleroma/web/api_spec/schemas/poll.ex index 943ad8bd4..13a5e8be2 100644 --- a/lib/pleroma/web/api_spec/schemas/poll.ex +++ b/lib/pleroma/web/api_spec/schemas/poll.ex @@ -32,7 +32,9 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Poll do }, voters_count: %Schema{ type: :integer, - description: "How many unique accounts have voted. Number." + nullable: true, + description: + "How many unique accounts have voted for a multi-selection poll. Number, or null if single-selection poll." }, voted: %Schema{ type: :boolean, diff --git a/lib/pleroma/web/mastodon_api/views/poll_view.ex b/lib/pleroma/web/mastodon_api/views/poll_view.ex index aa6443754..411cbd15a 100644 --- a/lib/pleroma/web/mastodon_api/views/poll_view.ex +++ b/lib/pleroma/web/mastodon_api/views/poll_view.ex @@ -19,7 +19,7 @@ defmodule Pleroma.Web.MastodonAPI.PollView do expired: expired, multiple: multiple, votes_count: votes_count, - voters_count: voters_count(object), + voters_count: voters_count(multiple, object), options: options, emojis: Pleroma.Web.MastodonAPI.StatusView.build_emojis(object.data["emoji"]) } @@ -68,11 +68,19 @@ defmodule Pleroma.Web.MastodonAPI.PollView do end) end - defp voters_count(%{data: %{"voters" => voters}}) when is_list(voters) do + defp voters_count(false, _poll_data) do + # Mastodon always sets voter count to "null" unless multiple options were selectable + # Some clients may rely on this to detect multiple selection polls and it can mess + # up percentages for some clients if we never got a correct remote voter count and + # only count local voters here; see https://akkoma.dev/AkkomaGang/akkoma/issues/190 + nil + end + + defp voters_count(_multiple, %{data: %{"voters" => voters}}) when is_list(voters) do length(voters) end - defp voters_count(_), do: 0 + defp voters_count(_, _), do: 0 defp voted_and_own_votes(%{object: object} = params, options) do if params[:for] do 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 224b26cb9..91d95f229 100644 --- a/test/pleroma/web/mastodon_api/views/poll_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/poll_view_test.exs @@ -43,7 +43,7 @@ defmodule Pleroma.Web.MastodonAPI.PollViewTest do %{title: "why are you even asking?", votes_count: 0} ], votes_count: 0, - voters_count: 0 + voters_count: nil } result = PollView.render("show.json", %{object: object})