Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🤖 Commands #1453

Merged
merged 26 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c0e34c8
Start with commands
nickvergessen Jan 17, 2019
46e51bd
Add routes and controller
nickvergessen Jan 21, 2019
e53ce04
Multiselect option and value propagation
skjnldsv Jan 21, 2019
c460128
No patterns for now
nickvergessen Jan 21, 2019
ed00502
Execute commands
nickvergessen Jan 22, 2019
d7feb1d
Fix actor type and displaying
nickvergessen Jan 23, 2019
b6e5120
Add a service
nickvergessen Jan 23, 2019
8ad3535
Add a help command
nickvergessen Jan 23, 2019
dda4b0d
Cleanup execution and trigger an event for apps
nickvergessen Jan 23, 2019
18aa82d
Do not allow changing/adding scripts via the Web
nickvergessen Jan 30, 2019
835e6bd
Add console commands to manage commands
nickvergessen Jan 30, 2019
098a8b6
Escape arguments correctly
nickvergessen Feb 1, 2019
7b3589e
Help command
nickvergessen Feb 7, 2019
55844ab
Do not group bot responses
nickvergessen Feb 7, 2019
105eda5
Add docs about commands
nickvergessen Feb 7, 2019
9a1980d
Add validation for name, cmd and script
nickvergessen Feb 7, 2019
513062e
Check enable flag for participant level
nickvergessen Feb 7, 2019
214143c
Support aliases for commands
nickvergessen Feb 8, 2019
9f4cfb5
Document the alias
nickvergessen Feb 8, 2019
360db9e
Adjust UI to the new structure
nickvergessen Feb 8, 2019
9c9dac5
Adjust tests to the new executor
nickvergessen Feb 11, 2019
747b925
Ignore compiled JS from eslint
nickvergessen Feb 12, 2019
f828ab9
New coding guideline
nickvergessen Feb 19, 2019
2f60aab
Introduce a Message model for parsing which also allows to hide messages
nickvergessen Feb 20, 2019
a6e915e
Should also add new files
nickvergessen Feb 20, 2019
4eec2a7
Use the new and correct way to set the message type
nickvergessen Feb 20, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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