From 6bfd497f4afeb4182cc865087e6f4863bc48a4f4 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Fri, 22 Jan 2021 09:47:59 -0600 Subject: [PATCH 1/3] Include own_votes in the poll data --- CHANGELOG.md | 1 + .../web/mastodon_api/views/poll_view.ex | 27 ++++++++++++++----- .../web/mastodon_api/views/poll_view_test.exs | 6 ++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1dfeae01..328a7c28c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). API Changes - Mastodon API: Current user is now included in conversation if it's the only participant. - Mastodon API: Fixed last_status.account being not filled with account data. + - Mastodon API: Fixed own_votes being not returned with poll data. ## Unreleased (Patch) 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 diff --git a/test/pleroma/web/mastodon_api/views/poll_view_test.exs b/test/pleroma/web/mastodon_api/views/poll_view_test.exs index a8600e1c2..f087d50e8 100644 --- a/test/pleroma/web/mastodon_api/views/poll_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/poll_view_test.exs @@ -44,7 +44,8 @@ defmodule Pleroma.Web.MastodonAPI.PollViewTest do ], voted: false, votes_count: 0, - voters_count: 0 + voters_count: 0, + own_votes: [] } result = PollView.render("show.json", %{object: object}) @@ -123,7 +124,10 @@ defmodule Pleroma.Web.MastodonAPI.PollViewTest do result = PollView.render("show.json", %{object: object, for: other_user}) + _own_votes = result[:own_votes] + assert result[:voted] == true + assert own_votes = [1, 2] assert Enum.at(result[:options], 1)[:votes_count] == 1 assert Enum.at(result[:options], 2)[:votes_count] == 1 end From 3f3d64acbfe0f8219911cb053e7fdab25137a23a Mon Sep 17 00:00:00 2001 From: Alexander Strizhakov Date: Mon, 25 Jan 2021 19:46:36 +0300 Subject: [PATCH 2/3] little refactor and tests for voted & own_votes fields in polls --- .../web/mastodon_api/views/poll_view.ex | 35 +++++--- .../controllers/poll_controller_test.exs | 81 +++++++++++++++++-- .../web/mastodon_api/views/poll_view_test.exs | 9 +-- 3 files changed, 101 insertions(+), 24 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/views/poll_view.ex b/lib/pleroma/web/mastodon_api/views/poll_view.ex index 94bc1c139..de536c8fb 100644 --- a/lib/pleroma/web/mastodon_api/views/poll_view.ex +++ b/lib/pleroma/web/mastodon_api/views/poll_view.ex @@ -10,9 +10,8 @@ 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) - %{ + poll = %{ # 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), @@ -22,10 +21,16 @@ defmodule Pleroma.Web.MastodonAPI.PollView do votes_count: votes_count, voters_count: voters_count(object), options: options, - voted: voted, - own_votes: own_votes, emojis: Pleroma.Web.MastodonAPI.StatusView.build_emojis(object.data["emoji"]) } + + if params[:for] do + # if a user is not authenticated Mastodon doesn't include `voted` & `own_votes` keys in response + {voted, own_votes} = voted_and_own_votes(params, options) + Map.merge(poll, %{voted: voted, own_votes: own_votes}) + else + poll + end end def render("show.json", %{object: object} = params) do @@ -70,21 +75,25 @@ defmodule Pleroma.Web.MastodonAPI.PollView do defp voters_count(_), do: 0 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"] + 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 + {voted, own_votes} else {false, []} diff --git a/test/pleroma/web/mastodon_api/controllers/poll_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/poll_controller_test.exs index 95df48ab0..da0a631a9 100644 --- a/test/pleroma/web/mastodon_api/controllers/poll_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/poll_controller_test.exs @@ -47,6 +47,78 @@ defmodule Pleroma.Web.MastodonAPI.PollControllerTest do end end + test "own_votes" do + %{conn: conn} = oauth_access(["write:statuses", "read:statuses"]) + + other_user = insert(:user) + + {:ok, activity} = + CommonAPI.post(other_user, %{ + status: "A very delicious sandwich", + poll: %{ + options: ["Lettuce", "Grilled Bacon", "Tomato"], + expires_in: 20, + multiple: true + } + }) + + object = Object.normalize(activity, fetch: false) + + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 2]}) + |> json_response_and_validate_schema(200) + + object = Object.get_by_id(object.id) + + assert [ + %{ + "name" => "Lettuce", + "replies" => %{"totalItems" => 1, "type" => "Collection"}, + "type" => "Note" + }, + %{ + "name" => "Grilled Bacon", + "replies" => %{"totalItems" => 0, "type" => "Collection"}, + "type" => "Note" + }, + %{ + "name" => "Tomato", + "replies" => %{"totalItems" => 1, "type" => "Collection"}, + "type" => "Note" + } + ] == object.data["anyOf"] + + assert %{"replies" => %{"totalItems" => 0}} = + Enum.find(object.data["anyOf"], fn %{"name" => name} -> name == "Grilled Bacon" end) + + Enum.each(["Lettuce", "Tomato"], fn title -> + %{"replies" => %{"totalItems" => total_items}} = + Enum.find(object.data["anyOf"], fn %{"name" => name} -> name == title end) + + assert total_items == 1 + end) + + assert %{ + "own_votes" => own_votes, + "voted" => true + } = + conn + |> get("/api/v1/polls/#{object.id}") + |> json_response_and_validate_schema(200) + + assert 0 in own_votes + assert 2 in own_votes + # for non authenticated user + response = + build_conn() + |> get("/api/v1/polls/#{object.id}") + |> json_response_and_validate_schema(200) + + refute Map.has_key?(response, "own_votes") + refute Map.has_key?(response, "voted") + end + describe "POST /api/v1/polls/:id/votes" do setup do: oauth_access(["write:statuses"]) @@ -65,12 +137,11 @@ defmodule Pleroma.Web.MastodonAPI.PollControllerTest do object = Object.normalize(activity, fetch: false) - conn = - conn - |> put_req_header("content-type", "application/json") - |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]}) + conn + |> put_req_header("content-type", "application/json") + |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]}) + |> json_response_and_validate_schema(200) - assert json_response_and_validate_schema(conn, 200) object = Object.get_by_id(object.id) assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} -> diff --git a/test/pleroma/web/mastodon_api/views/poll_view_test.exs b/test/pleroma/web/mastodon_api/views/poll_view_test.exs index f087d50e8..224b26cb9 100644 --- a/test/pleroma/web/mastodon_api/views/poll_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/poll_view_test.exs @@ -42,10 +42,8 @@ defmodule Pleroma.Web.MastodonAPI.PollViewTest do %{title: "yes", votes_count: 0}, %{title: "why are you even asking?", votes_count: 0} ], - voted: false, votes_count: 0, - voters_count: 0, - own_votes: [] + voters_count: 0 } result = PollView.render("show.json", %{object: object}) @@ -124,10 +122,9 @@ defmodule Pleroma.Web.MastodonAPI.PollViewTest do result = PollView.render("show.json", %{object: object, for: other_user}) - _own_votes = result[:own_votes] - assert result[:voted] == true - assert own_votes = [1, 2] + assert 1 in result[:own_votes] + assert 2 in result[:own_votes] assert Enum.at(result[:options], 1)[:votes_count] == 1 assert Enum.at(result[:options], 2)[:votes_count] == 1 end From 2cb5c16723b7e65e6e1bfae6bf8319f62d667def Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Mon, 25 Jan 2021 18:25:53 -0600 Subject: [PATCH 3/3] Credo --- lib/pleroma/web/mastodon_api/views/poll_view.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/mastodon_api/views/poll_view.ex b/lib/pleroma/web/mastodon_api/views/poll_view.ex index de536c8fb..71bc8b949 100644 --- a/lib/pleroma/web/mastodon_api/views/poll_view.ex +++ b/lib/pleroma/web/mastodon_api/views/poll_view.ex @@ -25,7 +25,7 @@ defmodule Pleroma.Web.MastodonAPI.PollView do } if params[:for] do - # if a user is not authenticated Mastodon doesn't include `voted` & `own_votes` keys in response + # when unauthenticated Mastodon doesn't include `voted` & `own_votes` keys in response {voted, own_votes} = voted_and_own_votes(params, options) Map.merge(poll, %{voted: voted, own_votes: own_votes}) else