Skip to content

Commit

Permalink
Api changes to react to a message: reactions add
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <vitor@php.rio>
  • Loading branch information
vitormattos committed Feb 15, 2022
1 parent 213d9ea commit 3b30c90
Show file tree
Hide file tree
Showing 16 changed files with 454 additions and 1 deletion.
130 changes: 130 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,43 @@ trigger:
- pull_request
- push

---
kind: pipeline
name: int-sqlite-reaction

steps:
- name: integration-reaction
image: ghcr.io/nextcloud/continuous-integration-php8.0:latest
environment:
APP_NAME: spreed
CORE_BRANCH: master
GUESTS_BRANCH: master
DATABASEHOST: sqlite
commands:
- bash tests/drone-run-integration-tests.sh || exit 0
- wget https://raw.githubusercontent.com/nextcloud/travis_ci/master/before_install.sh
- bash ./before_install.sh $APP_NAME $CORE_BRANCH $DATABASEHOST
- cd ../server
- git clone --depth 1 -b "$GUESTS_BRANCH" https://github.com/nextcloud/guests apps/guests
- ./occ app:enable $APP_NAME
- cd apps/$APP_NAME

# Run integration tests
- cd tests/integration/
- bash run.sh features/reaction

services:
- name: cache
image: ghcr.io/nextcloud/continuous-integration-redis:latest

trigger:
branch:
- master
- stable*
event:
- pull_request
- push

---
kind: pipeline
name: int-sqlite-sharing
Expand Down Expand Up @@ -475,6 +512,53 @@ trigger:
# - pull_request
- push

---
kind: pipeline
name: int-mysql-reaction

steps:
- name: integration-reaction
image: ghcr.io/nextcloud/continuous-integration-php8.0:latest
environment:
APP_NAME: spreed
CORE_BRANCH: master
GUESTS_BRANCH: master
DATABASEHOST: mysql
commands:
- bash tests/drone-run-integration-tests.sh || exit 0
- wget https://raw.githubusercontent.com/nextcloud/travis_ci/master/before_install.sh
- bash ./before_install.sh $APP_NAME $CORE_BRANCH $DATABASEHOST
- cd ../server
- git clone --depth 1 -b "$GUESTS_BRANCH" https://github.com/nextcloud/guests apps/guests
- ./occ app:enable $APP_NAME
- cd apps/$APP_NAME

# Run integration tests
- cd tests/integration/
- bash run.sh features/reaction

services:
- name: cache
image: ghcr.io/nextcloud/continuous-integration-redis:latest
- name: mysql
image: ghcr.io/nextcloud/continuous-integration-mariadb-10.4:10.4
environment:
MYSQL_ROOT_PASSWORD: owncloud
MYSQL_USER: oc_autotest
MYSQL_PASSWORD: owncloud
MYSQL_DATABASE: oc_autotest
command: [ "--innodb_large_prefix=true", "--innodb_file_format=barracuda", "--innodb_file_per_table=true" ]
tmpfs:
- /var/lib/mysql

trigger:
branch:
- master
- stable*
event:
# - pull_request
- push

---
kind: pipeline
name: int-mysql-sharing
Expand Down Expand Up @@ -789,6 +873,52 @@ trigger:
# - pull_request
- push

---
kind: pipeline
name: int-pgsql-reaction

steps:
- name: integration-reaction
image: ghcr.io/nextcloud/continuous-integration-php8.0:latest
environment:
APP_NAME: spreed
CORE_BRANCH: master
GUESTS_BRANCH: master
DATABASEHOST: pgsql
commands:
- bash tests/drone-run-integration-tests.sh || exit 0
- wget https://raw.githubusercontent.com/nextcloud/travis_ci/master/before_install.sh
- bash ./before_install.sh $APP_NAME $CORE_BRANCH $DATABASEHOST
- cd ../server
- git clone --depth 1 -b "$GUESTS_BRANCH" https://github.com/nextcloud/guests apps/guests
- ./occ app:enable $APP_NAME
- cd apps/$APP_NAME

# Run integration tests
- cd tests/integration/
- bash run.sh features/reaction

services:
- name: cache
image: ghcr.io/nextcloud/continuous-integration-redis:latest
- name: pgsql
image: ghcr.io/nextcloud/continuous-integration-postgres-13:postgres-13
environment:
POSTGRES_USER: oc_autotest
POSTGRES_DB: oc_autotest_dummy
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_PASSWORD:
tmpfs:
- /var/lib/postgresql/data

trigger:
branch:
- master
- stable*
event:
# - pull_request
- push

---
kind: pipeline
name: int-pgsql-sharing
Expand Down
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
include(__DIR__ . '/routes/routesMatterbridgeSettingsController.php'),
include(__DIR__ . '/routes/routesPageController.php'),
include(__DIR__ . '/routes/routesPublicShareAuthController.php'),
include(__DIR__ . '/routes/routesReactionController.php'),
include(__DIR__ . '/routes/routesRoomController.php'),
include(__DIR__ . '/routes/routesSettingsController.php'),
include(__DIR__ . '/routes/routesSignalingController.php'),
Expand Down
33 changes: 33 additions & 0 deletions appinfo/routes/routesReactionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

