Skip to content

Commit

Permalink
Merge pull request #1453 from nextcloud/feature/noid/commands
Browse files Browse the repository at this point in the history
🤖 Commands
  • Loading branch information
Ivansss authored Feb 20, 2019
2 parents bea75cf + 4eec2a7 commit ffb42eb
Show file tree
Hide file tree
Showing 48 changed files with 8,535 additions and 218 deletions.
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/js/admin/sha1.js
/js/admin/commands.js
/js/admin/commands.js.map
/js/tests/*
/js/vendor/*
/js/simplewebrtc.js
/js/**/templates.js
/karma.conf.js
/tests/*
/vue/*
16 changes: 15 additions & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m
]]></description>

<version>5.99.0</version>
<version>5.99.2</version>
<licence>agpl</licence>

<author>Daniel Calviño Sánchez</author>
Expand Down Expand Up @@ -52,7 +52,20 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m
<job>OCA\Spreed\BackgroundJob\RemoveEmptyRooms</job>
</background-jobs>

<repair-steps>
<post-migration>
<step>OCA\Spreed\Migration\CreateHelpCommand</step>
</post-migration>
<install>
<step>OCA\Spreed\Migration\CreateHelpCommand</step>
</install>
</repair-steps>

<commands>
<command>OCA\Spreed\Command\Command\Add</command>
<command>OCA\Spreed\Command\Command\Delete</command>
<command>OCA\Spreed\Command\Command\ListCommand</command>
<command>OCA\Spreed\Command\Command\Update</command>
<command>OCA\Spreed\Command\Stun\Add</command>
<command>OCA\Spreed\Command\Stun\Delete</command>
<command>OCA\Spreed\Command\Stun\ListCommand</command>
Expand All @@ -68,6 +81,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m
<admin>OCA\Spreed\Settings\Admin\TurnServer</admin>
<admin>OCA\Spreed\Settings\Admin\StunServer</admin>
<admin>OCA\Spreed\Settings\Admin\SignalingServer</admin>
<admin>OCA\Spreed\Settings\Admin\Commands</admin>
<admin-section>OCA\Spreed\Settings\Admin\Section</admin-section>
</settings>

Expand Down
30 changes: 30 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,36 @@
'token' => '^[a-z0-9]{4,30}$',
],
],

/**
* Commands
*/
[
'name' => 'Command#index',
'url' => '/api/{apiVersion}/command',
'verb' => 'GET',
'requirements' => [
'apiVersion' => 'v1',
],
],
[
'name' => 'Command#update',
'url' => '/api/{apiVersion}/command/{id}',
'verb' => 'PUT',
'requirements' => [
'apiVersion' => 'v1',
'id' => '^\d+$',
],
],
[
'name' => 'Command#destroy',
'url' => '/api/{apiVersion}/command/{id}',
'verb' => 'DELETE',
'requirements' => [
'apiVersion' => 'v1',
'id' => '^\d+$',
],
],
],
];

86 changes: 86 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Chat commands


## Admin defined commands

For security reasons commands can only be added via the command line. `./occ talk:command:add --help` gives you a short overview of the required arguments, but they are explained here in more depth:

### "Add command" arguments

Argument | Allowed chars | Description
---|---|---
`cmd` | [a-z0-9] | The keyword the user has to type to run this command (min. 1, max. 64 characters)
`name` | * | The author name of the response that is posted by the command (min. 1, max. 64 characters)
`script` | * | Actual command that is being ran. The script must be executable by the user of your webserver. See the parameter table below for options. The script is invoked with `--help` as argument on set up, to check if it can be executed correctly.
`response` | 0-2 | Who should see the response: 0 - No one, 1 - User who executed the command, 2 - Everyone
`enabled` | 0-3 | Who can use the command: 0 - No one, 1 - Moderators of the room, 2 - Logged in users, 3 - Everyone

### Script parameter

Parameter | Description
---|---
`{ROOM}` | The token of the room the command was used in
`{USER}` | ID of the user that called the command
`{ARGUMENTS}` | Everything the user write after the actual command
`{ARGUMENTS_DOUBLEQUOTE_ESCAPED}` | … but with double quotes `"` escaped.

### Example

* `/path/to/calc.sh`:

