domma-cms 1.37.0 → 1.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/CLAUDE.md +9 -0
- package/admin/js/api.js +1 -1
- package/admin/js/lib/plugins-arrange.js +2 -2
- package/admin/js/templates/plugins.html +2 -0
- package/admin/js/views/plugins.js +24 -12
- package/bin/update.js +2 -0
- package/docs/DommaFeatures.md +3 -3
- package/docs/configuration.md +1 -0
- package/docs/plugin-development.md +11 -0
- package/package.json +1 -1
- package/plugins/_lib/games/hub.js +35 -0
- package/plugins/_lib/public/games/games.css +1 -1
- package/plugins/_lib/public/games/hub.js +79 -0
- package/server/routes/api/plugin-marketplace.js +249 -19
- package/server/services/siteGitignore.js +2 -0
- package/server/services/storeClient.js +333 -0
|
@@ -825,6 +825,17 @@ These free plugins ship in the core package's `plugins/` directory and are good
|
|
|
825
825
|
Analytics, effects, the theme switcher, Notes, Todo, Contacts and the demo viewer used to be plugins and are now part
|
|
826
826
|
of the core CMS. Theme Roller and the other paid plugins are installed from the Marketplace.
|
|
827
827
|
|
|
828
|
+
### Buying a Tool on a self-hosted site
|
|
829
|
+
|
|
830
|
+
A site you host yourself buys paid Tools from the Domma store, inside the admin: **Marketplace > Browse > Buy…**
|
|
831
|
+
opens the payment page in a new tab, and the Tool installs on the site as soon as the payment is through (restart to
|
|
832
|
+
switch it on). It is yours to keep - it carries on running offline whatever happens - and comes with a term of updates;
|
|
833
|
+
when that ends, **Renew…** buys another term with a one-off payment, and the version you have keeps working without
|
|
834
|
+
one. One licence covers one site address, so buy from the site's public address (set the **Site URL** in Site Settings),
|
|
835
|
+
not `localhost`. The licence key is emailed with the receipt, together with a link to the `.dcmsplugin` file for
|
|
836
|
+
Install from file. A Tool bought on the store's own Marketplace web page instead arrives as a key by email: on the Tool's row in Browse choose **Enter licence key…** and it installs. Free Tools in the store install with no key. `config/server.json` `storeUrl` (above) points the
|
|
837
|
+
store elsewhere or switches it off.
|
|
838
|
+
|
|
828
839
|
## Signed plugins and `requireSignedPlugins`
|
|
829
840
|
|
|
830
841
|
Plugins from the marketplace are distributed as signed `.dcmsplugin` bundles, and the signature is verified at install.
|
package/package.json
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Games - the hub shortcode [games-leaderboards games="quiz,hangman" period="week" limit="10" /].
|
|
3
|
+
*
|
|
4
|
+
* Every Game with a leaderboard registers it through registerHub() (the same
|
|
5
|
+
* handler each time, so whichever registers last makes no difference). It only
|
|
6
|
+
* writes a placeholder; each Game's player script adds its own group of boards
|
|
7
|
+
* (from its GET /public/boards) through joinHub() in public/games/hub.js. A Game
|
|
8
|
+
* the site does not have never adds a group, so the hub shows exactly the Games
|
|
9
|
+
* a site has. `games` names which, in that order; empty means all of them.
|
|
10
|
+
*
|
|
11
|
+
* @module _lib/games/hub
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const PERIODS = ['today', 'week', 'month', 'all'];
|
|
15
|
+
const SLUG = /^[a-z0-9][a-z0-9-]{0,63}$/;
|
|
16
|
+
const MAX_GAMES = 40;
|
|
17
|
+
|
|
18
|
+
const esc = (s) => String(s ?? '').replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
|
|
19
|
+
|
|
20
|
+
/** The placeholder's config from the shortcode's attributes. */
|
|
21
|
+
export function hubConfig(a = {}) {
|
|
22
|
+
const games = [...new Set(String(a.games || '').split(',').map(g => g.trim().toLowerCase()).filter(g => SLUG.test(g)))].slice(0, MAX_GAMES);
|
|
23
|
+
const period = PERIODS.includes(String(a.period || '')) ? String(a.period) : '';
|
|
24
|
+
return {games, period, limit: Math.max(1, Math.min(100, Number(a.limit) || 10))};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function hubShortcode(attrStr, _body, ctx) {
|
|
28
|
+
const cfg = hubConfig(ctx.parseShortcodeAttrs(attrStr || ''));
|
|
29
|
+
return `<div class="gm-hub" data-games-leaderboards='${esc(JSON.stringify(cfg))}'><p class="gm-muted gm-hub-empty">No leaderboards to show yet.</p></div>`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Registers [games-leaderboards] ({priority: 5}: ahead of any shortcode whose name it starts with). */
|
|
33
|
+
export function registerHub(hooks) {
|
|
34
|
+
if (typeof hooks?.registerShortcode === 'function') hooks.registerShortcode('games-leaderboards', hubShortcode, {priority: 5});
|
|
35
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
.gm-app{--gm-gap: 1rem;--gm-radius: var(--dm-radius, 10px);--gm-line: var(--dm-border, rgba(127, 127, 127, .3));--gm-soft: var(--dm-surface, rgba(127, 127, 127, .08));--gm-accent: var(--dm-primary, #3b82f6);--gm-good: var(--dm-success, #16a34a);--gm-bad: var(--dm-danger, #dc2626);--gm-warn: var(--dm-warning, #d97706)
|
|
1
|
+
.gm-app,.gm-hub{--gm-gap: 1rem;--gm-radius: var(--dm-radius, 10px);--gm-line: var(--dm-border, rgba(127, 127, 127, .3));--gm-soft: var(--dm-surface, rgba(127, 127, 127, .08));--gm-accent: var(--dm-primary, #3b82f6);--gm-good: var(--dm-success, #16a34a);--gm-bad: var(--dm-danger, #dc2626);--gm-warn: var(--dm-warning, #d97706)}.gm-app{max-width:760px;margin:1.5rem auto;color:var(--dm-text, inherit)}.gm-app [hidden],.gm-board [hidden]{display:none!important}.gm-card{border:1px solid var(--gm-line);border-radius:var(--gm-radius);background:var(--dm-surface-raised, var(--dm-background, transparent));padding:1.25rem 1.5rem;margin-bottom:var(--gm-gap)}.gm-title{margin:0 0 .5rem;font-size:1.6rem;line-height:1.2}.gm-intro{margin:0 0 1rem;white-space:pre-line}.gm-muted{color:var(--dm-text-muted, #6b7280);margin:.25rem 0}.gm-facts{display:flex;flex-wrap:wrap;gap:.5rem 1.25rem;margin:0 0 1rem;padding:0;list-style:none;color:var(--dm-text-muted, #6b7280)}.gm-facts li:before{content:"\2022 "}.gm-facts li:first-child:before{content:""}.gm-start{display:flex;flex-wrap:wrap;gap:.75rem;align-items:flex-end}.gm-field{display:flex;flex-direction:column;gap:.3rem;flex:1 1 220px}.gm-field label{font-weight:600}.gm-input{font:inherit;padding:.6rem .75rem;border:1px solid var(--gm-line);border-radius:8px;background:var(--dm-input-bg, var(--dm-background, #fff));color:inherit;width:100%;box-sizing:border-box}.gm-input:focus{outline:2px solid var(--gm-accent);outline-offset:1px}.gm-hp{position:absolute!important;left:-9999px!important;width:1px;height:1px;overflow:hidden}.gm-error{color:var(--gm-bad);margin:.5rem 0 0;font-weight:600}.gm-note{margin:.5rem 0 0}.gm-bar{display:flex;flex-wrap:wrap;justify-content:space-between;align-items:center;gap:.5rem;margin-bottom:1rem}.gm-progress{font-weight:600}.gm-clock{font-variant-numeric:tabular-nums;font-weight:700;padding:.2rem .65rem;border-radius:999px;background:var(--gm-soft);min-width:4.5rem;text-align:center}.gm-clock.is-low{background:var(--gm-bad);color:#fff}.gm-track{height:6px;border-radius:999px;background:var(--gm-soft);overflow:hidden;margin-bottom:1rem}.gm-track>span{display:block;height:100%;background:var(--gm-accent);transition:width .2s linear}.gm-actions{display:flex;flex-wrap:wrap;gap:.5rem;margin-top:1rem}.gm-result-score{font-size:2.4rem;font-weight:800;margin:.25rem 0}.gm-result-place{font-size:1.15rem;margin:0 0 .75rem}.gm-board{border:1px solid var(--gm-line, rgba(127, 127, 127, .3));border-radius:var(--gm-radius, 10px);padding:1rem 1.25rem;margin:0 0 1rem}.gm-board-head{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:.5rem;margin-bottom:.5rem}.gm-board-title{margin:0;font-size:1.2rem}.gm-periods{display:flex;flex-wrap:wrap;gap:.25rem}.gm-period{font:inherit;font-size:.85rem;padding:.2rem .6rem;border-radius:999px;cursor:pointer;border:1px solid var(--gm-line, rgba(127, 127, 127, .3));background:transparent;color:inherit}.gm-period.is-on{background:var(--dm-primary, #3b82f6);border-color:var(--dm-primary, #3b82f6);color:#fff}.gm-board-list{list-style:none;margin:0;padding:0}.gm-board-row{display:grid;grid-template-columns:3.2rem 1fr auto auto;gap:.75rem;align-items:center;padding:.45rem .5rem;border-bottom:1px solid var(--gm-line, rgba(127, 127, 127, .2))}.gm-board-row:last-child{border-bottom:0}.gm-board-row:nth-child(1) .gm-rank{color:#c9a227}.gm-board-row:nth-child(2) .gm-rank{color:#9aa3ad}.gm-board-row:nth-child(3) .gm-rank{color:#b0703a}.gm-board-row.is-mine{background:color-mix(in srgb,var(--dm-primary, #3b82f6) 14%,transparent);border-radius:6px;font-weight:700}.gm-rank{font-weight:800}.gm-name{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.gm-score,.gm-time{font-variant-numeric:tabular-nums;text-align:right}.gm-score{font-weight:700}@media(max-width:520px){.gm-card{padding:1rem}.gm-board-row{grid-template-columns:2.6rem 1fr auto auto;gap:.5rem}}.gm-hub{display:flex;flex-direction:column;gap:1.5rem;margin:1.5rem 0;color:var(--dm-text, inherit)}.gm-hub:has(.gm-hub-group)>.gm-hub-empty{display:none}.gm-hub-group{display:flex;flex-direction:column;gap:.5rem;max-width:none;margin:0}.gm-hub-title{margin:0;font-size:1.4rem;line-height:1.2}.gm-hub-boards{display:grid;grid-template-columns:repeat(auto-fill,minmax(min(22rem,100%),1fr));gap:1rem}.gm-hub-board>.gm-board,.gm-hub-board .gm-board{margin:0}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Games - a Game's group of boards in the hub [games-leaderboards] (the
|
|
3
|
+
* placeholder comes from _lib/games/hub.js).
|
|
4
|
+
*
|
|
5
|
+
* Each Game's player script calls joinHub() from its mountAll(): for every hub
|
|
6
|
+
* on the page it asks the Game's GET /public/boards and adds one group, a
|
|
7
|
+
* heading and a board per item, mounted by the Game's own mountBoard(). Groups
|
|
8
|
+
* stand in the order the shortcode's `games` names them, else by name. A Game
|
|
9
|
+
* with nothing to show adds nothing, and the "No leaderboards" line goes once
|
|
10
|
+
* any group is there (games.css).
|
|
11
|
+
*
|
|
12
|
+
* Served from /plugins/_lib/public/games/hub.js.
|
|
13
|
+
*
|
|
14
|
+
* @module _lib/public/games/hub
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import {addStyles, configOf, request} from './games.js';
|
|
18
|
+
|
|
19
|
+
const ATTR = 'data-games-leaderboards';
|
|
20
|
+
|
|
21
|
+
/** The group's place: its position in `games` when the hub names Games, else its name. */
|
|
22
|
+
export function sortKey(cfg, game, label) {
|
|
23
|
+
const i = Array.isArray(cfg?.games) ? cfg.games.indexOf(game) : -1;
|
|
24
|
+
return i >= 0 ? `0:${String(i).padStart(3, '0')}` : `1:${String(label || game).toLowerCase()}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Does this hub want the Game? */
|
|
28
|
+
export function wants(cfg, game) {
|
|
29
|
+
return !Array.isArray(cfg?.games) || !cfg.games.length || cfg.games.includes(game);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async function addGroup(el, {game, label, boards, boardUrl, mountBoard, css, className}) {
|
|
33
|
+
const cfg = configOf(el, ATTR) || {};
|
|
34
|
+
if (!wants(cfg, game)) return;
|
|
35
|
+
let res;
|
|
36
|
+
try { res = await request(boards); } catch { return; }
|
|
37
|
+
if (!res?.items?.length || !el.isConnected) return;
|
|
38
|
+
addStyles(css);
|
|
39
|
+
const name = String(res.label || label || game);
|
|
40
|
+
const group = document.createElement('section');
|
|
41
|
+
group.className = `gm-hub-group ${className}`.trim();
|
|
42
|
+
group.dataset.game = game;
|
|
43
|
+
group.dataset.sort = sortKey(cfg, game, name);
|
|
44
|
+
const title = document.createElement('h2');
|
|
45
|
+
title.className = 'gm-hub-title';
|
|
46
|
+
title.textContent = name;
|
|
47
|
+
const list = document.createElement('div');
|
|
48
|
+
list.className = 'gm-hub-boards';
|
|
49
|
+
group.append(title, list);
|
|
50
|
+
const after = [...el.querySelectorAll(':scope > .gm-hub-group')].find(g => g.dataset.sort > group.dataset.sort);
|
|
51
|
+
el.insertBefore(group, after || null);
|
|
52
|
+
for (const item of res.items) {
|
|
53
|
+
const slot = document.createElement('div');
|
|
54
|
+
slot.className = 'gm-hub-board';
|
|
55
|
+
list.append(slot);
|
|
56
|
+
mountBoard(slot, {url: boardUrl(item.slug), period: cfg.period || item.period, limit: cfg.limit || item.limit, title: item.title});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Adds this Game's group to every hub on the page not yet holding it.
|
|
62
|
+
*
|
|
63
|
+
* @param {object} o
|
|
64
|
+
* @param {string} o.game the Game's slug, as the shortcode's `games` names it
|
|
65
|
+
* @param {string} o.label its heading when /public/boards gives none
|
|
66
|
+
* @param {string} o.boards the URL of its GET /public/boards ({label, items: [{slug, title, period, limit}]})
|
|
67
|
+
* @param {(slug: string) => string} o.boardUrl one item's leaderboard URL
|
|
68
|
+
* @param {(el: Element, opts: {url, period, limit, title}) => void} o.mountBoard the Game's own board
|
|
69
|
+
* @param {string[]} [o.css] the Game's stylesheets, so its board looks as it does on its own page
|
|
70
|
+
* @param {string} [o.className] classes the Game's board styles are scoped under
|
|
71
|
+
*/
|
|
72
|
+
export function joinHub({game, label = '', boards, boardUrl, mountBoard, css = [], className = ''}) {
|
|
73
|
+
const flag = `gmHub${String(game).replace(/-(\w)/g, (_, c) => c.toUpperCase()).replace(/^\w/, c => c.toUpperCase())}`;
|
|
74
|
+
for (const el of document.querySelectorAll(`[${ATTR}]`)) {
|
|
75
|
+
if (el.dataset[flag]) continue;
|
|
76
|
+
el.dataset[flag] = '1';
|
|
77
|
+
addGroup(el, {game, label, boards, boardUrl, mountBoard, css, className});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -1,17 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Plugin Marketplace API
|
|
2
|
+
* Plugin Marketplace API.
|
|
3
3
|
*
|
|
4
|
-
* GET /api/plugins/marketplace
|
|
5
|
-
* POST /api/plugins/marketplace/install
|
|
6
|
-
*
|
|
4
|
+
* GET /api/plugins/marketplace - the catalogue as this site sees it
|
|
5
|
+
* POST /api/plugins/marketplace/install - fetch a bundle and install it
|
|
6
|
+
* POST /api/plugins/marketplace/buy - self-hosted: start buying (or renewing) a Tool
|
|
7
|
+
* POST /api/plugins/marketplace/claim - self-hosted: has it been paid? (installs it when it has)
|
|
8
|
+
* DELETE /api/plugins/marketplace/purchases/:id - self-hosted: stop waiting for a purchase
|
|
9
|
+
* DELETE /api/plugins/marketplace/:slug - uninstall (a paid plugin also tells the manager, for billing)
|
|
7
10
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
11
|
+
* Two delivery paths behind the same screen:
|
|
12
|
+
* managed a site the manager forked buys from the fleet's own catalogue
|
|
13
|
+
* over loopback (managerClient.js) - rented, billed monthly.
|
|
14
|
+
* self-hosted any other site buys from the public Domma store (storeClient.js)
|
|
15
|
+
* - bought outright, with a term of updates. The claim secret and
|
|
16
|
+
* the licence keys stay on this server; the browser only ever sees
|
|
17
|
+
* the payment page's address.
|
|
18
|
+
* An owner instance (the manager itself) has neither, and the screen says so.
|
|
19
|
+
* Install from file (`POST /plugins/install-upload`) works everywhere.
|
|
15
20
|
*
|
|
16
21
|
* All routes require authentication + admin role.
|
|
17
22
|
*/
|
|
@@ -19,13 +24,74 @@ import {authenticate, requireAdmin} from '../../middleware/auth.js';
|
|
|
19
24
|
import {fetchCatalogue, fetchPluginBundle, isManaged, releasePlugin, siteSlug} from '../../services/managerClient.js';
|
|
20
25
|
import {cmsVersionShortfall, compareVersions, installedCmsVersion, installPlugin, uninstallPlugin} from '../../services/pluginInstaller.js';
|
|
21
26
|
import {discoverPlugins} from '../../services/plugins.js';
|
|
27
|
+
import {readEnvelope, readManifestFromTarball} from '../../services/pluginEnvelope.js';
|
|
28
|
+
import {originOf, trustedOrigin} from '../../services/passwordReset.js';
|
|
29
|
+
import {getUserById} from '../../services/users.js';
|
|
30
|
+
import * as storeClient from '../../services/storeClient.js';
|
|
31
|
+
import {classifyEntitlement} from '../../services/pluginEntitlement.js';
|
|
32
|
+
import fs from 'node:fs/promises';
|
|
33
|
+
import path from 'node:path';
|
|
34
|
+
|
|
35
|
+
const SLUG = /^[a-z0-9][a-z0-9-_]{0,63}$/;
|
|
36
|
+
/** Entitlement states a plugin runs under (pluginEntitlement.mayLoad). */
|
|
37
|
+
const LOADS = new Set(['valid', 'grace', 'support-ended']);
|
|
38
|
+
|
|
39
|
+
/** Messages the installer raises deliberately - things an admin can act on. Anything else stays vague (it can carry a path). */
|
|
40
|
+
function installError(err) {
|
|
41
|
+
const safe = err.expose === true || [
|
|
42
|
+
'Plugin already installed', 'Plugin signature invalid', 'Unknown public key',
|
|
43
|
+
'Invalid plugin slug', 'Refusing to downgrade', 'Refusing to upgrade'
|
|
44
|
+
].some(m => err.message.startsWith(m));
|
|
45
|
+
return safe ? err.message : 'Installation failed.';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Download a plugin from the store and install it. The file is the store's
|
|
50
|
+
* signed bundle; an unsigned one is refused here whatever it says, since nothing
|
|
51
|
+
* the store serves is unsigned.
|
|
52
|
+
*/
|
|
53
|
+
async function installFromStore(slug, {version = null, update = false} = {}) {
|
|
54
|
+
const parts = readEnvelope(await storeClient.downloadPlugin(slug));
|
|
55
|
+
if (!parts.signature) throw Object.assign(new Error('The store sent an unsigned file - nothing was installed.'), {expose: true});
|
|
56
|
+
const manifest = readManifestFromTarball(parts.tarball);
|
|
57
|
+
if (manifest?.name !== slug) throw Object.assign(new Error('The store sent a different plugin - nothing was installed.'), {expose: true});
|
|
58
|
+
if (version && manifest.version !== version) {
|
|
59
|
+
throw Object.assign(new Error(`The store now has ${slug} ${manifest.version} - reload the Marketplace and try again.`), {expose: true});
|
|
60
|
+
}
|
|
61
|
+
const result = await installPlugin(slug, {
|
|
62
|
+
tarball: parts.tarball, signature: parts.signature, publicKeyId: 'marketplace-1',
|
|
63
|
+
entitlement: parts.entitlement, allowUpgrade: update === true
|
|
64
|
+
});
|
|
65
|
+
return {action: result?.action ?? 'install', from: result?.from ?? null, version: result?.version ?? manifest.version};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* After a renewal: the store's fresh entitlement for the version installed, written beside it - only when it is
|
|
70
|
+
* that same version and the entitlement verifies. True when written.
|
|
71
|
+
*/
|
|
72
|
+
async function refreshEntitlement(slug, installedVersion) {
|
|
73
|
+
const parts = readEnvelope(await storeClient.downloadPlugin(slug));
|
|
74
|
+
const manifest = readManifestFromTarball(parts.tarball);
|
|
75
|
+
if (!parts.entitlement || manifest?.name !== slug || manifest.version !== installedVersion) return false;
|
|
76
|
+
if (classifyEntitlement(slug, parts.entitlement).state !== 'valid') return false;
|
|
77
|
+
await fs.writeFile(path.resolve('plugins', slug, '.entitlement.json'), JSON.stringify(parts.entitlement, null, 2) + '\n', 'utf8');
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** This site's public address, as the store will know it. */
|
|
82
|
+
async function siteAddress(request) {
|
|
83
|
+
return (await trustedOrigin(originOf(request)).catch(() => '')) || originOf(request);
|
|
84
|
+
}
|
|
22
85
|
|
|
23
86
|
export async function pluginMarketplaceRoutes(fastify) {
|
|
24
87
|
const adminOnly = { preHandler: [authenticate, requireAdmin] };
|
|
25
88
|
|
|
26
89
|
// GET /plugins/marketplace
|
|
27
|
-
fastify.get('/plugins/marketplace', adminOnly, async (
|
|
28
|
-
if (!isManaged())
|
|
90
|
+
fastify.get('/plugins/marketplace', adminOnly, async (request, reply) => {
|
|
91
|
+
if (!isManaged()) {
|
|
92
|
+
if (!storeClient.storeOpen()) return reply.send({ available: false, mode: storeClient.isOwnerInstance() ? 'owner' : 'none', catalogue: [] });
|
|
93
|
+
return reply.send(await storeCatalogue(request));
|
|
94
|
+
}
|
|
29
95
|
|
|
30
96
|
const catalogue = await fetchCatalogue();
|
|
31
97
|
|
|
@@ -66,7 +132,14 @@ export async function pluginMarketplaceRoutes(fastify) {
|
|
|
66
132
|
return reply.code(400).send({ error: 'Invalid version format. Expected semver (e.g. 1.0.0).' });
|
|
67
133
|
}
|
|
68
134
|
if (!isManaged()) {
|
|
69
|
-
return reply.code(503).send({ error: 'This site
|
|
135
|
+
if (!storeClient.storeOpen()) return reply.code(503).send({ error: 'This site has no store - install from a file instead.' });
|
|
136
|
+
try {
|
|
137
|
+
const r = await installFromStore(slug, { version, update: update === true });
|
|
138
|
+
return reply.send({ success: true, name: slug, ...r, restartRequired: true });
|
|
139
|
+
} catch (err) {
|
|
140
|
+
fastify.log.warn({ err }, '[marketplace] store install failed');
|
|
141
|
+
return reply.code(err instanceof storeClient.StoreError ? err.status : 400).send({ error: installError(err), code: err.code || null });
|
|
142
|
+
}
|
|
70
143
|
}
|
|
71
144
|
|
|
72
145
|
let bundle;
|
|
@@ -110,14 +183,171 @@ export async function pluginMarketplaceRoutes(fastify) {
|
|
|
110
183
|
// `expose` marks a refusal written for the admin (a plugin that needs
|
|
111
184
|
// a newer CMS, naming both versions) - it used to arrive here as a
|
|
112
185
|
// bare "Installation failed." while Install from file showed it.
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
186
|
+
return reply.code(400).send({ error: installError(err) });
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
// ── The self-hosted store ─────────────────────────────────────────────
|
|
191
|
+
|
|
192
|
+
/** The store's catalogue in the shape the screen already reads, plus where purchases stand. */
|
|
193
|
+
async function storeCatalogue(request) {
|
|
194
|
+
const plugins = await discoverPlugins();
|
|
195
|
+
const installed = new Map(plugins.map(m => [m.name, m.version]));
|
|
196
|
+
const cmsVersion = installedCmsVersion();
|
|
197
|
+
const site = await siteAddress(request);
|
|
198
|
+
// Connected to Domma: pick up what was bought for this site since last time (a failure just waits for next time).
|
|
199
|
+
await storeClient.syncConnection().catch(() => {});
|
|
200
|
+
let answer;
|
|
201
|
+
try {
|
|
202
|
+
answer = await storeClient.fetchStoreCatalogue(site, storeClient.heldKeys([...installed.keys()]));
|
|
203
|
+
} catch (err) {
|
|
204
|
+
return { available: true, mode: 'store', failed: err.message, site: null, catalogue: [], pending: [], connection: storeClient.connectionView() };
|
|
205
|
+
}
|
|
206
|
+
const pending = storeClient.pendingPurchases().map(p => ({ id: p.id, slug: p.slug, kind: p.kind, startedAt: p.startedAt }));
|
|
207
|
+
const catalogue = (Array.isArray(answer.plugins) ? answer.plugins : []).filter(e => SLUG.test(e?.slug || '')).map(e => {
|
|
208
|
+
const have = installed.get(e.slug);
|
|
209
|
+
const owned = e.tier !== 'paid' || e.owned === true;
|
|
210
|
+
return {
|
|
211
|
+
...e,
|
|
212
|
+
store: true,
|
|
213
|
+
entitled: owned && e.updates !== false,
|
|
214
|
+
// The words the screen already has: licensed, support ended, or not bought.
|
|
215
|
+
licenceStatus: e.tier !== 'paid' ? null : e.owned ? (e.updates ? 'active' : 'support-ended') : 'none',
|
|
216
|
+
installed: have !== undefined,
|
|
217
|
+
installedVersion: have ?? null,
|
|
218
|
+
// Already licensed here by other means (an owner licence, a file bought by email): nothing to buy.
|
|
219
|
+
licensedHere: have !== undefined && e.tier === 'paid' && LOADS.has(classifyEntitlement(e.slug).state),
|
|
220
|
+
updatable: have !== undefined && compareVersions(e.version, have) > 0,
|
|
221
|
+
needsCms: cmsVersionShortfall({ name: e.slug, displayName: e.displayName, minCmsVersion: e.minCmsVersion }, cmsVersion),
|
|
222
|
+
pending: pending.find(p => p.slug === e.slug)?.id ?? null
|
|
223
|
+
};
|
|
224
|
+
});
|
|
225
|
+
return { available: true, mode: 'store', site: answer.site || null, siteError: answer.siteError || null, cmsVersion, catalogue, pending, connection: storeClient.connectionView() };
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// POST /plugins/marketplace/buy {slug} -> {id, checkoutUrl}
|
|
229
|
+
fastify.post('/plugins/marketplace/buy', adminOnly, async (request, reply) => {
|
|
230
|
+
const slug = request.body?.slug;
|
|
231
|
+
if (!SLUG.test(String(slug || ''))) return reply.code(400).send({ error: 'Invalid plugin slug.' });
|
|
232
|
+
if (isManaged() || !storeClient.storeOpen()) return reply.code(409).send({ error: 'This site does not buy from the Domma store.' });
|
|
233
|
+
const user = await getUserById(request.user?.id);
|
|
234
|
+
if (!user?.email) return reply.code(400).send({ error: 'Your account has no email address for the receipt and licence key - add one under My profile.' });
|
|
235
|
+
try {
|
|
236
|
+
const out = await storeClient.startPurchase({ site: await siteAddress(request), slug, email: user.email, name: user.name || user.displayName || '' });
|
|
237
|
+
return reply.send(out);
|
|
238
|
+
} catch (err) {
|
|
239
|
+
return reply.code(err instanceof storeClient.StoreError ? err.status : 500).send({ error: err instanceof storeClient.StoreError ? err.message : 'The purchase could not be started.' });
|
|
118
240
|
}
|
|
119
241
|
});
|
|
120
242
|
|
|
243
|
+
// POST /plugins/marketplace/claim {id} -> {status, slug, installed?}
|
|
244
|
+
// Paid for a first purchase: installed here and then (the admin restarts to switch it on).
|
|
245
|
+
fastify.post('/plugins/marketplace/claim', adminOnly, async (request, reply) => {
|
|
246
|
+
const id = String(request.body?.id || '');
|
|
247
|
+
if (!/^[0-9a-f-]{36}$/i.test(id)) return reply.code(400).send({ error: 'Which purchase?' });
|
|
248
|
+
// One claim at a time: two tabs asking at once must not both install it.
|
|
249
|
+
const run = claimChain.then(() => claimAndInstall(id), () => claimAndInstall(id));
|
|
250
|
+
claimChain = run.catch(() => {});
|
|
251
|
+
const r = await run;
|
|
252
|
+
return reply.code(r.code || 200).send(r.body);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
let claimChain = Promise.resolve();
|
|
256
|
+
async function claimAndInstall(id) {
|
|
257
|
+
let out;
|
|
258
|
+
try {
|
|
259
|
+
out = await storeClient.claimPurchase(id);
|
|
260
|
+
} catch (err) {
|
|
261
|
+
return { code: err instanceof storeClient.StoreError ? err.status : 500, body: { error: err instanceof storeClient.StoreError ? err.message : 'The purchase could not be checked.' } };
|
|
262
|
+
}
|
|
263
|
+
if (out.status !== 'paid') return { body: out };
|
|
264
|
+
const main = await installClaimed(out);
|
|
265
|
+
// What came free with it (the MongoDB Adapter with Email): installed beside it, the same way.
|
|
266
|
+
const included = [];
|
|
267
|
+
for (const slug of out.included || []) {
|
|
268
|
+
const r = await installClaimed({ ...out, slug, kind: out.kind, included: [] });
|
|
269
|
+
included.push({ slug, installed: r.body.installed === true, installError: r.body.installError || null });
|
|
270
|
+
}
|
|
271
|
+
return { code: main.code, body: { ...main.body, included } };
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** One claimed Tool onto this site: installed, or its licence written beside a copy already here. */
|
|
275
|
+
async function installClaimed(out) {
|
|
276
|
+
const here = (await discoverPlugins()).find(m => m.name === out.slug);
|
|
277
|
+
if (here) {
|
|
278
|
+
// Already on disk (a renewal, or a copy that was here before it was bought): the licence file the purchase
|
|
279
|
+
// brings is written beside it - at once when the store has that same version; a newer one waits for Update.
|
|
280
|
+
const refreshed = await refreshEntitlement(out.slug, here.version).catch(() => false);
|
|
281
|
+
if (refreshed || out.kind === 'renew') return { body: { ...out, installed: true, refreshed } };
|
|
282
|
+
// A first purchase over an older copy: bring it up to the store's version, which carries the licence.
|
|
283
|
+
try {
|
|
284
|
+
const r = await installFromStore(out.slug, { update: true });
|
|
285
|
+
return { body: { ...out, installed: true, refreshed: true, ...r, restartRequired: true } };
|
|
286
|
+
} catch (err) {
|
|
287
|
+
fastify.log.warn({ err }, '[marketplace] update after purchase failed');
|
|
288
|
+
return { body: { ...out, installed: true, refreshed: false, installError: installError(err) } };
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
if (out.kind === 'renew') return { body: { ...out, installed: false } };
|
|
292
|
+
try {
|
|
293
|
+
const r = await installFromStore(out.slug);
|
|
294
|
+
return { body: { ...out, installed: true, ...r, restartRequired: true } };
|
|
295
|
+
} catch (err) {
|
|
296
|
+
fastify.log.warn({ err }, '[marketplace] install after purchase failed');
|
|
297
|
+
// Paid and the key kept: Install on the Browse tab tries again, and the email has the file.
|
|
298
|
+
return { body: { ...out, installed: false, installError: installError(err) } };
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// POST /plugins/marketplace/key {slug, key} -> {owned}
|
|
303
|
+
// A key bought on the public /marketplace page (or found in the email): kept, then checked with the store.
|
|
304
|
+
fastify.post('/plugins/marketplace/key', adminOnly, async (request, reply) => {
|
|
305
|
+
if (isManaged() || !storeClient.storeOpen()) return reply.code(409).send({ error: 'This site does not buy from the Domma store.' });
|
|
306
|
+
const slug = String(request.body?.slug || '');
|
|
307
|
+
try {
|
|
308
|
+
const before = storeClient.readState().keys[slug];
|
|
309
|
+
await storeClient.saveKey(slug, request.body?.key);
|
|
310
|
+
const answer = await storeClient.fetchStoreCatalogue(await siteAddress(request), storeClient.heldKeys([slug]));
|
|
311
|
+
const row = (answer.plugins || []).find(e => e.slug === slug);
|
|
312
|
+
if (!row?.owned) {
|
|
313
|
+
// Not this site's key for this Tool: put back what was there.
|
|
314
|
+
await storeClient.changeState(s => { if (before) s.keys[slug] = before; else delete s.keys[slug]; });
|
|
315
|
+
return reply.code(400).send({ error: answer.siteError || `That key is not a licence for this Tool on ${answer.site || 'this site'}. A licence is for the address it was bought for.` });
|
|
316
|
+
}
|
|
317
|
+
return reply.send({ owned: true, updates: row.updates === true });
|
|
318
|
+
} catch (err) {
|
|
319
|
+
return reply.code(err instanceof storeClient.StoreError ? err.status : 500).send({ error: err instanceof storeClient.StoreError ? err.message : 'The key could not be checked.' });
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// Connect to Domma: POST {email?} starts it (a link goes to that email), POST /check asks, DELETE disconnects.
|
|
324
|
+
fastify.post('/plugins/marketplace/connect', adminOnly, async (request, reply) => {
|
|
325
|
+
if (isManaged() || !storeClient.storeOpen()) return reply.code(409).send({ error: 'This site does not buy from the Domma store.' });
|
|
326
|
+
const email = String(request.body?.email || '').trim() || (await getUserById(request.user?.id))?.email || '';
|
|
327
|
+
try {
|
|
328
|
+
return reply.send({ connection: await storeClient.startConnection(await siteAddress(request), email) });
|
|
329
|
+
} catch (err) {
|
|
330
|
+
return reply.code(err instanceof storeClient.StoreError ? err.status : 500).send({ error: err instanceof storeClient.StoreError ? err.message : 'Could not connect.' });
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
fastify.post('/plugins/marketplace/connect/check', adminOnly, async (_request, reply) => {
|
|
334
|
+
try {
|
|
335
|
+
return reply.send({ connection: await storeClient.syncConnection() });
|
|
336
|
+
} catch (err) {
|
|
337
|
+
return reply.code(err instanceof storeClient.StoreError ? err.status : 500).send({ error: err instanceof storeClient.StoreError ? err.message : 'Could not check.' });
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
fastify.delete('/plugins/marketplace/connect', adminOnly, async (_request, reply) => {
|
|
341
|
+
await storeClient.forgetConnection();
|
|
342
|
+
return reply.send({ success: true });
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
// DELETE /plugins/marketplace/purchases/:id - stop waiting (nothing is cancelled at the store).
|
|
346
|
+
fastify.delete('/plugins/marketplace/purchases/:id', adminOnly, async (request, reply) => {
|
|
347
|
+
await storeClient.dropPurchase(String(request.params.id || ''));
|
|
348
|
+
return reply.send({ success: true });
|
|
349
|
+
});
|
|
350
|
+
|
|
121
351
|
// DELETE /plugins/marketplace/:slug
|
|
122
352
|
fastify.delete('/plugins/marketplace/:slug', adminOnly, async (request, reply) => {
|
|
123
353
|
const { slug } = request.params;
|
|
@@ -332,6 +332,8 @@ export function buildGitignore({sitePlugins = [], pluginContent = {}, sitePublic
|
|
|
332
332
|
// Each admin's own sidebar Frequent list (keyed by user id).
|
|
333
333
|
'/content/sidebar/frequent/',
|
|
334
334
|
'/config/connections.json*',
|
|
335
|
+
// The Domma store's licence keys and payment claims (services/storeClient.js).
|
|
336
|
+
'/config/store.json*',
|
|
335
337
|
'/config/keys/',
|
|
336
338
|
'/plugins/*/data/*.jsonl',
|
|
337
339
|
'/plugins/*/backups/',
|