Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(pages): say publishing isn't available on self-hosted servers instead of asking to sign in (#4107)

Aditya Umale04da4426f58b
diff --git a/server/index.ts b/server/index.ts
index fcac530d48..f63fc70070 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -12261,7 +12261,7 @@ const { react } = createReactions({
store, reply: (input) => teamMessages.reactedIn(input.auth, input.threadId, input.target.id, input.answer, { sendId: input.sendId, sender: messageSender(input.auth) }) ?? personReplies.deliver({ ...input, about: "message" }),
answerCard: (input) => respondToCard({ ...input, via: "reaction" }), withdraw: personReplies.withdraw,
});
-const publishedPages = createPublishedPages({ dataDir: DATA_DIR, cloudHome: CLOUD_HOME, patch: (threadId, messageId, text, publishedPage) => void store.patchMessage(threadId, messageId, { text, publishedPage }), apps: { chat: chatApps, store } }); ROUTES.push(createReactionRoutes({ react, reactorFor, cloudGuestSendRefusal }), publishedPages.routes, savedSteps.routes, createPlacesElsewhereRoutes({ elsewhere: placesElsewhere, owner: (auth) => !sharedMembership() && !HOSTED_WORKSPACE && (auth.kind === "loopback" || ownHome.owner(auth) || (CLOUD_PERSONAL && cloudOwnerSession(auth))) }));
+const publishedPages = createPublishedPages({ dataDir: DATA_DIR, cloudHome: CLOUD_HOME, selfHosted: !CLOUD_HOME && !HOSTED_WORKSPACE && utilityParentPort === undefined, patch: (threadId, messageId, text, publishedPage) => void store.patchMessage(threadId, messageId, { text, publishedPage }), apps: { chat: chatApps, store } }); ROUTES.push(createReactionRoutes({ react, reactorFor, cloudGuestSendRefusal }), publishedPages.routes, savedSteps.routes, createPlacesElsewhereRoutes({ elsewhere: placesElsewhere, owner: (auth) => !sharedMembership() && !HOSTED_WORKSPACE && (auth.kind === "loopback" || ownHome.owner(auth) || (CLOUD_PERSONAL && cloudOwnerSession(auth))) }));
// `let` slots are read per request.
ROUTES.push(createThreadRoutes({
groupFor: personRooms.groupForAuth,
diff --git a/server/pages/client.ts b/server/pages/client.ts
index 55b1ff9afc..17d5ee4c02 100644
--- a/server/pages/client.ts
+++ b/server/pages/client.ts
@@ -28,6 +28,8 @@ interface Minted {
export const SIGN_IN_MESSAGE = "Publishing a page needs a MausBot account. Sign in to MausBot in the app's Settings, then ask again.";
const UNAVAILABLE = "Publishing isn't available right now. Try again in a few minutes.";
const NOT_TURNED_ON = "Publishing pages isn't turned on for this account yet.";
+/** A server with no desktop app to sign in through and no Cloud home (`openmausbot serve`): no Settings sign-in can help. */
+export const SELF_HOSTED_MESSAGE = "Publishing pages isn't available on self-hosted servers yet.";
const TOKEN_TIMEOUT_MS = 15_000, UPLOAD_TIMEOUT_MS = 60_000;
const LOOPBACK = new Set(["127.0.0.1", "localhost", "[::1]"]);
@@ -57,12 +59,15 @@ const readJson = async (response: Response): Promise<Record<string, unknown>> =>
const callSignal = (call: PagesCall, ms: number) => call.signal ? AbortSignal.any([call.signal, AbortSignal.timeout(ms)]) : AbortSignal.timeout(ms);
const said = (body: Record<string, unknown>, fallback: string) => typeof body.message === "string" && body.message.trim() ? body.message.trim().slice(0, 500) : fallback;
-export function createPagesClient(options: { access: () => PagesAccess | null; notOffered?: () => boolean; fetch?: typeof fetch; now?: () => number }) {
+export function createPagesClient(options: { access: () => PagesAccess | null; notOffered?: () => boolean; selfHosted?: boolean; fetch?: typeof fetch; now?: () => number }) {
const http = options.fetch ?? fetch, now = options.now ?? Date.now;
const mint = async (action: "create" | "update" | "delete", call: PagesCall, pageId?: string): Promise<Minted | PagesFailure> => {
const access = options.access();
- if (!access) return options.notOffered?.() ? fail("unavailable", NOT_TURNED_ON) : fail("sign_in", SIGN_IN_MESSAGE);
+ if (!access) {
+ if (options.notOffered?.()) return fail("unavailable", NOT_TURNED_ON);
+ return options.selfHosted ? fail("unavailable", SELF_HOSTED_MESSAGE) : fail("sign_in", SIGN_IN_MESSAGE);
+ }
const body = JSON.stringify(pageId ? { action, pageId } : { action });
let response: Response;
call.assertActive?.();
diff --git a/server/pages/published-pages.test.ts b/server/pages/published-pages.test.ts
index 462a8b7d19..d8b1a7b2dc 100644
--- a/server/pages/published-pages.test.ts
+++ b/server/pages/published-pages.test.ts
@@ -29,13 +29,13 @@ function writeSite(folder: string, files: Record<string, string> = SITE) {
}
const signIn = () => applyIncludedPagesMessage({ type: "openmausbot:included-pages", access: { url: FAKE_TOKEN_URL, token: FAKE_DESKTOP_TOKEN } });
-function setup(options: { home?: boolean; quota?: number } = {}) {
+function setup(options: { home?: boolean; quota?: number; selfHosted?: boolean } = {}) {
const service = fakePagesService({ quota: options.quota });
const work = join(dir, "work"), data = join(dir, "data");
mkdirSync(work, { recursive: true });
const cards: Array<{ threadId: string; id: string; text: string; card: PublishedPageCard }> = [];
let ids = 0;
- const pages = createPublishedPages({ dataDir: data, cloudHome: options.home ? FAKE_HOME : null, fetch: (input, init) => service.fetch(input, init),
+ const pages = createPublishedPages({ dataDir: data, cloudHome: options.home ? FAKE_HOME : null, selfHosted: options.selfHosted, fetch: (input, init) => service.fetch(input, init),
patch: (threadId, messageId, text, card) => { const shown = cards.find((each) => each.id === messageId && each.threadId === threadId); if (shown) Object.assign(shown, { text, card }); } });
const post = (threadId: string) => (text: string, card: PublishedPageCard) => { const id = `m${++ids}`; cards.push({ threadId, id, text, card }); return id; };
const publish = (input: { path: string; title?: string; pageId?: string; botId?: string; threadId?: string }) => pages.publisher.publish({
@@ -54,6 +54,15 @@ describe("publishing a page", () => {
expect(f.cards).toEqual([]);
});
+ it("says publishing isn't available on a self-hosted server, never \"sign in\", since it has no Settings sign-in to hand over", async () => {
+ const f = setup({ selfHosted: true });
+ writeSite(join(f.work, "site"));
+ const outcome = await f.publish({ path: "site" });
+ expect(outcome).toEqual({ status: 503, text: "Publishing pages isn't available on self-hosted servers yet." });
+ expect(f.service.minted).toEqual([]);
+ expect(f.cards).toEqual([]);
+ });
+
it("says publishing isn't turned on yet, never \"sign in\", when this signed-in desktop's Admin offers no pages", async () => {
const f = setup();
applyIncludedPagesMessage({ type: "openmausbot:included-pages", access: null, offered: false });
diff --git a/server/pages/service.ts b/server/pages/service.ts
index fc911e5ee0..4401b9a2d8 100644
--- a/server/pages/service.ts
+++ b/server/pages/service.ts
@@ -14,6 +14,8 @@ export function createPublishedPages(options: {
dataDir: string;
/** This server's Cloud home configuration, or null on a desktop or any other server. */
cloudHome: { adminOrigin: string; machineId: string; bootstrapSecret: string } | null;
+ /** True for `openmausbot serve`: no desktop app hands over a MausBot sign-in and there is no Cloud home. */
+ selfHosted?: boolean;
patch: (threadId: string, messageId: string, text: string, card: PublishedPageCard) => void;
fetch?: typeof fetch;
apps?: {
@@ -23,6 +25,7 @@ export function createPublishedPages(options: {
}) {
const store = new PublishedPagesStore(join(options.dataDir, "published-pages.json"));
const client = createPagesClient({ access: () => pagesAccess(options.cloudHome), notOffered: () => pagesNotOffered(options.cloudHome),
+ selfHosted: options.selfHosted === true,
...(options.fetch ? { fetch: options.fetch } : {}) });
const apps = options.apps;
const pageRow = (threadId: string, messageId: string) => apps?.store.messagesFor(threadId).find((message) => message.id === messageId && message.kind === "app");