d2460f51d9edd3da336df33c11f123367e1800c0
[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.OAuthScopesPlug
12
13 plug(OAuthScopesPlug, %{scopes: ["write:accounts"]} when action == :put_settings)
14
15 # Note: :index action handles attempt of unauthenticated access to private instance with redirect
16 plug(:skip_public_check when action == :index)
17
18 plug(
19 OAuthScopesPlug,
20 %{scopes: ["read"], fallback: :proceed_unauthenticated}
21 when action == :index
22 )
23
24 plug(:skip_auth when action == :manifest)
25
26 @doc "GET /web/*path"
27 def index(conn, _params) do
28 with %{assigns: %{user: %User{} = user, token: %Token{app_id: token_app_id} = token}} <- conn,
29 {:ok, %{id: ^token_app_id}} <- AuthController.local_mastofe_app() do
30 conn
31 |> put_layout(false)
32 |> render("index.html",
33 token: token.token,
34 user: user,
35 custom_emojis: Pleroma.Emoji.get_all()
36 )
37 else
38 _ ->
39 conn
40 |> put_session(:return_to, conn.request_path)
41 |> redirect(to: "/web/login")
42 end
43 end
44
45 @doc "GET /web/manifest.json"
46 def manifest(conn, _params) do
47 render(conn, "manifest.json")
48 end
49
50 @doc "PUT /api/web/settings: Backend-obscure settings blob for MastoFE, don't parse/reuse elsewhere"
51 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
52 with {:ok, _} <- User.mastodon_settings_update(user, settings) do
53 json(conn, %{})
54 else
55 e ->
56 conn
57 |> put_status(:internal_server_error)
58 |> json(%{error: inspect(e)})
59 end
60 end
61 end