Skip to content

Commit

Permalink
Merge pull request #33 from nextcloud/fix_new_users
Browse files Browse the repository at this point in the history
Update roster when creating, deleting and changing users

fix jsxc/jsxc#565
  • Loading branch information
sualko authored Jul 19, 2017
2 parents 6e55221 + 55d3cfc commit 6255934
Show file tree
Hide file tree
Showing 15 changed files with 435 additions and 15 deletions.
1 change: 1 addition & 0 deletions ajax/getSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function validateBoolean($val)
$data ['loginForm'] ['startMinimized'] = validateBoolean($config->getAppValue('ojsxc', 'xmppStartMinimized'));

if ($data ['serverType'] === 'internal') {
$data['adminSettings']['xmppDomain'] = \OC::$server->query('Request')->getServerHost();
echo json_encode(array(
'result' => 'success',
'data' => $data,
Expand Down
5 changes: 5 additions & 0 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* Released under the MIT license
* @author Klaus Herberth <[email protected]>
*/
use OCA\OJSXC\AppInfo\Application;

if (!interface_exists('\OCP\Settings\ISettings')) {
\OCP\App::registerAdmin ( 'ojsxc', 'settings/admin' );
}
Expand Down Expand Up @@ -69,6 +71,9 @@
$config->setAppValue('ojsxc', 'apiSecret', $apiSecret);
}

$app = new Application();
$app->getContainer()->query('UserHooks')->register();

if (!class_exists("\\Sabre\\Xml\\Version")) {
require_once __DIR__ . "/../vendor/autoload.php";
}
Expand Down
60 changes: 53 additions & 7 deletions appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

namespace OCA\OJSXC\AppInfo;

use OCA\OJSXC\Command\RefreshRoster;
use OCA\OJSXC\Controller\HttpBindController;
use OCA\OJSXC\Db\IQRosterPushMapper;
use OCA\OJSXC\Db\MessageMapper;
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;
Expand All @@ -16,6 +19,7 @@
use OCA\OJSXC\ILock;
use OCA\OJSXC\DbLock;
use OCA\OJSXC\MemLock;
use OCA\OJSXC\Hooks;
use OCP\IContainer;
use OCP\IRequest;

Expand Down Expand Up @@ -43,7 +47,7 @@ public function __construct(array $urlParams=array()){
return new HttpBindController(
$c->query('AppName'),
$c->query('Request'),
$c->query('UserId'),
$c->query('OJSXC_UserId'),
$c->query('StanzaMapper'),
$c->query('IQHandler'),
$c->query('MessageHandler'),
Expand Down Expand Up @@ -71,6 +75,14 @@ public function __construct(array $urlParams=array()){
);
});

$container->registerService('IQRosterPushMapper', function(IContainer $c) use ($container) {
return new IQRosterPushMapper(
$container->getServer()->getDatabaseConnection(),
$c->query('Host'),
$c->query('StanzaLogger')
);
});

$container->registerService('StanzaMapper', function(IContainer $c) use ($container) {
return new StanzaMapper(
$container->getServer()->getDatabaseConnection(),
Expand All @@ -83,7 +95,7 @@ public function __construct(array $urlParams=array()){
return new PresenceMapper(
$container->getServer()->getDatabaseConnection(),
$c->query('Host'),
$c->query('UserId'),
$c->query('OJSXC_UserId'),
$c->query('MessageMapper'),
$c->query('NewContentContainer'),
self::$config['polling']['timeout']
Expand All @@ -96,15 +108,15 @@ public function __construct(array $urlParams=array()){
*/
$container->registerService('IQHandler', function(IContainer $c) {
return new IQ(
$c->query('UserId'),
$c->query('OJSXC_UserId'),
$c->query('Host'),
$c->query('OCP\IUserManager')
);
});

$container->registerService('PresenceHandler', function(IContainer $c) {
return new Presence(
$c->query('UserId'),
$c->query('OJSXC_UserId'),
$c->query('Host'),
$c->query('PresenceMapper'),
$c->query('MessageMapper')
Expand All @@ -113,7 +125,7 @@ public function __construct(array $urlParams=array()){

$container->registerService('MessageHandler', function(IContainer $c) {
return new Message(
$c->query('UserId'),
$c->query('OJSXC_UserId'),
$c->query('Host'),
$c->query('MessageMapper')
);
Expand All @@ -140,6 +152,40 @@ public function __construct(array $urlParams=array()){
});


$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('RosterPush'),
$c->query('PresenceMapper'),
$c->query('StanzaMapper')
);
});

$container->registerService('RefreshRosterCommand', function($c) {
return new RefreshRoster(
$c->query('ServerContainer')->getUserManager(),
$c->query('RosterPush')
);
});

/**
* A modified userID for use in OJSXC.
* This is automatically made lowercase.
*/
$container->registerService('OJSXC_UserId', function(IContainer $c) {
return strtolower($c->query('UserId'));
});

}

/**
Expand All @@ -155,7 +201,7 @@ private function getLock() {
} else if ($cache->isAvailable()) {
$memcache = $cache->create('ojsxc');
return new MemLock(
$c->query('UserId'),
$c->query('OJSXC_UserId'),
$memcache
);
} else {
Expand All @@ -165,7 +211,7 @@ private function getLock() {

// default
return new DbLock(
$c->query('UserId'),
$c->query('OJSXC_UserId'),
$c->query('OCP\IConfig'),
$c->getServer()->getDatabaseConnection()
);
Expand Down
8 changes: 8 additions & 0 deletions appinfo/register_command.php
Original file line number Diff line number Diff line change
@@ -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'));
3 changes: 3 additions & 0 deletions js/ojsxc.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@
jsxc.options.set('xmpp', {
url: OC.generateUrl('apps/ojsxc/http-bind')
});

jsxc.options.set('adminSettings', d.data.adminSettings);

if (d.data.loginForm) {
jsxc.options.set('loginForm', {
startMinimized: d.data.loginForm.startMinimized
Expand Down
48 changes: 48 additions & 0 deletions lib/command/refreshroster.php
Original file line number Diff line number Diff line change
@@ -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>");
}

}
1 change: 1 addition & 0 deletions lib/controller/httpbindcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public function __construct($appName,
*/
public function index() {
$this->lock->setLock();
$this->presenceMapper->updatePresence();
$input = $this->body;
$longpoll = true; // set to false when the response should directly be returned and no polling should be done
$longpollStart = true; // start the first long poll cycle
Expand Down
70 changes: 70 additions & 0 deletions lib/db/iqrosterpush.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace OCA\OJSXC\Db;

use Sabre\Xml\Reader;
use Sabre\Xml\Writer;
use Sabre\Xml\XmlDeserializable;
use Sabre\Xml\XmlSerializable;


/**
* This entity represents a roster push.
* @see https://tools.ietf.org/html/rfc6121#section-2.1.6
* Class IQRosterPush
*
* @package OCA\OJSXC\Db
* @method void setJid($jid)
* @method void setName($name)
* @method void setSubscription($subscription)
* @method string getJid()
* @method string getName()
* @method string getSubscription()
*/
class IQRosterPush extends Stanza implements XmlSerializable{

/**
* @var string jid of the user, when inserting this into the DB, only userid
* is needed.
*/
public $jid;

/**
* @var string displayname of the user
*/
public $name;

/**
* @var string subscription type. Both and remove are used.
*/
public $subscription;

public function xmlSerialize(Writer $writer) {
$writer->write([
[
'name' => 'iq',
'attributes' => [
'to' => $this->to,
'type' => 'set',
'id' => uniqid()
],
'value' => [[
'name' => 'query',
'attributes' => [
'xmlns' => 'jabber:iq:roster',
],
'value' => [
"name" => "item",
"attributes" => [
"jid" => $this->jid,
"name" => $this->name,
"subscription" => $this->subscription
],
"value" => ''
]
]]
]
]);
}

}
12 changes: 12 additions & 0 deletions lib/db/iqrosterpushmapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace OCA\OJSXC\Db;

/**
* Class IQRosterPushMapper
*
* @package OCA\OJSXC\Db
*/
class IQRosterPushMapper extends StanzaMapper {

}
13 changes: 11 additions & 2 deletions lib/db/presencemapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ public function __construct(IDBConnection $db, $host, $userId, MessageMapper $me
$this->messageMapper = $messageMapper;
$this->newContentContainer = $newContentContainer;
$this->timeout = $timeout;

$this->updatePresence();
}

/**
Expand Down Expand Up @@ -200,4 +198,15 @@ public function updatePresence() {
}
}

/**
* @brief Deletes the presence records of a user.
* @param string $user
*/
public function deletePresence($user) {
$sql = "DELETE FROM `*PREFIX*ojsxc_presence` WHERE `userid` = ?";

$q = $this->db->prepare($sql);
$q->execute([$user]);

}
}
20 changes: 16 additions & 4 deletions lib/db/stanza.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@
* @package OCA\OJSXC\Db
* @brief this class is used as the entity which is fetched from the stanza table OR extended by a specific stanza
* for inserting into the stanza table
* @method string getTo()
* @method string getFrom()
* @method string getStanza()
* @method void setTo($to)
* @method void setFrom($from)
* @method void setStanza($stanza)
*/
class Stanza extends Entity implements XmlSerializable{
Expand All @@ -41,6 +37,22 @@ public function __construct($stanza='') {
*/
public $stanza;

public function getTo() {
return $this->to;
}

public function setTo($userId) {
$this->to = strtolower($userId);
}

public function setFrom($userId) {
$this->from = strtolower($userId);
}

public function getFrom() {
return $this->from;
}

public function xmlSerialize(Writer $writer) {
$writer->writeRaw($this->getStanza());
}
Expand Down
Loading

0 comments on commit 6255934

Please sign in to comment.