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