Handle voters count correctly

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-08-12 23:47:11 +02:00
parent a12441c9a5
commit 9ffb4d055a
5 changed files with 16 additions and 2 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"
}
]
}