]> Untitled Git - lemmy-ui.git/blob - src/server/utils/generate-manifest-json.ts
2f9d8b801c6b48e87572d88f617b412f81a6a86f
[lemmy-ui.git] / src / server / utils / generate-manifest-json.ts
1 import { getHttpBaseExternal } from "@utils/env";
2 import { readFile } from "fs/promises";
3 import { GetSiteResponse } from "lemmy-js-client";
4 import path from "path";
5 import sharp from "sharp";
6 import { fetchIconPng } from "./fetch-icon-png";
7
8 const iconSizes = [72, 96, 128, 144, 152, 192, 384, 512];
9
10 const defaultLogoPathDirectory = path.join(
11   process.cwd(),
12   "dist",
13   "assets",
14   "icons"
15 );
16
17 export default async function ({
18   my_user,
19   site_view: {
20     site,
21     local_site: { community_creation_admin_only },
22   },
23 }: GetSiteResponse) {
24   const url = getHttpBaseExternal();
25
26   const icon = site.icon ? await fetchIconPng(site.icon) : null;
27
28   return {
29     name: site.name,
30     description: site.description ?? "A link aggregator for the fediverse",
31     start_url: url,
32     scope: url,
33     display: "standalone",
34     id: "/",
35     background_color: "#222222",
36     theme_color: "#222222",
37     icons: await Promise.all(
38       iconSizes.map(async size => {
39         let src = await readFile(
40           path.join(defaultLogoPathDirectory, `icon-${size}x${size}.png`)
41         ).then(buf => buf.toString("base64"));
42
43         if (icon) {
44           src = await sharp(icon)
45             .resize(size, size)
46             .png()
47             .toBuffer()
48             .then(buf => buf.toString("base64"));
49         }
50
51         return {
52           sizes: `${size}x${size}`,
53           type: "image/png",
54           src: `data:image/png;base64,${src}`,
55           purpose: "any maskable",
56         };
57       })
58     ),
59     shortcuts: [
60       {
61         name: "Search",
62         short_name: "Search",
63         description: "Perform a search.",
64         url: "/search",
65       },
66       {
67         name: "Communities",
68         url: "/communities",
69         short_name: "Communities",
70         description: "Browse communities",
71       },
72       {
73         name: "Create Post",
74         url: "/create_post",
75         short_name: "Create Post",
76         description: "Create a post.",
77       },
78     ].concat(
79       my_user?.local_user_view.person.admin || !community_creation_admin_only
80         ? [
81             {
82               name: "Create Community",
83               url: "/create_community",
84               short_name: "Create Community",
85               description: "Create a community",
86             },
87           ]
88         : []
89     ),
90     related_applications: [
91       {
92         platform: "f-droid",
93         url: "https://f-droid.org/packages/com.jerboa/",
94         id: "com.jerboa",
95       },
96     ],
97   };
98 }