return [
'ocs' => [
['name' => 'Reaction#react', 'url' => '/api/{apiVersion}/reaction/{token}/{messageId}', 'verb' => 'POST', 'requirements' => [
'apiVersion' => 'v1',
'token' => '^[a-z0-9]{4,30}$',
]],
],
];
2 changes: 2 additions & 0 deletions docs/chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Base endpoint is: `/ocs/v2.php/apps/spreed/api/v1`
`message` | string | Message string with placeholders (see [Rich Object String](https://github.com/nextcloud/server/issues/1706))
`messageParameters` | array | Message parameters for `message` (see [Rich Object String](https://github.com/nextcloud/server/issues/1706))
`parent` | array | **Optional:** See `Parent data` below
`reactions` | array | **Optional:** An array map with relation between reaction emoji and total of reactions with this emoji

#### Parent data

Expand Down Expand Up @@ -324,3 +325,4 @@ See [OCP\RichObjectStrings\Definitions](https://github.com/nextcloud/server/blob
* `matterbridge_config_removed` - {actor} removed the Matterbridge configuration
* `matterbridge_config_enabled` - {actor} started Matterbridge
* `matterbridge_config_disabled` - {actor} stopped Matterbridge

1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* [Participant API](participant.md)
* [Call API](call.md)
* [Chat API](chat.md)
* [Reaction API](reaction.md)
* [Webinar API](webinar.md)
* [Internal Signaling API](internal-signaling.md)
* [Standalone Signaling API](https://nextcloud-spreed-signaling.readthedocs.io/en/latest/)
Expand Down
20 changes: 20 additions & 0 deletions docs/reaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Reaction API

Base endpoint is: `/ocs/v2.php/apps/spreed/api/v1`

## React to a message

* Method: `POST`
* Endpoint: `/chat/{token}/{messageId}`
* Data:

field | type | Description
---|---|---
`reaction` | string | the reaction emoji

* Response:
- Status code:
+ `201 Created`
+ `400 Bad Request` In case of any other error
+ `404 Not Found` When the conversation or message to react could not be found for the participant
+ `409 Conflict` User already did this reaction to this message
12 changes: 12 additions & 0 deletions lib/Chat/Parser/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ public static function register(IEventDispatcher $dispatcher): void {
}
});

$dispatcher->addListener(MessageParser::EVENT_MESSAGE_PARSE, static function (ChatMessageEvent $event) {
$chatMessage = $event->getMessage();

if ($chatMessage->getMessageType() !== 'reaction') {
return;
}

/** @var ReactionParser $parser */
$parser = \OC::$server->get(ReactionParser::class);
$parser->parseMessage($chatMessage);
});

$dispatcher->addListener(MessageParser::EVENT_MESSAGE_PARSE, static function (ChatMessageEvent $event) {
$chatMessage = $event->getMessage();

Expand Down
43 changes: 43 additions & 0 deletions lib/Chat/Parser/ReactionParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Talk\Chat\Parser;

use OCA\Talk\Model\Message;

class ReactionParser {
/**
* @param Message $message
* @throws \OutOfBoundsException
*/
public function parseMessage(Message $message): void {
$comment = $message->getComment();
if (!in_array($comment->getVerb(), ['reaction'])) {
throw new \OutOfBoundsException('Not a reaction');
}
$message->setMessageType('system');
$message->setMessage($message->getMessage(), [], $comment->getVerb());
}
}
54 changes: 54 additions & 0 deletions lib/Chat/ReactionManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio>
*
* @author Vitor Mattos <vitor@php.rio>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Talk\Chat;

use OCA\Talk\Participant;
use OCA\Talk\Room;
use OCP\Comments\IComment;
use OCP\Comments\ICommentsManager;

class ReactionManager {
/** @var ICommentsManager|CommentsManager */
private $commentsManager;

public function __construct(CommentsManager $commentsManager) {
$this->commentsManager = $commentsManager;
}

public function addReactionMessage(Room $chat, Participant $participant, int $messageId, string $reaction): IComment {
$comment = $this->commentsManager->create(
$participant->getAttendee()->getActorType(),
$participant->getAttendee()->getActorId(),
'chat',
(string) $chat->getId()
);
$comment->setParentId((string) $messageId);
$comment->setMessage($reaction);
$comment->setVerb('reaction');
$this->commentsManager->save($comment);
return $comment;
}
}
6 changes: 6 additions & 0 deletions lib/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use OCA\Talk\Chat\AutoComplete\SearchPlugin;
use OCA\Talk\Chat\AutoComplete\Sorter;
use OCA\Talk\Chat\ChatManager;
use OCA\Talk\Chat\CommentsManager;
use OCA\Talk\Chat\MessageParser;
use OCA\Talk\GuestManager;
use OCA\Talk\MatterbridgeManager;
Expand Down Expand Up @@ -68,6 +69,9 @@ class ChatController extends AEnvironmentAwareController {
/** @var IAppManager */
private $appManager;

/** @var CommentsManager */
private $commentsManager;

/** @var ChatManager */
private $chatManager;

Expand Down Expand Up @@ -121,6 +125,7 @@ public function __construct(string $appName,
IRequest $request,
IUserManager $userManager,
IAppManager $appManager,
CommentsManager $commentsManager,
ChatManager $chatManager,
ParticipantService $participantService,
SessionService $sessionService,
Expand All @@ -141,6 +146,7 @@ public function __construct(string $appName,
$this->userId = $UserId;
$this->userManager = $userManager;
$this->appManager = $appManager;
$this->commentsManager = $commentsManager;
$this->chatManager = $chatManager;
$this->participantService = $participantService;
$this->sessionService = $sessionService;
Expand Down
Loading

0 comments on commit 3b30c90

Please sign in to comment.