Browse Source

Delete the chat messages of a room when the room is deleted

Signed-off-by: Daniel Calviño Sánchez <danxuliu@gmail.com>
pull/457/head
Daniel Calviño Sánchez 8 years ago
parent
commit
b18db10a1a
  1. 14
      lib/AppInfo/Application.php
  2. 9
      lib/Chat/ChatManager.php
  3. 8
      tests/php/Chat/ChatManagerTest.php

14
lib/AppInfo/Application.php

@ -22,6 +22,7 @@
namespace OCA\Spreed\AppInfo;
use OCA\Spreed\Activity\Hooks;
use OCA\Spreed\Chat\ChatManager;
use OCA\Spreed\HookListener;
use OCA\Spreed\Notification\Notifier;
use OCA\Spreed\Room;
@ -51,6 +52,7 @@ class Application extends App {
$dispatcher = $server->getEventDispatcher();
$this->registerSignalingHooks($dispatcher);
$this->registerActivityHooks($dispatcher);
$this->registerChatHooks($dispatcher);
}
protected function registerNotifier(IServerContainer $server) {
@ -110,4 +112,16 @@ class Application extends App {
$dispatcher->addListener(Room::class . '::postRemoveUser', $listener);
$dispatcher->addListener(Room::class . '::postUserDisconnectRoom', $listener);
}
protected function registerChatHooks(EventDispatcherInterface $dispatcher) {
$listener = function(GenericEvent $event, $eventName) {
/** @var Room $room */
$room = $event->getSubject();
/** @var ChatManager $chatManager */
$chatManager = $this->getContainer()->query(ChatManager::class);
$chatManager->deleteMessages((string) $room->getId());
};
$dispatcher->addListener(Room::class . '::postDeleteRoom', $listener);
}
}

9
lib/Chat/ChatManager.php

@ -120,4 +120,13 @@ class ChatManager {
return $comments;
}
/**
* Deletes all the messages for the given chat.
*
* @param string $chatId
*/
public function deleteMessages($chatId) {
$this->commentsManager->deleteCommentsAtObject('chat', $chatId);
}
}

8
tests/php/Chat/ChatManagerTest.php

@ -185,4 +185,12 @@ class ChatManagerTest extends \Test\TestCase {
$this->assertEquals($expected, $comments);
}
public function testDeleteMessages() {
$this->commentsManager->expects($this->once())
->method('deleteCommentsAtObject')
->with('chat', 'testChatId');
$this->chatManager->deleteMessages('testChatId');
}
}
Loading…
Cancel
Save