First
[anni] / lib / pleroma / web / mastodon_api / controllers / marker_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.MarkerController do
6   use Pleroma.Web, :controller
7   alias Pleroma.Web.Plugs.OAuthScopesPlug
8
9   plug(Pleroma.Web.ApiSpec.CastAndValidate)
10
11   plug(
12     OAuthScopesPlug,
13     %{scopes: ["read:statuses"]}
14     when action == :index
15   )
16
17   plug(OAuthScopesPlug, %{scopes: ["write:statuses"]} when action == :upsert)
18
19   action_fallback(Pleroma.Web.MastodonAPI.FallbackController)
20
21   defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.MarkerOperation
22
23   # GET /api/v1/markers
24   def index(%{assigns: %{user: user}} = conn, params) do
25     markers = Pleroma.Marker.get_markers(user, params[:timeline])
26     render(conn, "markers.json", %{markers: markers})
27   end
28
29   # POST /api/v1/markers
30   def upsert(%{assigns: %{user: user}, body_params: params} = conn, _) do
31     params = Map.new(params, fn {key, value} -> {to_string(key), value} end)
32
33     with {:ok, result} <- Pleroma.Marker.upsert(user, params),
34          markers <- Map.values(result) do
35       render(conn, "markers.json", %{markers: markers})
36     end
37   end
38 end