2019-09-30 22:44:34 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 04:11:47 -07:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2019-09-30 22:44:34 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.PollController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
import Pleroma.Web.ControllerHelper, only: [try_render: 3, json_response: 3]
|
|
|
|
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.Web.ActivityPub.Visibility
|
|
|
|
alias Pleroma.Web.CommonAPI
|
2020-06-24 04:07:47 -06:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
2019-09-30 22:44:34 -06:00
|
|
|
|
|
|
|
action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
|
|
|
|
|
2020-05-05 10:14:22 -06:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
|
|
|
|
2019-10-06 08:12:17 -06:00
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["read:statuses"], fallback: :proceed_unauthenticated} when action == :show
|
|
|
|
)
|
|
|
|
|
|
|
|
plug(OAuthScopesPlug, %{scopes: ["write:statuses"]} when action == :vote)
|
|
|
|
|
2020-05-05 10:14:22 -06:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.PollOperation
|
|
|
|
|
2020-12-18 09:44:46 -07:00
|
|
|
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
|
|
|
|
|
2019-09-30 22:44:34 -06:00
|
|
|
@doc "GET /api/v1/polls/:id"
|
2020-05-05 10:14:22 -06:00
|
|
|
def show(%{assigns: %{user: user}} = conn, %{id: id}) do
|
2019-09-30 22:44:34 -06:00
|
|
|
with %Object{} = object <- Object.get_by_id_and_maybe_refetch(id, interval: 60),
|
|
|
|
%Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
|
|
|
|
true <- Visibility.visible_for_user?(activity, user) do
|
|
|
|
try_render(conn, "show.json", %{object: object, for: user})
|
|
|
|
else
|
|
|
|
error when is_nil(error) or error == false ->
|
|
|
|
render_error(conn, :not_found, "Record not found")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/polls/:id/votes"
|
2020-05-05 10:14:22 -06:00
|
|
|
def vote(%{assigns: %{user: user}, body_params: %{choices: choices}} = conn, %{id: id}) do
|
2019-09-30 22:44:34 -06:00
|
|
|
with %Object{data: %{"type" => "Question"}} = object <- Object.get_by_id(id),
|
|
|
|
%Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
|
|
|
|
true <- Visibility.visible_for_user?(activity, user),
|
|
|
|
{:ok, _activities, object} <- get_cached_vote_or_vote(user, object, choices) do
|
|
|
|
try_render(conn, "show.json", %{object: object, for: user})
|
|
|
|
else
|
|
|
|
nil -> render_error(conn, :not_found, "Record not found")
|
|
|
|
false -> render_error(conn, :not_found, "Record not found")
|
|
|
|
{:error, message} -> json_response(conn, :unprocessable_entity, %{error: message})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp get_cached_vote_or_vote(user, object, choices) do
|
|
|
|
idempotency_key = "polls:#{user.id}:#{object.data["id"]}"
|
|
|
|
|
2020-12-21 04:03:58 -07:00
|
|
|
@cachex.fetch!(:idempotency_cache, idempotency_key, fn _ ->
|
2019-09-30 22:44:34 -06:00
|
|
|
case CommonAPI.vote(user, object, choices) do
|
|
|
|
{:error, _message} = res -> {:ignore, res}
|
|
|
|
res -> {:commit, res}
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|