Something went wrong. Try again.
A self-hosted web platform for applications & services with design based on Google's Material 3 Expressive. (Work In Progress)
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275import { existsSync as fsExistsSync, promises as fs, readFileSync as fsReadFileSync,} from "node:fs";import path from "node:path";import type { Instance } from "../index.ts";import System from "../system.ts";
export enum WorkspacesFeatureFlags { SlashCommands = "slash_commands", ShootYourselfInTheFoot = "shoot_yourself_in_the_foot", AllowUserSignups = "allow_user_signups", ExperimentalTerminalGui = "experimental_terminal_gui", DisplayProfilesAtLogon = "display_profiles_at_logon"}
export const FEATURE_FLAG_DESCRIPTIONS = { [WorkspacesFeatureFlags.SlashCommands]: "Enable the ability to use slash commands in the instance's console", [WorkspacesFeatureFlags.ShootYourselfInTheFoot]: "Allow administrators to alter settings / configuration options which may cause the instance to malfunction. (Only enable this if you are sure you know what you are doing!)", [WorkspacesFeatureFlags.AllowUserSignups]: "Allow new users to create accounts from the instance user login page",};
export default class ConfigurationSystem extends System { isDevMode: boolean = true; databases: { postgres: { user: string; password: string; host: string; port: number; database: string; }; } = { postgres: { user: "postgres", password: "postgres", host: "localhost", port: 5432, database: "onlineworkspace_workspace", }, }; proxy: { secure: boolean; hostname: string } = { secure: true, hostname: "localhost", }; enabledFeatures: (WorkspacesFeatureFlags | string)[] = [ WorkspacesFeatureFlags.SlashCommands, ]; signupRequirements: { email: boolean; twoFactorAuthentication: boolean; passwordMinimumLength?: number; passwordContains?: { minimumUppercase?: number; minimumLowercase?: number; minimumNumbers?: number; minimumSymbols?: number; }; } = { email: false, twoFactorAuthentication: false, passwordMinimumLength: 5, passwordContains: { minimumLowercase: 1, minimumNumbers: 1, minimumSymbols: 1, minimumUppercase: 1, }, }; displayName: string; mailServer: { host: string; port: number; secure: boolean; auth: { user: string; pass: string; }; }; termsOfUse: { message: string; lastUpdated: number }; defaultQuickShortcuts: string[] = [ "uk.ewsgit.dashboard", "uk.ewsgit.store", "uk.ewsgit.settings", "uk.ewsgit.photos", "uk.ewsgit.files", ]; defaultApplications: { id: string; uri: string }[] = [ { id: "uk.ewsgit.dashboard", uri: "local:uk.ewsgit.dashboard" }, { id: "uk.ewsgit.store", uri: "local:uk.ewsgit.store" }, { id: "uk.ewsgit.settings", uri: "local:uk.ewsgit.settings" }, { id: "uk.ewsgit.photos", uri: "local:uk.ewsgit.photos" }, { id: "uk.ewsgit.files", uri: "local:uk.ewsgit.files" }, ]; userDefault: { homeDirectories: string[]; quotaSize: number; displayNameFormat: string; } = { homeDirectories: ["Documents", "Photos", "Videos", "Projects"], quotaSize: 1024 * 1024 * 10, displayNameFormat: "New User %num%", }; caddyfile: string | undefined = "../Caddyfile"; apiPort: number = 3563;
constructor(instance: Instance) { super("configuration", instance);
if (process.env.POSTGRES_DATABASE_HOST) { this.databases.postgres.host = process.env.POSTGRES_DATABASE_HOST; } this.displayName = "Workspace"; this.mailServer = { host: "smtp.example.com", port: 587, secure: true, auth: { user: "user", pass: "password", }, }; this.termsOfUse = { message: `1. Acceptance of Terms - By logging in, you agree to these rules. If you do not agree, please do not use the service.2. Account Security - You are the gatekeeper of your account. Keep your password private, as you are responsible for all activity that happens under your login.3. Content Ownership - What is yours remains yours. We claim no ownership over the files, photos, or data you upload to this instance.4. Acceptable Use - Do not use this space for anything illegal, malicious, or harmful. This includes uploading malware or attempting to disrupt the service for others.5. Privacy and Access - We value your privacy. We will not access your stored data unless it is strictly necessary for technical support or required by legal authorities.6. Storage and Maintenance - While we strive for 100% uptime, this service is provided "as is." We may occasionally perform maintenance that results in temporary downtime.7. Personal Responsibility - Hardware and software can fail. You agree to maintain your own external backups of any mission-critical data. We are not liable for data loss.8. Termination - We reserve the right to suspend or close accounts that violate these terms or compromise the security of the server.9. Policy Updates - These terms may change. If we make significant updates, we will post a notification within the app or send an email.`, lastUpdated: Date.now(), }; if ( fsExistsSync( path.join( this.instance.sys.filesystem.AUTO_INSTALL_PATH, "configuration.json", ), ) ) { this.log.info( "Auto-install configuration detected. Loading configuration from auto-install.", );
const autoInstallConfig = JSON.parse( fsReadFileSync( path.join( this.instance.sys.filesystem.AUTO_INSTALL_PATH, "configuration.json", ), ).toString(), );
for (const key of Object.keys(autoInstallConfig)) { if (key in this) { // @ts-ignore this[key] = autoInstallConfig[key]; } } } }
hasFeature(feature: WorkspacesFeatureFlags | string): boolean { return !!this.enabledFeatures.find((f) => f === feature); }
async enableFeature(feature: WorkspacesFeatureFlags | string) { if (!this.enabledFeatures.includes(feature)) { this.enabledFeatures.push(feature); }
await this.saveConfiguration();
return true; }
async disableFeature(feature: WorkspacesFeatureFlags | string) { this.enabledFeatures = this.enabledFeatures.filter((feat) => feat !== feature );
await this.saveConfiguration();
return true; }
async saveConfiguration(): Promise<boolean> { const CONFIGURATION_FILE_PATH = path.join( this.instance.sys.filesystem.FS_ROOT, "configuration.json", );
const allowedProperties = (Object.keys(this) as (keyof ConfigurationSystem)[]).filter( (property) => { return typeof this[property] !== "function" && property !== "instance" && property !== "id" && property !== "log"; }, );
const configurationFileContents: Record<string, unknown> = {};
for (const propertyKey of allowedProperties) { console.log(propertyKey); configurationFileContents[propertyKey] = this[propertyKey]; }
await fs.writeFile( CONFIGURATION_FILE_PATH, JSON.stringify(configurationFileContents, null, 2), );
return true; }
override async startup(): Promise<boolean> { const CONFIGURATION_FILE_PATH = path.join( this.instance.sys.filesystem.FS_ROOT, "configuration.json", );
if (!fsExistsSync(CONFIGURATION_FILE_PATH)) { await this.saveConfiguration(); }
const configurationFile = JSON.parse( (await fs.readFile(CONFIGURATION_FILE_PATH)).toString(), );
const allowedProperties = (Object.keys(this) as (keyof ConfigurationSystem)[]).filter( (property) => { return typeof this[property] !== "function" && property !== "instance" && property !== "id" && property !== "log"; }, );
for (const propertyKey of allowedProperties) { if (propertyKey in configurationFile) { // @ts-expect-error We can ignore this as the properties which are read-only are already removed from the allowedProperties by this stage. this[propertyKey] = configurationFile[propertyKey]; } }
for ( const feature of Object.keys( WorkspacesFeatureFlags, ) as (keyof typeof WorkspacesFeatureFlags)[] ) { this.log.info( `Feature ${feature} -> ${ this.enabledFeatures.includes(WorkspacesFeatureFlags[feature]) }`, ); }
return true; }}