Merge branch 'fix/missing-own_votes' into 'develop'

Include own_votes in the poll data

See merge request pleroma/pleroma!3274
This commit is contained in:
Haelwenn 2021-01-26 00:55:05 +00:00
commit 250e202098
4 changed files with 110 additions and 13 deletions

View File

@ -63,6 +63,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
<summary>API Changes</summary> <summary>API Changes</summary>
- Mastodon API: Current user is now included in conversation if it's the only participant. - 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 last_status.account being not filled with account data.
- Mastodon API: Fixed own_votes being not returned with poll data.
</details> </details>
## Unreleased (Patch) ## Unreleased (Patch)

View File

@ -11,7 +11,7 @@ def render("show.json", %{object: object, multiple: multiple, options: options}
{end_time, expired} = end_time_and_expired(object) {end_time, expired} = end_time_and_expired(object)
{options, votes_count} = options_and_votes_count(options) {options, votes_count} = options_and_votes_count(options)
%{ poll = %{
# Mastodon uses separate ids for polls, but an object can't have # Mastodon uses separate ids for polls, but an object can't have
# more than one poll embedded so object id is fine # more than one poll embedded so object id is fine
id: to_string(object.id), id: to_string(object.id),
@ -21,9 +21,16 @@ def render("show.json", %{object: object, multiple: multiple, options: options}
votes_count: votes_count, votes_count: votes_count,
voters_count: voters_count(object), voters_count: voters_count(object),
options: options, options: options,
voted: voted?(params),
emojis: Pleroma.Web.MastodonAPI.StatusView.build_emojis(object.data["emoji"]) emojis: Pleroma.Web.MastodonAPI.StatusView.build_emojis(object.data["emoji"])
} }
if params[:for] do
# 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
poll
end
end end
def render("show.json", %{object: object} = params) do def render("show.json", %{object: object} = params) do
@ -67,12 +74,29 @@ defp voters_count(%{data: %{"voters" => [_ | _] = voters}}) do
defp voters_count(_), do: 0 defp voters_count(_), do: 0
defp voted?(%{object: object} = opts) do defp voted_and_own_votes(%{object: object} = params, options) do
if opts[:for] do if params[:for] do
existing_votes = Pleroma.Web.ActivityPub.Utils.get_existing_votes(opts[:for].ap_id, object) existing_votes =
existing_votes != [] or opts[:for].ap_id == object.data["actor"] Pleroma.Web.ActivityPub.Utils.get_existing_votes(params[:for].ap_id, object)
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 else
false {false, []}
end end
end end
end end

View File

@ -47,6 +47,78 @@ test "does not expose polls for private statuses", %{conn: conn} do
end end
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 describe "POST /api/v1/polls/:id/votes" do
setup do: oauth_access(["write:statuses"]) setup do: oauth_access(["write:statuses"])
@ -65,12 +137,11 @@ test "votes are added to the poll", %{conn: conn} do
object = Object.normalize(activity, fetch: false) object = Object.normalize(activity, fetch: false)
conn = conn
conn |> put_req_header("content-type", "application/json")
|> put_req_header("content-type", "application/json") |> post("/api/v1/polls/#{object.id}/votes", %{"choices" => [0, 1, 2]})
|> 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) object = Object.get_by_id(object.id)
assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} -> assert Enum.all?(object.data["anyOf"], fn %{"replies" => %{"totalItems" => total_items}} ->

View File

@ -42,7 +42,6 @@ test "renders a poll" do
%{title: "yes", votes_count: 0}, %{title: "yes", votes_count: 0},
%{title: "why are you even asking?", votes_count: 0} %{title: "why are you even asking?", votes_count: 0}
], ],
voted: false,
votes_count: 0, votes_count: 0,
voters_count: 0 voters_count: 0
} }
@ -124,6 +123,8 @@ test "detects vote status" do
result = PollView.render("show.json", %{object: object, for: other_user}) result = PollView.render("show.json", %{object: object, for: other_user})
assert result[:voted] == true assert result[:voted] == true
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], 1)[:votes_count] == 1
assert Enum.at(result[:options], 2)[:votes_count] == 1 assert Enum.at(result[:options], 2)[:votes_count] == 1
end end