Skip to content

Commit 8539a66

Browse files
committed
docs: Improve handleMessageSecurity documentation
1 parent f1856cf commit 8539a66

File tree

1 file changed

+31
-49
lines changed

1 file changed

+31
-49
lines changed

doc/api/hooks_server-side.md

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -574,78 +574,60 @@ exports.authzFailure = (hookName, context, cb) => {
574574
};
575575
```
576576

577-
## handleMessage
578-
Called from: src/node/handler/PadMessageHandler.js
579-
580-
Things in context:
577+
## `handleMessage`
581578

582-
1. message - the message being handled
583-
2. socket - the socket.io Socket object
584-
3. client - **deprecated** synonym of socket
579+
Called from: `src/node/handler/PadMessageHandler.js`
585580

586581
This hook allows plugins to drop or modify incoming socket.io messages from
587-
clients, before Etherpad processes them.
582+
clients, before Etherpad processes them. If any hook function returns `null`
583+
then the message will not be subject to further processing.
588584

589-
The handleMessage function must return a Promise. If the Promise resolves to
590-
`null`, the message is dropped. Returning `callback(value)` will return a
591-
Promise that is resolved to `value`.
585+
Context properties:
592586

593-
Examples:
587+
* `message`: The message being handled.
588+
* `socket`: The socket.io Socket object.
589+
* `client`: (**Deprecated**; use `socket` instead.) Synonym of `socket`.
594590

595-
```
596-
// Using an async function:
597-
exports.handleMessage = async (hookName, {message, socket}) => {
598-
if (message.type === 'USERINFO_UPDATE') {
599-
// Force the display name to the name associated with the account.
600-
const user = socket.client.request.session.user || {};
601-
if (user.name) message.data.userInfo.name = user.name;
602-
}
603-
};
591+
Example:
604592

605-
// Using a regular function:
606-
exports.handleMessage = (hookName, {message, socket}, callback) => {
593+
```javascript
594+
exports.handleMessage = async (hookName, {message, socket}) => {
607595
if (message.type === 'USERINFO_UPDATE') {
608596
// Force the display name to the name associated with the account.
609597
const user = socket.client.request.session.user || {};
610598
if (user.name) message.data.userInfo.name = user.name;
611599
}
612-
return callback();
613600
};
614601
```
615602

616-
## handleMessageSecurity
617-
Called from: src/node/handler/PadMessageHandler.js
603+
## `handleMessageSecurity`
618604

619-
Things in context:
605+
Called from: `src/node/handler/PadMessageHandler.js`
620606

621-
1. message - the message being handled
622-
2. socket - the socket.io Socket object
623-
3. client - **deprecated** synonym of socket
607+
Called for each incoming message from a client. Allows plugins to grant
608+
temporary write access to a pad.
624609

625-
This hook allows plugins to grant temporary write access to a pad. It is called
626-
for each incoming message from a client. If write access is granted, it applies
627-
to the current message and all future messages from the same socket.io
628-
connection until the next `CLIENT_READY` message. Read-only access is reset
629-
**after** each `CLIENT_READY` message, so granting write access has no effect
630-
for those message types.
610+
Supported return values:
631611

632-
The handleMessageSecurity function must return a Promise. If the Promise
633-
resolves to `true`, write access is granted as described above. Returning
634-
`callback(value)` will return a Promise that is resolved to `value`.
612+
* `undefined`: No change in access status.
613+
* `true`: Override the user's read-only access for all `COLLABROOM` messages
614+
from the same socket.io connection (including the current message, if
615+
applicable) until the client's next `CLIENT_READY` message. Has no effect if
616+
the user already has write access to the pad. Read-only access is reset
617+
**after** each `CLIENT_READY` message, so returning `true` has no effect for
618+
`CLIENT_READY` messages.
635619

636-
Examples:
620+
Context properties:
637621

638-
```
639-
// Using an async function:
622+
* `message`: The message being handled.
623+
* `socket`: The socket.io Socket object.
624+
* `client`: (**Deprecated**; use `socket` instead.) Synonym of `socket`.
625+
626+
Example:
627+
628+
```javascript
640629
exports.handleMessageSecurity = async (hookName, {message, socket}) => {
641630
if (shouldGrantWriteAccess(message, socket)) return true;
642-
return;
643-
};
644-
645-
// Using a regular function:
646-
exports.handleMessageSecurity = (hookName, {message, socket}, callback) => {
647-
if (shouldGrantWriteAccess(message, socket)) return callback(true);
648-
return callback();
649631
};
650632
```
651633

0 commit comments

Comments
 (0)