```
while test $# -gt 0; do
case "$1" in
--help)
echo "/calc - A Nextcloud Talk chat wrapper for gnome-calculator"
echo " "
echo "Simple equations: /calc 3 + 4 * 5"
echo "Complex equations: /calc sin(3) + 3^3 * sqrt(5)"
exit 0
;;
*)
break
;;
esac
done
set -f
echo "$@ ="
echo $(gnome-calculator --solve="$@")
```
Please note, that your command should also understand the argument `--help`.
It should return a useful description, the first line is also displayed in a list of all commands when the user just types `/help`.
* `./occ` command used to add the command:
```
./occ talk:command:add calculator calculator "/path/to/calc.sh \"{ARGUMENTS_DOUBLEQUOTE_ESCAPED}\" {ROOM} {USER}" 1 3
```
* User input by user `my user id` in the chat of room `index.php/call/4tf349j`:
```
/calculator 1 + 2 + 3 + "hello"
```
* Executed shell command:
```
/path/to/calc.sh "1 + 2 + 3 + \"hello\"" '4tf349j' 'my user id'
```
## Aliases
It is also possible to define an alias for a command. This allows e.g. to get the `/help` command also with the german word `/hilfe`.
An alias for the `/calculator` command from above could be created using the following command:
```
./occ talk:command:add calc calculator "alias:calculator" 1 3
```
Now `/calculator 1 + 2 + 3` and `/calc 1 + 2 + 3` result in the same message.
**Note:** The enabled and response flag of the alias are ignored and the flags of the original command will be used and respected.
36 changes: 36 additions & 0 deletions js/admin/commands.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions js/admin/commands.js.map

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions js/views/chatview.js
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,8 @@
return false;
}

return (model1.get('systemMessage').length === 0) === (model2.get('systemMessage').length === 0) &&
return model1.get('actorType') !== 'bots' &&
(model1.get('systemMessage').length === 0) === (model2.get('systemMessage').length === 0) &&
model1.get('actorId') === model2.get('actorId') &&
model1.get('actorType') === model2.get('actorType');
},
Expand Down Expand Up @@ -512,7 +513,12 @@
}
};
$el.find('.authorRow .avatar').each(function() {
setAvatar($(this), 32);
if (model && model.get('actorType') === 'bots') {
$(this).imageplaceholder('>_', $(this).data('displayname'), 32);
$(this).css('background-color', '#363636');
} else {
setAvatar($(this), 32);
}
});
var inlineAvatars = $el.find('.message .avatar');
if ($($el.context).hasClass('message')) {
Expand Down
6 changes: 4 additions & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
use OCA\Spreed\Activity\Listener as ActivityListener;
use OCA\Spreed\Capabilities;
use OCA\Spreed\Chat\ChatManager;
use OCA\Spreed\Chat\Listener as ChatListener;
use OCA\Spreed\Chat\Command\Listener as CommandListener;
use OCA\Spreed\Chat\Parser\Listener as ParserListener;
use OCA\Spreed\Chat\SystemMessage\Listener as SystemMessageListener;
use OCA\Spreed\Config;
use OCA\Spreed\Files\Listener as FilesListener;
Expand Down Expand Up @@ -65,13 +66,14 @@ public function register(): void {
ActivityListener::register($dispatcher);
NotificationListener::register($dispatcher);
SystemMessageListener::register($dispatcher);
ChatListener::register($dispatcher);
ParserListener::register($dispatcher);
PublicShareAuthListener::register($dispatcher);
PublicShareAuthTemplateLoader::register($dispatcher);
FilesListener::register($dispatcher);
FilesTemplateLoader::register($dispatcher);
RoomShareProvider::register($dispatcher);
SignalingListener::register($dispatcher);
CommandListener::register($dispatcher);

$this->registerRoomActivityHooks($dispatcher);
$this->registerChatHooks($dispatcher);
Expand Down
12 changes: 11 additions & 1 deletion lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Spreed\Chat;

use OCA\Spreed\Participant;
use OCA\Spreed\Room;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Comments\IComment;
Expand Down Expand Up @@ -102,20 +103,27 @@ public function addSystemMessage(Room $chat, string $actorType, string $actorId,
* Sends a new message to the given chat.
*
* @param Room $chat
* @param Participant $participant
* @param string $actorType
* @param string $actorId
* @param string $message
* @param \DateTime $creationDateTime
* @return IComment
*/
public function sendMessage(Room $chat, string $actorType, string $actorId, string $message, \DateTime $creationDateTime): IComment {
public function sendMessage(Room $chat, Participant $participant, string $actorType, string $actorId, string $message, \DateTime $creationDateTime): IComment {
$comment = $this->commentsManager->create($actorType, $actorId, 'chat', (string) $chat->getId());
$comment->setMessage($message);
$comment->setCreationDateTime($creationDateTime);
// A verb ('comment', 'like'...) must be provided to be able to save a
// comment
$comment->setVerb('comment');

$this->dispatcher->dispatch(self::class . '::preSendMessage', new GenericEvent($chat, [
'comment' => $comment,
'room' => $chat,
'participant' => $participant,
]));

try {
$this->commentsManager->save($comment);

Expand All @@ -132,6 +140,8 @@ public function sendMessage(Room $chat, string $actorType, string $actorId, stri

$this->dispatcher->dispatch(self::class . '::sendMessage', new GenericEvent($chat, [
'comment' => $comment,
'room' => $chat,
'participant' => $participant,
]));
} catch (NotFoundException $e) {
}
Expand Down
Loading

0 comments on commit ffb42eb

Please sign in to comment.