move to 2.5.5
[anni] / lib / pleroma / web / mastodon_api / views / app_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AppView do
6   use Pleroma.Web, :view
7
8   alias Pleroma.Web.OAuth.App
9
10   def render("index.json", %{apps: apps, count: count, page_size: page_size, admin: true}) do
11     %{
12       apps: render_many(apps, Pleroma.Web.MastodonAPI.AppView, "show.json", %{admin: true}),
13       count: count,
14       page_size: page_size
15     }
16   end
17
18   def render("show.json", %{admin: true, app: %App{} = app} = assigns) do
19     "show.json"
20     |> render(Map.delete(assigns, :admin))
21     |> Map.put(:trusted, app.trusted)
22     |> Map.put(:id, app.id)
23   end
24
25   def render("show.json", %{app: %App{} = app}) do
26     %{
27       id: app.id |> to_string,
28       name: app.client_name,
29       client_id: app.client_id,
30       client_secret: app.client_secret,
31       redirect_uri: app.redirect_uris,
32       website: app.website
33     }
34     |> with_vapid_key()
35   end
36
37   def render("compact_non_secret.json", %{app: %App{website: website, client_name: name}}) do
38     %{
39       name: name,
40       website: website
41     }
42     |> with_vapid_key()
43   end
44
45   defp with_vapid_key(data) do
46     vapid_key = Application.get_env(:web_push_encryption, :vapid_details, [])[:public_key]
47
48     Pleroma.Maps.put_if_present(data, "vapid_key", vapid_key)
49   end
50 end