Skip to content

An open-source, self-hosted system for apps built with web technologies.

4.0 KB
Raw Blame
import { ProgramRecord } from "@server/core/link-manager/auth-manager/program-manager/entry"
import { parsePermissions, type Launch, type Permissions, type ProgramInstallOptions, type ProgramUninstallOptions } from "@phreshos/core"
import ProgramManager from "./program-manager"

/**
 * A program this session knows about, rebuilt from what the core
 * represented. What it holds is what a program says of itself and what
 * is needed to draw it — never a path, because this side has no disk
 * and where the machine put things is not a program's own word.
 *
 * Running it is a request; the process appears when its start echoes.
 */
export default class Program {

    public readonly programManager: ProgramManager

    public readonly identity: string

    public readonly assetId: string

    /** Opaque identity of this runtime Program entity. */
    public readonly reference: string

    /** When the System made this runtime Program entity, as an ISO time. */
    public readonly createdAt: string

    /** When it was first installed, as an ISO time; `null` without an installed form. */
    public readonly installedAt: string | null

    public readonly name: string

    public readonly version: string

    public readonly description: string | null

    public readonly website: string | null

    public readonly categories: readonly string[]

    public readonly keywords: readonly string[]

    public readonly declaredPermissions: ProgramRecord["declaredPermissions"]

    public readonly hasAgent: boolean

    public readonly server: ProgramRecord["server"]

    public readonly client: ProgramRecord["client"]

    /** Effective authority synchronized with the authoritative Program state. */
    public permissions: Permissions

    /** The media types it opens, as its definition declares them. */
    public readonly opens: readonly string[]

    /** Whether it starts with the System. */
    public startup: boolean

    /** When it was pinned to the Taskbar, as an ISO time, which orders it there; `null` when it is not. */
    public pinnedAt: string | null

    public constructor(programManager: ProgramManager, payload: ProgramRecord) {

        this.programManager = programManager

        this.identity = payload.identity

        this.assetId = payload.assetId

        this.reference = payload.reference

        this.createdAt = payload.createdAt

        this.installedAt = payload.installedAt

        this.name = payload.name

        this.version = payload.version

        this.description = payload.description

        this.website = payload.website

        this.categories = payload.categories

        this.keywords = payload.keywords

        this.declaredPermissions = payload.declaredPermissions

        this.hasAgent = payload.hasAgent

        this.server = payload.server

        this.client = payload.client

        this.permissions = parsePermissions(payload.permissions)

        this.opens = payload.opens

        this.startup = payload.startup

        this.pinnedAt = payload.pinnedAt
    }

    public updatePermissions(permissions: unknown) {

        this.permissions = parsePermissions(permissions)
    }

    public async createProcess(launch: Launch = {}) {

        await this.programManager.$outbound.publish("/create-process", this.address, launch)
    }

    public async open() {

        await this.createProcess()
    }

    /** Keeps its place on the Taskbar, or gives it up. */
    public async pin(pinned: boolean) {

        await this.programManager.pinned(this.address, pinned ? "pin" : "unpin")
    }

    public install(options: ProgramInstallOptions = {}, asker = "") {

        return this.programManager.command(this.address, "install", options, asker)
    }

    public uninstall(options: ProgramUninstallOptions = {}, asker = "") {

        return this.programManager.command(this.address, "uninstall", options, asker)
    }

    public async forget(asker: string) {

        await this.programManager.forget(this.address, asker)
    }

    private get address() {

        return { identity: this.identity, reference: this.reference }
    }
}