Activity modules
An activity module is a package a server owner installs under modules/: a game or an app that brings its own client script and stylesheet, its own server side, its own strings, its own database tables and its own deploy step. It appears in the Activities picker, and an app opens from there in a tab of its own (a window of its own in the desktop app).
The difference from a plugin is what a module is allowed to do. A plugin is sandboxed and can only use the API you grant it; a module is a first-class part of the server: its server code runs inside api.php, it may keep tables in the database, and its script draws in the app's own page. That is what it takes to be a whole app, and it is why only the server owner installs one.
To run one, put its folder (the one holding its module.json) in the server's modules/<id>/ folder; the next release links it in and runs the module's own deploy/setup.sh if it has one. No deploy fetches anything, so a module is installed by hand and updates the same way. The live service watches that folder, so installing or updating one is noticed by every open app at once, with nothing to restart. The reference for authoring one is modules/README.md: the manifest (including read, the actions a browser may fetch with GET), the server contract (actions, live, migrate), the window.HubActivity runtime a module's script is handed, and how to be loaded again when its files change.
A module is not sandboxed. Install only what you trust.
What a server offers, and to whom
The server's own decisions are in data/modules.json, which The Hub writes out with open defaults the first time anything asks for it and reads on every request, so editing it takes effect at once:
{ "activities": true, "access": "everyone", "developers": [], "modules": { "cc": { "enabled": false } } }
activities: false turns off the Activities picker and everything behind it; access: "developers" (with developers, the usernames that count; the server's owners always do) restricts it to those few; and modules overrides that per module, either way. A module an account may not use is not offered to it at all, and every action it declares answers 403, so it is hidden rather than merely greyed out.
What a module looks like
modules/<id>/
module.json the manifest (below)
<main>.js the client script (module.json: main)
<styles>.css its stylesheet (module.json: styles)
<strings>.json its rows and patterns (module.json: i18n)
<server>.php its server side (module.json: server), answering with a contract (below)
deploy/setup.sh optional: the module's own deploy step, run by deploy/modules.sh
module.json
| field | ||
|---|---|---|
id | required | must equal the folder name; the module's namespace (^[a-z0-9][a-z0-9-]{0,39}$) |
name | required | what the picker calls it (≤60) |
version | required | bump it to update; also the cache key for the client files, and what makes a migration run again |
author, description | ≤60 / ≤300 | |
app | true: it opens full screen from the Activities picker, anywhere | |
icon, players, blurb | the picker card | |
main | required | its client script, a plain .js inside the module |
styles, i18n, server | its stylesheet, strings and server side, plain files inside the module | |
read | the actions that may be called with GET (no CSRF token): only what truly reads | |
config | the constants it reads from config.local.php, for documentation and setup | |
enabled | false hides it without uninstalling it |
A manifest that breaks any rule is ignored as a whole, like a plugin's.
The server side
server/…php is required once by lib/modules.php (it is never a request target: modules/.htaccess and deploy/nginx-thehub.conf both refuse to run anything in here) and answers with its contract:
return [
// api.php?action=<name>; fn($action) answers with out(). Names must be prefixed with the module's id.
'actions' => ['cc_me' => 'cc_action', 'cc_art' => 'cc_action'],
// who hears about a change, for lib/live.php: fn($action, $channel_id, $my_user_id, $input) → user ids
'live' => ['cc_share' => 'cc_live_share'],
// the tables it keeps in The Hub's database: fn(PDO $pdo, bool $mysql), idempotent, once per module version
'migrate' => 'cc_migrate',
];
It runs after The Hub's own code, so it may use its helpers exactly as a lib/*.php file does (q, q1, x, fail, uid, int_in, str_in, out, input, now_ms, safe_fetch, require_channel, channel_can, is_blocked, live_channel_people, add_column_once, …).
Keep a module's tables prefixed with its id (cc_players); they live in The Hub's database alongside everyone else's, and the module's migration is what creates them.
The client side
The Hub loads a module's strings, stylesheet and script the first time sync mentions it, then the script talks to the page through window.HubActivity (assets/activity-modules.js). The helpers it hands over — api, esc, ic, toast, md, confirmModal, openModal, modalHead, lookupUser, avatar, syncNow, currentChannel, findMsg, motionReduced, durText, tone, sfxOut, mergeMessages, renderMessages, Prefs, S, I18N, Activities, MsgEmbeds — are the app's own, so a module is written exactly as one of the app's scripts is. It adds two of its own (register, listen), beside the three the app itself calls (available, action, openUrl):
const { api, esc, ic, toast, S, I18N, Activities, MsgEmbeds, register, listen } = window.HubActivity;
register({
id: 'cc',
views: ['home', 'shop'], // what ?app=cc&view=<view> accepts
open: () => openMyApp(), // the picker card
openView: view => openMyApp(view),
actions: { 'my-thing': t => doThing(t) }, // data-act names the app's delegate doesn't know
embeds: { cc_card: ccEmbedCard }, // the MsgEmbeds types it draws
unload: () => takeMyAppOut(), // everything it added itself, undone (below)
});
register(def) is the one place a module sets itself up: what it opens, the data-act names it answers, the message embeds it draws (messages.embed is The Hub's generic payload column; a type nothing knows is simply not drawn). The picker's wording comes from module.json, not from register. ?app=<id>&view=<view> opens it from a link, a notification or the Android app.
A module may be run again. The Hub stamps each module's files (lib/modules.php, module_stamp) and sends the stamp in every sync; when it changes — an update installed on the server, and the live service tells the open apps to sync at once — the page loads the new file and runs it again, and a module that is no longer offered to that account is taken back out. So anything a module adds outside its own markup goes through listen(target, type, fn) rather than target.addEventListener, and anything else it adds itself — an overlay, a timer, an animation loop — is undone in unload(). The Hub takes out what it can see (its actions, its embeds, its listeners, the <link> and <script> it loaded); the rest is the module's own to clean.
A module is not sandboxed. Its script runs in the page with the app's own scope, and its server file runs inside api.php. That is deliberate — it is how a module can be a full app — but it means a module is trusted code, unlike a plugin (plugins/, which runs in a sandboxed iframe behind a permission list). Install only what you trust.
Turning one off, and who may use it
Decisions of that kind are this Hub's, not the module's, so they live in data/modules.json — beside the database, written by The Hub itself the first time anything asks for it, and read on every request, so editing it takes effect at once with nothing to restart. It is not in the repository: what a server allows is that server's business.
{
"activities": true,
"access": "developers",
"developers": ["ari", "raytroll"],
"modules": {
"cc": { "enabled": false },
"quiz": { "access": "everyone" }
}
}
| key | ||
|---|---|---|
activities | true | everything off in one word: the Activities picker, and every action a module offers |
access | "everyone" | who may use them: "everyone", or "developers" |
developers | [] | whose usernames count as developers, when access is "developers" |
modules | {} | per module: {"<id>": {"enabled": false, "access": "developers"}} |
The Hub's owners (is_vl_owner) are always developers; developers adds the others by username. A module's own access overrides the Hub's, and its enabled has to be true as well — so a Hub that is otherwise closed can open one module to everyone, and one module can stay off while it is being worked on. A module nobody may reach is not offered at all: it is not in the sync, the picker does not list it, its client files are never fetched, and every action it declares answers 403 (its read actions too), so a page that already has its script cannot use it either. Switching activities off, or one module off, goes further than that: its server file is not loaded at all and the action is not even found — which is what makes "off" mean none of it runs. enabled: false in a module's own module.json hides it on every Hub, config or no config.
Anything in the file that is not understood is ignored rather than fatal — an unknown key, an "<id>" that is not a module id, an "access" that is neither of the two — and a file that is not valid JSON falls back to the defaults with a line in the PHP error log, so a Hub with a bad config still serves its modules.
deploy/live/live.js watches the modules folder: a module installed or updated there is noticed where it matters, because every open app is told to sync — and the sync carries the new stamps, so the page runs what changed and drops what is gone, with nothing to restart. Editing this config needs no watcher: it is read on every request, so the next sync (and every action, at once) already follows it.
Installing
A module is a folder in /srv/thehub/shared/modules/<id>/ — the same place plugins live, and for the same reason: it survives releases. Put the package (the folder holding its module.json) there and it is installed: the next deploy/release.sh links the release's modules/ to that folder, and deploy/modules.sh runs the module's own deploy/setup.sh if it has one. Removing it is deleting the folder.
No deploy fetches anything. A module is put there by whoever runs the server; no release clones a repository or needs a key for one, so a module with a private home is installed the same way as any other.
The package is whatever folder holds the manifest, so the simplest shape is a module/ folder inside the project it belongs to — which is what Character Collector does (CharacterCollector, module/README.md).
From a .zip
A module's project usually ships one on its release page: a .zip holding the module's folder — character-collector-module-1.1.0/module/…, or module/… at the top. Two ways in, and both are the same code (lib/modules.php, module_install_zip):
# now, by hand
php tools/module-install.php character-collector-module-1.1.0.zip
php tools/module-install.php module.zip --replace # the same version again (a rebuilt release)
# or drop it in the modules folder and let the next deploy install it
scp character-collector-module-1.1.0.zip server:/srv/thehub/shared/modules/
The folder inside the archive becomes modules/<id>/. Everything in the archive must be inside that folder — a ZIP of a whole repository is refused, and the file it names is in the message — and only the file types a module may ship are written (php, js, css, json, md, sql, sh, service, images, fonts: MODULE_FILE_TYPES). A different version replaces what is there; the same version is left alone unless you ask for --replace. It is unpacked beside its folder and checked in full before it is moved into place, so a half-written module is never live. Nothing under modules/ is ever executed by the web server.
A module is code The Hub runs (lib/modules.php), so install only what you trust.