Skip to content

Commit

Permalink
feat(chat): Allow marking chat as read without a last message ID
Browse files Browse the repository at this point in the history
This is required for federated conversations where the clients
might not know the hosted message ID but only the proxy ID instead

Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Mar 5, 2024
1 parent 11a1888 commit 08e21d5
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/capabilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,4 @@
* `delete-messages-unlimited` - Whether messages can be deleted at any time (used to be restricted to 6 hours after posting)
* `edit-messages` - Whether messages can be edited (restricted to 24 hours after posting)
* `silent-send-state` - Whether messages contain a flag that they were sent silently
* `chat-read-last` - Whether chat can be marked read without giving a message ID (will fall back to the conversations last message ID)
6 changes: 3 additions & 3 deletions docs/chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,9 @@ See [OCP\RichObjectStrings\Definitions](https://github.com/nextcloud/server/blob
* Endpoint: `/chat/{token}/read`
* Data:

| field | type | Description |
|-------------------|------|--------------------------|
| `lastReadMessage` | int | The last read message ID |
| field | type | Description |
|-------------------|----------|----------------------------------------------------------------------|
| `lastReadMessage` | int/null | The last read message ID (Optional with `chat-read-last` capability) |

* Response:
- Status code:
Expand Down
1 change: 1 addition & 0 deletions lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public function getCapabilities(): array {
'delete-messages-unlimited',
'edit-messages',
'silent-send-state',
'chat-read-last',
],
'config' => [
'attachments' => [
Expand Down
8 changes: 5 additions & 3 deletions lib/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1043,25 +1043,27 @@ public function clearHistory(): DataResponse {
return new DataResponse($data, $bridge['enabled'] ? Http::STATUS_ACCEPTED : Http::STATUS_OK, $headers);
}


/**
* Set the read marker to a specific message
*
* @param int $lastReadMessage ID if the last read message
* @psalm-param non-negative-int $lastReadMessage
* @param int|null $lastReadMessage ID if the last read message (Optional only with `chat-read-last` capability)
* @psalm-param non-negative-int|null $lastReadMessage
* @return DataResponse<Http::STATUS_OK, TalkRoom, array{X-Chat-Last-Common-Read?: numeric-string}>
*
* 200: Read marker set successfully
*/
#[FederationSupported]
#[PublicPage]
#[RequireAuthenticatedParticipant]
public function setReadMarker(int $lastReadMessage): DataResponse {
public function setReadMarker(?int $lastReadMessage = null): DataResponse {
if ($this->room->getRemoteServer() !== '') {
/** @var \OCA\Talk\Federation\Proxy\TalkV1\Controller\ChatController $proxy */
$proxy = \OCP\Server::get(\OCA\Talk\Federation\Proxy\TalkV1\Controller\ChatController::class);
return $proxy->setReadMarker($this->room, $this->participant, $this->getResponseFormat(), $lastReadMessage);
}

$lastReadMessage = $lastReadMessage ?? $this->room->getLastMessageId();
$this->participantService->updateLastReadMessage($this->participant, $lastReadMessage);
$attendee = $this->participant->getAttendee();

Expand Down
4 changes: 2 additions & 2 deletions openapi-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -6266,11 +6266,11 @@
{
"name": "lastReadMessage",
"in": "query",
"description": "ID if the last read message",
"required": true,
"description": "ID if the last read message (Optional only with `chat-read-last` capability)",
"schema": {
"type": "integer",
"format": "int64",
"nullable": true,
"minimum": 0
}
},
Expand Down
4 changes: 2 additions & 2 deletions openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -6148,11 +6148,11 @@
{
"name": "lastReadMessage",
"in": "query",
"description": "ID if the last read message",
"required": true,
"description": "ID if the last read message (Optional only with `chat-read-last` capability)",
"schema": {
"type": "integer",
"format": "int64",
"nullable": true,
"minimum": 0
}
},
Expand Down
6 changes: 3 additions & 3 deletions src/types/openapi/openapi-full.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2431,9 +2431,9 @@ export type operations = {
/** Set the read marker to a specific message */
"chat-set-read-marker": {
parameters: {
query: {
/** @description ID if the last read message */
lastReadMessage: number;
query?: {
/** @description ID if the last read message (Optional only with `chat-read-last` capability) */
lastReadMessage?: number | null;
};
header: {
/** @description Required to be true for the API request to pass */
Expand Down
6 changes: 3 additions & 3 deletions src/types/openapi/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2268,9 +2268,9 @@ export type operations = {
/** Set the read marker to a specific message */
"chat-set-read-marker": {
parameters: {
query: {
/** @description ID if the last read message */
lastReadMessage: number;
query?: {
/** @description ID if the last read message (Optional only with `chat-read-last` capability) */
lastReadMessage?: number | null;
};
header: {
/** @description Required to be true for the API request to pass */
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2365,7 +2365,7 @@ public function userReadsMessageInRoom($user, $message, $identifier, $statusCode
$this->setCurrentUser($user);
$this->sendRequest(
'POST', '/apps/spreed/api/' . $apiVersion . '/chat/' . self::$identifierToToken[$identifier] . '/read',
new TableNode([['lastReadMessage', self::$textToMessageId[$message]]])
$message === 'NULL' ? null : new TableNode([['lastReadMessage', self::$textToMessageId[$message]]]),
);
$this->assertStatusCode($this->response, $statusCode);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/features/chat-2/unread-messages.feature
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Feature: chat-2/unread-messages
Then user "participant2" is participant of room "group room" (v4)
| unreadMessages |
| 1 |
When user "participant2" reads message "NULL" in room "group room" with 200
Then user "participant2" is participant of room "group room" (v4)
| unreadMessages |
| 0 |



Expand Down
1 change: 1 addition & 0 deletions tests/php/CapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function setUp(): void {
'delete-messages-unlimited',
'edit-messages',
'silent-send-state',
'chat-read-last',
'message-expiration',
'reactions',
];
Expand Down

0 comments on commit 08e21d5

Please sign in to comment.