450943aee18862bd6c25c9cf45c541216393ee2c
[akkoma] / app_view.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.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("index.json", %{apps: apps}) do
19 render_many(apps, Pleroma.Web.MastodonAPI.AppView, "show.json")
20 end
21
22 def render("show.json", %{admin: true, app: %App{} = app} = assigns) do
23 "show.json"
24 |> render(Map.delete(assigns, :admin))
25 |> Map.put(:trusted, app.trusted)
26 |> Map.put(:id, app.id)
27 end
28
29 def render("show.json", %{app: %App{} = app}) do
30 %{
31 id: app.id |> to_string,
32 name: app.client_name,
33 client_id: app.client_id,
34 client_secret: app.client_secret,
35 redirect_uri: app.redirect_uris,
36 website: app.website
37 }
38 |> with_vapid_key()
39 end
40
41 def render("compact_non_secret.json", %{app: %App{website: website, client_name: name}}) do
42 %{
43 name: name,
44 website: website
45 }
46 |> with_vapid_key()
47 end
48
49 defp with_vapid_key(data) do
50 vapid_key = Application.get_env(:web_push_encryption, :vapid_details, [])[:public_key]
51
52 Pleroma.Maps.put_if_present(data, "vapid_key", vapid_key)
53 end
54 end