2020-07-21 13:20:51 +00:00
|
|
|
/**
|
|
|
|
* Module to manage the clients of an ActiveDoc. It keeps track of how many clients have the doc
|
|
|
|
* open, and what FD they are using.
|
|
|
|
*/
|
|
|
|
|
2022-06-04 04:12:30 +00:00
|
|
|
import {CommDocEventType} from 'app/common/CommTypes';
|
2020-07-21 13:20:51 +00:00
|
|
|
import {arrayRemove} from 'app/common/gutil';
|
|
|
|
import {ActiveDoc} from 'app/server/lib/ActiveDoc';
|
|
|
|
import {Authorizer} from 'app/server/lib/Authorizer';
|
|
|
|
import {Client} from 'app/server/lib/Client';
|
|
|
|
import {sendDocMessage} from 'app/server/lib/Comm';
|
2020-09-11 20:27:09 +00:00
|
|
|
import {DocSession, OptDocSession} from 'app/server/lib/DocSession';
|
2021-10-25 13:29:06 +00:00
|
|
|
import {LogMethods} from "app/server/lib/LogMethods";
|
2020-07-21 13:20:51 +00:00
|
|
|
|
2021-10-07 23:46:22 +00:00
|
|
|
// Allow tests to impose a serial order for broadcasts if they need that for repeatability.
|
|
|
|
export const Deps = {
|
|
|
|
BROADCAST_ORDER: 'parallel' as 'parallel' | 'series',
|
|
|
|
};
|
|
|
|
|
2020-07-21 13:20:51 +00:00
|
|
|
export class DocClients {
|
|
|
|
private _docSessions: DocSession[] = [];
|
2021-10-25 13:29:06 +00:00
|
|
|
private _log = new LogMethods('DocClients ', (s: DocSession|null) => this.activeDoc.getLogMeta(s));
|
2020-07-21 13:20:51 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
public readonly activeDoc: ActiveDoc
|
|
|
|
) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of connected clients.
|
|
|
|
*/
|
|
|
|
public clientCount(): number {
|
|
|
|
return this._docSessions.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a client's open file to the list of connected clients.
|
|
|
|
*/
|
|
|
|
public addClient(client: Client, authorizer: Authorizer): DocSession {
|
|
|
|
const docSession = client.addDocSession(this.activeDoc, authorizer);
|
|
|
|
this._docSessions.push(docSession);
|
2021-10-25 13:29:06 +00:00
|
|
|
this._log.debug(docSession, "now %d clients; new client is %s (fd %s)",
|
2020-07-21 13:20:51 +00:00
|
|
|
this._docSessions.length, client.clientId, docSession.fd);
|
|
|
|
return docSession;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a client from the list of connected clients for this document. In other words, closes
|
|
|
|
* this DocSession.
|
|
|
|
*/
|
|
|
|
public removeClient(docSession: DocSession): void {
|
2021-10-25 13:29:06 +00:00
|
|
|
this._log.debug(docSession, "removeClient", docSession.client.clientId);
|
2020-07-21 13:20:51 +00:00
|
|
|
docSession.client.removeDocSession(docSession.fd);
|
|
|
|
|
|
|
|
if (arrayRemove(this._docSessions, docSession)) {
|
2021-10-25 13:29:06 +00:00
|
|
|
this._log.debug(docSession, "now %d clients", this._docSessions.length);
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all active clients from this document, i.e. closes all DocSessions.
|
|
|
|
*/
|
|
|
|
public removeAllClients(): void {
|
2021-10-25 13:29:06 +00:00
|
|
|
this._log.debug(null, "removeAllClients() removing %s docSessions", this._docSessions.length);
|
2020-07-21 13:20:51 +00:00
|
|
|
const docSessions = this._docSessions.splice(0);
|
|
|
|
for (const docSession of docSessions) {
|
|
|
|
docSession.client.removeDocSession(docSession.fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public interruptAllClients() {
|
2021-10-25 13:29:06 +00:00
|
|
|
this._log.debug(null, "interruptAllClients() interrupting %s docSessions", this._docSessions.length);
|
2020-07-21 13:20:51 +00:00
|
|
|
for (const docSession of this._docSessions) {
|
|
|
|
docSession.client.interruptConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Broadcasts a message to all clients of this document using Comm.sendDocMessage. Also sends all
|
|
|
|
* docAction to active doc's plugin manager.
|
|
|
|
* @param {Object} client: Originating client used to set the `fromSelf` flag in the message.
|
|
|
|
* @param {String} type: The type of the message, e.g. 'docUserAction'.
|
|
|
|
* @param {Object} messageData: The data for this type of message.
|
2020-09-11 20:27:09 +00:00
|
|
|
* @param {Object} filterMessage: Optional callback to filter message per client.
|
2020-07-21 13:20:51 +00:00
|
|
|
*/
|
2022-06-04 04:12:30 +00:00
|
|
|
public async broadcastDocMessage(client: Client|null, type: CommDocEventType, messageData: any,
|
2020-09-11 20:27:09 +00:00
|
|
|
filterMessage?: (docSession: OptDocSession,
|
2020-11-30 15:50:00 +00:00
|
|
|
messageData: any) => Promise<any>): Promise<void> {
|
2021-10-07 23:46:22 +00:00
|
|
|
const send = (curr: DocSession) => this._send(curr, client, type, messageData, filterMessage);
|
|
|
|
if (Deps.BROADCAST_ORDER === 'parallel') {
|
|
|
|
await Promise.all(this._docSessions.map(send));
|
|
|
|
} else {
|
|
|
|
for (const session of this._docSessions) {
|
|
|
|
await send(session);
|
2020-09-02 18:17:17 +00:00
|
|
|
}
|
2021-10-07 23:46:22 +00:00
|
|
|
}
|
2020-07-21 13:20:51 +00:00
|
|
|
if (type === "docUserAction" && messageData.docActions) {
|
|
|
|
for (const action of messageData.docActions) {
|
|
|
|
this.activeDoc.docPluginManager.receiveAction(action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-07 23:46:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a message to a single client. See broadcastDocMessage for parameters.
|
|
|
|
*/
|
2022-06-04 04:12:30 +00:00
|
|
|
private async _send(target: DocSession, client: Client|null, type: CommDocEventType, messageData: any,
|
2021-10-07 23:46:22 +00:00
|
|
|
filterMessage?: (docSession: OptDocSession,
|
|
|
|
messageData: any) => Promise<any>): Promise<void> {
|
|
|
|
const fromSelf = (target.client === client);
|
|
|
|
try {
|
|
|
|
// Make sure user still has view access.
|
|
|
|
await target.authorizer.assertAccess('viewers');
|
|
|
|
if (!filterMessage) {
|
|
|
|
sendDocMessage(target.client, target.fd, type, messageData, fromSelf);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const filteredMessageData = await filterMessage(target, messageData);
|
|
|
|
if (filteredMessageData) {
|
|
|
|
sendDocMessage(target.client, target.fd, type, filteredMessageData, fromSelf);
|
|
|
|
} else {
|
2021-10-25 13:29:06 +00:00
|
|
|
this._log.debug(target, 'skip broadcastDocMessage because it is not allowed for this client');
|
2021-10-07 23:46:22 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code && e.code === 'NEED_RELOAD') {
|
|
|
|
sendDocMessage(target.client, target.fd, 'docShutdown', null, fromSelf);
|
|
|
|
} else {
|
|
|
|
sendDocMessage(target.client, target.fd, 'docUserAction', {error: String(e)}, fromSelf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code === 'AUTH_NO_VIEW') {
|
|
|
|
// Skip sending data to this user, they have no view access.
|
2021-10-25 13:29:06 +00:00
|
|
|
this._log.debug(target, 'skip broadcastDocMessage because AUTH_NO_VIEW');
|
2021-10-07 23:46:22 +00:00
|
|
|
// Go further and trigger a shutdown for this user, in case they are granted
|
|
|
|
// access again later.
|
|
|
|
sendDocMessage(target.client, target.fd, 'docShutdown', null, fromSelf);
|
|
|
|
} else {
|
|
|
|
throw(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-21 13:20:51 +00:00
|
|
|
}
|