Merge branch 'develop' into feature/reports-groups-and-multiple-state-update
[akkoma] / lib / pleroma / web / masto_fe_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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}} = conn, _params) do
24 token = get_session(conn, :oauth_token)
25
26 if user && token do
27 conn
28 |> put_layout(false)
29 |> render("index.html", token: token, user: user, custom_emojis: Pleroma.Emoji.get_all())
30 else
31 conn
32 |> put_session(:return_to, conn.request_path)
33 |> redirect(to: "/web/login")
34 end
35 end
36
37 @doc "PUT /api/web/settings"
38 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
39 with {:ok, _} <- User.update_info(user, &User.Info.mastodon_settings_update(&1, settings)) do
40 json(conn, %{})
41 else
42 e ->
43 conn
44 |> put_status(:internal_server_error)
45 |> json(%{error: inspect(e)})
46 end
47 end
48 end