Fix multiple-choice poll detection
This commit is contained in:
parent
10bd08ef07
commit
4644a8bd10
|
@ -255,6 +255,12 @@ def increase_replies_count(ap_id) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp poll_is_multiple?(%Object{data: %{"anyOf" => anyOf}}) do
|
||||||
|
!Enum.empty?(anyOf)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp poll_is_multiple?(_), do: false
|
||||||
|
|
||||||
def decrease_replies_count(ap_id) do
|
def decrease_replies_count(ap_id) do
|
||||||
Object
|
Object
|
||||||
|> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
|
|> where([o], fragment("?->>'id' = ?::text", o.data, ^to_string(ap_id)))
|
||||||
|
@ -281,10 +287,10 @@ def decrease_replies_count(ap_id) do
|
||||||
def increase_vote_count(ap_id, name, actor) 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")
|
key = if poll_is_multiple?(object), do: "anyOf", else: "oneOf"
|
||||||
|
|
||||||
options =
|
options =
|
||||||
(object.data["anyOf"] || object.data["oneOf"] || [])
|
object.data[key]
|
||||||
|> Enum.map(fn
|
|> Enum.map(fn
|
||||||
%{"name" => ^name} = option ->
|
%{"name" => ^name} = option ->
|
||||||
Kernel.update_in(option["replies"]["totalItems"], &(&1 + 1))
|
Kernel.update_in(option["replies"]["totalItems"], &(&1 + 1))
|
||||||
|
@ -296,11 +302,8 @@ def increase_vote_count(ap_id, name, actor) do
|
||||||
voters = [actor | object.data["voters"] || []] |> Enum.uniq()
|
voters = [actor | object.data["voters"] || []] |> Enum.uniq()
|
||||||
|
|
||||||
data =
|
data =
|
||||||
if multiple do
|
object.data
|
||||||
Map.put(object.data, "anyOf", options)
|
|> Map.put(key, options)
|
||||||
else
|
|
||||||
Map.put(object.data, "oneOf", options)
|
|
||||||
end
|
|
||||||
|> Map.put("voters", voters)
|
|> Map.put("voters", voters)
|
||||||
|
|
||||||
object
|
object
|
||||||
|
|
|
@ -340,8 +340,13 @@ defp validate_existing_votes(%{ap_id: ap_id}, object) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp get_options_and_max_count(%{data: %{"anyOf" => any_of}}), do: {any_of, Enum.count(any_of)}
|
defp get_options_and_max_count(%{data: %{"anyOf" => any_of}})
|
||||||
defp get_options_and_max_count(%{data: %{"oneOf" => one_of}}), do: {one_of, 1}
|
when is_list(any_of) and any_of != [],
|
||||||
|
do: {any_of, Enum.count(any_of)}
|
||||||
|
|
||||||
|
defp get_options_and_max_count(%{data: %{"oneOf" => one_of}})
|
||||||
|
when is_list(one_of) and one_of != [],
|
||||||
|
do: {one_of, 1}
|
||||||
|
|
||||||
defp normalize_and_validate_choices(choices, object) do
|
defp normalize_and_validate_choices(choices, object) do
|
||||||
choices = Enum.map(choices, fn i -> if is_binary(i), do: String.to_integer(i), else: i end)
|
choices = Enum.map(choices, fn i -> if is_binary(i), do: String.to_integer(i), else: i end)
|
||||||
|
|
|
@ -28,10 +28,10 @@ def render("show.json", %{object: object, multiple: multiple, options: options}
|
||||||
|
|
||||||
def render("show.json", %{object: object} = params) do
|
def render("show.json", %{object: object} = params) do
|
||||||
case object.data do
|
case object.data do
|
||||||
%{"anyOf" => options} when is_list(options) ->
|
%{"anyOf" => options} when is_list(options) and options != [] ->
|
||||||
render(__MODULE__, "show.json", Map.merge(params, %{multiple: true, options: options}))
|
render(__MODULE__, "show.json", Map.merge(params, %{multiple: true, options: options}))
|
||||||
|
|
||||||
%{"oneOf" => options} when is_list(options) ->
|
%{"oneOf" => options} when is_list(options) and options != [] ->
|
||||||
render(__MODULE__, "show.json", Map.merge(params, %{multiple: false, options: options}))
|
render(__MODULE__, "show.json", Map.merge(params, %{multiple: false, options: options}))
|
||||||
|
|
||||||
_ ->
|
_ ->
|
||||||
|
|
Loading…
Reference in New Issue