function lists_get_for_user($extended = false)
{
    global $mongo;
    $where = array('uids' => current_user());
    $lists = $mongo->lists->find($where);
    $lists = array_values(array_map('_map_list', iterator_to_array($lists)));
    foreach ($lists as &$list) {
        $list_id = $list['id'];
        $list['n_products'] = products_count(array('lists' => $list_id));
        if ($extended) {
            $list['products'] = products_find(array('lists' => $list_id), 10);
        }
    }
    return $lists;
}
<?php

require '_init.php';
$extended = @$_GET['extended'] == 'true';
$lists = lists_get_for_user($extended);
$def_list_id = 'def_' . current_user();
$deflist = array('id' => $def_list_id, 'title' => 'All stashes', 'n_users' => 1, 'date' => '13th September 2014');
$deflist['n_products'] = products_count(array('lists' => $def_list_id));
if ($extended) {
    $deflist['products'] = products_find(array('lists' => $def_list_id), 10);
}
array_unshift($lists, $deflist);
$ret = array('lists' => $lists);
send_json($ret);
<?php

require '_init.php';
$where = array();
if (@$_GET['retailer']) {
    $where['retailer'] = $_GET['retailer'];
}
$where['lists'] = @$_GET['list'] ? $_GET['list'] : 'def_' . current_user();
$products = products_find($where);
$ret = array('products' => $products);
if (@$_GET['list']) {
    $list = list_get($_GET['list']);
} else {
    $list = array('id' => 'def_' . current_user(), 'title' => 'All stashes', 'n_users' => 1, 'date' => '13th September 2014');
}
$ret['list'] = $list;
send_json($ret);