diff options
author | lain <lain@soykaf.club> | 2019-06-12 16:36:23 +0200 |
---|---|---|
committer | lain <lain@soykaf.club> | 2019-06-12 16:36:23 +0200 |
commit | 966543379d7d0b0dbf53979c9d26ff212963729b (patch) | |
tree | 0103432ba3463d79631555aa2ead72868976d631 | |
parent | 822a9f28d08af9da69a8e0f72cc6511252f25ddd (diff) | |
download | pleroma-966543379d7d0b0dbf53979c9d26ff212963729b.tar.gz |
MastodonAPI Controller: Band-Aid double vote problem.
-rw-r--r-- | lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 16 |
1 files changed, 15 insertions, 1 deletions
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 46049dd24..ed1aa9db2 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -439,12 +439,26 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end end + defp get_cached_vote_or_vote(user, object, choices) do + idempotency_key = "polls:#{user.id}:#{object.data["id"]}" + + {_, res} = + Cachex.fetch(:idempotency_cache, idempotency_key, fn _ -> + case CommonAPI.vote(user, object, choices) do + {:error, _message} = res -> {:ignore, res} + res -> {:commit, res} + end + end) + + res + end + def poll_vote(%{assigns: %{user: user}} = conn, %{"id" => id, "choices" => choices}) do with %Object{} = object <- Object.get_by_id(id), true <- object.data["type"] == "Question", %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]), true <- Visibility.visible_for_user?(activity, user), - {:ok, _activities, object} <- CommonAPI.vote(user, object, choices) do + {:ok, _activities, object} <- get_cached_vote_or_vote(user, object, choices) do conn |> put_view(StatusView) |> try_render("poll.json", %{object: object, for: user}) |