Merge branch 'feature/voters-count' into 'develop'
Polls: Persist and show voters' count Closes #1702 See merge request pleroma/pleroma!2411
This commit is contained in:
commit
1855c597c2
@ -261,7 +261,7 @@ def decrease_replies_count(ap_id) do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def increase_vote_count(ap_id, name) do
|
def increase_vote_count(ap_id, name, actor) do
|
||||||
with %Object{} = object <- Object.normalize(ap_id),
|
with %Object{} = object <- Object.normalize(ap_id),
|
||||||
"Question" <- object.data["type"] do
|
"Question" <- object.data["type"] do
|
||||||
multiple = Map.has_key?(object.data, "anyOf")
|
multiple = Map.has_key?(object.data, "anyOf")
|
||||||
@ -276,12 +276,15 @@ def increase_vote_count(ap_id, name) do
|
|||||||
option
|
option
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
voters = [actor | object.data["voters"] || []] |> Enum.uniq()
|
||||||
|
|
||||||
data =
|
data =
|
||||||
if multiple do
|
if multiple do
|
||||||
Map.put(object.data, "anyOf", options)
|
Map.put(object.data, "anyOf", options)
|
||||||
else
|
else
|
||||||
Map.put(object.data, "oneOf", options)
|
Map.put(object.data, "oneOf", options)
|
||||||
end
|
end
|
||||||
|
|> Map.put("voters", voters)
|
||||||
|
|
||||||
object
|
object
|
||||||
|> Object.change(%{data: data})
|
|> Object.change(%{data: data})
|
||||||
|
@ -118,9 +118,10 @@ def decrease_replies_count_if_reply(_object), do: :noop
|
|||||||
|
|
||||||
def increase_poll_votes_if_vote(%{
|
def increase_poll_votes_if_vote(%{
|
||||||
"object" => %{"inReplyTo" => reply_ap_id, "name" => name},
|
"object" => %{"inReplyTo" => reply_ap_id, "name" => name},
|
||||||
"type" => "Create"
|
"type" => "Create",
|
||||||
|
"actor" => actor
|
||||||
}) do
|
}) do
|
||||||
Object.increase_vote_count(reply_ap_id, name)
|
Object.increase_vote_count(reply_ap_id, name, actor)
|
||||||
end
|
end
|
||||||
|
|
||||||
def increase_poll_votes_if_vote(_create_data), do: :noop
|
def increase_poll_votes_if_vote(_create_data), do: :noop
|
||||||
|
@ -19,6 +19,7 @@ def render("show.json", %{object: object, multiple: multiple, options: options}
|
|||||||
expired: expired,
|
expired: expired,
|
||||||
multiple: multiple,
|
multiple: multiple,
|
||||||
votes_count: votes_count,
|
votes_count: votes_count,
|
||||||
|
voters_count: (multiple || nil) && voters_count(object),
|
||||||
options: options,
|
options: options,
|
||||||
voted: voted?(params),
|
voted: voted?(params),
|
||||||
emojis: Pleroma.Web.MastodonAPI.StatusView.build_emojis(object.data["emoji"])
|
emojis: Pleroma.Web.MastodonAPI.StatusView.build_emojis(object.data["emoji"])
|
||||||
@ -62,6 +63,12 @@ defp options_and_votes_count(options) do
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp voters_count(%{data: %{"voters" => [_ | _] = voters}}) do
|
||||||
|
length(voters)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp voters_count(_), do: 0
|
||||||
|
|
||||||
defp voted?(%{object: object} = opts) do
|
defp voted?(%{object: object} = opts) do
|
||||||
if opts[:for] do
|
if opts[:for] do
|
||||||
existing_votes = Pleroma.Web.ActivityPub.Utils.get_existing_votes(opts[:for].ap_id, object)
|
existing_votes = Pleroma.Web.ActivityPub.Utils.get_existing_votes(opts[:for].ap_id, object)
|
||||||
|
@ -43,7 +43,8 @@ test "renders a poll" do
|
|||||||
%{title: "why are you even asking?", votes_count: 0}
|
%{title: "why are you even asking?", votes_count: 0}
|
||||||
],
|
],
|
||||||
voted: false,
|
voted: false,
|
||||||
votes_count: 0
|
votes_count: 0,
|
||||||
|
voters_count: nil
|
||||||
}
|
}
|
||||||
|
|
||||||
result = PollView.render("show.json", %{object: object})
|
result = PollView.render("show.json", %{object: object})
|
||||||
@ -69,9 +70,20 @@ test "detects if it is multiple choice" do
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
voter = insert(:user)
|
||||||
|
|
||||||
object = Object.normalize(activity)
|
object = Object.normalize(activity)
|
||||||
|
|
||||||
assert %{multiple: true} = PollView.render("show.json", %{object: object})
|
{:ok, _votes, object} = CommonAPI.vote(voter, object, [0, 1])
|
||||||
|
|
||||||
|
assert match?(
|
||||||
|
%{
|
||||||
|
multiple: true,
|
||||||
|
voters_count: 1,
|
||||||
|
votes_count: 2
|
||||||
|
},
|
||||||
|
PollView.render("show.json", %{object: object})
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "detects emoji" do
|
test "detects emoji" do
|
||||||
|
Loading…
Reference in New Issue
Block a user