-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRoster.php
293 lines (258 loc) · 8.14 KB
/
Roster.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
/**
* Nucleus - XMPP Library for PHP
*
* Copyright (C) 2016, Some rights reserved.
*
* @author Kacper "Kadet" Donat <[email protected]>
*
* Contact with author:
* Xmpp: [email protected]
* E-mail: [email protected]
*
* From Kadet with love.
*/
namespace Kadet\Xmpp\Component;
use Kadet\Highlighter\Utils\Console;
use Kadet\Xmpp\Exception\BadMethodCallException;
use Kadet\Xmpp\Exception\ReadOnlyException;
use Kadet\Xmpp\Jid;
use Kadet\Xmpp\Stanza\Iq;
use Kadet\Xmpp\Utils\Accessors;
use Kadet\Xmpp\Utils\BetterEmitter;
use Kadet\Xmpp\Utils\filter as with;
use Kadet\Xmpp\XmppClient;
use Traversable;
use function Kadet\Xmpp\Utils\helper\format;
/**
* Class Roster
* @package Kadet\Xmpp\Component
*
* @property-read Iq\Query\Roster\Item[] $items Copy of all roster items
*
* @event item(Iq\Query\Roster\Item $item) Emitted on item update
* @event remove(Iq\Query\Roster\Item $item) Emitted on item removal
* @event update() Emitted on roster update (after processing roster item)
*/
class Roster extends Component implements \IteratorAggregate, \ArrayAccess
{
use BetterEmitter, Accessors;
private $_items = [];
public function setClient(XmppClient $client)
{
if($this->_client !== null) {
throw new BadMethodCallException("You cannot change rosters XmppClient instance.");
}
parent::setClient($client);
$this->_client->on('init', function(\SplQueue $queue) {
$queue->enqueue($this->_client->send(new Iq('get', ['query' => new Iq\Query\Roster()])));
});
$this->_client->on('iq', function(Iq $iq) {
/** @var Roster $iq->query */
switch ($iq->type) {
case "result":
$this->handleResult($iq->query);
break;
case "set":
$this->handleSet($iq->query);
break;
}
}, with\iq\query(Iq\Query\Roster::class));
}
private function handleSet(Iq\Query\Roster $query)
{
foreach ($query->items as $item) {
if($item->subscription == 'remove') {
$this->removeItem($item->jid);
} else {
$this->setItem($item);
}
}
$this->emit('update');
}
private function handleResult(Iq\Query\Roster $query)
{
$this->_client->getLogger()->debug(format('Received roster (version: {version}) update with {no} roster items.', [
'no' => count($query->items),
'version' => $query->version ?: 'no version'
]));
$this->_items = [];
$this->handleSet($query);
}
/**
* Whether a offset exists
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset <p>
* An offset to check for.
* </p>
* @return boolean true on success or false on failure.
* </p>
* <p>
* The return value will be casted to boolean if non-boolean was returned.
* @since 5.0.0
*/
public function offsetExists($offset)
{
return array_key_exists($offset, $this->_items);
}
/**
* Offset to retrieve
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param mixed $offset <p>
* The offset to retrieve.
* </p>
* @return mixed Can return all value types.
* @since 5.0.0
*/
public function offsetGet($offset)
{
return clone ($this->_items[(string)$offset] ?? null);
}
/**
* Offset to set
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset <p>
* The offset to assign the value to.
* </p>
* @param mixed $value <p>
* The value to set.
* </p>
* @return void
* @since 5.0.0
*/
public function offsetSet($offset, $value)
{
throw new ReadOnlyException('You should not modify roster directly, use update() method.');
}
/**
* Offset to unset
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset <p>
* The offset to unset.
* </p>
* @return void
* @since 5.0.0
*/
public function offsetUnset($offset)
{
throw new ReadOnlyException('You should not modify roster directly, use remove() method.');
}
/**
* Removes item from servers roster.
*
* @param Jid|string|\Closure $what Predicate used to determine items.
*
* @return \React\Promise\ExtendedPromiseInterface Removal result promise.
*/
public function remove($what)
{
$predicate = $what instanceof \Closure ? $what : with\property('jid', with\equals($what));
$remove = array_filter($this->_items, $predicate);
$iq = new Iq('remove', ['query' => new Iq\Query\Roster()]);
/** @var Iq\Query\Roster\Item $item */
foreach($remove as $item) {
$iq->query->append(new Iq\Query\Roster\Item($item->jid, ['subscription' => 'remove']));
}
return $this->_client->send($iq);
}
/**
* Saves item to roster.
*
* @param Iq\Query\Roster\Item $item Item to save.
*
* @return \React\Promise\ExtendedPromiseInterface Update result promise.
*/
public function update(Iq\Query\Roster\Item $item)
{
$iq = new Iq('set', ['query' => new Iq\Query\Roster([
'items' => [ $item ]
])]);
return $this->_client->send($iq);
}
/**
* Adds item to roster.
*
* @param Iq\Query\Roster\Item $item Item to add.
*
* @return \React\Promise\ExtendedPromiseInterface Addition result promise.
*/
public function add(Iq\Query\Roster\Item $item)
{
return $this->update($item); // From XMPPs perspective adding is same as updating non existent item.
}
/**
* Retrieve an external iterator
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php
* @return Traversable An instance of an object implementing <b>Iterator</b> or
* <b>Traversable</b>
* @since 5.0.0
*/
public function getIterator(): Traversable
{
return new \ArrayIterator($this->_items);
}
/**
* @return Iq\Query\Roster\Item[]
*/
public function getItems()
{
return \Kadet\Xmpp\Utils\helper\copy($this->_items);
}
public static function group($name)
{
return with\property('groups', with\contains($name));
}
/**
* @param callable(Item $item) $mapper
* @return array
*/
public function map(callable $mapper)
{
return array_map($mapper, $this->items);
}
/**
* @param callable $predicate
* @return Iq\Query\Roster\Item[]
*/
public function filter(callable $predicate)
{
return array_filter($this->items, $predicate);
}
public function asArray() : array
{
return $this->items;
}
public static function fromArray(array $array)
{
// TODO: Implement fromArray() method.
}
/**
* Count elements of an object
* @link http://php.net/manual/en/countable.count.php
* @return int The custom count as an integer.
* </p>
* <p>
* The return value is cast to an integer.
* @since 5.1.0
*/
public function count()
{
return count($this->items);
}
private function setItem(Iq\Query\Roster\Item $item)
{
$this->emit('item', [ $item ]);
$this->_items[(string)$item->jid] = $item;
}
private function removeItem(Jid $jid)
{
if (!isset($this->_items[(string)$jid])) {
$this->_client->getLogger()->warning(format('Trying to remove non-existing roster item {jid}', [
'item' => Console::styled(['color' => 'green'], (string)$jid)
]));
return;
}
$this->emit('remove', [ $this->_items[(string)$jid] ]);
unset($this->_items[(string)$jid]);
}
}