Merge branch 'stable' into mergeback/2.2.2
[akkoma] / lib / pleroma / web / masto_fe_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.User
9 alias Pleroma.Web.MastodonAPI.AuthController
10 alias Pleroma.Web.OAuth.Token
11 alias Pleroma.Web.Plugs.EnsurePublicOrAuthenticatedPlug
12 alias Pleroma.Web.Plugs.OAuthScopesPlug
13
14 plug(OAuthScopesPlug, %{scopes: ["write:accounts"]} when action == :put_settings)
15
16 # Note: :index action handles attempt of unauthenticated access to private instance with redirect
17 plug(:skip_plug, EnsurePublicOrAuthenticatedPlug when action == :index)
18
19 plug(
20 OAuthScopesPlug,
21 %{scopes: ["read"], fallback: :proceed_unauthenticated}
22 when action == :index
23 )
24
25 plug(
26 :skip_plug,
27 [OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug] when action == :manifest
28 )
29
30 @doc "GET /web/*path"
31 def index(conn, _params) do
32 with %{assigns: %{user: %User{} = user, token: %Token{app_id: token_app_id} = token}} <- conn,
33 {:ok, %{id: ^token_app_id}} <- AuthController.local_mastofe_app() do
34 conn
35 |> put_layout(false)
36 |> render("index.html",
37 token: token.token,
38 user: user,
39 custom_emojis: Pleroma.Emoji.get_all()
40 )
41 else
42 _ ->
43 conn
44 |> put_session(:return_to, conn.request_path)
45 |> redirect(to: "/web/login")
46 end
47 end
48
49 @doc "GET /web/manifest.json"
50 def manifest(conn, _params) do
51 render(conn, "manifest.json")
52 end
53
54 @doc "PUT /api/web/settings: Backend-obscure settings blob for MastoFE, don't parse/reuse elsewhere"
55 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
56 with {:ok, _} <- User.mastodon_settings_update(user, settings) do
57 json(conn, %{})
58 else
59 e ->
60 conn
61 |> put_status(:internal_server_error)
62 |> json(%{error: inspect(e)})
63 end
64 end
65 end