2019-10-01 04:44:34 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-10-01 04:44:34 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.PollView do
|
|
|
|
use Pleroma.Web, :view
|
|
|
|
|
|
|
|
alias Pleroma.Web.CommonAPI.Utils
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-01-25 16:46:36 +00:00
|
|
|
poll = %{
|
2019-10-01 04:44:34 +00:00
|
|
|
# Mastodon uses separate ids for polls, but an object can't have
|
|
|
|
# more than one poll embedded so object id is fine
|
|
|
|
id: to_string(object.id),
|
|
|
|
expires_at: end_time,
|
|
|
|
expired: expired,
|
|
|
|
multiple: multiple,
|
|
|
|
votes_count: votes_count,
|
2020-11-04 09:14:00 +00:00
|
|
|
voters_count: voters_count(object),
|
2019-10-01 04:44:34 +00:00
|
|
|
options: options,
|
|
|
|
emojis: Pleroma.Web.MastodonAPI.StatusView.build_emojis(object.data["emoji"])
|
|
|
|
}
|
2021-01-25 16:46:36 +00:00
|
|
|
|
|
|
|
if params[:for] do
|
2021-01-26 00:25:53 +00:00
|
|
|
# when unauthenticated Mastodon doesn't include `voted` & `own_votes` keys in response
|
2021-01-25 16:46:36 +00:00
|
|
|
{voted, own_votes} = voted_and_own_votes(params, options)
|
|
|
|
Map.merge(poll, %{voted: voted, own_votes: own_votes})
|
|
|
|
else
|
|
|
|
poll
|
|
|
|
end
|
2019-10-01 04:44:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render("show.json", %{object: object} = params) do
|
|
|
|
case object.data do
|
2020-08-05 12:51:33 +00:00
|
|
|
%{"anyOf" => [_ | _] = options} ->
|
2019-10-01 04:44:34 +00:00
|
|
|
render(__MODULE__, "show.json", Map.merge(params, %{multiple: true, options: options}))
|
|
|
|
|
2020-08-05 12:51:33 +00:00
|
|
|
%{"oneOf" => [_ | _] = options} ->
|
2019-10-01 04:44:34 +00:00
|
|
|
render(__MODULE__, "show.json", Map.merge(params, %{multiple: false, options: options}))
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp end_time_and_expired(object) do
|
2020-06-15 03:18:30 +00:00
|
|
|
if object.data["closed"] do
|
|
|
|
end_time = NaiveDateTime.from_iso8601!(object.data["closed"])
|
|
|
|
expired = NaiveDateTime.compare(end_time, NaiveDateTime.utc_now()) == :lt
|
2019-10-01 04:44:34 +00:00
|
|
|
|
2020-06-15 03:18:30 +00:00
|
|
|
{Utils.to_masto_date(end_time), expired}
|
|
|
|
else
|
|
|
|
{nil, false}
|
2019-10-01 04:44:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp options_and_votes_count(options) do
|
|
|
|
Enum.map_reduce(options, 0, fn %{"name" => name} = option, count ->
|
|
|
|
current_count = option["replies"]["totalItems"] || 0
|
|
|
|
|
|
|
|
{%{
|
2020-01-30 16:47:57 +00:00
|
|
|
title: name,
|
2019-10-01 04:44:34 +00:00
|
|
|
votes_count: current_count
|
|
|
|
}, current_count + count}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-04-22 12:06:39 +00:00
|
|
|
defp voters_count(%{data: %{"voters" => [_ | _] = voters}}) do
|
|
|
|
length(voters)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp voters_count(_), do: 0
|
|
|
|
|
2021-01-22 15:47:59 +00:00
|
|
|
defp voted_and_own_votes(%{object: object} = params, options) do
|
|
|
|
if params[:for] do
|
|
|
|
existing_votes =
|
|
|
|
Pleroma.Web.ActivityPub.Utils.get_existing_votes(params[:for].ap_id, object)
|
|
|
|
|
|
|
|
voted = existing_votes != [] or params[:for].ap_id == object.data["actor"]
|
|
|
|
|
2021-01-25 16:46:36 +00:00
|
|
|
own_votes =
|
|
|
|
if voted do
|
|
|
|
titles = Enum.map(options, & &1[:title])
|
|
|
|
|
|
|
|
Enum.reduce(existing_votes, [], fn vote, acc ->
|
|
|
|
data = vote |> Map.get(:object) |> Map.get(:data)
|
|
|
|
index = Enum.find_index(titles, &(&1 == data["name"]))
|
|
|
|
[index | acc]
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2021-01-22 15:47:59 +00:00
|
|
|
{voted, own_votes}
|
2019-10-01 04:44:34 +00:00
|
|
|
else
|
2021-01-22 15:47:59 +00:00
|
|
|
{false, []}
|
2019-10-01 04:44:34 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|