Merge remote-tracking branch 'remotes/origin/develop' into automatic-authentication...
[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.EnsurePublicOrAuthenticatedPlug
9 alias Pleroma.Plugs.OAuthScopesPlug
10 alias Pleroma.User
11
12 plug(OAuthScopesPlug, %{scopes: ["write:accounts"]} when action == :put_settings)
13
14 # Note: :index action handles attempt of unauthenticated access to private instance with redirect
15 plug(:skip_plug, EnsurePublicOrAuthenticatedPlug when action == :index)
16
17 plug(
18 OAuthScopesPlug,
19 %{scopes: ["read"], fallback: :proceed_unauthenticated}
20 when action == :index
21 )
22
23 plug(
24 :skip_plug,
25 [OAuthScopesPlug, EnsurePublicOrAuthenticatedPlug] when action == :manifest
26 )
27
28 @doc "GET /web/*path"
29 def index(%{assigns: %{user: user, token: token}} = conn, _params)
30 when not is_nil(user) and not is_nil(token) do
31 conn
32 |> put_layout(false)
33 |> render("index.html",
34 token: token.token,
35 user: user,
36 custom_emojis: Pleroma.Emoji.get_all()
37 )
38 end
39
40 def index(conn, _params) do
41 conn
42 |> put_session(:return_to, conn.request_path)
43 |> redirect(to: "/web/login")
44 end
45
46 @doc "GET /web/manifest.json"
47 def manifest(conn, _params) do
48 conn
49 |> render("manifest.json")
50 end
51
52 @doc "PUT /api/web/settings"
53 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
54 with {:ok, _} <- User.mastodon_settings_update(user, settings) do
55 json(conn, %{})
56 else
57 e ->
58 conn
59 |> put_status(:internal_server_error)
60 |> json(%{error: inspect(e)})
61 end
62 end
63 end