Skip to content

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

3.4 KB
Raw Blame
import { AuthManagerSnapshot } from "@server/core/link-manager/auth-manager/auth-manager"
import { LinkManagerSnapshot } from "@server/core/link-manager/link-manager"
import { type AuthenticationState } from "@server/core/authentication/authentication"
import { type SignUpResponse } from "@server/core/link-manager/link-manager"
import { type Appearance, type DesktopPreferences, type DesktopPreferencesUpdate, type ResolvedDesktopPreferences } from "@phreshos/core"
import { Forward } from "@the-link/core/decorators"
import { TheLink } from "@the-link/core"
import { Property } from "@the-link/core"
import Application from "../application"
import { v4 as uuidv4 } from "uuid"
import { type RequestOutcome, unwrap } from "@libs/request-outcome"
import DesktopPreferencesState from "./desktop-preferences-state"

export default class LinkManager extends TheLink {

    public readonly application: Application

    public readonly sourceLink: TheLink

    /** Immediate unresolved System Appearance and future authoritative updates. */
    public readonly appearance: Property<Appearance>

    /** What this Desktop's owner chose, what it resolves to, and their future changes. */
    public readonly desktopPreferences: DesktopPreferencesState

    public constructor(application: Application, sourceLink: TheLink, payload: LinkManagerSnapshot, desktopPreferences: DesktopPreferences, resolvedDesktopPreferences: ResolvedDesktopPreferences) {

        super()

        this.application = application

        this.sourceLink = sourceLink

        this.subscribeTo(this.sourceLink)

        this.appearance = Property.consumer(this, payload.appearance.key, payload.appearance.value)

        this.desktopPreferences = new DesktopPreferencesState(desktopPreferences, resolvedDesktopPreferences)
    }

    public requestDesktopPreferences(preferences: DesktopPreferencesUpdate) {
        return this.$inbound.publish("/change-desktop-preferences", preferences)
    }

    public updateDesktopPreferences(preferences: DesktopPreferences, resolved: ResolvedDesktopPreferences) {
        return this.desktopPreferences.update(preferences, resolved)
    }

    @Forward("outbound")
    public async forwardToSession(event: string, ...values: unknown[]) {

        const responseUuid = uuidv4()

        await this.sourceLink.$outbound.publish(event, responseUuid, ...values)

        const response = await this.sourceLink.$inbound.waitFirst<RequestOutcome<unknown[]>>(responseUuid)

        return unwrap(response)
    }

    // A transport emission with no response address. Used for private facts
    // whose producer must never wait for, retry, or observe host handling.
    public emitToSession(event: string, ...values: unknown[]) {

        this.sourceLink.$outbound.publish(event, null, ...values).catch(() => undefined)
    }

    public async authenticationState() {

        return await this.$outbound.publishFirst<AuthenticationState>("/owner/state")
    }

    public async signUp(username: string, password: string) {

        return await this.$outbound.publishFirst<SignUpResponse>("/owner/sign-up", username, password)
    }

    public async signIn(username: string, password: string) {

        return await this.$outbound.publishFirst<boolean>("/owner/sign-in", username, password)
    }

    public async sessionAuthenticate(token: string | null) {

        return await this.$outbound.publishFirst<[string, AuthManagerSnapshot] | false>("/session-authenticate", token)
    }
}