Merge branch 'fix/dymamic-docs' into 'develop'
[akkoma] / lib / pleroma / web / mastodon_api / views / poll_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.PollView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.HTML
9 alias Pleroma.Web.CommonAPI.Utils
10
11 def render("show.json", %{object: object, multiple: multiple, options: options} = params) do
12 {end_time, expired} = end_time_and_expired(object)
13 {options, votes_count} = options_and_votes_count(options)
14
15 %{
16 # Mastodon uses separate ids for polls, but an object can't have
17 # more than one poll embedded so object id is fine
18 id: to_string(object.id),
19 expires_at: end_time,
20 expired: expired,
21 multiple: multiple,
22 votes_count: votes_count,
23 options: options,
24 voted: voted?(params),
25 emojis: Pleroma.Web.MastodonAPI.StatusView.build_emojis(object.data["emoji"])
26 }
27 end
28
29 def render("show.json", %{object: object} = params) do
30 case object.data do
31 %{"anyOf" => options} when is_list(options) ->
32 render(__MODULE__, "show.json", Map.merge(params, %{multiple: true, options: options}))
33
34 %{"oneOf" => options} when is_list(options) ->
35 render(__MODULE__, "show.json", Map.merge(params, %{multiple: false, options: options}))
36
37 _ ->
38 nil
39 end
40 end
41
42 defp end_time_and_expired(object) do
43 case object.data["closed"] || object.data["endTime"] do
44 end_time when is_binary(end_time) ->
45 end_time = NaiveDateTime.from_iso8601!(end_time)
46 expired = NaiveDateTime.compare(end_time, NaiveDateTime.utc_now()) == :lt
47
48 {Utils.to_masto_date(end_time), expired}
49
50 _ ->
51 {nil, false}
52 end
53 end
54
55 defp options_and_votes_count(options) do
56 Enum.map_reduce(options, 0, fn %{"name" => name} = option, count ->
57 current_count = option["replies"]["totalItems"] || 0
58
59 {%{
60 title: HTML.strip_tags(name),
61 votes_count: current_count
62 }, current_count + count}
63 end)
64 end
65
66 defp voted?(%{object: object} = opts) do
67 if opts[:for] do
68 existing_votes = Pleroma.Web.ActivityPub.Utils.get_existing_votes(opts[:for].ap_id, object)
69 existing_votes != [] or opts[:for].ap_id == object.data["actor"]
70 else
71 false
72 end
73 end
74 end