-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp-store.ts
More file actions
192 lines (177 loc) · 6.68 KB
/
Copy pathapp-store.ts
File metadata and controls
192 lines (177 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// SPDX-FileCopyrightText: Max Health Inc.
// SPDX-License-Identifier: AGPL-3.0-or-later OR LicenseRef-Commercial
import { Elysia, t } from 'elysia'
import { join } from 'path'
import { readdirSync, readFileSync, existsSync } from 'fs'
import { logger } from '@/lib/logger'
import { getAppStoreConfig, hideApp, showApp, publishApp, unpublishApp } from '@/lib/app-store-config'
import { resolveAppIcon } from '@/lib/app-store-icons'
/**
* Admin App Store management routes.
* Lets admins list all discovered + published apps and toggle visibility.
*/
type AppStoreAppType = {
id: string; launch_url: string; client_id: string; client_name: string;
description: string; category: string; icon: string; hidden: boolean; source: 'filesystem' | 'registered'
}
/** Discover all filesystem apps (unfiltered) */
function discoverAllApps(): AppStoreAppType[] {
const appsDir = join(process.cwd(), 'public', 'apps')
if (!existsSync(appsDir)) return []
const { hiddenAppIds } = getAppStoreConfig()
return readdirSync(appsDir, { withFileTypes: true })
.filter(d => d.isDirectory())
.map(d => {
const manifestPath = join(appsDir, d.name, 'smart-manifest.json')
if (!existsSync(manifestPath)) return null
try {
const manifest = JSON.parse(readFileSync(manifestPath, 'utf-8'))
return {
id: d.name,
launch_url: `/apps/${d.name}/`,
client_id: manifest.client_id ?? d.name,
client_name: manifest.client_name ?? d.name,
description: manifest.description ?? '',
category: manifest.category ?? 'other',
icon: resolveAppIcon(manifest.logoUri ?? manifest.icon, manifest.category).icon,
hidden: hiddenAppIds.includes(d.name),
source: 'filesystem' as const,
}
} catch { return null }
})
.filter(Boolean) as AppStoreAppType[]
}
/** Merge filesystem-discovered apps with published registered apps */
function getAllStoreApps(): AppStoreAppType[] {
const fsApps = discoverAllApps()
const config = getAppStoreConfig()
const fsClientIds = new Set(fsApps.map(a => a.client_id))
const publishedApps: AppStoreAppType[] = config.publishedApps
.filter(pa => !fsClientIds.has(pa.clientId))
.map(pa => ({
id: pa.clientId,
launch_url: pa.launchUrl,
client_id: pa.clientId,
client_name: pa.name,
description: pa.description,
category: pa.category,
icon: resolveAppIcon(pa.logoUri, pa.category).icon,
hidden: config.hiddenAppIds.includes(pa.clientId),
source: 'registered' as const,
}))
return [...fsApps, ...publishedApps]
}
const AppStoreApp = t.Object({
id: t.String(),
launch_url: t.String(),
client_id: t.String(),
client_name: t.String(),
description: t.String(),
category: t.String(),
icon: t.String(),
hidden: t.Boolean(),
source: t.UnionEnum(['filesystem', 'registered']),
}, { title: 'AppStoreApp' })
const PublishedAppSchema = t.Object({
clientId: t.String(),
name: t.String(),
description: t.String(),
launchUrl: t.String(),
category: t.String(),
logoUri: t.Optional(t.String()),
}, { title: 'PublishedApp' })
const AppStoreListResponse = t.Object({
apps: t.Array(AppStoreApp),
publishedApps: t.Array(PublishedAppSchema),
hiddenAppIds: t.Array(t.String()),
updatedAt: t.String(),
}, { title: 'AppStoreListResponse' })
const AppStoreToggleResponse = t.Object({
success: t.Boolean(),
hiddenAppIds: t.Array(t.String()),
updatedAt: t.String(),
}, { title: 'AppStoreToggleResponse' })
const AppStorePublishResponse = t.Object({
success: t.Boolean(),
publishedApps: t.Array(PublishedAppSchema),
updatedAt: t.String(),
}, { title: 'AppStorePublishResponse' })
export const appStoreAdminRoutes = new Elysia({ prefix: '/app-store' })
// GET /admin/app-store — list all apps (filesystem + published) with visibility status
.get('/', () => {
const apps = getAllStoreApps()
const config = getAppStoreConfig()
return {
apps,
publishedApps: config.publishedApps,
hiddenAppIds: config.hiddenAppIds,
updatedAt: config.updatedAt,
}
}, {
response: { 200: AppStoreListResponse },
detail: {
summary: 'List App Store Apps',
description: 'List all discovered and published SMART apps with their visibility status',
tags: ['app-store'],
},
})
// POST /admin/app-store/:appId/hide — hide an app from the public store
.post('/:appId/hide', async ({ params }) => {
const config = await hideApp(params.appId)
logger.server.info(`App store: hid app "${params.appId}"`)
return { success: true, hiddenAppIds: config.hiddenAppIds, updatedAt: config.updatedAt }
}, {
params: t.Object({ appId: t.String() }),
response: { 200: AppStoreToggleResponse },
detail: {
summary: 'Hide App from Store',
description: 'Hide a SMART app from the public app store page',
tags: ['app-store'],
},
})
// POST /admin/app-store/:appId/show — show an app in the public store
.post('/:appId/show', async ({ params }) => {
const config = await showApp(params.appId)
logger.server.info(`App store: showed app "${params.appId}"`)
return { success: true, hiddenAppIds: config.hiddenAppIds, updatedAt: config.updatedAt }
}, {
params: t.Object({ appId: t.String() }),
response: { 200: AppStoreToggleResponse },
detail: {
summary: 'Show App in Store',
description: 'Show a previously hidden SMART app in the public app store page',
tags: ['app-store'],
},
})
// POST /admin/app-store/publish — publish a registered app to the store
.post('/publish', async ({ body }) => {
const config = await publishApp(body)
logger.server.info(`App store: published registered app "${body.clientId}" (${body.name})`)
return { success: true, publishedApps: config.publishedApps, updatedAt: config.updatedAt }
}, {
body: PublishedAppSchema,
response: {
200: AppStorePublishResponse,
},
detail: {
summary: 'Publish App to Store',
description: 'Publish a registered SMART app to the public app store',
tags: ['app-store'],
},
})
// POST /admin/app-store/:appId/unpublish — remove a registered app from the store
.post('/:appId/unpublish', async ({ params }) => {
const config = await unpublishApp(params.appId)
logger.server.info(`App store: unpublished registered app "${params.appId}"`)
return { success: true, publishedApps: config.publishedApps, updatedAt: config.updatedAt }
}, {
params: t.Object({ appId: t.String() }),
response: {
200: AppStorePublishResponse,
},
detail: {
summary: 'Unpublish App from Store',
description: 'Remove a registered SMART app from the public app store',
tags: ['app-store'],
},
})