aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMark Felder <feld@feld.me>2021-01-22 09:47:59 -0600
committerMark Felder <feld@feld.me>2021-01-22 09:47:59 -0600
commit6bfd497f4afeb4182cc865087e6f4863bc48a4f4 (patch)
treed48c9c04ff0c83b85d2ff0bae7c940a8f4b14543 /lib
parentd8860eaee46c9bc0a079e90dfb008c54923d7330 (diff)
downloadpleroma-6bfd497f4afeb4182cc865087e6f4863bc48a4f4.tar.gz
Include own_votes in the poll data
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/web/mastodon_api/views/poll_view.ex27
1 files changed, 21 insertions, 6 deletions
diff --git a/lib/pleroma/web/mastodon_api/views/poll_view.ex b/lib/pleroma/web/mastodon_api/views/poll_view.ex
index d6b544037..94bc1c139 100644
--- a/lib/pleroma/web/mastodon_api/views/poll_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/poll_view.ex
@@ -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