aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrinpatch <rinpatch@sdf.org>2019-05-17 11:44:47 +0300
committerrinpatch <rinpatch@sdf.org>2019-05-17 11:44:47 +0300
commit642a67dd4492f31b5b9fe457e34c1589c9d70c3f (patch)
treeef2f8ac68e9f654fa273bdb35c32acac4b03d70b
parent62e42b03abd2cede85e85f62c35f62a8c42e8ea1 (diff)
downloadpleroma-642a67dd4492f31b5b9fe457e34c1589c9d70c3f.tar.gz
Render polls in statuses
-rw-r--r--lib/pleroma/web/mastodon_api/views/status_view.ex52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex
index bd2372944..6337c50e8 100644
--- a/lib/pleroma/web/mastodon_api/views/status_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/status_view.ex
@@ -232,6 +232,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
spoiler_text: summary_html,
visibility: get_visibility(object),
media_attachments: attachments,
+ poll: render("poll.json", %{object: object, for: opts[:for]}),
mentions: mentions,
tags: build_tags(tags),
application: %{
@@ -321,6 +322,57 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
}
end
+ # TODO: Add tests for this view
+ def render("poll.json", %{object: object} = opts) do
+ {multiple, options} =
+ case object.data do
+ %{"anyOf" => options} when is_list(options) -> {true, options}
+ %{"oneOf" => options} when is_list(options) -> {false, options}
+ _ -> {nil, nil}
+ end
+
+ if options do
+ end_time =
+ (object.data["closed"] || object.data["endTime"])
+ |> NaiveDateTime.from_iso8601!()
+
+ votes_count = object.data["votes_count"] || 0
+
+ expired =
+ end_time
+ |> NaiveDateTime.compare(NaiveDateTime.utc_now())
+ |> case do
+ :lt -> true
+ _ -> false
+ end
+
+ options =
+ Enum.map(options, fn %{"name" => name} = option ->
+ name =
+ HTML.filter_tags(
+ name,
+ User.html_filter_policy(opts[:for])
+ )
+
+ %{title: name, votes_count: option["replies"]["votes_count"] || 0}
+ end)
+
+ %{
+ # Mastodon uses separate ids for polls, but an object can't have more than one poll embedded so object id is fine
+ id: object.id,
+ expires_at: Utils.to_masto_date(end_time),
+ expired: expired,
+ multiple: multiple,
+ votes_count: votes_count,
+ options: options,
+ voted: false,
+ emojis: build_emojis(object.data["emoji"])
+ }
+ else
+ nil
+ end
+ end
+
def get_reply_to(activity, %{replied_to_activities: replied_to_activities}) do
object = Object.normalize(activity)