Skip to content

Commit

Permalink
Refactor H_AS_State_Mongo::_getPIMChangeTS for recent changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrubinsk committed Oct 16, 2013
1 parent 61289cc commit e800d7f
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions framework/ActiveSync/lib/Horde/ActiveSync/State/Mongo.php
Expand Up @@ -1150,16 +1150,16 @@ public function deleteSyncCache($devid, $user = null)
}

/**
* Get a timestamp from the map table for the last PIM-initiated change for
* the provided uid. Used to avoid mirroring back changes to the PIM that it
* sent to the server.
* Return an array of timestamps from the map table for the last
* PIM-initiated change for the provided uid. Used to avoid mirroring back
* changes to the PIM that it sent to the server.
*
* @param string $uid The uid of the entry to check.
* @param array $changes The changes array, containing 'id' and 'type'.
*
* @return integer|null The timestamp of the last PIM-initiated change for
* the specified uid, or null if none found.
* @return array An array of UID -> timestamp of the last PIM-initiated
* change for the specified uid, or null if none found.
*/
protected function _getPIMChangeTS($uid, $type)
protected function _getPIMChangeTS(array $changes)
{
// // Get the allowed synckeys to include.
$uuid = self::getSyncKeyUid($this->_syncKey);
Expand All @@ -1171,27 +1171,37 @@ protected function _getPIMChangeTS($uid, $type)
$match = array(
'sync_devid' => $this->_deviceInfo->id,
'sync_user' => $this->_deviceInfo->user,
'message_uid' => $uid,
'sync_key' => array('$in' => $keys)
);

if ($type == Horde_ActiveSync::CHANGE_TYPE_DELETE) {
$match['sync_deleted'] = true;
$uids = array();
$match['$or'] = array();
foreach ($changes as $change) {
$match['$or'][] = array(
'$and' => array(
'message_uid' => $change['id'],
'sync_deleted' => $change['type'] == Horde_ActiveSync_Exception::CHANGE_TYPE_DELETE
)
);
}
try {
$results = $this->_db->map->aggregate(
$rows = $this->_db->map->aggregate(
array('$match' => $match),
array('$group' => array('_id' => '$message_uid', 'max' => array('$max' => '$sync_modtime')))

);
} catch (Exception $e) {
$this->_logger->err($e->getMessage());
throw new Horde_ActiveSync_Exception($e);
}
if (empty($results) || empty($results['ok'])) {
if (empty($rows) || empty($rows['ok'])) {
throw new Horde_ActiveSync_Exception('Error running aggregation.');
}
$result = current($results);
return $result['max'];
$results = array();
foreach ($rows as $row) {
$results[$row['_id']] = $row['max'];
}

return $results;
}

/**
Expand Down

0 comments on commit e800d7f

Please sign in to comment.