diff options
-rw-r--r-- | appinfo/application.php | 21 | ||||
-rw-r--r-- | appinfo/register_command.php | 8 | ||||
-rw-r--r-- | lib/Command/RefreshRoster.php | 48 | ||||
-rw-r--r-- | lib/RosterPush.php | 80 | ||||
-rw-r--r-- | lib/hooks.php | 42 |
5 files changed, 160 insertions, 39 deletions
diff --git a/appinfo/application.php b/appinfo/application.php index 5a8b477..310373e 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -2,6 +2,7 @@ namespace OCA\OJSXC\AppInfo; +use OCA\OJSXC\Command\RefreshRoster; use OCA\OJSXC\Controller\HttpBindController; use OCA\OJSXC\Db\IQRosterPushMapper; use OCA\OJSXC\Db\MessageMapper; @@ -9,6 +10,7 @@ use OCA\OJSXC\Db\PresenceMapper; use OCA\OJSXC\Db\Stanza; use OCA\OJSXC\Db\StanzaMapper; use OCA\OJSXC\NewContentContainer; +use OCA\OJSXC\RosterPush; use OCA\OJSXC\StanzaHandlers\IQ; use OCA\OJSXC\StanzaHandlers\Message; use OCA\OJSXC\StanzaHandlers\Presence; @@ -150,17 +152,32 @@ class Application extends App { }); + $container->registerService('RosterPush', function($c) { + return new RosterPush( + $c->query('ServerContainer')->getUserManager(), + $c->query('ServerContainer')->getUserSession(), + $c->query('Host'), + $c->query('IQRosterPushMapper') + ); + }); + $container->registerService('UserHooks', function($c) { return new Hooks( $c->query('ServerContainer')->getUserManager(), $c->query('ServerContainer')->getUserSession(), - $c->query('Host'), - $c->query('IQRosterPushMapper'), + $c->query('RosterPush'), $c->query('PresenceMapper'), $c->query('StanzaMapper') ); }); + $container->registerService('RefreshRosterCommand', function($c) { + return new RefreshRoster( + $c->query('ServerContainer')->getUserManager(), + $c->query('RosterPush') + ); + }); + } /** diff --git a/appinfo/register_command.php b/appinfo/register_command.php new file mode 100644 index 0000000..fb69e9f --- /dev/null +++ b/appinfo/register_command.php @@ -0,0 +1,8 @@ +<?php + +use OCA\OJSXC\AppInfo\Application; + +$app = new Application(); + +/** @var Symfony\Component\Console\Application $application */ +$application->add($app->getContainer()->query('RefreshRosterCommand'));
\ No newline at end of file diff --git a/lib/Command/RefreshRoster.php b/lib/Command/RefreshRoster.php new file mode 100644 index 0000000..46e20ee --- /dev/null +++ b/lib/Command/RefreshRoster.php @@ -0,0 +1,48 @@ +<?php + +namespace OCA\OJSXC\Command; + +use OCA\OJSXC\RosterPush; +use OCP\IUserManager; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class RefreshRoster extends Command { + + /** + * @var IUserManager + */ + private $userManager; + + /** + * @var RosterPush + */ + private $rosterPush; + + public function __construct(IUserManager $userManager, + RosterPush $rosterPush) { + parent::__construct(); + $this->userManager = $userManager; + $this->rosterPush = $rosterPush; + + } + + protected function configure() { + $this->setName('ojsxc:refresh-roster'); + $this->setDescription('Refresh the roster of all users'); + } + + protected function execute(InputInterface $input, OutputInterface $output) { + + $users = $this->userManager->search(''); + + foreach ($users as $user) { + $this->rosterPush->createOrUpdateRosterItem($user); + } + + $output->writeln("<info>Refreshed " . count($users) . " rosters. </info>"); + } + +} diff --git a/lib/RosterPush.php b/lib/RosterPush.php new file mode 100644 index 0000000..ac317f8 --- /dev/null +++ b/lib/RosterPush.php @@ -0,0 +1,80 @@ +<?php + +namespace OCA\OJSXC; + +use OCA\OJSXC\Db\IQRosterPush; +use OCA\OJSXC\Db\IQRosterPushMapper; +use OCA\OJSXC\Db\PresenceMapper; +use OCP\IUserManager; + +use OCP\IUser; +use OCP\IUserSession; + +class RosterPush { + + /** + * @var IUserManager + */ + private $userManager; + + /** + * @var IQRosterPushMapper + */ + private $iqRosterPushMapper; + + private $host; + + /** + * @var IUserSession + */ + private $userSession; + + public function __construct(IUserManager $userManager, + IUserSession $userSession, $host, + IQRosterPushMapper $iqRosterPushMapper) { + $this->userManager = $userManager; + $this->userSession = $userSession; + $this->host = $host; + $this->iqRosterPushMapper = $iqRosterPushMapper; + } + + /** + * @see https://tools.ietf.org/html/rfc6121#section-2.1.6 + * @param IUser $user + */ + public function createOrUpdateRosterItem(IUser $user) { + $iq = new IQRosterPush(); + $iq->setJid($user->getUID()); + $iq->setName($user->getDisplayName()); + $iq->setSubscription('both'); + $iq->setFrom(''); + + + foreach ($this->userManager->search('') as $recipient) { + if($recipient->getUID() !== $user->getUID()) { + $iq->setTo($recipient->getUID()); + $this->iqRosterPushMapper->insert($iq); + } + } + } + + /** + * @see https://tools.ietf.org/html/rfc6121#section-2.1.6 + * @param IUser $user + */ + public function removeRosterItem(IUser $user) { + $iq = new IQRosterPush(); + $iq->setJid($user->getUID()); + $iq->setName($user->getDisplayName()); + $iq->setSubscription('remove'); + $iq->setFrom(''); + + + foreach ($this->userManager->search('') as $recipient) { + if($recipient->getUID() !== $user->getUID()) { + $iq->setTo($recipient->getUID()); + $this->iqRosterPushMapper->insert($iq); + } + } + } +}
\ No newline at end of file diff --git a/lib/hooks.php b/lib/hooks.php index 9c61b53..e04ed7e 100644 --- a/lib/hooks.php +++ b/lib/hooks.php @@ -19,13 +19,6 @@ class Hooks { private $userManager; /** - * @var IQRosterPushMapper - */ - private $iqRosterPushMapper; - - private $host; - - /** * @var IUserSession */ private $userSession; @@ -41,14 +34,13 @@ class Hooks { private $stanzaMapper; public function __construct(IUserManager $userManager, - IUserSession $userSession, $host, - IQRosterPushMapper $iqRosterPushMapper, + IUserSession $userSession, + RosterPush $rosterPush, PresenceMapper $presenceMapper, StanzaMapper $stanzaMapper) { $this->userManager = $userManager; $this->userSession = $userSession; - $this->host = $host; - $this->iqRosterPushMapper = $iqRosterPushMapper; + $this->rosterPush = $rosterPush; $this->presenceMapper = $presenceMapper; $this->stanzaMapper = $stanzaMapper; } @@ -69,19 +61,7 @@ class Hooks { * @param string $password */ public function onCreateUser(IUser $user, $password) { - $iq = new IQRosterPush(); - $iq->setJid($user->getUID()); - $iq->setName($user->getDisplayName()); - $iq->setSubscription('both'); - $iq->setFrom(''); - - - foreach ($this->userManager->search('') as $recipient) { - if($recipient->getUID() !== $user->getUID()) { - $iq->setTo($recipient->getUID()); - $this->iqRosterPushMapper->insert($iq); - } - } + $this->rosterPush->createOrUpdateRosterItem($user); } /** @@ -94,19 +74,7 @@ class Hooks { * @param IUser $user */ public function onDeleteUser(IUser $user) { - $iq = new IQRosterPush(); - $iq->setJid($user->getUID()); - $iq->setName($user->getDisplayName()); - $iq->setSubscription('remove'); - $iq->setFrom(''); - - - foreach ($this->userManager->search('') as $recipient) { - if($recipient->getUID() !== $user->getUID()) { - $iq->setTo($recipient->getUID()); - $this->iqRosterPushMapper->insert($iq); - } - } + $this->rosterPush->removeRosterItem($user); // delete the presence record of this user $this->presenceMapper->deletePresence($user->getUID()); |