Fix order of args for vote/3
This commit is contained in:
parent
8127e0d8cc
commit
1cccc0fc21
@ -329,8 +329,8 @@ defmodule Pleroma.Web.CommonAPI do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec vote(User.t(), Object.t(), list()) :: {:ok, list(), Object.t()} | {:error, any()}
|
@spec vote(Object.t(), User.t(), list()) :: {:ok, list(), Object.t()} | {:error, any()}
|
||||||
def vote(user, %{data: %{"type" => "Question"}} = object, choices) do
|
def vote(%Object{data: %{"type" => "Question"}} = object, %User{} = user, choices) do
|
||||||
with :ok <- validate_not_author(object, user),
|
with :ok <- validate_not_author(object, user),
|
||||||
:ok <- validate_existing_votes(user, object),
|
:ok <- validate_existing_votes(user, object),
|
||||||
{:ok, options, choices} <- normalize_and_validate_choices(choices, object) do
|
{:ok, options, choices} <- normalize_and_validate_choices(choices, object) do
|
||||||
|
@ -51,7 +51,7 @@ defmodule Pleroma.Web.MastodonAPI.PollController do
|
|||||||
with %Object{data: %{"type" => "Question"}} = object <- Object.get_by_id(id),
|
with %Object{data: %{"type" => "Question"}} = object <- Object.get_by_id(id),
|
||||||
%Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
|
%Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
|
||||||
true <- Visibility.visible_for_user?(activity, user),
|
true <- Visibility.visible_for_user?(activity, user),
|
||||||
{:ok, _activities, object} <- get_cached_vote_or_vote(user, object, choices) do
|
{:ok, _activities, object} <- get_cached_vote_or_vote(object, user, choices) do
|
||||||
try_render(conn, "show.json", %{object: object, for: user})
|
try_render(conn, "show.json", %{object: object, for: user})
|
||||||
else
|
else
|
||||||
nil -> render_error(conn, :not_found, "Record not found")
|
nil -> render_error(conn, :not_found, "Record not found")
|
||||||
@ -60,11 +60,11 @@ defmodule Pleroma.Web.MastodonAPI.PollController do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp get_cached_vote_or_vote(user, object, choices) do
|
defp get_cached_vote_or_vote(object, user, choices) do
|
||||||
idempotency_key = "polls:#{user.id}:#{object.data["id"]}"
|
idempotency_key = "polls:#{user.id}:#{object.data["id"]}"
|
||||||
|
|
||||||
@cachex.fetch!(:idempotency_cache, idempotency_key, fn _ ->
|
@cachex.fetch!(:idempotency_cache, idempotency_key, fn _ ->
|
||||||
case CommonAPI.vote(user, object, choices) do
|
case CommonAPI.vote(object, user, choices) do
|
||||||
{:error, _message} = res -> {:ignore, res}
|
{:error, _message} = res -> {:ignore, res}
|
||||||
res -> {:commit, res}
|
res -> {:commit, res}
|
||||||
end
|
end
|
||||||
|
@ -180,8 +180,8 @@ defmodule Pleroma.NotificationTest do
|
|||||||
question = insert(:question, user: user1)
|
question = insert(:question, user: user1)
|
||||||
activity = insert(:question_activity, question: question)
|
activity = insert(:question_activity, question: question)
|
||||||
|
|
||||||
{:ok, _, _} = CommonAPI.vote(user2, question, [0])
|
{:ok, _, _} = CommonAPI.vote(question, user2, [0])
|
||||||
{:ok, _, _} = CommonAPI.vote(user3, question, [1])
|
{:ok, _, _} = CommonAPI.vote(question, user3, [1])
|
||||||
|
|
||||||
{:ok, notifications} = Notification.create_poll_notifications(activity)
|
{:ok, notifications} = Notification.create_poll_notifications(activity)
|
||||||
|
|
||||||
|
@ -1402,7 +1402,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
|
|||||||
|
|
||||||
assert question = Object.normalize(activity, fetch: false)
|
assert question = Object.normalize(activity, fetch: false)
|
||||||
|
|
||||||
{:ok, [activity], _object} = CommonAPI.vote(voter, question, [1])
|
{:ok, [activity], _object} = CommonAPI.vote(question, voter, [1])
|
||||||
|
|
||||||
assert outbox_get =
|
assert outbox_get =
|
||||||
conn
|
conn
|
||||||
|
@ -201,7 +201,7 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
|
|||||||
})
|
})
|
||||||
|
|
||||||
object = Object.normalize(activity, fetch: false)
|
object = Object.normalize(activity, fetch: false)
|
||||||
{:ok, votes, object} = CommonAPI.vote(other_user, object, [0, 1])
|
{:ok, votes, object} = CommonAPI.vote(object, other_user, [0, 1])
|
||||||
assert Enum.sort(Utils.get_existing_votes(other_user.ap_id, object)) == Enum.sort(votes)
|
assert Enum.sort(Utils.get_existing_votes(other_user.ap_id, object)) == Enum.sort(votes)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -219,7 +219,7 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
|
|||||||
})
|
})
|
||||||
|
|
||||||
object = Object.normalize(activity, fetch: false)
|
object = Object.normalize(activity, fetch: false)
|
||||||
{:ok, [vote], object} = CommonAPI.vote(other_user, object, [0])
|
{:ok, [vote], object} = CommonAPI.vote(object, other_user, [0])
|
||||||
{:ok, _activity} = CommonAPI.favorite(activity.id, user)
|
{:ok, _activity} = CommonAPI.favorite(activity.id, user)
|
||||||
[fetched_vote] = Utils.get_existing_votes(other_user.ap_id, object)
|
[fetched_vote] = Utils.get_existing_votes(other_user.ap_id, object)
|
||||||
assert fetched_vote.id == vote.id
|
assert fetched_vote.id == vote.id
|
||||||
|
@ -1559,9 +1559,9 @@ defmodule Pleroma.Web.CommonAPITest do
|
|||||||
|
|
||||||
object = Object.normalize(activity, fetch: false)
|
object = Object.normalize(activity, fetch: false)
|
||||||
|
|
||||||
{:ok, _, object} = CommonAPI.vote(other_user, object, [0])
|
{:ok, _, object} = CommonAPI.vote(object, other_user, [0])
|
||||||
|
|
||||||
assert {:error, "Already voted"} == CommonAPI.vote(other_user, object, [1])
|
assert {:error, "Already voted"} == CommonAPI.vote(object, other_user, [1])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -74,7 +74,7 @@ defmodule Pleroma.Web.MastodonAPI.PollViewTest do
|
|||||||
|
|
||||||
object = Object.normalize(activity, fetch: false)
|
object = Object.normalize(activity, fetch: false)
|
||||||
|
|
||||||
{:ok, _votes, object} = CommonAPI.vote(voter, object, [0, 1])
|
{:ok, _votes, object} = CommonAPI.vote(object, voter, [0, 1])
|
||||||
|
|
||||||
assert match?(
|
assert match?(
|
||||||
%{
|
%{
|
||||||
@ -119,7 +119,7 @@ defmodule Pleroma.Web.MastodonAPI.PollViewTest do
|
|||||||
|
|
||||||
object = Object.normalize(activity, fetch: false)
|
object = Object.normalize(activity, fetch: false)
|
||||||
|
|
||||||
{:ok, _, object} = CommonAPI.vote(other_user, object, [1, 2])
|
{:ok, _, object} = CommonAPI.vote(object, other_user, [1, 2])
|
||||||
|
|
||||||
result = PollView.render("show.json", %{object: object, for: other_user})
|
result = PollView.render("show.json", %{object: object, for: other_user})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user