Fix up Roster::addContact

This commit is contained in:
Alexei Sorokin 2020-09-13 23:00:21 +03:00
parent 7cbdc99d08
commit 37f69546e8
2 changed files with 22 additions and 16 deletions

View File

@ -79,14 +79,14 @@ class Roster
* Retrieve contact via jid * Retrieve contact via jid
* *
* @param string $jid * @param string $jid
* @return mixed|void * @return array|null
*/ */
public function getContact(string $jid) public function getContact(string $jid): ?array
{ {
if ($this->isContact($jid)) { if ($this->isContact($jid)) {
return $this->roster_array[$jid]['contact']; return $this->roster_array[$jid]['contact'];
} }
return; return null;
} }
/** /**
@ -128,12 +128,16 @@ class Roster
* Add given contact to roster * Add given contact to roster
* *
* @param string $jid * @param string $jid
* @param string $subscription * @param string $subscription (optional)
* @param string $name (optional) * @param string $name (optional)
* @param array $groups (optional) * @param array $groups (optional)
*/ */
public function addContact(string $jid, string $subscription, string $name = '', array $groups = []): void public function addContact(
{ string $jid,
string $subscription = 'none',
string $name = '',
array $groups = []
): void {
$contact = ['jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups]; $contact = ['jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups];
if ($this->isContact($jid)) { if ($this->isContact($jid)) {
$this->roster_array[$jid]['contact'] = $contact; $this->roster_array[$jid]['contact'] = $contact;

View File

@ -435,28 +435,30 @@ class XMPP extends XMLStream
*/ */
protected function roster_iq_handler(XMLObj $xml): void protected function roster_iq_handler(XMLObj $xml): void
{ {
$status = "result"; $status = 'result';
$xmlroster = $xml->sub('query'); $xmlroster = $xml->sub('query');
$contacts = []; $contacts = [];
foreach ($xmlroster->subs as $item) { foreach ($xmlroster->subs as $item) {
$groups = []; $groups = [];
if ($item->name == 'item') { if ($item->name === 'item') {
$jid = $item->attrs['jid']; // REQUIRED $jid = $item->attrs['jid']; // REQUIRED
$name = $item->attrs['name']; //MAY $name = $item->attrs['name'] ?? '';
$subscription = $item->attrs['subscription']; $subscription = $item->attrs['subscription'] ?? 'none';
foreach ($item->subs as $subitem) { foreach ($item->subs as $subitem) {
if ($subitem->name == 'group') { if ($subitem->name === 'group') {
$groups[] = $subitem->data; $groups[] = $subitem->data;
} }
} }
$contacts[] = [$jid, $subscription, $name, $groups]; //Store for action if no errors happen // Store for action if no errors happen
$contacts[] = [$jid, $subscription, $name, $groups];
} else { } else {
$status = "error"; $status = 'error';
} }
} }
if ($status == "result") { //No errors, add contacts // No errors, add contacts
if ($status === 'result') {
foreach ($contacts as $contact) { foreach ($contacts as $contact) {
$this->roster->addContact($contact[0], $contact[1], $contact[2], $contact[3]); $this->roster->addContact(...$contact);
} }
} }
if ($xml->attrs['type'] == 'set') { if ($xml->attrs['type'] == 'set') {