Include own_votes in the poll data
authorMark Felder <feld@feld.me>
Fri, 22 Jan 2021 15:47:59 +0000 (09:47 -0600)
committerMark Felder <feld@feld.me>
Fri, 22 Jan 2021 15:47:59 +0000 (09:47 -0600)
CHANGELOG.md
lib/pleroma/web/mastodon_api/views/poll_view.ex
test/pleroma/web/mastodon_api/views/poll_view_test.exs

index e1dfeae01c00199f45de696ae910d63b94627242..328a7c28c3b0060120410ee615fdc81d9e797a34 100644 (file)
@@ -59,6 +59,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
   <summary>API Changes</summary>
   - Mastodon API: Current user is now included in conversation if it's the only participant.
   - Mastodon API: Fixed last_status.account being not filled with account data.
+  - Mastodon API: Fixed own_votes being not returned with poll data.
 </details>
 
 ## Unreleased (Patch)
index d6b544037c221791a6e9eac6514ba86824bb28b8..94bc1c13992dfba57b37c88d958a13ec8487dac3 100644 (file)
@@ -10,6 +10,7 @@ defmodule Pleroma.Web.MastodonAPI.PollView do
   def render("show.json", %{object: object, multiple: multiple, options: options} = params) do
     {end_time, expired} = end_time_and_expired(object)
     {options, votes_count} = options_and_votes_count(options)
+    {voted, own_votes} = voted_and_own_votes(params, options)
 
     %{
       # Mastodon uses separate ids for polls, but an object can't have
@@ -21,7 +22,8 @@ defmodule Pleroma.Web.MastodonAPI.PollView do
       votes_count: votes_count,
       voters_count: voters_count(object),
       options: options,
-      voted: voted?(params),
+      voted: voted,
+      own_votes: own_votes,
       emojis: Pleroma.Web.MastodonAPI.StatusView.build_emojis(object.data["emoji"])
     }
   end
@@ -67,12 +69,25 @@ defmodule Pleroma.Web.MastodonAPI.PollView do
 
   defp voters_count(_), do: 0
 
-  defp voted?(%{object: object} = opts) do
-    if opts[:for] do
-      existing_votes = Pleroma.Web.ActivityPub.Utils.get_existing_votes(opts[:for].ap_id, object)
-      existing_votes != [] or opts[:for].ap_id == object.data["actor"]
+  defp voted_and_own_votes(%{object: object} = params, options) do
+    options = options |> Enum.map(fn x -> Map.get(x, :title) end)
+
+    if params[:for] do
+      existing_votes =
+        Pleroma.Web.ActivityPub.Utils.get_existing_votes(params[:for].ap_id, object)
+
+      own_votes =
+        for vote <- existing_votes do
+          data = Map.get(vote, :object) |> Map.get(:data)
+
+          Enum.find_index(options, fn x -> x == data["name"] end)
+        end || []
+
+      voted = existing_votes != [] or params[:for].ap_id == object.data["actor"]
+
+      {voted, own_votes}
     else
-      false
+      {false, []}
     end
   end
 end
index a8600e1c28f6eb10d09293975833c2645c5d9a0c..f087d50e8a4b3c2d3a9ef1d52790871269d79f6b 100644 (file)
@@ -44,7 +44,8 @@ defmodule Pleroma.Web.MastodonAPI.PollViewTest do
       ],
       voted: false,
       votes_count: 0,
-      voters_count: 0
+      voters_count: 0,
+      own_votes: []
     }
 
     result = PollView.render("show.json", %{object: object})
@@ -123,7 +124,10 @@ defmodule Pleroma.Web.MastodonAPI.PollViewTest do
 
     result = PollView.render("show.json", %{object: object, for: other_user})
 
+    _own_votes = result[:own_votes]
+
     assert result[:voted] == true
+    assert own_votes = [1, 2]
     assert Enum.at(result[:options], 1)[:votes_count] == 1
     assert Enum.at(result[:options], 2)[:votes_count] == 1
   end