Merge remote-tracking branch 'remotes/origin/develop' into 1560-non-federating-instan...
[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, skip_instance_privacy_check: true}
17 when action == :index
18 )
19
20 plug(Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug when action != :index)
21
22 @doc "GET /web/*path"
23 def index(%{assigns: %{user: user, token: token}} = conn, _params)
24 when not is_nil(user) and not is_nil(token) do
25 conn
26 |> put_layout(false)
27 |> render("index.html",
28 token: token.token,
29 user: user,
30 custom_emojis: Pleroma.Emoji.get_all()
31 )
32 end
33
34 def index(conn, _params) do
35 conn
36 |> put_session(:return_to, conn.request_path)
37 |> redirect(to: "/web/login")
38 end
39
40 @doc "GET /web/manifest.json"
41 def manifest(conn, _params) do
42 conn
43 |> render("manifest.json")
44 end
45
46 @doc "PUT /api/web/settings"
47 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
48 with {:ok, _} <- User.mastodon_settings_update(user, settings) do
49 json(conn, %{})
50 else
51 e ->
52 conn
53 |> put_status(:internal_server_error)
54 |> json(%{error: inspect(e)})
55 end
56 end
57 end