wf_akkoma/patches/pr814_09_fix-multi-selection-poll-counts.patch

82 lines
3.3 KiB
Diff
Raw Normal View History

2024-10-03 19:14:29 +00:00
From d488cf476ea2ea662c5ec6acfa319331c3490d27 Mon Sep 17 00:00:00 2001
From: Oneric <oneric@oneric.stub>
Date: Thu, 20 Jun 2024 19:52:47 +0200
Subject: [PATCH] Fix voters count field
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Mastodon API demands this be null unless its a multi-selection poll.
Not abiding by this can mess up display in some clients.
Fixes: https://akkoma.dev/AkkomaGang/akkoma/issues/190
---
lib/pleroma/web/api_spec/schemas/poll.ex | 4 +++-
lib/pleroma/web/mastodon_api/views/poll_view.ex | 14 +++++++++++---
.../web/mastodon_api/views/poll_view_test.exs | 2 +-
4 files changed, 15 insertions(+), 5 deletions(-)
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})