Skip to content

Commit

Permalink
Update roster according to adding/removing of users to groups
Browse files Browse the repository at this point in the history
  • Loading branch information
LEDfan committed Sep 16, 2017
1 parent cddfd17 commit a6977ba
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
3 changes: 2 additions & 1 deletion appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ public function __construct(array $urlParams=array()){
$c->query('ServerContainer')->getUserSession(),
$c->query('RosterPush'),
$c->query('PresenceMapper'),
$c->query('StanzaMapper')
$c->query('StanzaMapper'),
$c->query('ServerContainer')->query('OCP\IGroupManager')
);
});

Expand Down
29 changes: 28 additions & 1 deletion lib/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use OCA\OJSXC\Db\PresenceMapper;
use OCA\OJSXC\Db\StanzaMapper;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUserManager;

use OCP\IUser;
Expand Down Expand Up @@ -32,25 +34,39 @@ class Hooks
*/
private $stanzaMapper;

/**
* @var RosterPush
*/
private $rosterPush;

/**
* @var IGroupManager
*/
private $groupManager;

public function __construct(
IUserManager $userManager,
IUserSession $userSession,
RosterPush $rosterPush,
PresenceMapper $presenceMapper,
StanzaMapper $stanzaMapper
StanzaMapper $stanzaMapper,
IGroupManager $groupManager
) {
$this->userManager = $userManager;
$this->userSession = $userSession;
$this->rosterPush = $rosterPush;
$this->presenceMapper = $presenceMapper;
$this->stanzaMapper = $stanzaMapper;
$this->groupManager = $groupManager;
}

public function register()
{
$this->userManager->listen('\OC\User', 'postCreateUser', [$this, 'onCreateUser']);
$this->userManager->listen('\OC\User', 'postDelete', [$this, 'onDeleteUser']);
$this->userSession->listen('\OC\User', 'changeUser', [$this, 'onChangeUser']);
$this->groupManager->listen('\OC\Group', 'postAddUser', [$this, 'onAddUserToGroup']);
$this->groupManager->listen('\OC\Group', 'postRemoveUser', [$this, 'onRemoveUserFromGroup']);
}

/**
Expand Down Expand Up @@ -112,4 +128,15 @@ public function onChangeUser(IUser $user, $feature, $value)
$this->onCreateUser($user, '');
}
}

public function onAddUserToGroup(IGroup $group, IUser $user)
{
$this->rosterPush->createOrUpdateRosterItem($user);
}

public function onRemoveUserFromGroup(IGroup $group, IUser $user)
{
$this->rosterPush->removeRosterItemForUsersInGroup($group, $user->getUID());
}

}
18 changes: 18 additions & 0 deletions lib/rosterpush.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OCA\OJSXC\Db\IQRosterPush;
use OCA\OJSXC\Db\IQRosterPushMapper;
use OCP\IDBConnection;
use OCP\IGroup;
use OCP\IUserManager;

use OCP\IUser;
Expand Down Expand Up @@ -158,4 +159,21 @@ public function refreshRoster()

return $stats;
}

public function removeRosterItemForUsersInGroup(IGroup $group, $userId) {
$iq = new IQRosterPush();
$iq->setJid($userId);
$iq->setSubscription('remove');
$iq->setFrom('');


foreach ($group->getUsers() as $recipient) {
if ($recipient->getUID() !== $userId) {
$iq->setTo($recipient->getUID());
$this->iqRosterPushMapper->insert($iq);
}
}

}

}

0 comments on commit a6977ba

Please sign in to comment.