Merge pull request 'metrics' (#375) from stats into develop
[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 flavour =
31 [:frontends, :mastodon]
32 |> Pleroma.Config.get()
33 |> Map.get("name", "mastodon-fe")
34
35 index =
36 if flavour == "fedibird-fe" do
37 "fedibird.index.html"
38 else
39 "glitchsoc.index.html"
40 end
41
42 conn
43 |> put_layout(false)
44 |> render(index,
45 token: token.token,
46 user: user,
47 custom_emojis: Pleroma.Emoji.get_all()
48 )
49 else
50 _ ->
51 conn
52 |> put_session(:return_to, conn.request_path)
53 |> redirect(to: "/web/login")
54 end
55 end
56
57 @doc "GET /web/manifest.json"
58 def manifest(conn, _params) do
59 render(conn, "manifest.json")
60 end
61
62 @doc "PUT /api/web/settings: Backend-obscure settings blob for MastoFE, don't parse/reuse elsewhere"
63 def put_settings(%{assigns: %{user: user}} = conn, %{"data" => settings} = _params) do
64 with {:ok, _} <- User.mastodon_settings_update(user, settings) do
65 json(conn, %{})
66 else
67 e ->
68 conn
69 |> put_status(:internal_server_error)
70 |> json(%{error: inspect(e)})
71 end
72 end
73 end