Skip to content

Commit

Permalink
CRISTAL-424: Provide a default content for new file system and nextcl…
Browse files Browse the repository at this point in the history
…oud wikis

Also support content initialization for nextcloud.
  • Loading branch information
manuelleduc committed Jan 27, 2025
1 parent c7c0c13 commit 142f7f3
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions core/backends/backend-nextcloud/src/nextcloudStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: Logger) {
super(logger, "storage.components.nextcloudStorage");
Expand All @@ -56,15 +57,14 @@ export class NextcloudStorage extends AbstractStorage {
}

async getPageContent(page: string): Promise<PageData | undefined> {
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(),
},
);

Expand Down Expand Up @@ -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);
}
}
}
}

0 comments on commit 142f7f3

Please sign in to comment.