import { RoomPlugin } from '../plugins/RoomPlugin.js'; import { Connection } from './Connection.js'; import { IBroadcaster } from './IBroadcaster.js'; import { Message, MessageConstructorOptions } from './Message.js'; import { RoomManager } from './RoomManager.js'; import { Session } from './Session.js'; export type StoredRoom = { name: string; order: number; pluginGroupNames: string[]; isPrivate: boolean; whitelist: string[]; }; export type SanitizedRoom = { id: number; name: string; isPrivate: boolean; whitelist: string[]; lastReceivedMessageId: number; lastReceivedMessageTimestamp: number; plugins: { [pluginName: string]: unknown; }; }; export declare class Room implements IBroadcaster { /** * Base path for rooms persistent storage */ static readonly STORAGE_BASE_PATH: string; /** * Room tick duration */ private static TICK_INTERVAL; /** * Current global room id */ static CURRENT_ID: number; /** * Number of messages kept in memory */ static readonly MESSAGE_HISTORY_LENGTH = 1000; /** * Number of messages sent to clients that join the room. Must be lower than message history length. */ static readonly MESSAGE_HISTORY_VISIBLE_LENGTH = 25; /** * Room manager */ readonly manager: RoomManager; /** * This room's unique id */ readonly id: number; /** * Whether this room's access is based on an identifier whitelist */ isPrivate: boolean; /** * This room name */ name: string; /** * Room order in the list (only for public rooms) */ order: number; /** * List of enabled plugin groups */ pluginGroupNames: string[]; /** * Connections that are within this room */ connections: Connection[]; /** * History of the last messages */ messages: Message[]; /** * Only identifiers in this whitelist can access this room. Only used when isPrivate is set to true. */ whitelist: string[]; /** * Plugins. All plugin aliases points to the same command plugin instance. */ readonly commands: { [commandName: string]: RoomPlugin; }; /** * Plugins */ readonly plugins: RoomPlugin[]; constructor(manager: RoomManager, isPrivate?: boolean, id?: number); /** * Whether this room can accept a specific connection */ accepts(session: Session): boolean; /** * Allow an identifier to access this room (only for private rooms) * @param identifier */ allow(identifier: string): boolean; /** * Unallow an identifier to access this room (only for private rooms) * @param identifier */ unallow(identifier: string): boolean; /** * Get the sessions that have at least one connection within this room */ getSessions(): Session[]; /** * Get this room own storage path */ getStoragePath(): string; /** * Try to load this room's data from disk storage */ private load; /** * Save this room's data to disk */ private save; /** * Periodical cleanup/storage of this room data */ private tick; /** * Load last messages from the database */ loadLastMessagesFromDB(): Promise; /** * Detach a connection from this room * @param connection */ detachConnection(connection: Connection): void; /** * Attach a connection to this room * @param connection */ attachConnection(connection: Connection): Promise; /** * Send the history of last messages to a specific connection * @param connection */ sendHistory(connection: Connection, lastId?: number, count?: number): void; /** * * @param userId */ containsUser(userId: number): boolean; /** * Get a plugin instance by its name * @param name */ getPlugin(name: string): T | undefined; /** * Execute before room join hook * @param connection */ executeBeforeConnectionJoinedRoom(connection: Connection, room: Room): Promise; /** * Execute room join hook * @param connection */ executeConnectionJoinedRoom(connection: Connection): Promise; /** * Execute connection closed hook * @param connection */ executeOnConnectionLeftRoom(connection: Connection): Promise; /** * Execute on before mesasge broadcast hooks * @param message * @param connection */ executeOnBeforeMessageBroadcastHook(message: Message, connection?: Connection): Promise; /** * Send to all sessions * @param event * @param payload */ send(event: string, payload: unknown): void; /** * Find a message from history by its unique id. Try to load it from cache, or go find it in database if it does not exist. */ getMessageById(id: number): Promise; /** * Send a new message to the room */ getLastSentMessage(): Message | null; /** * Send a new message to the room * @param options */ sendMessage(options: MessageConstructorOptions & { connection?: Connection; }): Promise; /** * Clear message history */ clearHistory(): void; /** * Get metadata about this room */ sanitized(): SanitizedRoom; }