diff --git a/core/backends/backend-nextcloud/src/nextcloudStorage.ts b/core/backends/backend-nextcloud/src/nextcloudStorage.ts index cdfc8a590..d0ff39d15 100644 --- a/core/backends/backend-nextcloud/src/nextcloudStorage.ts +++ b/core/backends/backend-nextcloud/src/nextcloudStorage.ts @@ -40,6 +40,7 @@ import type { UserDetails } from "@xwiki/cristal-authentication-api"; @injectable() export class NextcloudStorage extends AbstractStorage { private readonly ATTACHMENTS = "attachments"; + private initBaseContentCalled: boolean = false; constructor(@inject("Logger") logger: Logger) { super(logger, "storage.components.nextcloudStorage"); @@ -56,15 +57,14 @@ export class NextcloudStorage extends AbstractStorage { } async getPageContent(page: string): Promise { + await this.initBaseContent(); const baseRestURL = this.getWikiConfig().baseRestURL; - const headers = this.getBaseHeaders(); - try { const response = await fetch( `${baseRestURL}/${USERNAME}/.cristal/${page}/page.json`, { method: "GET", - headers, + headers: this.getBaseHeaders(), }, ); @@ -334,4 +334,36 @@ export class NextcloudStorage extends AbstractStorage { author: undefined, }; } + + private async initBaseContent() { + if (!this.initBaseContentCalled) { + this.initBaseContentCalled = true; + const baseRestURL = this.getWikiConfig().baseRestURL; + const headers = this.getBaseHeaders(); + try { + const res = await fetch(`${baseRestURL}/${USERNAME}/.cristal`, { + method: "GET", + headers, + }); + // if .cristal does not exist, initialize it with a default content. + if (res.status === 404) { + await this.save( + "home", + "# Welcome\n" + + "\n" + + "This is a new **Cristal** wiki.\n" + + "\n" + + "You can use it to take your *own* notes.\n" + + "\n" + + "You can also create new [[pages|home/newpage]].\n" + + "\n" + + "Enjoy!", + "", + ); + } + } catch (e) { + console.error("Failed to initialize Cristal default content", e); + } + } + } }