Revert "Merge branch 'revert-f6e02270' into 'develop'"

This reverts commit 5b8ad6bc91, reversing
changes made to f6e02270c3.
This commit is contained in:
marcin mikołajczak 2022-08-18 23:06:18 +02:00
parent 5b8ad6bc91
commit 322e00e45a
7 changed files with 43 additions and 3 deletions

View file

@ -22,7 +22,8 @@ defmodule Pleroma.Constants do
"generator",
"assigned_account",
"rules",
"content_type"
"content_type",
"voters"
]
)

View file

@ -29,6 +29,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do
field(:closed, ObjectValidators.DateTime)
field(:voters, {:array, ObjectValidators.ObjectID}, default: [])
field(:votersCount, :integer)
field(:nonAnonymous, :boolean)
embeds_many(:anyOf, QuestionOptionsValidator)
embeds_many(:oneOf, QuestionOptionsValidator)

View file

@ -723,6 +723,12 @@ def replies(%{"replies" => %{"items" => items}}) when not is_nil(items) do
def replies(_), do: []
defp set_voters_count(%{"voters" => [_ | _] = voters} = obj) do
Map.merge(obj, %{"votersCount" => length(voters)})
end
defp set_voters_count(obj), do: obj
# Prepares the object of an outgoing create activity.
def prepare_object(object) do
object
@ -735,6 +741,7 @@ def prepare_object(object) do
|> set_reply_to_uri
|> set_quote_url
|> set_replies
|> set_voters_count
|> strip_internal_fields
|> strip_internal_tags
|> set_type

View file

@ -71,6 +71,10 @@ defp options_and_votes_count(options) do
end)
end
defp voters_count(%{data: %{"votersCount" => voters}}) when is_number(voters) do
voters
end
defp voters_count(%{data: %{"voters" => [_ | _] = voters}}) do
length(voters)
end

View file

@ -41,7 +41,8 @@
},
"vcard": "http://www.w3.org/2006/vcard/ns#",
"sm": "http://smithereen.software/ns#",
"nonAnonymous": "sm:nonAnonymous"
"nonAnonymous": "sm:nonAnonymous",
"votersCount" : "toot:votersCount"
}
]
}

View file

@ -173,4 +173,23 @@ test "accepts a Question with no content" do
assert {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
end
test "it strips voters list and displays voters count instead" do
user = insert(:user)
other_user = insert(:user)
{:ok, activity} =
CommonAPI.post(user, %{
status: "???",
poll: %{expires_in: 10, options: ["yes", "no"]}
})
object = Object.normalize(activity, fetch: false)
{:ok, _, _} = CommonAPI.vote(other_user, object, [1])
{:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
refute Map.has_key?(modified["object"], "voters")
assert modified["object"]["votersCount"] == 1
end
end

View file

@ -167,7 +167,14 @@ test "doesn't strips HTML tags" do
} = PollView.render("show.json", %{object: object})
end
test "that poll is non anonymous" do
test "displays correct voters count" do
object = Object.normalize("https://friends.grishka.me/posts/54642", fetch: true)
result = PollView.render("show.json", %{object: object})
assert result[:voters_count] == 14
end
test "detects that poll is non anonymous" do
object = Object.normalize("https://friends.grishka.me/posts/54642", fetch: true)
result = PollView.render("show.json", %{object: object})