MastoAPI: Add GET /api/v1/polls/:id
[akkoma] / lib / pleroma / web / mastodon_api / views / status_view.ex
index 6337c50e8941554922ffcdc1ddf44bbc267f1621..c501c213c3e584c96b54d873f32b5dede5804c7a 100644 (file)
@@ -16,6 +16,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   alias Pleroma.Web.MastodonAPI.StatusView
   alias Pleroma.Web.MediaProxy
 
+  import Pleroma.Web.ActivityPub.Visibility, only: [get_visibility: 1]
+
   # TODO: Add cached version.
   defp get_replied_to_activities(activities) do
     activities
@@ -323,7 +325,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   end
 
   # TODO: Add tests for this view
-  def render("poll.json", %{object: object} = opts) do
+  def render("poll.json", %{object: object} = _opts) do
     {multiple, options} =
       case object.data do
         %{"anyOf" => options} when is_list(options) -> {true, options}
@@ -336,8 +338,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         (object.data["closed"] || object.data["endTime"])
         |> NaiveDateTime.from_iso8601!()
 
-      votes_count = object.data["votes_count"] || 0
-
       expired =
         end_time
         |> NaiveDateTime.compare(NaiveDateTime.utc_now())
@@ -346,15 +346,14 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
           _ -> false
         end
 
-      options =
-        Enum.map(options, fn %{"name" => name} = option ->
-          name =
-            HTML.filter_tags(
-              name,
-              User.html_filter_policy(opts[:for])
-            )
+      {options, votes_count} =
+        Enum.map_reduce(options, 0, fn %{"name" => name} = option, count ->
+          current_count = option["replies"]["totalItems"] || 0
 
-          %{title: name, votes_count: option["replies"]["votes_count"] || 0}
+          {%{
+             title: HTML.strip_tags(name),
+             votes_count: current_count
+           }, current_count + count}
         end)
 
       %{
@@ -365,6 +364,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         multiple: multiple,
         votes_count: votes_count,
         options: options,
+        # TODO: Actually check for a vote
         voted: false,
         emojis: build_emojis(object.data["emoji"])
       }
@@ -392,30 +392,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     end
   end
 
-  def get_visibility(object) do
-    public = "https://www.w3.org/ns/activitystreams#Public"
-    to = object.data["to"] || []
-    cc = object.data["cc"] || []
-
-    cond do
-      public in to ->
-        "public"
-
-      public in cc ->
-        "unlisted"
-
-      # this should use the sql for the object's activity
-      Enum.any?(to, &String.contains?(&1, "/followers")) ->
-        "private"
-
-      length(cc) > 0 ->
-        "private"
-
-      true ->
-        "direct"
-    end
-  end
-
   def render_content(%{data: %{"type" => "Video"}} = object) do
     with name when not is_nil(name) and name != "" <- object.data["name"] do
       "<p><a href=\"#{object.data["id"]}\">#{name}</a></p>#{object.data["content"]}"