Browse Source
Start with system chat messages
Start with system chat messages
Signed-off-by: Joas Schilling <coding@schilljs.com>pull/1067/head
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
13 changed files with 336 additions and 16 deletions
-
12css/comments.scss
-
2docs/api-v1.md
-
8js/views/chatview.js
-
5lib/AppInfo/Application.php
-
1lib/Capabilities.php
-
28lib/Chat/ChatManager.php
-
2lib/Chat/RichMessageHelper.php
-
143lib/Chat/SystemMessage/Listener.php
-
117lib/Chat/SystemMessage/Parser.php
-
15lib/Controller/ChatController.php
-
6lib/Manager.php
-
4lib/Room.php
-
1tests/php/CapabilitiesTest.php
@ -0,0 +1,143 @@ |
|||
<?php |
|||
/** |
|||
* @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com> |
|||
* |
|||
* @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\Spreed\Chat\SystemMessage; |
|||
|
|||
|
|||
use OCA\Spreed\Chat\ChatManager; |
|||
use OCA\Spreed\Participant; |
|||
use OCA\Spreed\Room; |
|||
use OCA\Spreed\TalkSession; |
|||
use OCP\IUser; |
|||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|||
use Symfony\Component\EventDispatcher\GenericEvent; |
|||
|
|||
class Listener { |
|||
|
|||
/** @var EventDispatcherInterface */ |
|||
protected $dispatcher; |
|||
/** @var ChatManager */ |
|||
protected $chatManager; |
|||
/** @var TalkSession */ |
|||
protected $session; |
|||
/** @var string */ |
|||
protected $userId; |
|||
|
|||
public function __construct(EventDispatcherInterface $dispatcher, ChatManager $chatManager, TalkSession $session, $userId) { |
|||
$this->dispatcher = $dispatcher; |
|||
$this->chatManager = $chatManager; |
|||
$this->session = $session; |
|||
$this->userId = $userId; |
|||
} |
|||
|
|||
public function register() { |
|||
$this->dispatcher->addListener(Room::class . '::postSessionJoinCall', function(GenericEvent $event) { |
|||
/** @var Room $room */ |
|||
$room = $event->getSubject(); |
|||
$this->sendSystemMessage($room, 'joined_call'); |
|||
}); |
|||
$this->dispatcher->addListener(Room::class . '::postSessionLeaveCall', function(GenericEvent $event) { |
|||
/** @var Room $room */ |
|||
$room = $event->getSubject(); |
|||
$this->sendSystemMessage($room, 'left_call'); |
|||
}); |
|||
|
|||
$this->dispatcher->addListener(Room::class . '::createRoom', function(GenericEvent $event) { |
|||
/** @var Room $room */ |
|||
$room = $event->getSubject(); |
|||
$this->sendSystemMessage($room, 'created_conversation'); |
|||
}); |
|||
$this->dispatcher->addListener(Room::class . '::postSetName', function(GenericEvent $event) { |
|||
/** @var Room $room */ |
|||
$room = $event->getSubject(); |
|||
$this->sendSystemMessage($room, 'renamed_conversation', $event->getArguments()); |
|||
}); |
|||
$this->dispatcher->addListener(Room::class . '::postSetPassword', function(GenericEvent $event) { |
|||
/** @var Room $room */ |
|||
$room = $event->getSubject(); |
|||
if ($event->getArgument('password')) { |
|||
$this->sendSystemMessage($room, 'set_password'); |
|||
} else { |
|||
$this->sendSystemMessage($room, 'removed_password'); |
|||
} |
|||
}); |
|||
$this->dispatcher->addListener(Room::class . '::postChangeType', function(GenericEvent $event) { |
|||
$arguments = $event->getArguments(); |
|||
|
|||
if ($arguments['newType'] !== Room::PUBLIC_CALL && $arguments['newType'] !== Room::PUBLIC_CALL) { |
|||
// one2one => group: Only added a user
|
|||
return; |
|||
} |
|||
|
|||
/** @var Room $room */ |
|||
$room = $event->getSubject(); |
|||
$this->sendSystemMessage($room, 'change_type', $event->getArguments()); |
|||
}); |
|||
|
|||
$this->dispatcher->addListener(Room::class . '::postAddUsers', function(GenericEvent $event) { |
|||
$participants = $event->getArgument('users'); |
|||
|
|||
/** @var Room $room */ |
|||
$room = $event->getSubject(); |
|||
foreach ($participants as $participant) { |
|||
if ($this->userId !== $participant['userId']) { |
|||
$this->sendSystemMessage($room, 'user_added', ['user' => $participant['userId']]); |
|||
} |
|||
} |
|||
}); |
|||
$this->dispatcher->addListener(Room::class . '::postRemoveUser', function(GenericEvent $event) { |
|||
/** @var IUser $user */ |
|||
$user = $event->getArgument('user'); |
|||
/** @var Room $room */ |
|||
$room = $event->getSubject(); |
|||
|
|||
$this->sendSystemMessage($room, 'user_removed', ['user' => $user->getUID()]); |
|||
}); |
|||
$this->dispatcher->addListener(Room::class . '::postSetParticipantType', function(GenericEvent $event) { |
|||
/** @var Room $room */ |
|||
$room = $event->getSubject(); |
|||
|
|||
if ($event->getArgument('newType') === Participant::MODERATOR) { |
|||
$this->sendSystemMessage($room, 'moderator_promoted', ['user' => $event->getArgument('user')]); |
|||
} else if ($event->getArgument('newType') === Participant::USER) { |
|||
$this->sendSystemMessage($room, 'moderator_demoted', ['user' => $event->getArgument('user')]); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
protected function sendSystemMessage(Room $room, string $message, array $parameters = []) { |
|||
|
|||
if ($this->userId === null) { |
|||
$actorType = 'guests'; |
|||
$sessionId = $this->session->getSessionForRoom($room->getToken()); |
|||
$actorId = $sessionId ? sha1($sessionId) : ''; |
|||
} else { |
|||
$actorType = 'users'; |
|||
$actorId = $this->userId; |
|||
} |
|||
|
|||
$this->chatManager->addSystemMessage( |
|||
$room, $actorType, $actorId, |
|||
json_encode(['message' => $message, 'parameters' => $parameters]), |
|||
new \DateTime() |
|||
); |
|||
} |
|||
} |
|||
@ -0,0 +1,117 @@ |
|||
<?php |
|||
/** |
|||
* @copyright Copyright (c) 2018 Joas Schilling <coding@schilljs.com> |
|||
* |
|||
* @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\Spreed\Chat\SystemMessage; |
|||
|
|||
|
|||
use OCA\Spreed\Room; |
|||
use OCP\Comments\IComment; |
|||
use OCP\IL10N; |
|||
use OCP\IUser; |
|||
use OCP\IUserManager; |
|||
|
|||
class Parser { |
|||
|
|||
/** @var IUserManager */ |
|||
protected $userManager; |
|||
/** @var IL10N */ |
|||
protected $l; |
|||
|
|||
/** @var string[] */ |
|||
protected $displayNames = []; |
|||
|
|||
public function __construct(IUserManager $userManager, IL10N $l) { |
|||
$this->userManager = $userManager; |
|||
$this->l = $l; |
|||
} |
|||
|
|||
public function parseMessage(IComment $comment, string $displayName): array { |
|||
$data = json_decode($comment->getMessage(), true); |
|||
$message = $data['message']; |
|||
$parameters = $data['parameters']; |
|||
|
|||
$parsedParameters = ['actor' => $this->getActor($comment, $displayName)]; |
|||
$parsedMessage = $comment->getMessage(); |
|||
|
|||
if ($message === 'joined_call') { |
|||
$parsedMessage = $this->l->t('{actor} joined the call'); |
|||
} else if ($message === 'left_call') { |
|||
$parsedMessage = $this->l->t('{actor} left the call'); |
|||
} else if ($message === 'created_conversation') { |
|||
$parsedMessage = $this->l->t('{actor} created the conversation'); |
|||
} else if ($message === 'renamed_conversation') { |
|||
$parsedMessage = $this->l->t('{actor} renamed the conversation from "%1$s" to "%2$s"', [$parameters['oldName'], $parameters['newName']]); |
|||
} else if ($message === 'change_type') { |
|||
if ($parameters['newType'] === Room::PUBLIC_CALL) { |
|||
$parsedMessage = $this->l->t('{actor} allowed guests in the conversation'); |
|||
} else { |
|||
$parsedMessage = $this->l->t('{actor} disallowed guests in the conversation'); |
|||
} |
|||
} else if ($message === 'set_password') { |
|||
$parsedMessage = $this->l->t('{actor} set a password for the conversation'); |
|||
} else if ($message === 'removed_password') { |
|||
$parsedMessage = $this->l->t('{actor} removed the password for the conversation'); |
|||
} else if ($message === 'user_added') { |
|||
$parsedParameters['user'] = $this->getUser($parameters['user']); |
|||
$parsedMessage = $this->l->t('{actor} added {user} to the conversation'); |
|||
} else if ($message === 'user_removed') { |
|||
$parsedParameters['user'] = $this->getUser($parameters['user']); |
|||
$parsedMessage = $this->l->t('{actor} removed {user} from the conversation'); |
|||
} else if ($message === 'moderator_promoted') { |
|||
$parsedParameters['user'] = $this->getUser($parameters['user']); |
|||
$parsedMessage = $this->l->t('{actor} promoted {user} to moderator'); |
|||
} else if ($message === 'moderator_demoted') { |
|||
$parsedParameters['user'] = $this->getUser($parameters['user']); |
|||
$parsedMessage = $this->l->t('{actor} demoted {user} from moderator'); |
|||
} |
|||
|
|||
|
|||
return [$parsedMessage, $parsedParameters]; |
|||
} |
|||
|
|||
protected function getActor(IComment $comment, string $displayName): array { |
|||
return [ |
|||
'type' => 'user', |
|||
'id' => $comment->getActorId(), |
|||
'name' => $displayName, |
|||
]; |
|||
} |
|||
|
|||
protected function getUser(string $uid): array { |
|||
if (!isset($this->displayNames[$uid])) { |
|||
$this->displayNames[$uid] = $this->getDisplayName($uid); |
|||
} |
|||
|
|||
return [ |
|||
'type' => 'user', |
|||
'id' => $uid, |
|||
'name' => $this->displayNames[$uid], |
|||
]; |
|||
} |
|||
|
|||
protected function getDisplayName(string $uid): string { |
|||
$user = $this->userManager->get($uid); |
|||
if ($user instanceof IUser) { |
|||
return $user->getDisplayName(); |
|||
} |
|||
return $uid; |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue