]> Untitled Git - lemmy-ui.git/blob - src/server/utils/generate-manifest-base64.ts
Merge branch 'main' into expand-video-embeds-to-fullwidth
[lemmy-ui.git] / src / server / utils / generate-manifest-base64.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 { getHttpBaseExternal } from "../../shared/env";
6 import { fetchIconPng } from "./fetch-icon-png";
7
8 const iconSizes = [72, 96, 144, 192, 512];
9
10 const defaultLogoPathDirectory = path.join(
11   process.cwd(),
12   "dist",
13   "assets",
14   "icons"
15 );
16
17 export async function generateManifestBase64({
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   const manifest = {
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       .concat(
74         my_user
75           ? [
76               {
77                 name: "Create Post",
78                 url: "/create_post",
79                 short_name: "Create Post",
80                 description: "Create a post.",
81               },
82             ]
83           : []
84       )
85       .concat(
86         my_user?.local_user_view.person.admin || !community_creation_admin_only
87           ? [
88               {
89                 name: "Create Community",
90                 url: "/create_community",
91                 short_name: "Create Community",
92                 description: "Create a community",
93               },
94             ]
95           : []
96       ),
97     related_applications: [
98       {
99         platform: "f-droid",
100         url: "https://f-droid.org/packages/com.jerboa/",
101         id: "com.jerboa",
102       },
103     ],
104   };
105
106   return Buffer.from(JSON.stringify(manifest)).toString("base64");
107 }