Automatic checks of authentication / instance publicity. Definition of missing OAuth...
[akkoma] / lib / pleroma / web / masto_fe_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastoFEController do
6 use Pleroma.Web, :controller
7
8 alias Pleroma.Plugs.OAuthScopesPlug
9 alias Pleroma.User
10
11 plug(OAuthScopesPlug, %{scopes: ["write:accounts"]} when action == :put_settings)
12
13 # Note: :index action handles attempt of unauthenticated access to private instance with redirect
14 plug(
15 OAuthScopesPlug,
16 %{scopes: ["read"], fallback: :proceed_unauthenticated}
17 when action == :index
18 )
19
20 plug(
21 :skip_plug,
22 Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug when action in [:index, :manifest]
23 )
24
25 @doc "GET /web/*path"
26 def index(%{assigns: %{user: user, token: token}} = conn, _params)
27 when not is_nil(user) and not is_nil(token) do
28 conn
29 |> put_layout(false)
30 |> render("index.html",
31 token: token.token,
32 user: user,
33 custom_emojis: Pleroma.Emoji.get_all()
34 )
35 end
36
37 def index(conn, _params) do
38 conn
39 |> put_session(:return_to, conn.request_path)
40 |> redirect(to: "/web/login")
41 end
42
43 @doc "GET /web/manifest.json"
44 def manifest(conn, _params) do
45 conn
46 |> render("manifest.json")
47 end
48
49 @doc "PUT /api/web/settings"
50 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
51 with {:ok, _} <- User.mastodon_settings_update(user, settings) do
52 json(conn, %{})
53 else
54 e ->
55 conn
56 |> put_status(:internal_server_error)
57 |> json(%{error: inspect(e)})
58 end
59 end
60 end