-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhpLiveChatMongo.php
61 lines (47 loc) · 1.63 KB
/
PhpLiveChatMongo.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
<?php
class PhpLiveChatMongo extends PhpLiveChat {
private $db = null;
private $collection = null;
public function setDatabase($db)
{
$this->db = $db;
$this->collection = $this->db->messages;
$this->collection->ensureIndex( array( "time" => 1, 'channel' => 1, 'to' => 1 ) );
}
protected function addMessage($message, $type, $channel, $from, $to = false)
{
//echo 'JA, mongo :)';
$doc = array(
'time' => microtime(true),
'message' => $message,
'type' => $type,
'channel' => $channel,
'from' => $from,
'to' => $to
);
$this->collection->insert($doc);
//$this->sendMessages();
}
protected function getMessages($fromTime, $to = false, $channels = null)
{
$query = array('time' => array('$gte' => (float) $fromTime), 'to' => array('$in' => array($to, false)));
if ($channels !== null) {
$query['channel'] = array('$in' => array_keys($channels));
}
//var_dump($query);
$cursor = $this->collection
->find($query, array('time' => 1, 'message' => 1, 'type' => 1, 'channel' => 1))
->sort(array('time' => -1))->limit($this->maxMessages);
$messages = array();
while( $cursor->hasNext() ) {
$messages[] = $cursor->getNext();
}
$messages = array_reverse($messages);
if ($last = end($messages)) {
$last = $last['time'];
} else {
$last = $fromTime;
}
return array($messages, $last);
}
}